dgl.DGLGraph.num_edgesΒΆ

DGLGraph.num_edges(etype=None)[source]ΒΆ

Return the number of edges in the graph.

Parameters

etype (str or (str, str, str), optional) –

The type name 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.

If not provided, return the total number of edges regardless of the types in the graph.

Returns

The number of edges.

Return type

int

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Create a graph with three canonical edge types.

>>> g = dgl.heterograph({
...     ('user', 'follows', 'user'): (torch.tensor([0, 1]), torch.tensor([1, 2])),
...     ('user', 'follows', 'game'): (torch.tensor([0, 1, 2]), torch.tensor([1, 2, 3])),
...     ('user', 'plays', 'game'): (torch.tensor([1, 3]), torch.tensor([2, 3]))
... })

Query for the number of edges.

>>> g.num_edges('plays')
2
>>> g.num_edges()
7

Use a canonical edge type instead when there is ambiguity for an edge type.

>>> g.num_edges(('user', 'follows', 'user'))
2
>>> g.num_edges(('user', 'follows', 'game'))
3