dgl.DGLGraph.in_edges

DGLGraph.in_edges(v, form='uv', etype=None)[source]

Return the incoming edges of the given nodes.

Parameters
  • v (node ID(s)) –

    The node IDs. The allowed formats are:

    • int: A single node.

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

    • iterable[int]: Each element is a node ID.

  • form (str, optional) –

    The result format, which can be one of the following:

    • 'eid': The returned result is a 1D tensor \(EID\), representing the IDs of all edges.

    • 'uv' (default): The returned result is a 2-tuple of 1D tensors \((U, V)\), representing the source and destination nodes of all edges. For each \(i\), \((U[i], V[i])\) forms an edge.

    • 'all': The returned result is a 3-tuple of 1D tensors \((U, V, EID)\), representing the source nodes, destination nodes and IDs of all edges. For each \(i\), \((U[i], V[i])\) forms an edge with ID \(EID[i]\).

  • 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

All incoming edges of the nodes with the specified type. For a description of the returned result, see the description of form.

Return type

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

Query for the nodes 1 and 0.

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

Specify a different value for form.

>>> g.in_edges(torch.tensor([1, 0]), form='all')
(tensor([0, 0]), tensor([1, 0]), tensor([0, 1]))

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.in_edges(torch.tensor([1, 0]), etype='follows')
(tensor([0]), tensor([1]))

See also

edges, out_edges