DGLGraph.out_edges(u, form='uv', etype=None)[source]

Return the outgoing edges of the given nodes.

  • u (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 return form, 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.


All outgoing 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)


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

>>> g.out_edges(torch.tensor([1, 2]))
(tensor([1, 1]), tensor([2, 3]))

Specify a different value for form.

>>> g.out_edges(torch.tensor([1, 2]), form='all')
(tensor([1, 1]), tensor([2, 3]), tensor([2, 3]))

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

See also

edges, in_edges