def test_breadth_first_empty_graph(): graph = Graph() amman = Node("Amman") with pytest.raises(ValueError): lst = graph.breadth_first(amman)
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_breadth_first_one_element(): graph = Graph() amman = graph.add_node('Amman') lst = graph.breadth_first(amman) assert lst == [amman]
def test_size(): g = Graph() a = g.add_node('a') b = g.add_node('b') c = g.add_node('c') assert g.size() == 3
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_nodes(): g = Graph() a = g.add_node('a') b = g.add_node('b') c = g.add_node('c') assert len(g.get_nodes()) == 3
def test_size(): graph = Graph() graph.add_node('batool') assert graph.size() == 1 graph.add_node('malkawi') assert graph.size() == 2
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_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__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_add_node(): graph = Graph() assert graph.add_node('batool').value == 'batool'
def test_get_nodes_empty(): graph = Graph() assert len(graph.get_nodes()) == 0
def test_get_nodes(): graph = Graph() banana = graph.add_node('banana') apple = graph.add_node('apple') dog = graph.add_node('dog') assert len(graph.get_nodes()) == 3
def test_to_neighbors_dict_no_neighbors(): graph = Graph() a = graph.add_node("a") neighbors_dict = to_neighbors_dict(graph.get_neighbors(a)) assert neighbors_dict == {}
def test_add_one_edge(): graph = Graph() end = graph.add_node('end') start = graph.add_node('start') graph.add_edge(start, end)
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_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_size_empty(): graph = Graph() assert graph.size() == 0
def test_add_node(): graph = Graph() assert graph.add_node('a').value == 'a'
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_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 }
def test_get_neighbors_no_neighbors(): graph = Graph() banana = graph.add_node('banana') neighbors = graph.get_neighbors(banana) assert len(neighbors) == 0 assert neighbors == []
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_get_nodes_no_nodes(): g = Graph() assert len(g.get_nodes())== 0
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_get_edges_one_vertex(): g = Graph() v1 = g.add_node('Pandora') v2 = g.add_node('Mordor') v3 = g.add_node('Monstropolis') v4 = g.add_node('Metroville') v5 = g.add_node('Naboo') v6 = g.add_node('Narnia') g.add_nondirectional_edge(v1, v2, 110) g.add_nondirectional_edge(v2, v3, 50) g.add_nondirectional_edge(v2, v4, 12) g.add_nondirectional_edge(v3, v5, 43) g.add_nondirectional_edge(v4, v5, 23) g.add_nondirectional_edge(v4, v6, 14) g.add_nondirectional_edge(v5, v6, 21) actual = g.get_edge(['Mordor']) expected = (True, '$0') assert actual == expected
def test_depth_first_empty_graph(): graph = Graph() banana = Node("banana") with pytest.raises(ValueError): nodes_list = graph.depth_first(banana)
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