2017-06-21 56 views
1

使用networkX來調用端點之間的所有最短路徑。NetworkX不對稱權重

在一個示例拓撲像graph1,其中權重是不對稱的R1 <之間 - > R2,我希望看到從R1只有一個最短路徑R3, 但是我看到兩(見下文)。我的目標是複製像OSPF或IS-IS那樣的協議。

有什麼辦法可以達到這個目的嗎? (度量/權重不能被修改,是直接從路由器獲得)

實施例:

>>> from networkx import nx 
>>> graph1 = { 
...  
...  'R1':{'R2':{'weight':50000},'R3':{'weight':200}}, 
...  'R2':{'R1':{'weight':100},'R3':{'weight':100}}, 
...  'R3':{'R1':{'weight':200},'R2':{'weight':100}} 
... } 
>>> network_graph = nx.from_dict_of_dicts(graph1) 
>>> print [p for p in 
nx.all_shortest_paths(network_graph,source='R1',target='R3', weight='weight')] 
[['R1', 'R3'], ['R1', 'R2', 'R3']] 

回答

1

在networkx默認的圖形不是一個有向圖(這是其可具有不對稱的邊緣的網絡被稱爲),你需要指定明確使用:

network_graph = nx.from_dict_of_dicts(graph1, create_using=nx.DiGraph()) 

哪樣正確答案:

print [p for p in nx.all_shortest_paths(network_graph,source='R1',target='R3', weight='weight')] 
[['R1', 'R3']] 
+0

非常感謝你的回答。工作正常 ! – psagrera