dgl.remove_self_loopΒΆ

dgl.remove_self_loop(g, etype=None)[source]ΒΆ

Remove self-loops for each node in the graph and return a new graph.

Parameters
  • g (DGLGraph) – The graph.

  • etype (str or (str, str, str), optional) –

    The type names 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.

Notes

If a node has multiple self-loops, remove them all. Do nothing for nodes without self-loops.

This function preserves the batch information.

Examples

>>> import dgl
>>> import torch

Homogeneous Graphs

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

Heterogeneous Graphs

>>> g = dgl.heterograph({
...     ('user', 'follows', 'user'): (torch.tensor([0, 1, 1, 1, 2]),
...                                   torch.tensor([0, 0, 1, 1, 1])),
...     ('user', 'plays', 'game'): (torch.tensor([0, 1]),
...                                 torch.tensor([0, 1]))
...     })
>>> g = dgl.remove_self_loop(g, etype='follows')
>>> g.num_nodes('user')
3
>>> g.num_nodes('game')
2
>>> g.num_edges('follows')
2
>>> g.num_edges('plays')
2

See also

add_self_loop