dgl.remove_nodes

dgl.remove_nodes(g, nids, ntype=None)[source]

Remove the specified nodes and return a new graph.

Also delete the features. Edges that connect from/to the nodes will be removed as well. After the removal, DGL re-labels the remaining nodes and edges with IDs from 0.

Parameters
  • nids (int, Tensor, iterable[int]) – The nodes to be removed.

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

Returns

The graph with nodes deleted.

Return type

DGLGraph

Examples

>>> import dgl
>>> import torch

Homogeneous Graphs

>>> 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 = dgl.remove_nodes(g, 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

>>> 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 = dgl.remove_nodes(g, torch.tensor([0, 1]), ntype='game')
>>> g.num_nodes('user')
3
>>> g.num_nodes('game')
0
>>> g.num_edges('plays')
0