# dgl.DGLGraph.incidence_matrix¶

DGLGraph.incidence_matrix(typestr, ctx=device(type='cpu'))[source]

Return the incidence matrix representation of this graph.

An incidence matrix is an n x m sparse matrix, where n is the number of nodes and m is the number of edges. Each nnz value indicating whether the edge is incident to the node or not.

There are three types of an incidence matrix $$I$$:

• in:

• $$I[v, e] = 1$$ if $$e$$ is the in-edge of $$v$$ (or $$v$$ is the dst node of $$e$$);
• $$I[v, e] = 0$$ otherwise.
• out:

• $$I[v, e] = 1$$ if $$e$$ is the out-edge of $$v$$ (or $$v$$ is the src node of $$e$$);
• $$I[v, e] = 0$$ otherwise.
• both:

• $$I[v, e] = 1$$ if $$e$$ is the in-edge of $$v$$;
• $$I[v, e] = -1$$ if $$e$$ is the out-edge of $$v$$;
• $$I[v, e] = 0$$ otherwise (including self-loop).
Parameters: typestr (str) – Can be either in, out or both ctx (context, optional (default=cpu)) – The context of returned incidence matrix. The incidence matrix. SparseTensor