dgl.DGLGraph.num_nodes

DGLGraph.num_nodes(ntype=None)[source]

Return the number of nodes in the graph.

Parameters

ntype (str, optional) – The node type name. If given, it returns the number of nodes of the type. If not given (default), it returns the total number of nodes of all types.

Returns

The number of nodes.

Return type

int

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch

Create a graph with two node types – ‘user’ and ‘game’.

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

Query for the number of nodes.

>>> g.num_nodes('user')
5
>>> g.num_nodes('game')
7
>>> g.num_nodes()
12