function I = digraph_adj_to_incidence ( A )

%*****************************************************************************80
%
%% digraph_adj_to_incidence() converts adjacency to incidence matrix.
%
%  Discussion:
%
%    I(node,arc) = 0 if NODE is not the beginning or end of ARC, or
%                    if ARC is a loop;
%                  1 if NODE is the beginning of ARC;
%                 -1 if NODE is the end of ARC.
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    11 April 2026
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer A(node_num,node_num), the adjacency matrix for the 
%    graph.  A(I,J) is nonzero if there is an edge from node I to node J.
%
%  Output:
%
%    integer I(node_num,arc_num), the incidence matrix.
%
  node_num = size ( A, 1 );
  arc_num = nnz ( A );
  I = zeros ( node_num, arc_num );

  arc_id = 0;
  for node_j = 1 : node_num
    for node_i = 1 : node_num
      
      if ( node_i ~= node_j )
        if ( A(node_i,node_j) ~= 0 )
          arc_id = arc_id + 1;
          I(node_i,arc_id) =  1;
          I(node_j,arc_id) = -1;
        end
      end

    end
  end

  return
end
