def test_edge_end_node_not_in_graph(): graph = Graph() end = Node('end') start = graph.add_node('start') with pytest.raises(KeyError): graph.add_edge(start, end)
def test_depth_first_xhallenge_example(): graph = Graph() a = graph.add_node('a') b = graph.add_node('b') c = graph.add_node('c') d = graph.add_node('d') e = graph.add_node('e') f = graph.add_node('f') g = graph.add_node('g') h = graph.add_node('h') graph.add_edge(a, b) graph.add_edge(b, c) graph.add_edge(b, d) graph.add_edge(c, g) graph.add_edge(a, d) graph.add_edge(d, e) graph.add_edge(d, h) graph.add_edge(d, f) graph.add_edge(f, h) vertices_lst = graph.depth_first(a) assert vertices_lst == [a.value, b.value, c.value, g.value, d.value, e.value, h.value, f.value]
def test_add_edge(): graph = Graph() end = graph.add_node('batool') start = graph.add_node('malkawi') graph.add_edge(start, end) assert graph.adjacency_list[start][0] == (end, 1) assert graph.get_neighbors(start) == [(end, 1)] graph.add_edge(end, start) assert graph.get_neighbors(end) == [(start, 1)]
def test_get_neighbors(): graph = Graph() banana = graph.add_node('banana') apple = graph.add_node('apple') graph.add_edge(apple, banana, 5) neighbors = graph.get_neighbors(apple) assert len(neighbors) == 1 assert neighbors[0][0].value == 'banana' assert isinstance(neighbors[0][0], Node) assert neighbors[0][1] == 5
def test_depth_first__node_not_in_graph(): graph = Graph() a = graph.add_node('a') b = graph.add_node('b') c = graph.add_node('c') d = Node('d') graph.add_edge(a, b) graph.add_edge(b, c) with pytest.raises(ValueError): nodes_list = graph.depth_first(d)
def test_get_neighbors(): g = Graph() a = g.add_node('a') b = g.add_node('b') c = g.add_node('c') g.add_edge(a,b,4) neighbors = g.get_neighbors(a) assert neighbors[0][0].value == 'b' assert len(neighbors) == 1 assert neighbors[0][1] == 4
def test_depth_first_given_vertex_not_in_graph(): graph = Graph() a = graph.add_node('a') b = graph.add_node('b') c = graph.add_node('c') d = Node('d') graph.add_edge(a, b) graph.add_edge(b, c) with pytest.raises(ValueError): vertices_lst = graph.depth_first(d)
def test_to_neighbors_dict(): graph = Graph() a = graph.add_node("a") b = graph.add_node("b") c = graph.add_node("c") d = graph.add_node("d") graph.add_edge(a, b, 150) graph.add_edge(a, c, 100) graph.add_edge(a, d, 50) neighbors_dict = to_neighbors_dict(graph.get_neighbors(a)) assert neighbors_dict == { "b": (b, 150), "c": (c, 100), "d": (d, 50), }
def test_depth_first(): graph = Graph() banana = graph.add_node('banana') apple = graph.add_node('apple') strawberry = graph.add_node('strawberry') tomato = graph.add_node('tomato') graph.add_edge(banana, apple) graph.add_edge(apple, strawberry) graph.add_edge(banana, tomato) nodes_list = graph.depth_first(banana) assert nodes_list == [ banana.value, tomato.value, apple.value, strawberry.value ]
def test_depth_first(): graph = Graph() a = graph.add_node('a') b = graph.add_node('b') c = graph.add_node('c') d = graph.add_node('d') e = graph.add_node('e') f = graph.add_node('f') graph.add_edge(a, b) graph.add_edge(a, c) graph.add_edge(a, d) graph.add_edge(c, e) graph.add_edge(e, f) vertices_lst = graph.depth_first(a) assert vertices_lst == [a.value, b.value, c.value, e.value, f.value, d.value]
def test_add_one_edge(): graph = Graph() end = graph.add_node('end') start = graph.add_node('start') graph.add_edge(start, end)
def test_is_path_bfs_false(): graph = Graph() n1 = graph.add_node(1) n2 = graph.add_node(2) graph.add_edge(n1, n2) assert (graph.isPathBFS(n2, n1)) == False
def test_is_path_bfs_true(): graph = Graph() n1 = graph.add_node(1) n2 = graph.add_node(2) graph.add_edge(n1, n2) assert (graph.isPathBFS(n1, n2)) == True
def my_graph(): graph = Graph() amman = graph.add_node("amman") zarqa = graph.add_node("zarqa") irbid = graph.add_node("irbid") salt = graph.add_node("salt") aqaba = graph.add_node("aqaba") damanhur = graph.add_node("damanhur") graph.add_edge(amman, irbid, 150) graph.add_edge(amman, zarqa, 82) graph.add_edge(irbid, zarqa, 99) graph.add_edge(zarqa, salt, 105) graph.add_edge(irbid, salt, 42) graph.add_edge(zarqa, aqaba, 37) graph.add_edge(zarqa, damanhur, 26) graph.add_edge(damanhur, damanhur, 250) graph.add_edge(salt, damanhur, 73) return graph
def test_to_nodes_dict(): graph = Graph() amman = graph.add_node("amman") zarqa = graph.add_node("zarqa") irbid = graph.add_node("irbid") salt = graph.add_node("salt") aqaba = graph.add_node("aqaba") damanhur = graph.add_node("damanhur") graph.add_edge(amman, irbid, 150) graph.add_edge(amman, zarqa, 82) graph.add_edge(irbid, zarqa, 99) graph.add_edge(zarqa, salt, 105) graph.add_edge(irbid, salt, 42) graph.add_edge(zarqa, aqaba, 37) graph.add_edge(zarqa, damanhur, 26) graph.add_edge(damanhur, damanhur, 250) graph.add_edge(salt, damanhur, 73) nodes_dict = to_nodes_dict(graph.get_nodes()) assert nodes_dict == { "amman": amman, "zarqa": zarqa, "irbid": irbid, "salt": salt, "aqaba": aqaba, "damanhur": damanhur }