dgl.DGLGraph.in_degrees¶
-
DGLGraph.
in_degrees
(v='__ALL__', etype=None)[source]¶ Return the in-degree(s) of the given nodes.
It computes the in-degree(s) w.r.t. to the edges of the given edge type.
- Parameters
v (node IDs) –
The node IDs. The allowed formats are:
int
: A single node.Int Tensor: Each element is a node ID. The tensor must have the same device type and ID data type as the graph’s.
iterable[int]: Each element is a node ID.
If not given, return the in-degrees of all the nodes.
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.
Can be omitted if the graph has only one type of edges.
- Returns
The in-degree(s) of the node(s) in a Tensor. The i-th element is the in-degree of the i-th input node. If
v
is anint
, return anint
too.- Return type
int or 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, 1, 2, 3])))
Query for all nodes.
>>> g.in_degrees() tensor([0, 2, 1, 1])
Query for nodes 1 and 2.
>>> g.in_degrees(torch.tensor([1, 2])) tensor([2, 1])
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.in_degrees(torch.tensor([1, 0]), etype='follows') tensor([1, 0])
See also