function connected = graph_adj_is_edgewise_connected ( adj )

%*****************************************************************************80
%
%% graph_adj_is_edgewise_connected() determines if a graph is edgewise connected.
%
%  Definition:
%
%    A graph is edgewise connected if from any edge it is possible to reach
%    any other edge.  An edgewise connected graph may include isolated nodes.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    15 March 2023
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer adj(node_num,node_num): the adjacency matrix.
%
%  Output:
%
%    logical connected: true if the graph is edgewise connected.
%

%
%  Select an arbitrary edge (ni,nj) from the graph.
%
  [ ni, nj ] = graph_adj_edge_select ( adj );
%
%  If the graph has no edges, then surprisingly, it is edgewise connected%
%  There are no pair of edges that cannot be connected by a path, because
%  there aren't any edges at all.
%
  if ( ni == -1 )
    connected = true;
    return
  end
%
%  visited(i) is true if edge I has been reached.
%  LIST(I) contains a list of the nodes as they are reached.
%
  list = zeros ( node_num, 1 );
  visited = false ( node_num, 1 );

  ilo = 1;
  ihi = 1;
  list(1) = ni;
  visited[ni] = true;
  if ( ni ~= nj )
    ihi = 2;
    list(2) = nj;
    visited(nj) = true;
  end

  adj(ni,nj) = - adj(ni,nj);
  adj(nj,ni) = - adj(nj,ni);
%
%  From the batch of edge nodes found last time, LIST(ILO:IHI),
%  look for unfound neighbors, and store their indices in LIST(JLO:JHI).
%
  while ( true )

    jlo = ihi + 1;
    jhi = ihi;

    for ii = ilo : ihi

      i = list(ii);

      for j = 1 : node_num

        if ( 0 < adj(i,j) )

          adj(i,j) = - adj(i,j);
          if ( 0 < adj(j,i) )
            adj(j,i) = - adj(j,i);
          end

          if ( ~ visited(j) )
            jhi = jhi + 1;
            list(jhi) = j;
            visited(j) = true;
          end

        end

      end

    end

    if ( jhi < jlo )
      break;
    end

    ilo = jlo;
    ihi = jhi;

  end
%
%  If any edges were unvisited, then the graph is not edgewise connected.
%
  connected = true;

  for i = 1 : node_num
    for j = 1 : node_num
      if ( 0 < adj(i,j) )
        connected = false;
      end
    end
  end
%
%  Restore the positive sign of ADJ.
%
  adj = abs ( adj );

  return
end

