dgl.DGLGraph.has_nodesยถ

DGLGraph.has_nodes(vid, ntype=None)[source]ยถ

Return whether the graph contains the given nodes.

Parameters
  • vid (node ID(s)) โ€“

    The nodes IDs. The allowed nodes ID formats are:

    • int: The ID of 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.

  • ntype (str, optional) โ€“ The node type name. Can be omitted if there is only one type of nodes in the graph.

Returns

A tensor of bool flags where each element is True if the node is in the graph. If the input is a single node, return one bool value.

Return type

bool or bool Tensor

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([0, 1]))
... })

Query for the nodes.

>>> g.has_nodes(0, 'user')
True
>>> g.has_nodes(3, 'game')
False
>>> g.has_nodes(torch.tensor([3, 0, 1]), 'game')
tensor([False,  True,  True])