正如網絡x中的@Gabor Csardi所建議的那樣,要添加類型1的邊:將屬性type_1添加到所需的值,並將屬性type_2添加到最大值int(在最短路徑計算中忽略,就好像它不存在)。同樣,執行同樣的操作來創建type_2的邊緣。
下面的代碼顯示了一個簡單的圖表,其中從1到4的最短路徑在type_1邊緣中短於2,在type_2邊緣中短於3。
g=nx.MultiDiGraph()
g.add_edge(1,2,type1=2,type2=sys.maxint) # add edge of type 1
g.add_edge(1,2,type1=sys.maxint,type2=3) # add edge of type 2
g.add_edge(2,4,type1=2,type2=sys.maxint)
g.add_edge(2,4,type1=sys.maxint,type2=4)
g.add_edge(3,4,type1=3,edge_type2=sys.maxint)
g.add_edge(3,4,type1=sys.maxint,type2=1)
g.add_edge(1,3,type1=sys.maxint,type2=1)
print nx.shortest_path(g,1,4,weight='type1')
print nx.shortest_path(g,1,4,weight='type2')
結果:
[1, 2, 4]
[1, 3, 4]
另外,附上所創建的圖表能夠想象它更容易。
在igraph中,根據類型設置邊的權重。即將所有類型1的邊權重設置爲1,並將所有其他邊權重設置爲無窮大。 – 2015-04-07 01:29:32