dgl.DGLGraph.predecessors

DGLGraph.predecessors(v, etype=None)[source]

Return the predecessor(s) of a particular node with the specified edge type.

Node u is a predecessor of node v if there is an edge (u, v) with type etype in the graph.

Parameters
  • v (int) – The node ID. If the graph has multiple edge types, the ID is for the destination type corresponding to the edge type.

  • 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

The predecessors of v with the specified edge type.

Return type

Tensor

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Create a homogeneous graph.

>>> g = dgl.graph((torch.tensor([0, 0, 1, 1]), torch.tensor([1, 1, 2, 3])))

Query for node 1.

>>> g.predecessors(1)
tensor([0, 0])

For a graph of multiple edge types, it is required to specify the edge type in query.

>>> hg = dgl.heterograph({
...     ('user', 'follows', 'user'): (torch.tensor([0, 1]), torch.tensor([1, 2])),
...     ('user', 'plays', 'game'): (torch.tensor([3, 4]), torch.tensor([5, 6]))
... })
>>> hg.predecessors(1, etype='follows')
tensor([0])

See also

successors