コード例 #1
0
ファイル: test_weighted.py プロジェクト: 666888/networkx
    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)])
コード例 #2
0
ファイル: test_weighted.py プロジェクト: networkx/networkx
 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)
コード例 #3
0
ファイル: test_weighted.py プロジェクト: AllenDowney/networkx
    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)])
コード例 #4
0
ファイル: test_weighted.py プロジェクト: 666888/networkx
 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)
コード例 #5
0
ファイル: test_weighted.py プロジェクト: 666888/networkx
 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)
コード例 #6
0
ファイル: test_weighted.py プロジェクト: networkx/networkx
 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}))
コード例 #7
0
ファイル: test_weighted.py プロジェクト: sr-murthy/networkx
 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})
コード例 #8
0
ファイル: test_weighted.py プロジェクト: networkx/networkx
 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}))
コード例 #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}))
コード例 #10
0
ファイル: test_weighted.py プロジェクト: 666888/networkx
    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}))
コード例 #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
コード例 #12
0
ファイル: test_weighted.py プロジェクト: networkx/networkx
 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)
コード例 #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)
コード例 #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}))
コード例 #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}))
コード例 #16
0
ファイル: test_weighted.py プロジェクト: networkx/networkx
    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}))
コード例 #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)
コード例 #18
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
     }))
コード例 #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
コード例 #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}))
コード例 #21
0
ファイル: test_weighted.py プロジェクト: networkx/networkx
 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}))
コード例 #22
0
ファイル: test_weighted.py プロジェクト: ynux/networkx
    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}
コード例 #23
0
ファイル: test_weighted.py プロジェクト: networkx/networkx
    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})
コード例 #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})
コード例 #25
0
ファイル: test_weighted.py プロジェクト: 666888/networkx
 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}))
コード例 #26
0
ファイル: test_weighted.py プロジェクト: ynux/networkx
 def test_absent_source_goldberg_radzik(self):
     with pytest.raises(nx.NodeNotFound):
         G = nx.path_graph(2)
         nx.goldberg_radzik(G, 3, 0)
コード例 #27
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)])
コード例 #28
0
 def test_absent_source_goldberg_radzik(self):
     G = nx.path_graph(2)
     nx.goldberg_radzik(G, 3, 0)
コード例 #29
0
ファイル: test_weighted.py プロジェクト: Rigosebuta/biathlon
    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
            },
        )
コード例 #30
0
ファイル: test_weighted.py プロジェクト: Rigosebuta/biathlon
 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)
コード例 #31
0
ファイル: test_weighted.py プロジェクト: sr-murthy/networkx
 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
         },
     )
コード例 #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)])
コード例 #33
0
ファイル: test_weighted.py プロジェクト: networkx/networkx
 def test_absent_source_goldberg_radzik(self):
     G = nx.path_graph(2)
     nx.goldberg_radzik(G, 3, 0)