def test_large_vs_known_implementation(): """ Creates a large random graph and runs dijkstra's algorithm with this implementation as well as a known good implementation I grabbed from github. Compares that the results returned are the same """ import random size = 1000 my_graph = Graph() my_nodes = [] other_graph = OtherGraph() for i in range(size): start_node = i end_node = random.randint(0, size) length = random.randrange(0, 10000) my_nodes.append([start_node, end_node, length]) other_graph.add_edge(start_node, end_node, length) my_graph.make_weighted_from_list(my_nodes) my_prev, my_unvisited = my_graph.dijkstra(start=Node(0)) other_visited, other_prev = known_dijkstras_implementation(other_graph, 0) unvisited_diff = my_unvisited.keys() - other_visited.keys() visited_diff = other_visited.keys() - my_unvisited.keys() assert my_prev == other_prev and my_unvisited.keys() == unvisited_diff and other_visited.keys() == visited_diff
def test_passes_with_unused_negative_weights(): graph = Graph() nodes = [["a","b", 4], ["c", "d", -1]] graph.make_weighted_from_list(nodes) actual_path = graph.dijkstras_with_target(start=Node("a"), target=Node("b")) expected_path = ["a", "b"] assert actual_path == expected_path
def test_straight_line_graph(): # a straight line graph is one that has only one path at each node graph = Graph() nodes = [["a","b",1],["b","c",1]] graph.make_weighted_from_list(nodes) expected_path = ["a","b","c"] actual_path = graph.dijkstras_with_target(Node("a"),Node("c")) assert actual_path == expected_path
def test_weighted_undirected(): graph = Graph() nodes = [["a","b",1],["b","c",2],["a","c",3]] graph.make_weighted_from_list(nodes, directed=False) test_graph = { Node("a"):[Edge("a", "b",1), Edge("a","c",3)], Node("b"):[Edge("b","a",1), Edge("b","c",2)], Node("c"):[Edge("c","b",2), Edge("c","a",3)] } assert compare_graphs(test_graph, graph.graph) == True
def test_weighted_modify_weight(): graph = Graph() nodes = [["a","b",1],["b","c",2],["a","c",3]] graph.make_weighted_from_list(nodes) graph.modify_weight(["a","b",1], 78) test_graph = { Node("a"):[Edge("a", "b",78), Edge("a","c",3)], Node("b"):[Edge("b","c",2)], Node("c"):[] } assert compare_graphs(test_graph, graph.graph) == True
def test_weighted_add_node(): graph = Graph() nodes = [["a","b",5],["b","c",4],["a","c",3]] graph.make_weighted_from_list(nodes) graph.add_node(["b","b",2]) graph.add_node(["b","d",1]) test_graph = { Node("a"):[Edge("a","b",5),Edge("a","c",3)], Node("b"):[Edge("b","c",4),Edge("b","b",2),Edge("b","d",1)], Node("c"):[], Node("d"):[] } assert compare_graphs(test_graph, graph.graph) == True
def test_weighted_remove_node(): graph = Graph() nodes = [["a","b",1],["b","b",99],["c","b",2],["d","b",3],["e","b",4],["f","z","a"]] graph.make_weighted_from_list(nodes) graph.remove_node(Node("b")) test_graph = { Node("a"):[], Node("c"):[], Node("d"):[], Node("e"):[], Node("f"):[Edge("f","z","a")], Node("z"): [] } assert compare_graphs(test_graph, graph.graph) == True
def test_fails_with_negative_weights(): graph = Graph() nodes = [["a","b", -1], ["c", "d", 4]] graph.make_weighted_from_list(nodes) with raises(DijkstrasException): graph.dijkstras_with_target(start=Node("a"), target=Node("b"))