dgl.DGLGraph.edgesΒΆ

property DGLGraph.edgesΒΆ

Return an edge view

One can use it for:

  1. Getting the edges for a single edge type. In this case, it can take the following optional arguments:

    • formstr, optional

      The return form, which can be one of the following:

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

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

      • '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]\).

    • orderstr, optional

      The order of the returned edges, which can be one of the following:

      • 'eid' (default): The edges are sorted by their IDs.

      • 'srcdst': The edges are sorted first by their source node IDs and then by their destination node IDs to break ties.

    • etypestr or tuple of str, optional

      The edge type for query, which can be an edge type (str) or a canonical edge type (3-tuple of str). When an edge type appears in multiple canonical edge types, one must use a canonical edge type. If the graph has multiple edge types, one must specify the argument. Otherwise, it can be omitted.

  2. Setting/getting features for all edges of a single edge type. To set/get a feature feat for edges of type etype in a graph g, one can use g.edges[etype].data[feat].

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Get the Edges for a Single Edge Type

Create a graph with a single edge type.

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

Specify a different value for form and order.

>>> g.edges(form='all', order='srcdst')
(tensor([0, 0, 1]), tensor([0, 1, 1]), tensor([2, 1, 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.edges(etype='plays')
(tensor([3, 4]), tensor([5, 6]))

Set/get Features for All Edges of a Single Edge Type

Create a heterogeneous graph of two edge types.

>>> 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]))
... })

Set and get a feature β€˜h’ for all edges of a single type in the heterogeneous graph.

>>> hg.edges['follows'].data['h'] = torch.ones(2, 1)
>>> hg.edges['follows'].data['h']
tensor([[1.], [1.]])

To set edge features for a graph with a single edge type, use DGLGraph.edata().

See also

edata