本文整理汇总了Python中networkx.goldberg_radzik函数的典型用法代码示例。如果您正苦于以下问题:Python goldberg_radzik函数的具体用法?Python goldberg_radzik怎么用?Python goldberg_radzik使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了goldberg_radzik函数的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_others
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: test_multigraph
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: test_others
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: test_multigraph
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: test_single_node_graph
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: test_single_node_graph
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: test_path_graph
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}))
示例8: test_others
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)
示例9: test_not_connected
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}))
示例10: test_not_connected
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}))
示例11: test_negative_weight_cycle
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}))
示例12: test_4_cycle
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})
示例13: test_negative_weight_cycle
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}))
示例14: test_absent_source_goldberg_radzik
def test_absent_source_goldberg_radzik(self):
G = nx.path_graph(2)
nx.goldberg_radzik(G, 3, 0)