簡體   English   中英

我的 for 循環有問題,使用 networkx 查找子路徑的最短路徑長度

[英]Problem with my for loop, finding the shortest path length of a subpath using networkx

所以我試圖創建一個代碼來使用 Networkx 找到子路徑的最短路徑長度,基本上我的代碼所做的是它需要一個 3D 數組來創建圖形,然后將它們保存在一個列表中,這樣我就可以使用這個列表來使用networkx找到the shortest paththe shortest path length

之后,根據列表中的信息,我想在圖中找到子路徑的最短路徑長度,如果路徑的len小於 3,則最短路徑在同一源節點和目標之間節點(因此長度將為零),如果len大於該值,則應該找到路徑中的第二個節點和倒數第二個節點(類似於路徑的“中心”)之間的shortest path length ,我的代碼在下面

import networkx as nx
import numpy as np


arr= np.array([[[  0., 191.,  16.,  17.,  15.,  18.,  18.],
                [  0.,   0.,   0.,   0.,   0.,   0.,   0.],
                [  0., 141.,   0.,   0.,   0.,  18.,   0.],
                [  0., 138.,   0.,   0.,   0.,   0.,  19.],
                [  0.,  80.,   0.,   0.,   0.,   0.,  15.],
                [  0., 130.,  11.,   0.,   0.,   0.,  19.],
                [  0., 135.,   0.,  12.,  16.,  12.,   0.]],

               [[  0., 156.,  17.,  13.,  19.,  10.,  11.],
                [  0.,   0.,   0.,   0.,   0.,   0.,   0.],
                [  0.,  21.,   0.,   0.,   0.,   6.,   0.],
                [  0., 147.,   0.,   0.,   0.,   0.,   4.],
                [  0., 143.,   0.,   0.,   0.,   0.,   6.],
                [  0.,  69.,   4.,   0.,   0.,   0.,   7.],
                [  0.,  87.,   0.,   1.,   5.,   9.,   0.]],

               [[  0., 161.,  18.,  16.,  13.,  13.,  17.],
                [  0.,   0.,   0.,   0.,   0.,   0.,   0.],
                [  0., 138.,   0.,   0.,   0.,  21.,   0.],
                [  0.,  64.,   0.,   0.,   0.,   0.,  29.],
                [  0.,  23.,   0.,   0.,   0.,   0.,  29.],
                [  0.,   2.,  24.,   0.,   0.,   0.,  27.],
                [  0.,  61.,   0.,  24.,  29.,  26.,   0.]],

               [[  0., 163.,  12.,  13.,  17.,  19.,  13.],
                [  0.,   0.,   0.,   0.,   0.,   0.,   0.],
                [  0., 142.,   0.,   0.,   0.,  35.,   0.],
                [  0., 122.,   0.,   0.,   0.,   0.,  31.],
                [  0.,  72.,   0.,   0.,   0.,   0.,  36.],
                [  0.,  50.,  39.,   0.,   0.,   0.,  31.],
                [  0.,   4.,   0.,  38.,  39.,  35.,   0.]],

               [[  0., 180.,  17.,  19.,  13.,  18.,  15.],
                [  0.,   0.,   0.,   0.,   0.,   0.,   0.],
                [  0.,  44.,   0.,   0.,   0.,  46.,   0.],
                [  0.,  27.,   0.,   0.,   0.,   0.,  47.],
                [  0.,  81.,   0.,   0.,   0.,   0.,  45.],
                [  0., 116.,  48.,   0.,   0.,   0.,  45.],
                [  0.,  16.,   0.,  42.,  49.,  49.,   0.]]])

graphs= [] 
paths = []
pathlenght = []
aux = []

for i in arr :
   graphs.append(nx.from_numpy_array(i, create_using = nx.DiGraph))  #List of graphs created by the 3D array

for j in graphs:
    paths.append(nx.shortest_path(j, 0, 1, weight = 'weight')) #Shortest paths of every graph
    pathlenght.append(nx.shortest_path_length(j, 0, 1, weight = 'weight')) #Shortest path length of every graphs

for i in graphs:
    for j in paths:
        if len(j) <= 3:
            aux.append(nx.shortest_path_length(i, j[0], j[0], weight = 'weight'))
        else:
            aux.append(nx.shortest_path_length(i, j[1], j[-2], weight = 'weight'))

print(paths)         # [[0, 4, 1], [0, 5, 2, 1], [0, 5, 1], [0, 6, 1], [0, 6, 1]]
print(pathlenght)    # [95.0, 35.0, 15.0, 17.0, 31.0]
print(aux)           #[ 0. 11.  0.  0.  0.  0.  4.  0.  0.  0.  0. 24.  0.  0.  0.  0. 39.  0. 0.  0.  0. 48.  0.  0.  0.]  shape = (25,)

路徑和路徑長度很好,但在輔助列表中我期望 output 是

#aux = [0, 4.0, 0, 0, 0] 

我知道問題出在雙for-loop ,因為有 5 個圖和 5 個路徑,輔助列表有 25 個元素,但我想根據他的圖使用路徑(路徑 1 與圖 1,路徑 2 與圖 2等等等等)所以aux的 output 將與上面相同。 我對使用for-loop有點新所以我希望你能幫助我,或者如果有另一種方法可以做我想要實現的目標,任何幫助將不勝感激,謝謝!

您可以使用zip function 遍歷相應的對(圖形、路徑)。

例子:

for g, path in zip(graphs, paths):
    if len(path) <= 3:
        aux.append(nx.shortest_path_length(g, path[0], path[0], weight = 'weight'))
    else:
         aux.append(nx.shortest_path_length(g, path[1], path[-2], weight = 'weight'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM