Last active
December 21, 2015 04:48
-
-
Save chebee7i/6251876 to your computer and use it in GitHub Desktop.
Quick and dirty line graph construction for multidigraphs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
def line_graph(G, create_using=None): | |
# This code works only for multidigraphs | |
if create_using is None: | |
L = G.__class__() | |
else: | |
# Already instantiated. | |
L = create_using | |
for u, v, key, data in G.out_edges_iter(data=True, keys=True): | |
node = (u, v, key) | |
L.add_node(node) | |
for from_node in L.nodes_iter(): | |
# The to_node of the original machine determines the new edges | |
edges = G.out_edges_iter(from_node[1], data=True, keys=True) | |
for u, v, key, data in edges: | |
to_node = (u, v, key) | |
L.add_edge(from_node, to_node) | |
return L | |
def test1(): | |
G = nx.MultiDiGraph() | |
G.add_edges_from([(0,0), (0,1), (1,0), (1,1)]) | |
nx.draw_pydot(G) | |
L = line_graph(G) | |
nx.draw_pydot(L) | |
def test2(): | |
G = nx.MultiDiGraph() | |
G.add_edges_from([(0,0), (0,1), (1,0), (1,1), (0,1), (1,1)]) | |
nx.draw_pydot(G) | |
L = line_graph(G) | |
nx.draw_pydot(L) | |
test1() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment