【问题标题】:Python networkx: Does the floyd_warshall_numpy work properly?Python networkx:floyd_warshall_numpy 工作正常吗?
【发布时间】:2021-04-22 13:47:27
【问题描述】:

有人可以确认我发现networkx 2.5的floyd_warshall_numpy方法的实现不正确吗?

要重现的代码是:

G = nx.balanced_tree(2, 3)
print(G.nodes())
print(nx.shortest_path(G, 2, 13))
print(nx.floyd_warshall_numpy(G, [2, 8, 13]))

我的输出是

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[2, 6, 13]
[[ 0. inf inf]
 [inf  0. inf]
 [inf inf  0.]] 

我希望为所有 [2, 8, 13] 节点对计算非 Inf 距离,因为它们之间存在最短路径。在我看来,这个实现试图以某种方式在子图中找到路径。

nx.floyd_warshall_numpy(G)

适用于所有节点。我发现这里的文档不直观。 https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.dense.floyd_warshall_numpy.html#networkx.algorithms.shortest_paths.dense.floyd_warshall_numpy

【问题讨论】:

    标签: python networkx floyd-warshall


    【解决方案1】:

    我查看了源代码,发生的情况是该算法只考虑涉及您给它的节点的路径。

    因此,由于 2 和 8 之间没有仅包含节点 2、8 和 13 的路径,因此它返回 inf

    我不确定应该如何最好地修复它 - 更新文档或方法是否更好。

    【讨论】:

    【解决方案2】:

    我怀疑你的代码是用一组节点 [2, 6, 13] 而不是 [2, 8, 13] 说的 print(nx.floyd_warshall_numpy(G, [2, 6, 13]))。对吗?

    G = nx.balanced_tree(2, 3) 
    print(G.nodes()) 
    print(nx.shortest_path(G, 2, 13)) 
    print(nx.floyd_warshall_numpy(G, [2, 6, 13]))                                                                         
    

    生产

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
    [2, 6, 13]
    [[0. 1. 2.]
     [1. 0. 1.]
     [2. 1. 0.]]
    

    【讨论】:

    • 没有。我的目标是在图 G 中获取节点 [2, 8, 13] 之间的距离矩阵,其中 G 具有节点 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12、13、14]
    【解决方案3】:

    我认为问题在于文档中对 nodelist 的解释。 根据文档:
    nodelist (list, optional) – 行和列按 nodelist 中的节点排序。如果 nodelist 为 None 则排序由 G.nodes() 生成。 但它没有提到节点列表实际上修改了图形。它使用节点列表中的节点制作另一个图。

    假设您的图表有 4 个节点 [1,2,3,4],但您将节点列表定义为 [2,3,4]。当您阅读文档时,您认为该函数将计算原始图中节点 2、3 和 4 之间的距离矩阵。但是,它似乎从图中删除了节点 1(从技术上改变了原始图),然后计算了 2、3 和 4 之间的距离矩阵。如果节点 1 连接节点 2 和 3,则可能会出现问题。

    Sample Graph with 4 nodes

    Dist_Mat=nx.algorithms.shortest_paths.dense.floyd_warshall_numpy(G,[1,2,3,4])  
    print(Dist_Mat)  
    [[0. 1. 1. 1.]
     [1. 0. 2. 2.]
     [1. 2. 0. 2.]
     [1. 2. 2. 0.]]
    
    Dist_Mat=nx.algorithms.shortest_paths.dense.floyd_warshall_numpy(G,[2,3,4])  
    print(Dist_Mat)
    [[ 0. inf inf]
     [inf  0. inf]
     [inf inf  0.]]
    

    请在下面找到代码

    import networkx as nx
    import matplotlib.pyplot as plt
    import nxviz as nv
    G=nx.Graph()
    G.add_node(1)
    G.nodes[1]['N']=10
    G.add_nodes_from([(2,{'N':20}),3,4])
    G.add_edge(1,2)
    G.edges[1,2]['E']=120
    G.add_edges_from([(1,3,{'E':130}),(1,4)])
    G.nodes()
    pos = {0: (0, 0),
           1: (1, 0),
           2: (0, 1),
           3: (1, 1),
           4: (0.5, 2.0)}
    print(G.nodes(data=True))
    print(G.edges(data=True))
    nx.draw(G, pos, with_labels=True, font_weight='bold')
    FIG=nv.CircosPlot(G,node_size=1)
    FIG.draw();plt.show()
    
    Dist_Mat=nx.algorithms.shortest_paths.dense.floyd_warshall_numpy(G,[1, 2,3,4])
    print(Dist_Mat)
    Dist_Mat=nx.algorithms.shortest_paths.dense.floyd_warshall_numpy(G,[2,3,4])
    print(Dist_Mat)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多