Bases: BaseTransform

Add a reverse edge $$(i,j)$$ for each edge $$(j,i)$$ in the input graph and return a new graph.

For a heterogeneous graph, it adds a βreverseβ edge type for each edge type to hold the reverse edges. For example, for a canonical edge type (βAβ, βrβ, βBβ), it adds a canonical edge type (βBβ, βrev_rβ, βAβ).

Parameters:
• copy_edata (bool, optional) β If True, the features of the reverse edges will be identical to the original ones.

• sym_new_etype (bool, optional) β If False, it will not add a reverse edge type if the source and destination node type in a canonical edge type are identical. Instead, it will directly add edges to the original edge type.

Example

The following example uses PyTorch backend.

>>> import dgl
>>> import torch
>>> from dgl import AddReverse


Case1: Add reverse edges for a homogeneous graph

>>> transform = AddReverse()
>>> g = dgl.graph(([0], [1]))
>>> g.edata['w'] = torch.ones(1, 2)
>>> new_g = transform(g)
>>> print(new_g.edges())
(tensor([0, 1]), tensor([1, 0]))
>>> print(new_g.edata['w'])
tensor([[1., 1.],
[0., 0.]])


Case2: Add reverse edges for a homogeneous graph and copy edata

>>> transform = AddReverse(copy_edata=True)
>>> new_g = transform(g)
>>> print(new_g.edata['w'])
tensor([[1., 1.],
[1., 1.]])


Case3: Add reverse edges for a heterogeneous graph

>>> g = dgl.heterograph({
...     ('user', 'plays', 'game'): ([0, 1], [1, 1]),
...     ('user', 'follows', 'user'): ([1, 2], [2, 2])
... })
>>> new_g = transform(g)
>>> print(new_g.canonical_etypes)
[('game', 'rev_plays', 'user'), ('user', 'follows', 'user'), ('user', 'plays', 'game')]
>>> print(new_g.edges(etype='rev_plays'))
(tensor([1, 1]), tensor([0, 1]))
>>> print(new_g.edges(etype='follows'))
(tensor([1, 2, 2, 2]), tensor([2, 2, 1, 2]))