dgl.DGLGraph.all_edges¶
-
DGLGraph.
all_edges
(form='uv', order='eid', etype=None)¶ Return all edges with the specified edge type.
- Parameters
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]\).
order (str, optional) –
The order of the returned edges, which can be one of the following:
'srcdst'
: The edges are sorted first by their source node IDs and then by their destination node IDs to break ties.'eid'
(default): The edges are sorted by their IDs.
etype (str 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.
- Returns
All edges of the specified edge 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 edges.
>>> g.all_edges() (tensor([0, 0, 1, 1]), tensor([1, 0, 2, 3]))
Specify a different value for
form
andorder
.>>> g.all_edges(form='all', order='srcdst') (tensor([0, 0, 1, 1]), tensor([0, 1, 2, 3]), tensor([1, 0, 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.all_edges(etype='plays') (tensor([3, 4]), tensor([5, 6]))