I have a graph composed by two different sets of edges. The first set is made by edges of weight 1 (list 1). The second set is made by edges of weight 2 (list 2). First, I create the graph with networkx and then use add_edges_from to add list 1 and list 2. I would like to compute all the shortest paths in this weighted graph. Basically I'm looking for the analogous of "all_shortest_paths" but with weights (looks like "dijkstra" module does not allow you to know all the possible routes between a given source and a given target). If I try to use "all_shortest_path" with weighted links (3-tuples, the two nodes and the weight) I get the error . Can anybody help me? Thanks a lot!
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Here is a simple example to show how all_shortest_paths() works
import networkx as nx
import StringIO
edges = StringIO.StringIO("""
a b 1
a bb 1
b c 2
bb c 2
c d 1
a d 10""")
G = nx.read_weighted_edgelist(edges, nodetype=str)
print list(nx.all_shortest_paths(G, 'a', 'd', weight='weight'))
# [['a', 'b', 'c', 'd'], ['a', 'bb', 'c', 'd']]