'networkx find egdes in shortest path
Can networkx tell me the edges that a shortest path travelled on? The example below gives me the output [['A', 'C']]. Is there a way to know the path travelled on the edge Charlie St?
import networkx as nx
graph = nx.DiGraph()
graph.add_node("A")
graph.add_node("B")
graph.add_edge("A", "B", street="Alpha St")
graph.add_edge("B", "A", street="Alpha St")
graph.add_node("B")
graph.add_node("C")
graph.add_edge("B", "C", street="Beta St")
graph.add_edge("C", "B", street="Beta St")
graph.add_node("C")
graph.add_node("A")
graph.add_edge("C", "A", street="Charlie St")
graph.add_edge("A", "C", street="Charlie St")
routes = list(nx.all_shortest_paths(graph, "A", "C"))
print(routes)
I could use get_edge_data for each "step" of the shortest path result. The graph I'm working doesn't have multiple edges between nodes in the same direction. So think that'll give me what I want. Is there a more "automatic" way to do it?
for r in routes:
for x, y in zip(r[:-1], r[1:]):
print(x, y, g.get_edge_data(x, y))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
