function connected = graph_adj_is_nodewise_connected_depth ( A )

%*****************************************************************************80
%
%% graph_adj_is_nodewise_connected_depth() determines if a graph is nodewise connected.
%
%  Discussion:
%
%    A graph is (nodewise) connected if, from every node, there is a path
%    to any other node.
%
%    This function performs a depth-first search of the graph.  
%    Add node 1 to the stack.
%    Repeat the following steps:
%      If the stack is empty, then if we have visited every node, the graph is connected.
%      The current node is defined as the last item on the stack.
%      Delete the last item on the stack.
%      Add all unvisited neighbor nodes of the current node to the stack.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    30 August 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer A(n,n), the adjacency matrix for the graph.
%
%  Output:
%
%    logical connected: is true if the graph is connected.
%
  value = matrix_is_adjacency ( A );
  if ( ~ value )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'graph_adj_is_nodewise_connected_depth(): Fatal error!\n' );
    fprintf ( 1, '  The input is not a legal adjacency matrix.\n' );  
    error ( 'graph_adj_is_nodewise_connected_depth(): Fatal error!\n' );
  end

  n = size ( A, 1 );

  step = 0;
  visited = zeros ( n, 1 );
  stack = zeros ( n, 1 );
  stack_num = 1;
  stack(stack_num) = 1;

  while ( true )
%
%  Seek a node on the stack that has not been visited.
%
    while ( true )

      if ( stack_num == 0 )
        connected = ( step == n );
        return;
      end

      node = stack(stack_num);
      stack_num = stack_num - 1;
      if ( ~ visited(node) )
        break;
      end

    end
%
%  "Visit" the node.
%
    step = step + 1;
    visited(node) = step;
%
%  Stack each node j that is an immediate neighbor and unvisited.
%
    for j = 1 : n
      if ( A(node,j) == 1 & ~ visited(j) )
        stack_num = stack_num + 1;
        stack(stack_num) = j;
      end
    end
    
  end

  return
end

