Reputation: 123
I have an unweighted graph which is basically a binary tree. Every node has a parent, and the main parent is parentNode. There are many crosslinks between the nodes. I want to find the shortest path from parentNode to every other Node.
import networkx as nx
G = nx.Graph()
G.add_nodes_from(nodeList)
G.add_edges_from(edgeList)
optimized_pathDict = nx.single_source_shortest_path(G,"parentNode")
How can i turn the optimized_pathDict into edgelist without parsing the optimized_pathDict dictionary ?
Upvotes: 0
Views: 143
Reputation: 30258
It is quite easy to generate the list of edges associated with the return paths:
edges = {e for p in pathDict.values() for e in zip(p[:-1],p[1:])}
oG = Graph(edges)
You can also add the paths directly:
oG = Graph()
for p in pathDict.values():
oG.add_path(p)
Upvotes: 1