LineGraphΒΆ

class dgl.transforms.LineGraph(backtracking=True)[source]ΒΆ

Bases: dgl.transforms.module.BaseTransform

Return the line graph of the input graph.

The line graph \(L(G)\) of a given graph \(G\) is a graph where the nodes in \(L(G)\) correspond to the edges in \(G\). For a pair of edges \((u, v)\) and \((v, w)\) in \(G\), there will be an edge from the node corresponding to \((u, v)\) to the node corresponding to \((v, w)\) in \(L(G)\).

This module only works for homogeneous graphs.

Parameters

backtracking (bool, optional) – If False, there will be an edge from the line graph node corresponding to \((u, v)\) to the line graph node corresponding to \((v, u)\).

Example

The following example uses PyTorch backend.

>>> import dgl
>>> import torch
>>> from dgl import LineGraph

Case1: Backtracking is True

>>> transform = LineGraph()
>>> g = dgl.graph(([0, 1, 1], [1, 0, 2]))
>>> g.ndata['h'] = torch.tensor([[0.], [1.], [2.]])
>>> g.edata['w'] = torch.tensor([[0.], [0.1], [0.2]])
>>> new_g = transform(g)
>>> print(new_g)
Graph(num_nodes=3, num_edges=3,
      ndata_schemes={'w': Scheme(shape=(1,), dtype=torch.float32)}
      edata_schemes={})
>>> print(new_g.edges())
(tensor([0, 0, 1]), tensor([1, 2, 0]))

Case2: Backtracking is False

>>> transform = LineGraph(backtracking=False)
>>> new_g = transform(g)
>>> print(new_g.edges())
(tensor([0]), tensor([2]))