哪个 python 包实现了 Bellman-Ford 最短路径算法?

Which python package implements the Bellman-Ford shortest path algorithm?

哪个 python 包实现了 Bellman-Ford 最短路径算法?

给定一个起始节点 i 和一个负权重的邻接矩阵 G 我想找到从 i 到另一个节点 j 的最短路径。例如。我的图表看起来像:

import numpy
G = numpy.array([[ 0.  ,  0.55,  1.22],
                 [-0.54,  0.  ,  0.63],
                 [-1.3 , -0.63,  0.  ]])

我只能找到一个 all-pairs shortest path 实现,考虑到我的图很大而且我只需要一对节点的最短路径,这对我的需求来说似乎太浪费了。性能对我来说很重要,因为我会将它用于数千张图表。

因此,我正在四处寻找 Bellman-Ford 实施方案 -- 有人见过吗?

自己动手

def bellmanFord(source, weights):
    '''
    This implementation takes in a graph and fills two arrays
    (distance and predecessor) with shortest-path (less cost/distance/metric) information

    https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
    '''
    n = weights.shape[0]

    # Step 1: initialize graph
    distance = np.empty(n)
    distance.fill(float('Inf'))      # At the beginning, all vertices have a weight of infinity
    predecessor = np.empty(n)
    predecessor.fill(float('NaN'))   # And a null predecessor

    distance[source] = 0             # Except for the Source, where the Weight is zero

    # Step 2: relax edges repeatedly
    for _ in xrange(1, n):
        for (u, v), w in np.ndenumerate(weights):
        if distance[u] + w < distance[v]:
        distance[v] = distance[u] + w
    predecessor[v] = u

    # Step 3: check for negative-weight cycles
    for (u, v), w in np.ndenumerate(weights):
        if distance[u] + w < distance[v]:
        raise ValueError("Graph contains a negative-weight cycle")

    return distance, predecessor