# dgl.DGLGraph.num_src_nodes¶

DGLGraph.num_src_nodes(ntype=None)

Return the number of source nodes in the graph.

If the graph can further divide its node types into two subsets A and B where all the edeges are from nodes of types in A to nodes of types in B, we call this graph a uni-bipartite graph and the nodes in A being the source nodes and the ones in B being the destination nodes. If the graph is not uni-bipartite, the source and destination nodes are just the entire set of nodes in the graph.

Parameters

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

Returns

The number of nodes

Return type

int

Examples

The following example uses PyTorch backend.

>>> import dgl
>>> import torch


Create a homogeneous graph for query.

>>> g = dgl.graph((torch.tensor([0, 1]), torch.tensor([1, 2])))
>>> g.num_src_nodes()
3


Create a heterogeneous graph with two source node types – ‘developer’ and ‘user’.

>>> g = dgl.heterograph({
...     ('developer', 'develops', 'game'): (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_src_nodes('developer')
2
>>> g.num_src_nodes('user')
5
>>> g.num_src_nodes()
7