Esempio n. 1
0
def reference_bellman_ford(G, seeds):
    """
    G : sparse matrix
    seeds : array
    """
    G = G.tocoo()
    N = G.shape[0]

    seeds = np.asarray(seeds, dtype='intc')

    distances = np.inf * np.ones(N, dtype=G.dtype)
    distances[seeds] = 0

    nearest_seed = -1 * np.ones(N, dtype='intc')
    nearest_seed[seeds] = seeds

    while True:
        update = False

        for (i, j, w) in zip(G.row, G.col, G.data):

            if distances[j] + w < distances[i]:
                update = True
                distances[i] = distances[j] + w
                nearest_seed[i] = nearest_seed[j]

        if not update:
            break

    # temporary: swap infs for max_value()
    distances[np.where(distances == np.inf)[0]] = max_value(G.dtype)
    return (distances, nearest_seed)
Esempio n. 2
0
def reference_bellman_ford(G, seeds):
    G = G.tocoo()
    N = G.shape[0]

    seeds = asarray(seeds, dtype='intc')

    distances = empty(N, dtype=G.dtype)
    distances[:] = max_value(G.dtype)
    distances[seeds] = 0

    nearest_seed = empty(N, dtype='intc')
    nearest_seed[:] = -1
    nearest_seed[seeds] = seeds

    while True:
        update = False

        for (i, j, v) in zip(G.row, G.col, G.data):

            if distances[j] + v < distances[i]:
                update = True
                distances[i] = distances[j] + v
                nearest_seed[i] = nearest_seed[j]

        if not update:
            break

    return (distances, nearest_seed)
Esempio n. 3
0
def reference_bellman_ford(G, seeds):
    G = G.tocoo()
    N = G.shape[0]

    seeds = asarray(seeds, dtype='intc')

    distances = empty(N, dtype=G.dtype)
    distances[:] = max_value(G.dtype)
    distances[seeds] = 0

    nearest_seed = empty(N, dtype='intc')
    nearest_seed[:] = -1
    nearest_seed[seeds] = seeds

    while True:
        update = False

        for (i, j, v) in zip(G.row, G.col, G.data):

            if distances[j] + v < distances[i]:
                update = True
                distances[i] = distances[j] + v
                nearest_seed[i] = nearest_seed[j]

        if not update:
            break

    return (distances, nearest_seed)
Esempio n. 4
0
    def test_bellman_ford_reference(self):
        Edges = np.array([[1, 4], [3, 1], [1, 3], [0, 1], [0, 2], [3, 2],
                          [1, 2], [4, 3]])
        w = np.array([2, 1, 2, -1, 4, 5, 3, -3], dtype=float)
        G = sparse.coo_matrix((w, (Edges[:, 0], Edges[:, 1])))
        #             distance
        # seed  0 :  [ 0. -1.  2. -2.  1.]
        # seed  1 :  [inf  0.  3. -1.  2.]
        # seed  2 :  [inf inf  0. inf inf]
        # seed  3 :  [inf  1.  4.  0.  3.]
        # seed  4 :  [inf -2.  1. -3.  0.]
        distances_FROM_seed = np.array([[0., -1., 2., -2., 1.],
                                        [np.inf, 0., 3., -1., 2.],
                                        [np.inf, np.inf, 0., np.inf, np.inf],
                                        [np.inf, 1., 4., 0., 3.],
                                        [np.inf, -2., 1., -3., 0.]])
        distances_FROM_seed[np.where(
            distances_FROM_seed == np.inf)] = max_value(G.dtype)
        for seed in range(5):
            distance, nearest = reference_bellman_ford(G.T, [seed])
            assert_equal(distance, distances_FROM_seed[seed])

            distance, nearest = bellman_ford(G.T, [seed])
            assert_equal(distance, distances_FROM_seed[seed])

        # seeds [0,1,2,3,4]
        # distance to closest: [-2. -1.  0.  0. -3.]
        #             closest: [3 3 2 3 3]
        distance_TO_closest = np.array([-2., -1., 0., 0., -3.])
        ref_closest = np.array([3, 3, 2, 3, 3])

        distance, closest = reference_bellman_ford(G, [0, 1, 2, 3, 4])
        assert_equal(distance, distance_TO_closest)
        assert_equal(closest, ref_closest)

        distance, closest = bellman_ford(G, [0, 1, 2, 3, 4])
        assert_equal(distance, distance_TO_closest)
        assert_equal(closest, ref_closest)