function distance = graph_adj_distance_from_node ( adj, node_k )

%*****************************************************************************80
%
%% graph_adj_distance_from_node() computes distance from node to all nodes.
%
%  Discussion:
%
%    The distance is measured by the number of links in the shortest path
%    starting at node k.
%
%    distance(node_k) = 0
%    distance(node_p) = Inf if node p is unreachable from node k.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    11 March 2023
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer adj(node_num,node_num), the adjacency information.
%
%    integer node_k: a specific node of the graph.
%
%  Output:
%
%    integer distance(node_num): the distance of each node to node k.
%
  node_num = size ( adj, 1 );
%
%  Set up distance.
%
  distance(1:node_num,1) = Inf;
  d = 0;
  distance(node_k) = d;
%
%  NEW holds the nodes we have just reached on this step.
%
  new = [ node_k ];
%
%  As long as we found at least one neighbor at distance D,
%  seek new neighbors at distance D+1.
%
  while ( 0 < length ( new ) )

    old = new;
    new = [];
    d = d + 1;
%
%  For each node J that we found on the previous step...
%
    for k = 1 : length ( old )
      j = old(k);
%
%  ...look for adjacent neighbors I...
%
      for i = 1 : node_num

        if ( adj(i,j) == 1 )
%
%  ...and if node I hasn't been reached yet:
%  mark that is has been reached,
%  add it to list,
%  and increase the count of nodes we have reached.
%
          if ( distance(i) == Inf )
            distance(i) = d;
            new = [ new, i ];
          end

        end
      end

    end

  end

  return
end
