def test_multigraph(self): assert nx.bellman_ford_path(self.MXG, "s", "v") == ["s", "x", "u", "v"] assert nx.bellman_ford_path_length(self.MXG, "s", "v") == 9 assert nx.single_source_bellman_ford_path(self.MXG, "s")["v"] == [ "s", "x", "u", "v", ] assert 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 D == 9 assert P == ["s", "x", "u", "v"] P, D = nx.bellman_ford_predecessor_and_distance(self.MXG, "s") assert P["v"] == ["u"] assert D["v"] == 9 P, D = nx.goldberg_radzik(self.MXG, "s") assert P["v"] == "u" assert D["v"] == 9 assert nx.bellman_ford_path(self.MXG4, 0, 2) == [0, 1, 2] assert nx.bellman_ford_path_length(self.MXG4, 0, 2) == 4 assert nx.single_source_bellman_ford_path(self.MXG4, 0)[2] == [0, 1, 2] assert 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 D == 4 assert P == [0, 1, 2] P, D = nx.bellman_ford_predecessor_and_distance(self.MXG4, 0) assert P[2] == [1] assert D[2] == 4 P, D = nx.goldberg_radzik(self.MXG4, 0) assert P[2] == 1 assert D[2] == 4
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( dict(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['v'], 9) assert_equal(P['v'], ['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( dict(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[2], 4) assert_equal(P[2], [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)
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)
def test_multigraph(self): assert nx.bellman_ford_path(self.MXG, 's', 'v') == ['s', 'x', 'u', 'v'] assert nx.bellman_ford_path_length(self.MXG, 's', 'v') == 9 assert nx.single_source_bellman_ford_path( self.MXG, 's')['v'] == ['s', 'x', 'u', 'v'] assert 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 D == 9 assert P == ['s', 'x', 'u', 'v'] P, D = nx.bellman_ford_predecessor_and_distance(self.MXG, 's') assert P['v'] == ['u'] assert D['v'] == 9 P, D = nx.goldberg_radzik(self.MXG, 's') assert P['v'] == 'u' assert D['v'] == 9 assert nx.bellman_ford_path(self.MXG4, 0, 2) == [0, 1, 2] assert nx.bellman_ford_path_length(self.MXG4, 0, 2) == 4 assert nx.single_source_bellman_ford_path(self.MXG4, 0)[2] == [0, 1, 2] assert 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 D == 4 assert P == [0, 1, 2] P, D = nx.bellman_ford_predecessor_and_distance(self.MXG4, 0) assert P[2] == [1] assert D[2] == 4 P, D = nx.goldberg_radzik(self.MXG4, 0) assert P[2] == 1 assert D[2] == 4
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)])
def test_path_graph(self): G = nx.path_graph(4) assert nx.single_source_bellman_ford_path(G, 0) == { 0: [0], 1: [0, 1], 2: [0, 1, 2], 3: [0, 1, 2, 3], } assert nx.single_source_bellman_ford_path_length(G, 0) == { 0: 0, 1: 1, 2: 2, 3: 3, } assert 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 nx.bellman_ford_predecessor_and_distance(G, 0) == ( {0: [], 1: [0], 2: [1], 3: [2]}, {0: 0, 1: 1, 2: 2, 3: 3}, ) assert nx.goldberg_radzik(G, 0) == ( {0: None, 1: 0, 2: 1, 3: 2}, {0: 0, 1: 1, 2: 2, 3: 3}, ) assert nx.single_source_bellman_ford_path(G, 3) == { 0: [3, 2, 1, 0], 1: [3, 2, 1], 2: [3, 2], 3: [3], } assert nx.single_source_bellman_ford_path_length(G, 3) == { 0: 3, 1: 2, 2: 1, 3: 0, } assert 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 nx.bellman_ford_predecessor_and_distance(G, 3) == ( {0: [1], 1: [2], 2: [3], 3: []}, {0: 3, 1: 2, 2: 1, 3: 0}, ) assert nx.goldberg_radzik(G, 3) == ( {0: 1, 1: 2, 2: 3, 3: None}, {0: 3, 1: 2, 2: 1, 3: 0}, )
def main(): G = nx.DiGraph() parser = argparse.ArgumentParser() parser.add_argument('inputfile', type=str) args = parser.parse_args() with open(args.inputfile, 'r') as filep: G.add_edges_from( map(lambda x: x.split(')'), filep.read().rstrip().split('\n'))) root = list(nx.topological_sort(G))[0] csum = sum( map(lambda x: len(x) - 1, nx.single_source_bellman_ford_path(G, root).values())) print(f'checksum: {csum}') san_parent = list(G.predecessors('SAN'))[0] you_parent = list(G.predecessors('YOU'))[0] shortest_path = nx.shortest_path_length(G.to_undirected(), source=you_parent, target=san_parent) print(f'shortest path to san is: {shortest_path}')
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}))
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})
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: [None]}, {0: 0})) assert_equal(nx.goldberg_radzik(G, 0), ({0: None}, {0: 0})) assert_raises(nx.NodeNotFound, nx.bellman_ford_predecessor_and_distance, G, 1) assert_raises(nx.NodeNotFound, nx.goldberg_radzik, G, 1)
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}))
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
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)
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)
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}))
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}))
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
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}))
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}))
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(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 }))
('A', 'C', 4), ('B', 'C', 3), ('B', 'D', 2), ('B', 'E', 2), ('D', 'B', 1), ('D', 'C', 5), ('E', 'D', -3), ]) posicoes = { 'A': (0, 1), 'B': (2, 2), 'C': (1, 0), 'D': (3, 0), 'E': (4, 1), } rotulos = nx.get_edge_attributes(G, 'weight') print('Caminho de A até E:', nx.bellman_ford_path(G, 'A', 'E')) print('Distância de A até E:', nx.bellman_ford_path_length(G, 'A', 'E')) caminhos = nx.single_source_bellman_ford_path(G, 'A') distancias = nx.single_source_bellman_ford_path_length(G, 'A') print("\nCaminhos a partir do nodo 'A'") for c, d in zip(caminhos.values(), distancias.values()): print(f'Caminho de A até {c[-1]:}', c, 'Distância:', d) nx.draw(G, posicoes, with_labels=True, connectionstyle='arc3, rad=0.1') nx.draw_networkx_edge_labels(G, posicoes, edge_labels=rotulos, label_pos=0.3)
def shortest_path(G, source=None, target=None, weight=None, method='dijkstra'): """Compute shortest paths in the graph. Parameters ---------- G : NetworkX graph source : node, optional Starting node for path. If not specified, compute shortest paths for each possible starting node. target : node, optional Ending node for path. If not specified, compute shortest paths to all possible nodes. weight : None or string, optional (default = None) If None, every edge has weight/distance/cost 1. If a string, use this edge attribute as the edge weight. Any edge attribute not present defaults to 1. method : string, optional (default = 'dijkstra') The algorithm to use to compute the path. Supported options: 'dijkstra', 'bellman-ford'. Other inputs produce a ValueError. If `weight` is None, unweighted graph methods are used, and this suggestion is ignored. Returns ------- path: list or dictionary All returned paths include both the source and target in the path. If the source and target are both specified, return a single list of nodes in a shortest path from the source to the target. If only the source is specified, return a dictionary keyed by targets with a list of nodes in a shortest path from the source to one of the targets. If only the target is specified, return a dictionary keyed by sources with a list of nodes in a shortest path from one of the sources to the target. If neither the source nor target are specified return a dictionary of dictionaries with path[source][target]=[list of nodes in path]. Raises ------ NodeNotFound If `source` is not in `G`. ValueError If `method` is not among the supported options. Examples -------- >>> G = nx.path_graph(5) >>> print(nx.shortest_path(G, source=0, target=4)) [0, 1, 2, 3, 4] >>> p = nx.shortest_path(G, source=0) # target not specified >>> p[4] [0, 1, 2, 3, 4] >>> p = nx.shortest_path(G, target=4) # source not specified >>> p[0] [0, 1, 2, 3, 4] >>> p = nx.shortest_path(G) # source, target not specified >>> p[0][4] [0, 1, 2, 3, 4] Notes ----- There may be more than one shortest path between a source and target. This returns only one of them. See Also -------- all_pairs_shortest_path() all_pairs_dijkstra_path() all_pairs_bellman_ford_path() single_source_shortest_path() single_source_dijkstra_path() single_source_bellman_ford_path() """ if method not in ('dijkstra', 'bellman-ford'): # so we don't need to check in each branch later raise ValueError('method not supported: {}'.format(method)) method = 'unweighted' if weight is None else method if source is None: if target is None: # Find paths between all pairs. if method == 'unweighted': paths = dict(nx.all_pairs_shortest_path(G)) elif method == 'dijkstra': paths = dict(nx.all_pairs_dijkstra_path(G, weight=weight)) else: # method == 'bellman-ford': paths = dict(nx.all_pairs_bellman_ford_path(G, weight=weight)) else: # Find paths from all nodes co-accessible to the target. with nx.utils.reversed(G): if method == 'unweighted': paths = nx.single_source_shortest_path(G, target) elif method == 'dijkstra': paths = nx.single_source_dijkstra_path(G, target, weight=weight) else: # method == 'bellman-ford': paths = nx.single_source_bellman_ford_path(G, target, weight=weight) # Now flip the paths so they go from a source to the target. for target in paths: paths[target] = list(reversed(paths[target])) else: if target is None: # Find paths to all nodes accessible from the source. if method == 'unweighted': paths = nx.single_source_shortest_path(G, source) elif method == 'dijkstra': paths = nx.single_source_dijkstra_path(G, source, weight=weight) else: # method == 'bellman-ford': paths = nx.single_source_bellman_ford_path(G, source, weight=weight) else: # Find shortest source-target path. if method == 'unweighted': paths = nx.bidirectional_shortest_path(G, source, target) elif method == 'dijkstra': paths = nx.dijkstra_path(G, source, target, weight) else: # method == 'bellman-ford': paths = nx.bellman_ford_path(G, source, target, weight) return paths
def find_path(digraph, start="USD"): try: path = nx.single_source_bellman_ford_path(digraph, start) except nx.exception.NetworkXUnbounded as e: return path return path
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 }, )
# g.add_edge("USD","JPY", {"weight": 91.9943456}) # g = nx.cycle_graph(nodes, create_using=nx.DiGraph()) g.add_nodes_from(nodes) g.add_weighted_edges_from(newdata) options = { 'node_color': 'yellow', 'node_size': 330, 'width': 1, 'arrowstyle': '-|>', 'arrowsize': 32, 'with_labels': True, 'font_size': 10 } # pos = nx.circular_layout(g) # nx.draw(g, with_labels=True, node_color='y') layout = nx.circular_layout(g) nx.draw_networkx_edge_labels(g, layout, label_pos=0.3) nx.draw_networkx_nodes(g, layout, node_color='yellow', node_size=500) nx.draw_networkx_edges(g, layout, edge_color='r', arrows=True, arrowstyle='-|>', arrowsize=20) nx.draw_networkx_labels(g, layout, font_size=10, font_family="sans-serif") # nx.draw_networkx(g, arrows=True, **options) print(nx.negative_edge_cycle(g)) print(nx.single_source_bellman_ford_path(g, "USD")) plt.show()
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)])
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 }, )