dgl.DGLGraph.adj

DGLGraph.adj(etype=None, eweight_name=None)[source]

Get the adjacency matrix of the graph.

Parameters:
  • 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.

  • eweight_name (str, optional) – The name of edge feature used as the non-zero values. If not given, the non-zero values are all 1.

Returns:

The adjacency matrix.

Return type:

SparseMatrix

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch
>>> g = dgl.graph(([0, 1, 2], [1, 2, 3]))
>>> g.adj()
SparseMatrix(indices=tensor([[0, 1, 2],
                             [1, 2, 3]]),
             values=tensor([1., 1., 1.]),
             shape=(4, 4), nnz=3)
>>> g = dgl.heterograph({
...     ('user', 'follows', 'user'): ([0, 1], [0, 1]),
...     ('developer', 'develops', 'game'): ([0, 1], [0, 2])
... })
>>> g.adj(etype='develops')
SparseMatrix(indices=tensor([[0, 1],
                             [0, 2]]),
             values=tensor([1., 1.]),
             shape=(2, 3), nnz=2)
>>> g.edata['h'] = {('user', 'follows', 'user'): torch.tensor([3, 2])}
>>> g.adj(etype='follows', eweight_name='h')
SparseMatrix(indices=tensor([[0, 1],
                             [0, 1]]),
             values=tensor([3, 2]),
             shape=(2, 2), nnz=2)