LineGraphο
- class dgl.transforms.LineGraph(backtracking=True)[source]ο
Bases:
BaseTransform
Return the line graph of the input graph.
The line graph
of a given graph is a graph where the nodes in correspond to the edges in . For a pair of edges and in , there will be an edge from the node corresponding to to the node corresponding to in .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
to the line graph node corresponding to .
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]))