% This function converts the given adjacency matrix of a simple, directed graph
% into its incidence matrix. A non-zero entry A(i,j) will be interpreted as an
% edge i->j.
%
% Input
% A: (square) adjacency matrix, zero diagonal
%
% Output
% I: incidence matrix
function I = adj2inc(A)
% Make sure the adjacency matrix is square
assert(size(A,1) == size(A,2),'adj2inc: Adjacency matrix must be square.')
% Make sure the adjacency matrix has zero diagonal
assert(~any(diag(A)),'adj2inc: Adjacency matrix must have zero diagonal.')
% Determine the number of vertices in the graph represented by
% the adjacency matrix A
nVertices = size(A,1);
% Initialize the edge counter
iterEdges = 1;
% Loop over the rows of A ('from' nodes) and create an edge
% for every non-zero column ('to' nodes)
for iterRows = 1:nVertices
for iterColumns = 1:nVertices
if (A(iterRows,iterColumns))
I(iterRows,iterEdges) = -1;
I(iterColumns,iterEdges) = 1;
iterEdges = iterEdges + 1;
end
end
end