DGLGraph.find_edges(eid, etype=None)[source]

Return the source and destination node ID(s) given the edge ID(s).

  • eid (edge ID(s)) –

    The edge IDs. The allowed formats are:

    • int: A single ID.

    • Int Tensor: Each element is an ID. The tensor must have the same device type and ID data type as the graph’s.

    • iterable[int]: Each element is an ID.

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


  • Tensor – The source node IDs of the edges. The i-th element is the source node ID of the i-th edge.

  • Tensor – The destination node IDs of the edges. The i-th element is the destination node ID of the i-th edge.


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, 0, 2, 3])))

Find edges of IDs 0 and 2.

>>> g.find_edges(torch.tensor([0, 2]))
(tensor([0, 1]), tensor([1, 2]))

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.find_edges(torch.tensor([1, 0]), 'plays')
(tensor([4, 3]), tensor([6, 5]))