dgl.DGLGraph.remove_edges

DGLGraph.remove_edges(eids, etype=None, store_ids=False)

Remove multiple edges with the specified edge type

Nodes will not be removed. After removing edges, the rest edges will be re-indexed using consecutive integers from 0, with their relative order preserved.

The features for the removed edges will be removed accordingly.

Parameters
  • eids (int, tensor, numpy.ndarray, list) – IDs for the edges to remove.

  • etype (str or tuple of str, optional) – The type of the edges to remove. Can be omitted if there is only one edge 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 preserves the batch information.

Examples

>>> import dgl
>>> import torch

Homogeneous Graphs or Heterogeneous Graphs with A Single Edge Type

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

Removing edges from a batched graph preserves batch information.

>>> g = dgl.graph((torch.tensor([0, 0, 2]), torch.tensor([0, 1, 2])))
>>> g2 = dgl.graph((torch.tensor([1, 2, 3]), torch.tensor([1, 3, 4])))
>>> bg = dgl.batch([g, g2])
>>> bg.batch_num_edges()
tensor([3, 3])
>>> bg.remove_edges([1, 4])
>>> bg.batch_num_edges()
tensor([2, 2])

Heterogeneous Graphs with Multiple Edge 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_edges(torch.tensor([0, 1]))
DGLError: Edge type name must be specified
if there are more than one edge types.
>>> g.remove_edges(torch.tensor([0, 1]), 'plays')
>>> g.edges('all', etype='plays')
(tensor([0, 1]), tensor([0, 0]), tensor([0, 1]))