Exemplo n.º 1
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)
Exemplo n.º 2
0
    def test_bellman_ford(self):
        np.random.seed(1643502758)

        for G in self.cases:
            G.data = np.random.rand(G.nnz)
            N = G.shape[0]

            for n_seeds in [int(N / 20), int(N / 10), N - 2, N]:
                if n_seeds > G.shape[0] or n_seeds < 1:
                    continue

                seeds = np.random.permutation(N)[:n_seeds]
                D_expected, S_expected = reference_bellman_ford(G, seeds)
                D_result, S_result = bellman_ford(G, seeds)

                assert_equal(D_result, D_expected)
                assert_equal(S_result, S_expected)
Exemplo n.º 3
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, 1], dtype=float)
        G = sparse.coo_matrix((w, (Edges[:, 0], Edges[:, 1])))
        distances_FROM_seed = np.array([[0., 1., 4., 3., 3.],
                                        [np.inf, 0., 3., 2., 2.],
                                        [np.inf, np.inf, 0., np.inf, np.inf],
                                        [np.inf, 1., 4., 0., 3.],
                                        [np.inf, 2., 5., 1., 0.]])

        for seed in range(5):
            distance, nearest = bellman_ford_reference(G, [seed])
            assert_equal(distance, distances_FROM_seed[seed])

            distance, nearest = bellman_ford(G, [seed])
            assert_equal(distance, distances_FROM_seed[seed])
Exemplo n.º 4
0
    def test_bellman_ford(self):
        numpy.random.seed(0)

        for G in self.cases:
            G.data = rand(G.nnz)
            N = G.shape[0]

            for n_seeds in [int(N/20), int(N/10), N-2, N]:
                if n_seeds > G.shape[0] or n_seeds < 1:
                    continue

                seeds = numpy.random.permutation(N)[:n_seeds]
                D_expected, S_expected = reference_bellman_ford(G, seeds)
                D_result, S_result = bellman_ford(G, seeds)

                assert_equal(D_result, D_expected)
                assert_equal(S_result, S_expected)
Exemplo n.º 5
0
if __name__  == '__main__':
    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, 1], dtype=float)
    A = sparse.coo_matrix((w, (Edges[:, 0], Edges[:, 1])))
    c = np.array([0,1,2,3,4])

    print('\nreference--')
    for cc in c:
        d, m = bellman_ford_reference(A, [cc])
        print(d, m)

    print('\npyamg--')
    from pyamg.graph import bellman_ford
    for cc in c:
        d, m = bellman_ford(A, [cc])
        print(d, m)

    print('\ncsgraph.bellman_ford')
    from scipy.sparse import csgraph
    for cc in c:
        d, p = csgraph.bellman_ford(A, directed=True, indices=[cc], return_predecessors=True)
        print(d.ravel(), p.ravel())