我的for循环出现问题,使用networkx查找子路径的最短路径长度

时间:2020-04-19 21:47:11

标签: python for-loop 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,)

路径和路径长度很好,但是在辅助列表中,我期望输出是

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

我知道双for-loop存在问题,因为有5个图和5个路径,辅助列表包含25个元素,但是我想根据他的图使用路径(路径1和图1,路径2与图表2等),因此aux的输出将与上面的相同。 我对使用for-loop有点陌生,所以我希望您能为我提供帮助,或者,如果有另一种方法可以帮助我实现目标,我们将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用zip函数在相应的对(图形,路径)上进行迭代。

示例:

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'))
相关问题