Exemplo n.º 1
0
    def test_others(self):
        (P, D) = nx.bellman_ford(self.XG, 's')
        assert_equal(P['v'], 'u')
        assert_equal(D['v'], 9)
        (P, D) = nx.goldberg_radzik(self.XG, 's')
        assert_equal(P['v'], 'u')
        assert_equal(D['v'], 9)

        G = nx.path_graph(4)
        assert_equal(nx.bellman_ford(G, 0),
                     ({0: None, 1: 0, 2: 1, 3: 2}, {0: 0, 1: 1, 2: 2, 3: 3}))
        assert_equal(nx.goldberg_radzik(G, 0),
                     ({0: None, 1: 0, 2: 1, 3: 2}, {0: 0, 1: 1, 2: 2, 3: 3}))
        assert_equal(nx.bellman_ford(G, 3),
                     ({0: 1, 1: 2, 2: 3, 3: None}, {0: 3, 1: 2, 2: 1, 3: 0}))
        assert_equal(nx.goldberg_radzik(G, 3),
                     ({0: 1, 1: 2, 2: 3, 3: None}, {0: 3, 1: 2, 2: 1, 3: 0}))

        G = nx.grid_2d_graph(2, 2)
        pred, dist = nx.bellman_ford(G, (0, 0))
        assert_equal(sorted(pred.items()),
                     [((0, 0), None), ((0, 1), (0, 0)),
                      ((1, 0), (0, 0)), ((1, 1), (0, 1))])
        assert_equal(sorted(dist.items()),
                     [((0, 0), 0), ((0, 1), 1), ((1, 0), 1), ((1, 1), 2)])
        pred, dist = nx.goldberg_radzik(G, (0, 0))
        assert_equal(sorted(pred.items()),
                     [((0, 0), None), ((0, 1), (0, 0)),
                      ((1, 0), (0, 0)), ((1, 1), (0, 1))])
        assert_equal(sorted(dist.items()),
                     [((0, 0), 0), ((0, 1), 1), ((1, 0), 1), ((1, 1), 2)])
Exemplo n.º 2
0
 def test_multigraph(self):
     assert_equal(nx.bellman_ford_path(self.MXG, 's', 'v'), ['s', 'x', 'u', 'v'])
     assert_equal(nx.bellman_ford_path_length(self.MXG, 's', 'v'), 9)
     assert_equal(nx.single_source_bellman_ford_path(self.MXG, 's')['v'], ['s', 'x', 'u', 'v'])
     assert_equal(nx.single_source_bellman_ford_path_length(self.MXG, 's')['v'], 9)
     D, P = nx.single_source_bellman_ford(self.MXG, 's', target='v')
     assert_equal(D, 9)
     assert_equal(P, ['s', 'x', 'u', 'v'])
     P, D = nx.bellman_ford_predecessor_and_distance(self.MXG, 's')
     assert_equal(P['v'], ['u'])
     assert_equal(D['v'], 9)
     P, D = nx.goldberg_radzik(self.MXG, 's')
     assert_equal(P['v'], 'u')
     assert_equal(D['v'], 9)
     assert_equal(nx.bellman_ford_path(self.MXG4, 0, 2), [0, 1, 2])
     assert_equal(nx.bellman_ford_path_length(self.MXG4, 0, 2), 4)
     assert_equal(nx.single_source_bellman_ford_path(self.MXG4, 0)[2], [0, 1, 2])
     assert_equal(nx.single_source_bellman_ford_path_length(self.MXG4, 0)[2], 4)
     D, P = nx.single_source_bellman_ford(self.MXG4, 0, target=2)
     assert_equal(D, 4)
     assert_equal(P, [0, 1, 2])
     P, D = nx.bellman_ford_predecessor_and_distance(self.MXG4, 0)
     assert_equal(P[2], [1])
     assert_equal(D[2], 4)
     P, D = nx.goldberg_radzik(self.MXG4, 0)
     assert_equal(P[2], 1)
     assert_equal(D[2], 4)
Exemplo n.º 3
0
    def test_others(self):
        assert_equal(nx.bellman_ford_path(self.XG, 's', 'v'), ['s', 'x', 'u', 'v'])
        assert_equal(nx.bellman_ford_path_length(self.XG, 's', 'v'), 9)
        assert_equal(nx.single_source_bellman_ford_path(self.XG, 's')['v'], ['s', 'x', 'u', 'v'])
        assert_equal(dict(nx.single_source_bellman_ford_path_length(self.XG, 's'))['v'], 9)
        D, P = nx.single_source_bellman_ford(self.XG, 's', target='v')
        assert_equal(D['v'], 9)
        assert_equal(P['v'], ['s', 'x', 'u', 'v'])
        (P, D) = nx.bellman_ford_predecessor_and_distance(self.XG, 's')
        assert_equal(P['v'], ['u'])
        assert_equal(D['v'], 9)
        (P, D) = nx.goldberg_radzik(self.XG, 's')
        assert_equal(P['v'], 'u')
        assert_equal(D['v'], 9)

        G = nx.path_graph(4)
        assert_equal(nx.single_source_bellman_ford_path(G, 0),
                     {0: [0], 1: [0, 1], 2: [0, 1, 2], 3: [0, 1, 2, 3]})
        assert_equal(dict(nx.single_source_bellman_ford_path_length(G, 0)),
                     {0: 0, 1: 1, 2: 2, 3: 3})
        assert_equal(nx.single_source_bellman_ford(G, 0),
                     ({0: 0, 1: 1, 2: 2, 3: 3}, {0: [0], 1: [0, 1], 2: [0, 1, 2], 3: [0, 1, 2, 3]}))
        assert_equal(nx.bellman_ford_predecessor_and_distance(G, 0),
                     ({0: [None], 1: [0], 2: [1], 3: [2]}, {0: 0, 1: 1, 2: 2, 3: 3}))
        assert_equal(nx.goldberg_radzik(G, 0),
                     ({0: None, 1: 0, 2: 1, 3: 2}, {0: 0, 1: 1, 2: 2, 3: 3}))
        assert_equal(nx.single_source_bellman_ford_path(G, 3),
                     {0: [3, 2, 1, 0], 1: [3, 2, 1], 2: [3, 2], 3: [3]})
        assert_equal(dict(nx.single_source_bellman_ford_path_length(G, 3)),
                     {0: 3, 1: 2, 2: 1, 3: 0})
        assert_equal(nx.single_source_bellman_ford(G, 3),
                     ({0: 3, 1: 2, 2: 1, 3: 0}, {0: [3, 2, 1, 0], 1: [3, 2, 1], 2: [3, 2], 3: [3]}))
        assert_equal(nx.bellman_ford_predecessor_and_distance(G, 3),
                     ({0: [1], 1: [2], 2: [3], 3: [None]}, {0: 3, 1: 2, 2: 1, 3: 0}))
        assert_equal(nx.goldberg_radzik(G, 3),
                     ({0: 1, 1: 2, 2: 3, 3: None}, {0: 3, 1: 2, 2: 1, 3: 0}))

        G = nx.grid_2d_graph(2, 2)
        dist, path = nx.single_source_bellman_ford(G, (0, 0))
        assert_equal(sorted(dist.items()),
                     [((0, 0), 0), ((0, 1), 1), ((1, 0), 1), ((1, 1), 2)])
        assert_equal(sorted(path.items()),
                     [((0, 0), [(0, 0)]), ((0, 1), [(0, 0), (0, 1)]),
                      ((1, 0), [(0, 0), (1, 0)]),  ((1, 1), [(0, 0), (0, 1), (1, 1)])])
        pred, dist = nx.bellman_ford_predecessor_and_distance(G, (0, 0))
        assert_equal(sorted(pred.items()),
                     [((0, 0), [None]), ((0, 1), [(0, 0)]),
                      ((1, 0), [(0, 0)]), ((1, 1), [(0, 1), (1, 0)])])
        assert_equal(sorted(dist.items()),
                     [((0, 0), 0), ((0, 1), 1), ((1, 0), 1), ((1, 1), 2)])
        pred, dist = nx.goldberg_radzik(G, (0, 0))
        assert_equal(sorted(pred.items()),
                     [((0, 0), None), ((0, 1), (0, 0)),
                      ((1, 0), (0, 0)), ((1, 1), (0, 1))])
        assert_equal(sorted(dist.items()),
                     [((0, 0), 0), ((0, 1), 1), ((1, 0), 1), ((1, 1), 2)])
Exemplo n.º 4
0
 def test_multigraph(self):
     P, D = nx.bellman_ford(self.MXG, 's')
     assert_equal(P['v'], 'u')
     assert_equal(D['v'], 9)
     P, D = nx.goldberg_radzik(self.MXG, 's')
     assert_equal(P['v'], 'u')
     assert_equal(D['v'], 9)
     P, D = nx.bellman_ford(self.MXG4, 0)
     assert_equal(P[2], 1)
     assert_equal(D[2], 4)
     P, D = nx.goldberg_radzik(self.MXG4, 0)
     assert_equal(P[2], 1)
     assert_equal(D[2], 4)
Exemplo n.º 5
0
 def test_single_node_graph(self):
     G = nx.DiGraph()
     G.add_node(0)
     assert_equal(nx.bellman_ford(G, 0), ({0: None}, {0: 0}))
     assert_equal(nx.goldberg_radzik(G, 0), ({0: None}, {0: 0}))
     assert_raises(KeyError, nx.bellman_ford, G, 1)
     assert_raises(KeyError, nx.goldberg_radzik, G, 1)
Exemplo n.º 6
0
 def test_single_node_graph(self):
     G = nx.DiGraph()
     G.add_node(0)
     assert_equal(nx.single_source_bellman_ford_path(G, 0), {0: [0]})
     assert_equal(nx.single_source_bellman_ford_path_length(G, 0), {0: 0})
     assert_equal(nx.single_source_bellman_ford(G, 0), ({0: 0}, {0: [0]}))
     assert_equal(nx.bellman_ford_predecessor_and_distance(G, 0), ({0: []}, {0: 0}))
     assert_equal(nx.goldberg_radzik(G, 0), ({0: None}, {0: 0}))
Exemplo n.º 7
0
 def test_single_node_graph(self):
     G = nx.DiGraph()
     G.add_node(0)
     assert nx.single_source_bellman_ford_path(G, 0) == {0: [0]}
     assert nx.single_source_bellman_ford_path_length(G, 0) == {0: 0}
     assert nx.single_source_bellman_ford(G, 0) == ({0: 0}, {0: [0]})
     assert nx.bellman_ford_predecessor_and_distance(G, 0) == ({
         0: []
     }, {
         0: 0
     })
     assert nx.goldberg_radzik(G, 0) == ({0: None}, {0: 0})
Exemplo n.º 8
0
 def test_path_graph(self):
     G = nx.path_graph(4)
     assert_equal(nx.single_source_bellman_ford_path(G, 0),
                  {0: [0], 1: [0, 1], 2: [0, 1, 2], 3: [0, 1, 2, 3]})
     assert_equal(nx.single_source_bellman_ford_path_length(G, 0),
                  {0: 0, 1: 1, 2: 2, 3: 3})
     assert_equal(nx.single_source_bellman_ford(G, 0),
                  ({0: 0, 1: 1, 2: 2, 3: 3}, {0: [0], 1: [0, 1], 2: [0, 1, 2], 3: [0, 1, 2, 3]}))
     assert_equal(nx.bellman_ford_predecessor_and_distance(G, 0),
                  ({0: [], 1: [0], 2: [1], 3: [2]}, {0: 0, 1: 1, 2: 2, 3: 3}))
     assert_equal(nx.goldberg_radzik(G, 0),
                  ({0: None, 1: 0, 2: 1, 3: 2}, {0: 0, 1: 1, 2: 2, 3: 3}))
     assert_equal(nx.single_source_bellman_ford_path(G, 3),
                  {0: [3, 2, 1, 0], 1: [3, 2, 1], 2: [3, 2], 3: [3]})
     assert_equal(nx.single_source_bellman_ford_path_length(G, 3),
                  {0: 3, 1: 2, 2: 1, 3: 0})
     assert_equal(nx.single_source_bellman_ford(G, 3),
                  ({0: 3, 1: 2, 2: 1, 3: 0}, {0: [3, 2, 1, 0], 1: [3, 2, 1], 2: [3, 2], 3: [3]}))
     assert_equal(nx.bellman_ford_predecessor_and_distance(G, 3),
                  ({0: [1], 1: [2], 2: [3], 3: []}, {0: 3, 1: 2, 2: 1, 3: 0}))
     assert_equal(nx.goldberg_radzik(G, 3),
                  ({0: 1, 1: 2, 2: 3, 3: None}, {0: 3, 1: 2, 2: 1, 3: 0}))
Exemplo n.º 9
0
 def test_path_graph(self):
     G = nx.path_graph(4)
     assert_equal(nx.single_source_bellman_ford_path(G, 0),
                  {0: [0], 1: [0, 1], 2: [0, 1, 2], 3: [0, 1, 2, 3]})
     assert_equal(nx.single_source_bellman_ford_path_length(G, 0),
                  {0: 0, 1: 1, 2: 2, 3: 3})
     assert_equal(nx.single_source_bellman_ford(G, 0),
                  ({0: 0, 1: 1, 2: 2, 3: 3}, {0: [0], 1: [0, 1], 2: [0, 1, 2], 3: [0, 1, 2, 3]}))
     assert_equal(nx.bellman_ford_predecessor_and_distance(G, 0),
                  ({0: [None], 1: [0], 2: [1], 3: [2]}, {0: 0, 1: 1, 2: 2, 3: 3}))
     assert_equal(nx.goldberg_radzik(G, 0),
                  ({0: None, 1: 0, 2: 1, 3: 2}, {0: 0, 1: 1, 2: 2, 3: 3}))
     assert_equal(nx.single_source_bellman_ford_path(G, 3),
                  {0: [3, 2, 1, 0], 1: [3, 2, 1], 2: [3, 2], 3: [3]})
     assert_equal(nx.single_source_bellman_ford_path_length(G, 3),
                  {0: 3, 1: 2, 2: 1, 3: 0})
     assert_equal(nx.single_source_bellman_ford(G, 3),
                  ({0: 3, 1: 2, 2: 1, 3: 0}, {0: [3, 2, 1, 0], 1: [3, 2, 1], 2: [3, 2], 3: [3]}))
     assert_equal(nx.bellman_ford_predecessor_and_distance(G, 3),
                  ({0: [1], 1: [2], 2: [3], 3: [None]}, {0: 3, 1: 2, 2: 1, 3: 0}))
     assert_equal(nx.goldberg_radzik(G, 3),
                  ({0: 1, 1: 2, 2: 3, 3: None}, {0: 3, 1: 2, 2: 1, 3: 0}))
Exemplo n.º 10
0
    def test_not_connected(self):
        G = nx.complete_graph(6)
        G.add_edge(10, 11)
        G.add_edge(10, 12)
        assert_equal(nx.bellman_ford(G, 0),
                     ({0: None, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))
        assert_equal(nx.goldberg_radzik(G, 0),
                     ({0: None, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))

        # not connected, with a component not containing the source that
        # contains a negative cost cycle.
        G = nx.complete_graph(6)
        G.add_edges_from([('A', 'B', {'load': 3}),
                          ('B', 'C', {'load': -10}),
                          ('C', 'A', {'load': 2})])
        assert_equal(nx.bellman_ford(G, 0, weight='load'),
                     ({0: None, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))
        assert_equal(nx.goldberg_radzik(G, 0, weight='load'),
                     ({0: None, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))
Exemplo n.º 11
0
 def test_others(self):
     assert nx.bellman_ford_path(self.XG, 's', 'v') == ['s', 'x', 'u', 'v']
     assert nx.bellman_ford_path_length(self.XG, 's', 'v') == 9
     assert nx.single_source_bellman_ford_path(self.XG, 's')['v'] == ['s', 'x', 'u', 'v']
     assert nx.single_source_bellman_ford_path_length(self.XG, 's')['v'] == 9
     D, P = nx.single_source_bellman_ford(self.XG, 's', target='v')
     assert D == 9
     assert P == ['s', 'x', 'u', 'v']
     (P, D) = nx.bellman_ford_predecessor_and_distance(self.XG, 's')
     assert P['v'] == ['u']
     assert D['v'] == 9
     (P, D) = nx.goldberg_radzik(self.XG, 's')
     assert P['v'] == 'u'
     assert D['v'] == 9
Exemplo n.º 12
0
 def test_others(self):
     assert_equal(nx.bellman_ford_path(self.XG, 's', 'v'), ['s', 'x', 'u', 'v'])
     assert_equal(nx.bellman_ford_path_length(self.XG, 's', 'v'), 9)
     assert_equal(nx.single_source_bellman_ford_path(self.XG, 's')['v'], ['s', 'x', 'u', 'v'])
     assert_equal(nx.single_source_bellman_ford_path_length(self.XG, 's')['v'], 9)
     D, P = nx.single_source_bellman_ford(self.XG, 's', target='v')
     assert_equal(D, 9)
     assert_equal(P, ['s', 'x', 'u', 'v'])
     (P, D) = nx.bellman_ford_predecessor_and_distance(self.XG, 's')
     assert_equal(P['v'], ['u'])
     assert_equal(D['v'], 9)
     (P, D) = nx.goldberg_radzik(self.XG, 's')
     assert_equal(P['v'], 'u')
     assert_equal(D['v'], 9)
Exemplo n.º 13
0
 def test_others(self):
     assert_equal(nx.bellman_ford_path(self.XG, 's', 'v'), ['s', 'x', 'u', 'v'])
     assert_equal(nx.bellman_ford_path_length(self.XG, 's', 'v'), 9)
     assert_equal(nx.single_source_bellman_ford_path(self.XG, 's')['v'], ['s', 'x', 'u', 'v'])
     assert_equal(nx.single_source_bellman_ford_path_length(self.XG, 's')['v'], 9)
     D, P = nx.single_source_bellman_ford(self.XG, 's', target='v')
     assert_equal(D, 9)
     assert_equal(P, ['s', 'x', 'u', 'v'])
     (P, D) = nx.bellman_ford_predecessor_and_distance(self.XG, 's')
     assert_equal(P['v'], ['u'])
     assert_equal(D['v'], 9)
     (P, D) = nx.goldberg_radzik(self.XG, 's')
     assert_equal(P['v'], 'u')
     assert_equal(D['v'], 9)
Exemplo n.º 14
0
    def test_not_connected(self):
        G = nx.complete_graph(6)
        G.add_edge(10, 11)
        G.add_edge(10, 12)
        assert_equal(nx.bellman_ford(G, 0),
                     ({0: None, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))
        assert_equal(nx.goldberg_radzik(G, 0),
                     ({0: None, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))

        # not connected, with a component not containing the source that
        # contains a negative cost cycle.
        G = nx.complete_graph(6)
        G.add_edges_from([('A', 'B', {'load': 3}),
                          ('B', 'C', {'load': -10}),
                          ('C', 'A', {'load': 2})])
        assert_equal(nx.bellman_ford(G, 0, weight='load'),
                     ({0: None, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))
        assert_equal(nx.goldberg_radzik(G, 0, weight='load'),
                     ({0: None, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))
Exemplo n.º 15
0
    def test_not_connected(self):
        G = nx.complete_graph(6)
        G.add_edge(10, 11)
        G.add_edge(10, 12)
        assert_equal(nx.single_source_bellman_ford_path(G, 0),
                     {0: [0], 1: [0, 1], 2: [0, 2], 3: [0, 3], 4: [0, 4], 5: [0, 5]})
        assert_equal(nx.single_source_bellman_ford_path_length(G, 0),
                     {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1})
        assert_equal(nx.single_source_bellman_ford(G, 0),
                     ({0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1},
                      {0: [0], 1: [0, 1], 2: [0, 2], 3: [0, 3], 4: [0, 4], 5: [0, 5]}))
        assert_equal(nx.bellman_ford_predecessor_and_distance(G, 0),
                     ({0: [None], 1: [0], 2: [0], 3: [0], 4: [0], 5: [0]},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))
        assert_equal(nx.goldberg_radzik(G, 0),
                     ({0: None, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))

        # not connected, with a component not containing the source that
        # contains a negative cost cycle.
        G = nx.complete_graph(6)
        G.add_edges_from([('A', 'B', {'load': 3}),
                          ('B', 'C', {'load': -10}),
                          ('C', 'A', {'load': 2})])
        assert_equal(nx.single_source_bellman_ford_path(G, 0, weight='load'),
                     {0: [0], 1: [0, 1], 2: [0, 2], 3: [0, 3], 4: [0, 4], 5: [0, 5]})
        assert_equal(nx.single_source_bellman_ford_path_length(G, 0, weight='load'),
                     {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1})
        assert_equal(nx.single_source_bellman_ford(G, 0, weight='load'),
                     ({0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1},
                      {0: [0], 1: [0, 1], 2: [0, 2], 3: [0, 3], 4: [0, 4], 5: [0, 5]}))
        assert_equal(nx.bellman_ford_predecessor_and_distance(G, 0, weight='load'),
                     ({0: [None], 1: [0], 2: [0], 3: [0], 4: [0], 5: [0]},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))
        assert_equal(nx.goldberg_radzik(G, 0, weight='load'),
                     ({0: None, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))
Exemplo n.º 16
0
    def test_not_connected(self):
        G = nx.complete_graph(6)
        G.add_edge(10, 11)
        G.add_edge(10, 12)
        assert_equal(nx.single_source_bellman_ford_path(G, 0),
                     {0: [0], 1: [0, 1], 2: [0, 2], 3: [0, 3], 4: [0, 4], 5: [0, 5]})
        assert_equal(nx.single_source_bellman_ford_path_length(G, 0),
                     {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1})
        assert_equal(nx.single_source_bellman_ford(G, 0),
                     ({0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1},
                      {0: [0], 1: [0, 1], 2: [0, 2], 3: [0, 3], 4: [0, 4], 5: [0, 5]}))
        assert_equal(nx.bellman_ford_predecessor_and_distance(G, 0),
                     ({0: [], 1: [0], 2: [0], 3: [0], 4: [0], 5: [0]},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))
        assert_equal(nx.goldberg_radzik(G, 0),
                     ({0: None, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))

        # not connected, with a component not containing the source that
        # contains a negative cost cycle.
        G = nx.complete_graph(6)
        G.add_edges_from([('A', 'B', {'load': 3}),
                          ('B', 'C', {'load': -10}),
                          ('C', 'A', {'load': 2})])
        assert_equal(nx.single_source_bellman_ford_path(G, 0, weight='load'),
                     {0: [0], 1: [0, 1], 2: [0, 2], 3: [0, 3], 4: [0, 4], 5: [0, 5]})
        assert_equal(nx.single_source_bellman_ford_path_length(G, 0, weight='load'),
                     {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1})
        assert_equal(nx.single_source_bellman_ford(G, 0, weight='load'),
                     ({0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1},
                      {0: [0], 1: [0, 1], 2: [0, 2], 3: [0, 3], 4: [0, 4], 5: [0, 5]}))
        assert_equal(nx.bellman_ford_predecessor_and_distance(G, 0, weight='load'),
                     ({0: [], 1: [0], 2: [0], 3: [0], 4: [0], 5: [0]},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))
        assert_equal(nx.goldberg_radzik(G, 0, weight='load'),
                     ({0: None, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
                      {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}))
Exemplo n.º 17
0
 def test_single_node_graph(self):
     G = nx.DiGraph()
     G.add_node(0)
     assert_equal(nx.single_source_bellman_ford_path(G, 0), {0: [0]})
     assert_equal(dict(nx.single_source_bellman_ford_path_length(G, 0)),
                  {0: 0})
     assert_equal(nx.single_source_bellman_ford(G, 0), ({0: 0}, {0: [0]}))
     assert_equal(nx.bellman_ford_predecessor_and_distance(G, 0), ({
         0: [None]
     }, {
         0: 0
     }))
     assert_equal(nx.goldberg_radzik(G, 0), ({0: None}, {0: 0}))
     assert_raises(KeyError, nx.bellman_ford_predecessor_and_distance, G, 1)
     assert_raises(KeyError, nx.goldberg_radzik, G, 1)
 def test_negative_weight_cycle(self):
     G = nx.cycle_graph(5, create_using=nx.DiGraph())
     G.add_edge(1, 2, weight=-7)
     for i in range(5):
         assert_raises(nx.NetworkXUnbounded, nx.bellman_ford, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.goldberg_radzik, G, i)
     G = nx.cycle_graph(5)  # undirected Graph
     G.add_edge(1, 2, weight=-3)
     for i in range(5):
         assert_raises(nx.NetworkXUnbounded, nx.bellman_ford, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.goldberg_radzik, G, i)
     G = nx.DiGraph([(1, 1, {'weight': -1})])
     assert_raises(nx.NetworkXUnbounded, nx.bellman_ford, G, 1)
     assert_raises(nx.NetworkXUnbounded, nx.goldberg_radzik, G, 1)
     # no negative cycle but negative weight
     G = nx.cycle_graph(5, create_using=nx.DiGraph())
     G.add_edge(1, 2, weight=-3)
     assert_equal(nx.bellman_ford(G, 0), ({
         0: None,
         1: 0,
         2: 1,
         3: 2,
         4: 3
     }, {
         0: 0,
         1: 1,
         2: -2,
         3: -1,
         4: 0
     }))
     assert_equal(nx.goldberg_radzik(G, 0), ({
         0: None,
         1: 0,
         2: 1,
         3: 2,
         4: 3
     }, {
         0: 0,
         1: 1,
         2: -2,
         3: -1,
         4: 0
     }))
Exemplo n.º 19
0
 def test_others(self):
     assert nx.bellman_ford_path(self.XG, "s", "v") == ["s", "x", "u", "v"]
     assert nx.bellman_ford_path_length(self.XG, "s", "v") == 9
     assert nx.single_source_bellman_ford_path(self.XG, "s")["v"] == [
         "s",
         "x",
         "u",
         "v",
     ]
     assert nx.single_source_bellman_ford_path_length(self.XG, "s")["v"] == 9
     D, P = nx.single_source_bellman_ford(self.XG, "s", target="v")
     assert D == 9
     assert P == ["s", "x", "u", "v"]
     (P, D) = nx.bellman_ford_predecessor_and_distance(self.XG, "s")
     assert P["v"] == ["u"]
     assert D["v"] == 9
     (P, D) = nx.goldberg_radzik(self.XG, "s")
     assert P["v"] == "u"
     assert D["v"] == 9
Exemplo n.º 20
0
 def test_negative_weight_cycle(self):
     G = nx.cycle_graph(5, create_using=nx.DiGraph())
     G.add_edge(1, 2, weight=-7)
     for i in range(5):
         assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford_path, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford_path_length, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.bellman_ford_predecessor_and_distance, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.goldberg_radzik, G, i)
     G = nx.cycle_graph(5)  # undirected Graph
     G.add_edge(1, 2, weight=-3)
     for i in range(5):
         assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford_path, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford_path_length, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.bellman_ford_predecessor_and_distance, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.goldberg_radzik, G, i)
     G = nx.DiGraph([(1, 1, {'weight': -1})])
     assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford_path, G, 1)
     assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford_path_length, G, 1)
     assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford, G, 1)
     assert_raises(nx.NetworkXUnbounded, nx.bellman_ford_predecessor_and_distance, G, 1)
     assert_raises(nx.NetworkXUnbounded, nx.goldberg_radzik, G, 1)
     # no negative cycle but negative weight
     G = nx.cycle_graph(5, create_using=nx.DiGraph())
     G.add_edge(1, 2, weight=-3)
     assert_equal(nx.single_source_bellman_ford_path(G, 0),
                  {0: [0], 1: [0, 1], 2: [0, 1, 2], 3: [0, 1, 2, 3], 4: [0, 1, 2, 3, 4]})
     assert_equal(nx.single_source_bellman_ford_path_length(G, 0),
                  {0: 0, 1: 1, 2: -2, 3: -1, 4: 0})
     assert_equal(nx.single_source_bellman_ford(G, 0),
                  ({0: 0, 1: 1, 2: -2, 3: -1, 4: 0},
                   {0: [0], 1: [0, 1], 2: [0, 1, 2], 3: [0, 1, 2, 3], 4: [0, 1, 2, 3, 4]}))
     assert_equal(nx.bellman_ford_predecessor_and_distance(G, 0),
                  ({0: [None], 1: [0], 2: [1], 3: [2], 4: [3]},
                   {0: 0, 1: 1, 2: -2, 3: -1, 4: 0}))
     assert_equal(nx.goldberg_radzik(G, 0),
                  ({0: None, 1: 0, 2: 1, 3: 2, 4: 3},
                   {0: 0, 1: 1, 2: -2, 3: -1, 4: 0}))
Exemplo n.º 21
0
 def test_negative_weight_cycle(self):
     G = nx.cycle_graph(5, create_using=nx.DiGraph())
     G.add_edge(1, 2, weight=-7)
     for i in range(5):
         assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford_path, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford_path_length, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.bellman_ford_predecessor_and_distance, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.goldberg_radzik, G, i)
     G = nx.cycle_graph(5)  # undirected Graph
     G.add_edge(1, 2, weight=-3)
     for i in range(5):
         assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford_path, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford_path_length, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.bellman_ford_predecessor_and_distance, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.goldberg_radzik, G, i)
     G = nx.DiGraph([(1, 1, {'weight': -1})])
     assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford_path, G, 1)
     assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford_path_length, G, 1)
     assert_raises(nx.NetworkXUnbounded, nx.single_source_bellman_ford, G, 1)
     assert_raises(nx.NetworkXUnbounded, nx.bellman_ford_predecessor_and_distance, G, 1)
     assert_raises(nx.NetworkXUnbounded, nx.goldberg_radzik, G, 1)
     # no negative cycle but negative weight
     G = nx.cycle_graph(5, create_using=nx.DiGraph())
     G.add_edge(1, 2, weight=-3)
     assert_equal(nx.single_source_bellman_ford_path(G, 0),
                  {0: [0], 1: [0, 1], 2: [0, 1, 2], 3: [0, 1, 2, 3], 4: [0, 1, 2, 3, 4]})
     assert_equal(nx.single_source_bellman_ford_path_length(G, 0),
                  {0: 0, 1: 1, 2: -2, 3: -1, 4: 0})
     assert_equal(nx.single_source_bellman_ford(G, 0),
                  ({0: 0, 1: 1, 2: -2, 3: -1, 4: 0},
                   {0: [0], 1: [0, 1], 2: [0, 1, 2], 3: [0, 1, 2, 3], 4: [0, 1, 2, 3, 4]}))
     assert_equal(nx.bellman_ford_predecessor_and_distance(G, 0),
                  ({0: [], 1: [0], 2: [1], 3: [2], 4: [3]},
                   {0: 0, 1: 1, 2: -2, 3: -1, 4: 0}))
     assert_equal(nx.goldberg_radzik(G, 0),
                  ({0: None, 1: 0, 2: 1, 3: 2, 4: 3},
                   {0: 0, 1: 1, 2: -2, 3: -1, 4: 0}))
Exemplo n.º 22
0
    def test_4_cycle(self):
        # 4-cycle
        G = nx.Graph([(0, 1), (1, 2), (2, 3), (3, 0)])
        dist, path = nx.single_source_bellman_ford(G, 0)
        assert dist == {0: 0, 1: 1, 2: 2, 3: 1}
        assert path[0] == [0]
        assert path[1] == [0, 1]
        assert path[2] in [[0, 1, 2], [0, 3, 2]]
        assert path[3] == [0, 3]

        pred, dist = nx.bellman_ford_predecessor_and_distance(G, 0)
        assert pred[0] == []
        assert pred[1] == [0]
        assert pred[2] in [[1, 3], [3, 1]]
        assert pred[3] == [0]
        assert dist == {0: 0, 1: 1, 2: 2, 3: 1}

        pred, dist = nx.goldberg_radzik(G, 0)
        assert pred[0] is None
        assert pred[1] == 0
        assert pred[2] in [1, 3]
        assert pred[3] == 0
        assert dist == {0: 0, 1: 1, 2: 2, 3: 1}
Exemplo n.º 23
0
    def test_4_cycle(self):
        # 4-cycle
        G = nx.Graph([(0, 1), (1, 2), (2, 3), (3, 0)])
        dist, path = nx.single_source_bellman_ford(G, 0)
        assert_equal(dist, {0: 0, 1: 1, 2: 2, 3: 1})
        assert_equal(path[0], [0])
        assert_equal(path[1], [0, 1])
        assert_true(path[2] in [[0, 1, 2], [0, 3, 2]])
        assert_equal(path[3], [0, 3])

        pred, dist = nx.bellman_ford_predecessor_and_distance(G, 0)
        assert_equal(pred[0], [])
        assert_equal(pred[1], [0])
        assert_true(pred[2] in [[1, 3], [3, 1]])
        assert_equal(pred[3], [0])
        assert_equal(dist, {0: 0, 1: 1, 2: 2, 3: 1})

        pred, dist = nx.goldberg_radzik(G, 0)
        assert_equal(pred[0], None)
        assert_equal(pred[1], 0)
        assert_true(pred[2] in [1, 3])
        assert_equal(pred[3], 0)
        assert_equal(dist, {0: 0, 1: 1, 2: 2, 3: 1})
Exemplo n.º 24
0
    def test_4_cycle(self):
        # 4-cycle
        G = nx.Graph([(0,1),(1,2),(2,3),(3,0)])
        dist, path = nx.single_source_bellman_ford(G, 0)
        assert_equal(dist, {0: 0, 1: 1, 2: 2, 3: 1})
        assert_equal(path[0],[0])
        assert_equal(path[1],[0,1])
        assert_true(path[2] in [[0,1,2],[0,3,2]])
        assert_equal(path[3],[0,3])

        pred, dist = nx.bellman_ford_predecessor_and_distance(G, 0)
        assert_equal(pred[0],[None])
        assert_equal(pred[1],[0])
        assert_true(pred[2] in [[1,3],[3,1]])
        assert_equal(pred[3],[0])
        assert_equal(dist, {0: 0, 1: 1, 2: 2, 3: 1})

        pred, dist = nx.goldberg_radzik(G, 0)
        assert_equal(pred[0],None)
        assert_equal(pred[1],0)
        assert_true(pred[2] in [1,3])
        assert_equal(pred[3],0)
        assert_equal(dist, {0: 0, 1: 1, 2: 2, 3: 1})
Exemplo n.º 25
0
 def test_negative_weight_cycle(self):
     G = nx.cycle_graph(5, create_using=nx.DiGraph())
     G.add_edge(1, 2, weight=-7)
     for i in range(5):
         assert_raises(nx.NetworkXUnbounded, nx.bellman_ford, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.goldberg_radzik, G, i)
     G = nx.cycle_graph(5)  # undirected Graph
     G.add_edge(1, 2, weight=-3)
     for i in range(5):
         assert_raises(nx.NetworkXUnbounded, nx.bellman_ford, G, i)
         assert_raises(nx.NetworkXUnbounded, nx.goldberg_radzik, G, i)
     G = nx.DiGraph([(1, 1, {'weight': -1})])
     assert_raises(nx.NetworkXUnbounded, nx.bellman_ford, G, 1)
     assert_raises(nx.NetworkXUnbounded, nx.goldberg_radzik, G, 1)
     # no negative cycle but negative weight
     G = nx.cycle_graph(5, create_using=nx.DiGraph())
     G.add_edge(1, 2, weight=-3)
     assert_equal(nx.bellman_ford(G, 0),
                  ({0: None, 1: 0, 2: 1, 3: 2, 4: 3},
                   {0: 0, 1: 1, 2: -2, 3: -1, 4: 0}))
     assert_equal(nx.goldberg_radzik(G, 0),
                  ({0: None, 1: 0, 2: 1, 3: 2, 4: 3},
                   {0: 0, 1: 1, 2: -2, 3: -1, 4: 0}))
Exemplo n.º 26
0
 def test_absent_source_goldberg_radzik(self):
     with pytest.raises(nx.NodeNotFound):
         G = nx.path_graph(2)
         nx.goldberg_radzik(G, 3, 0)
    def test_others(self):
        (P, D) = nx.bellman_ford(self.XG, 's')
        assert_equal(P['v'], 'u')
        assert_equal(D['v'], 9)
        (P, D) = nx.goldberg_radzik(self.XG, 's')
        assert_equal(P['v'], 'u')
        assert_equal(D['v'], 9)

        G = nx.path_graph(4)
        assert_equal(nx.bellman_ford(G, 0), ({
            0: None,
            1: 0,
            2: 1,
            3: 2
        }, {
            0: 0,
            1: 1,
            2: 2,
            3: 3
        }))
        assert_equal(nx.goldberg_radzik(G, 0), ({
            0: None,
            1: 0,
            2: 1,
            3: 2
        }, {
            0: 0,
            1: 1,
            2: 2,
            3: 3
        }))
        assert_equal(nx.bellman_ford(G, 3), ({
            0: 1,
            1: 2,
            2: 3,
            3: None
        }, {
            0: 3,
            1: 2,
            2: 1,
            3: 0
        }))
        assert_equal(nx.goldberg_radzik(G, 3), ({
            0: 1,
            1: 2,
            2: 3,
            3: None
        }, {
            0: 3,
            1: 2,
            2: 1,
            3: 0
        }))

        G = nx.grid_2d_graph(2, 2)
        pred, dist = nx.bellman_ford(G, (0, 0))
        assert_equal(sorted(pred.items()), [((0, 0), None), ((0, 1), (0, 0)),
                                            ((1, 0), (0, 0)),
                                            ((1, 1), (0, 1))])
        assert_equal(sorted(dist.items()), [((0, 0), 0), ((0, 1), 1),
                                            ((1, 0), 1), ((1, 1), 2)])
        pred, dist = nx.goldberg_radzik(G, (0, 0))
        assert_equal(sorted(pred.items()), [((0, 0), None), ((0, 1), (0, 0)),
                                            ((1, 0), (0, 0)),
                                            ((1, 1), (0, 1))])
        assert_equal(sorted(dist.items()), [((0, 0), 0), ((0, 1), 1),
                                            ((1, 0), 1), ((1, 1), 2)])
Exemplo n.º 28
0
 def test_absent_source_goldberg_radzik(self):
     G = nx.path_graph(2)
     nx.goldberg_radzik(G, 3, 0)
Exemplo n.º 29
0
    def test_not_connected(self):
        G = nx.complete_graph(6)
        G.add_edge(10, 11)
        G.add_edge(10, 12)
        assert nx.single_source_bellman_ford_path(G, 0) == {
            0: [0],
            1: [0, 1],
            2: [0, 2],
            3: [0, 3],
            4: [0, 4],
            5: [0, 5],
        }
        assert nx.single_source_bellman_ford_path_length(G, 0) == {
            0: 0,
            1: 1,
            2: 1,
            3: 1,
            4: 1,
            5: 1,
        }
        assert nx.single_source_bellman_ford(G, 0) == (
            {
                0: 0,
                1: 1,
                2: 1,
                3: 1,
                4: 1,
                5: 1
            },
            {
                0: [0],
                1: [0, 1],
                2: [0, 2],
                3: [0, 3],
                4: [0, 4],
                5: [0, 5]
            },
        )
        assert nx.bellman_ford_predecessor_and_distance(G, 0) == (
            {
                0: [],
                1: [0],
                2: [0],
                3: [0],
                4: [0],
                5: [0]
            },
            {
                0: 0,
                1: 1,
                2: 1,
                3: 1,
                4: 1,
                5: 1
            },
        )
        assert nx.goldberg_radzik(G, 0) == (
            {
                0: None,
                1: 0,
                2: 0,
                3: 0,
                4: 0,
                5: 0
            },
            {
                0: 0,
                1: 1,
                2: 1,
                3: 1,
                4: 1,
                5: 1
            },
        )

        # not connected, with a component not containing the source that
        # contains a negative cost cycle.
        G = nx.complete_graph(6)
        G.add_edges_from([
            ("A", "B", {
                "load": 3
            }),
            ("B", "C", {
                "load": -10
            }),
            ("C", "A", {
                "load": 2
            }),
        ])
        assert nx.single_source_bellman_ford_path(G, 0, weight="load") == {
            0: [0],
            1: [0, 1],
            2: [0, 2],
            3: [0, 3],
            4: [0, 4],
            5: [0, 5],
        }
        assert nx.single_source_bellman_ford_path_length(G, 0,
                                                         weight="load") == {
                                                             0: 0,
                                                             1: 1,
                                                             2: 1,
                                                             3: 1,
                                                             4: 1,
                                                             5: 1,
                                                         }
        assert nx.single_source_bellman_ford(G, 0, weight="load") == (
            {
                0: 0,
                1: 1,
                2: 1,
                3: 1,
                4: 1,
                5: 1
            },
            {
                0: [0],
                1: [0, 1],
                2: [0, 2],
                3: [0, 3],
                4: [0, 4],
                5: [0, 5]
            },
        )
        assert nx.bellman_ford_predecessor_and_distance(G, 0,
                                                        weight="load") == (
                                                            {
                                                                0: [],
                                                                1: [0],
                                                                2: [0],
                                                                3: [0],
                                                                4: [0],
                                                                5: [0]
                                                            },
                                                            {
                                                                0: 0,
                                                                1: 1,
                                                                2: 1,
                                                                3: 1,
                                                                4: 1,
                                                                5: 1
                                                            },
                                                        )
        assert nx.goldberg_radzik(G, 0, weight="load") == (
            {
                0: None,
                1: 0,
                2: 0,
                3: 0,
                4: 0,
                5: 0
            },
            {
                0: 0,
                1: 1,
                2: 1,
                3: 1,
                4: 1,
                5: 1
            },
        )
Exemplo n.º 30
0
 def goldberg_radzik(g, s, t, weight="weight"):
     pred, dist = nx.goldberg_radzik(g, s, weight=weight)
     dist = dist[t]
     return dist, getpath(pred, t, s)
Exemplo n.º 31
0
 def test_negative_weight(self):
     G = nx.cycle_graph(5, create_using=nx.DiGraph())
     G.add_edge(1, 2, weight=-3)
     assert nx.single_source_bellman_ford_path(G, 0) == {
         0: [0],
         1: [0, 1],
         2: [0, 1, 2],
         3: [0, 1, 2, 3],
         4: [0, 1, 2, 3, 4],
     }
     assert nx.single_source_bellman_ford_path_length(G, 0) == {
         0: 0,
         1: 1,
         2: -2,
         3: -1,
         4: 0,
     }
     assert nx.single_source_bellman_ford(G, 0) == (
         {
             0: 0,
             1: 1,
             2: -2,
             3: -1,
             4: 0
         },
         {
             0: [0],
             1: [0, 1],
             2: [0, 1, 2],
             3: [0, 1, 2, 3],
             4: [0, 1, 2, 3, 4]
         },
     )
     assert nx.bellman_ford_predecessor_and_distance(G, 0) == (
         {
             0: [],
             1: [0],
             2: [1],
             3: [2],
             4: [3]
         },
         {
             0: 0,
             1: 1,
             2: -2,
             3: -1,
             4: 0
         },
     )
     assert nx.goldberg_radzik(G, 0) == (
         {
             0: None,
             1: 0,
             2: 1,
             3: 2,
             4: 3
         },
         {
             0: 0,
             1: 1,
             2: -2,
             3: -1,
             4: 0
         },
     )
Exemplo n.º 32
0
    def test_others(self):
        assert_equal(nx.bellman_ford_path(self.XG, 's', 'v'),
                     ['s', 'x', 'u', 'v'])
        assert_equal(nx.bellman_ford_path_length(self.XG, 's', 'v'), 9)
        assert_equal(
            nx.single_source_bellman_ford_path(self.XG, 's')['v'],
            ['s', 'x', 'u', 'v'])
        assert_equal(
            dict(nx.single_source_bellman_ford_path_length(self.XG, 's'))['v'],
            9)
        D, P = nx.single_source_bellman_ford(self.XG, 's', target='v')
        assert_equal(D['v'], 9)
        assert_equal(P['v'], ['s', 'x', 'u', 'v'])
        (P, D) = nx.bellman_ford_predecessor_and_distance(self.XG, 's')
        assert_equal(P['v'], ['u'])
        assert_equal(D['v'], 9)
        (P, D) = nx.goldberg_radzik(self.XG, 's')
        assert_equal(P['v'], 'u')
        assert_equal(D['v'], 9)

        G = nx.path_graph(4)
        assert_equal(nx.single_source_bellman_ford_path(G, 0), {
            0: [0],
            1: [0, 1],
            2: [0, 1, 2],
            3: [0, 1, 2, 3]
        })
        assert_equal(dict(nx.single_source_bellman_ford_path_length(G, 0)), {
            0: 0,
            1: 1,
            2: 2,
            3: 3
        })
        assert_equal(nx.single_source_bellman_ford(G, 0), ({
            0: 0,
            1: 1,
            2: 2,
            3: 3
        }, {
            0: [0],
            1: [0, 1],
            2: [0, 1, 2],
            3: [0, 1, 2, 3]
        }))
        assert_equal(nx.bellman_ford_predecessor_and_distance(G, 0), ({
            0: [None],
            1: [0],
            2: [1],
            3: [2]
        }, {
            0: 0,
            1: 1,
            2: 2,
            3: 3
        }))
        assert_equal(nx.goldberg_radzik(G, 0), ({
            0: None,
            1: 0,
            2: 1,
            3: 2
        }, {
            0: 0,
            1: 1,
            2: 2,
            3: 3
        }))
        assert_equal(nx.single_source_bellman_ford_path(G, 3), {
            0: [3, 2, 1, 0],
            1: [3, 2, 1],
            2: [3, 2],
            3: [3]
        })
        assert_equal(dict(nx.single_source_bellman_ford_path_length(G, 3)), {
            0: 3,
            1: 2,
            2: 1,
            3: 0
        })
        assert_equal(nx.single_source_bellman_ford(G, 3), ({
            0: 3,
            1: 2,
            2: 1,
            3: 0
        }, {
            0: [3, 2, 1, 0],
            1: [3, 2, 1],
            2: [3, 2],
            3: [3]
        }))
        assert_equal(nx.bellman_ford_predecessor_and_distance(G, 3), ({
            0: [1],
            1: [2],
            2: [3],
            3: [None]
        }, {
            0: 3,
            1: 2,
            2: 1,
            3: 0
        }))
        assert_equal(nx.goldberg_radzik(G, 3), ({
            0: 1,
            1: 2,
            2: 3,
            3: None
        }, {
            0: 3,
            1: 2,
            2: 1,
            3: 0
        }))

        G = nx.grid_2d_graph(2, 2)
        dist, path = nx.single_source_bellman_ford(G, (0, 0))
        assert_equal(sorted(dist.items()), [((0, 0), 0), ((0, 1), 1),
                                            ((1, 0), 1), ((1, 1), 2)])
        assert_equal(sorted(path.items()), [((0, 0), [(0, 0)]),
                                            ((0, 1), [(0, 0), (0, 1)]),
                                            ((1, 0), [(0, 0), (1, 0)]),
                                            ((1, 1), [(0, 0), (0, 1),
                                                      (1, 1)])])
        pred, dist = nx.bellman_ford_predecessor_and_distance(G, (0, 0))
        assert_equal(sorted(pred.items()), [((0, 0), [None]),
                                            ((0, 1), [(0, 0)]),
                                            ((1, 0), [(0, 0)]),
                                            ((1, 1), [(0, 1), (1, 0)])])
        assert_equal(sorted(dist.items()), [((0, 0), 0), ((0, 1), 1),
                                            ((1, 0), 1), ((1, 1), 2)])
        pred, dist = nx.goldberg_radzik(G, (0, 0))
        assert_equal(sorted(pred.items()), [((0, 0), None), ((0, 1), (0, 0)),
                                            ((1, 0), (0, 0)),
                                            ((1, 1), (0, 1))])
        assert_equal(sorted(dist.items()), [((0, 0), 0), ((0, 1), 1),
                                            ((1, 0), 1), ((1, 1), 2)])
Exemplo n.º 33
0
 def test_absent_source_goldberg_radzik(self):
     G = nx.path_graph(2)
     nx.goldberg_radzik(G, 3, 0)