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_with_disconnected_nodes(): graph = Graph() nodes = [["a"], ["b"]] graph.make_unweighted_from_list(nodes) actual_path = graph.dijkstras_with_target(start=Node("a"), target=Node("b")) expected_path = [] assert actual_path == expected_path
def test_cyclic_graph(): graph = Graph() nodes = [["a","b"], ["b","c"], ["b", "a"], ["b", "z"], ["c", "d"], ["c", "e"], ["e", "a"]] graph.make_unweighted_from_list(nodes) expected_path = ["a", "b", "c", "e"] actual_path = graph.dijkstras_with_target(start=Node("a"), target=Node("e")) 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_strongly_connected_graph(): graph = Graph() nodes = [] nodes_to_add = set(["a","b","c","d","e","f","g","h","i"]) for node in nodes_to_add: for other_node in nodes_to_add: if other_node != node: nodes.append([node,other_node]) graph = Graph() graph.make_unweighted_from_list(nodes, directed=False) actual_path = graph.dijkstras_with_target(start = Node("f"), target = Node("i")) expected_path = ["f","i"] assert actual_path == expected_path
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"))