dgl.transform.reverse

dgl.transform.reverse(g, share_ndata=False, share_edata=False)[source]

Return the reverse of a graph

The reverse (also called converse, transpose) of a directed graph is another directed graph on the same nodes with edges reversed in terms of direction.

Given a DGLGraph object, we return another DGLGraph object representing its reverse.

Notes

  • This function does not support BatchedDGLGraph objects.
  • We do not dynamically update the topology of a graph once that of its reverse changes. This can be particularly problematic when the node/edge attrs are shared. For example, if the topology of both the original graph and its reverse get changed independently, you can get a mismatched node/edge feature.
Parameters:
  • g (dgl.DGLGraph) –
  • share_ndata (bool, optional) – If True, the original graph and the reversed graph share memory for node attributes. Otherwise the reversed graph will not be initialized with node attributes.
  • share_edata (bool, optional) – If True, the original graph and the reversed graph share memory for edge attributes. Otherwise the reversed graph will not have edge attributes.

Examples

Create a graph to reverse.

>>> import dgl
>>> import torch as th
>>> g = dgl.DGLGraph()
>>> g.add_nodes(3)
>>> g.add_edges([0, 1, 2], [1, 2, 0])
>>> g.ndata['h'] = th.tensor([[0.], [1.], [2.]])
>>> g.edata['h'] = th.tensor([[3.], [4.], [5.]])

Reverse the graph and examine its structure.

>>> rg = g.reverse(share_ndata=True, share_edata=True)
>>> print(rg)
DGLGraph with 3 nodes and 3 edges.
Node data: {'h': Scheme(shape=(1,), dtype=torch.float32)}
Edge data: {'h': Scheme(shape=(1,), dtype=torch.float32)}

The edges are reversed now.

>>> rg.has_edges_between([1, 2, 0], [0, 1, 2])
tensor([1, 1, 1])

Reversed edges have the same feature as the original ones.

>>> g.edges[[0, 2], [1, 0]].data['h'] == rg.edges[[1, 0], [0, 2]].data['h']
tensor([[1],
        [1]], dtype=torch.uint8)

The node/edge features of the reversed graph share memory with the original graph, which is helpful for both forward computation and back propagation.

>>> g.ndata['h'] = g.ndata['h'] + 1
>>> rg.ndata['h']
tensor([[1.],
        [2.],
        [3.]])