Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

line_graph

line_graph(G, create_using=None)[source]

Return the line graph of the graph or digraph G.

The line graph of a graph G has a node for each edge in G and an edge between those nodes if the two edges in G share a common node. For directed graphs, nodes are connected only if they form a directed path of length 2.

The nodes of the line graph are 2-tuples of nodes in the original graph (or 3-tuples for multigraphs, with the key of the edge as the 3rd element).

For more discussion, see the docstring in networkx.generators.line.

Parameters:

G : graph

A NetworkX Graph, DiGraph, MultiGraph, or MultiDigraph.

Returns:

L : graph

The line graph of G.

Notes

Graph, node, and edge data are not propagated to the new graph. For undirected graphs, the nodes in G must be sortable—otherwise, the constructed line graph may not be correct.

Examples

>>> G = nx.star_graph(3)
>>> L = nx.line_graph(G)
>>> print(sorted(L.edges())) # makes a clique, K3
[((0, 1), (0, 2)), ((0, 1), (0, 3)), ((0, 3), (0, 2))]