dgl.DGLGraph.remove_nodes

DGLGraph.remove_nodes(nids, ntype=None, store_ids=False)

Remove multiple nodes with the specified node type

Edges that connect to the nodes will be removed as well. After removing nodes and edges, the rest nodes and edges will be re-indexed using consecutive integers from 0, with their relative order preserved.

The features for the removed nodes/edges will be removed accordingly.

Parameters
  • nids (int, tensor, numpy.ndarray, list) – Nodes to remove.

  • ntype (str, optional) – The type of the nodes to remove. Can be omitted if there is only one node type in the graph.

  • store_ids (bool, optional) – If True, it will store the raw IDs of the extracted nodes and edges in the ndata and edata of the resulting graph under name dgl.NID and dgl.EID, respectively.

Notes

This function discards the batch information. Please use dgl.DGLGraph.set_batch_num_nodes() and dgl.DGLGraph.set_batch_num_edges() on the transformed graph to maintain the information.

Examples

>>> import dgl
>>> import torch

Homogeneous Graphs or Heterogeneous Graphs with A Single Node Type

>>> g = dgl.graph((torch.tensor([0, 0, 2]), torch.tensor([0, 1, 2])))
>>> g.ndata['hv'] = torch.arange(3).float().reshape(-1, 1)
>>> g.edata['he'] = torch.arange(3).float().reshape(-1, 1)
>>> g.remove_nodes(torch.tensor([0, 1]))
>>> g
Graph(num_nodes=1, num_edges=1,
    ndata_schemes={'hv': Scheme(shape=(1,), dtype=torch.float32)}
    edata_schemes={'he': Scheme(shape=(1,), dtype=torch.float32)})
>>> g.ndata['hv']
tensor([[2.]])
>>> g.edata['he']
tensor([[2.]])

Heterogeneous Graphs with Multiple Node Types

>>> g = dgl.heterograph({
...     ('user', 'plays', 'game'): (torch.tensor([0, 1, 1, 2]),
...                                 torch.tensor([0, 0, 1, 1])),
...     ('developer', 'develops', 'game'): (torch.tensor([0, 1]),
...                                         torch.tensor([0, 1]))
...     })
>>> g.remove_nodes(torch.tensor([0, 1]))
DGLError: Node type name must be specified
if there are more than one node types.
>>> g.remove_nodes(torch.tensor([0, 1]), ntype='game')
>>> g.num_nodes('user')
3
>>> g.num_nodes('game')
0
>>> g.num_edges('plays')
0