function C = digraph_adj_transitive_closure ( A )

%*****************************************************************************80
%
%% digraph_adj_transitive_closure() generates the transitive closure of a digraph.
%
%  Discussion:
%
%    The method is due to Stephen Warshall.
%
%  Definition:
%
%    The transitive closure of a graph is a function REACH(I,J) so that
%
%      REACH(I,J) = 0 if node J cannot be reached from node I;
%                   1 if node J can be reached from node I.
%
%    This is an extension of the idea of adjacency.  REACH(I,J)=1 if
%    node J is adjacent to node I, or if node J is adjacent to a node
%    that is adjacent to node I, etc.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    26 October 1999
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Robert Sedgewick,
%    Algorithms,
%    Addison Wesley, 1983, page 425.
%
%  Input:
%
%    integer A(node_num,node_num): the adjacency matrix.  ADJ(I,J)
%    is nonzero if there is an edge from node I to node J.
%
%  Output:
%
%    integer C(node_num,node_num): the transitive closure matrix.
%
  node_num = size ( A, 1 );

  C = A(1:node_num,1:node_num);
%
%  You can "reach" a node from itself.
%
  for i = 1 : node_num
    C(i,i) = 1;
  end

  for i = 1 : node_num
    for j = 1 : node_num
      if ( C(j,i) ~= 0 )
        for k = 1 : node_num
          if ( C(i,k) ~= 0 )
            C(j,k) = 1;
          end
        end
      end
    end
  end

  return
end
