dgl.DGLGraph.adj_sparse

DGLGraph.adj_sparse(fmt, etype=None)

Return the adjacency matrix of edges of the given edge type as tensors of a sparse matrix representation.

By default, a row of returned adjacency matrix represents the source of an edge and the column represents the destination.

Parameters
  • fmt (str) – Either coo, csr or csc.

  • etype (str or (str, str, str), optional) –

    The type names of the edges. The allowed type name formats are:

    • (str, str, str) for source node type, edge type and destination node type.

    • or one str edge type name if the name can uniquely identify a triplet format in the graph.

    Can be omitted if the graph has only one type of edges.

Returns

If fmt is coo, returns a pair of source and destination node ID tensors.

If fmt is csr or csc, return the CSR or CSC representation of the adjacency matrix as a triplet of tensors (indptr, indices, edge_ids). Namely edge_ids could be an empty tensor with 0 elements, in which case the edge IDs are consecutive integers starting from 0.

Return type

tuple[Tensor]

Examples

>>> g = dgl.graph(([0, 1, 2], [1, 2, 3]))
>>> g.adj_sparse('coo')
(tensor([0, 1, 2]), tensor([1, 2, 3]))
>>> g.adj_sparse('csr')
(tensor([0, 1, 2, 3, 3]), tensor([1, 2, 3]), tensor([0, 1, 2]))