def test_add_edge(): gr = Graph() start = gr.add_node('start') end = gr.add_node('end') gr.add_edge(start, end) assert gr.adjacency_list[start][0] == (end, 1)
def test_one_node(): graph = Graph() graph.add_node('node1') graph.add_edge('node1','node1') actual = graph.get_neighbors('node1') expected = 'node1 :edge--> node1 ... weight: 1' assert expected == actual
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_one_node_one_edge(): g = Graph() g.add_node('a') g.add_edge('a', 'a') actual = g.get_neighbors('a') expected = 'a --> a edge weight: 1' assert expected == actual
def test_add_edge_groovy(): graph = Graph() end = graph.add_node('end') start = graph.add_node('start') graph.add_edge(start, end)
def test_edge_added_to_graph(): g = Graph() g.add_node('a') g.add_node('b') g.add_edge('a', 'b', 1) actual = g.get_neighbors('a') expected = 'a --> b edge weight: 1' assert actual == expected
def test_add_edge_other_case(): graph = Graph() end = graph.add_node('end') start = graph.add_node('start') graph.add_edge(start, end)
def test_get_neighbors(): gr = Graph() start = gr.add_node('start') end = gr.add_node('end') gr.add_edge(start, end, 4) neighbors = gr.get_neighbors(start) assert neighbors[0][0].value == 'end' assert neighbors[0][1] == 4
def test_edge(): graph=Graph() graph.add_node('a') graph.add_node('b') graph.add_edge('a', 'b', 8) actual = graph.get_neighbors('a') expected = 'a :edge--> b ... weight: 8' assert actual == expected
def test_edge_end_node_not_in_graph(): graph = Graph() end = Vertex('end') start = graph.add_node('start') with pytest.raises(KeyError): graph.add_edge(start, end)
def test_all_neighbors_can_be_retrived_with_weight(): g = Graph() g.add_node('a') g.add_node('b') g.add_edge('a', 'b', 4) actual = g.get_neighbors('a') expected = 'a --> b edge weight: 4' assert expected == actual
def test_get_neighbors(): graph = Graph() hi = graph.add_node('hi') hello = graph.add_node('hello') graph.add_edge(hi, hello) actual = graph.get_neighbors(hi) assert actual == [(hello, 0)]
def test_breadth_first_regular(): graph = Graph() banana = graph.add_node('banana') vertices_lst = graph.breadth_first(banana) apple = graph.add_node('apple') graph.add_edge(banana, apple) vertices_lst = graph.breadth_first(banana) assert vertices_lst == [banana, apple]
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 = Vertex('d') graph.add_edge(a, b) graph.add_edge(b, c) with pytest.raises(ValueError): vertices_lst = graph.depth_first(d)
def test_breadth_first_regular2(): graph = Graph() banana = graph.add_node('banana') apple = graph.add_node('apple') strawberry = graph.add_node('strawberry') graph.add_edge(banana, apple) graph.add_edge(strawberry, banana) vertices_lst = graph.breadth_first(strawberry) assert vertices_lst == [strawberry, banana, apple]
def test_add_edge(): graph = Graph() end = graph.add_node('banana') start = graph.add_node('apple') graph.add_edge(start, end) assert graph._adjacency_list[start][0] == (end, 0) assert graph.get_neighbors(start) == [(end, 0)] graph.add_edge(end, start) assert graph.get_neighbors(end) == [(start, 0)]
def test_neighbors_retrived(): graph = Graph() graph.add_node('node1') graph.add_node('node2') graph.add_node('node3') graph.add_node('node4') graph.add_node('node2') graph.add_edge('node1', 'node2', 5) graph.add_edge('node1', 'node4', 3) graph.add_edge('node2', 'node4', 1) graph.add_edge('node1', 'node1', 2) actual = graph.get_nodes() expected = {'node1': [['node2', 5], ['node4', 3], ['node1', 2]], 'node2': [['node4', 1]], 'node3': [], 'node4': []} assert expected == actual
def test_add_edge_with_weight_letters(): graph = Graph() end = graph.add_node('dog') start = graph.add_node('cat') graph.add_edge(start, end, "a") assert graph.get_neighbors(start) == [(end, "a")] graph.add_edge(end, start, "b") assert graph.get_neighbors(end) == [(start, "b")]
def test_add_edge_with_weight(): graph = Graph() end = graph.add_node('banana') start = graph.add_node('apple') graph.add_edge(start, end, 44) assert graph.get_neighbors(start) == [(end, 44)] graph.add_edge(end, start, 23) assert graph.get_neighbors(end) == [(start, 23)]
def graph_test(): graph = Graph() graph = Graph() graph.add_node('a') graph.add_node('b') graph.add_node('c') graph.add_node('d') graph.add_edge('a', 'b', 4) graph.add_edge('a', 'd', 9) graph.add_edge('a', 'c', 3) graph.add_edge('b', 'a', 4) graph.add_edge('c', 'a', 3) graph.add_edge('c', 'd', 6) graph.add_edge('d', 'a', 9) graph.add_edge('d', 'b', 5) graph.add_edge('d', 'c', 6) return graph
def test_breadth_first_different_start_node(): graph = Graph() banana = graph.add_node('banana') apple = graph.add_node('apple') strawberry = graph.add_node('strawberry') graph.add_edge(banana, apple) graph.add_edge(strawberry, banana) vertices_lst = graph.breadth_first(banana) assert vertices_lst == [banana, apple]
def test_helper2(): 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 = helper2(graph.get_neighbors(a)) assert neighbors_dict == { "b": (b, 150), "c": (c, 100), "d": (d, 50), }
def test_breadth_firts_islands_nodes(): graph = Graph() banana = graph.add_node('banana') apple = graph.add_node('apple') strawberry = graph.add_node('strawberry') graph.add_edge(banana, apple) vertices_lst = graph.breadth_first(banana) assert vertices_lst == [banana, apple] vertices_lst = graph.breadth_first(apple) assert vertices_lst == [apple]
def test_get_neighbors(): graph = Graph() banana = graph.add_node('banana') apple = graph.add_node('apple') graph.add_edge(apple, banana, 44) neighbors = graph.get_neighbors(apple) assert len(neighbors) == 1 assert neighbors[0][0].value == 'banana' assert isinstance(neighbors[0][0], Vertex) assert neighbors[0][1] == 44
def test_add_edge_end(): graph = Graph() end = Node('end') start = graph.add_node('start') actual = graph.add_edge(start, end) assert actual == 'End node is not exist in Graph'
def test_add_edge_start(): graph = Graph() start = Node('start') end = graph.add_node('end') actual = graph.add_edge(start, end) assert actual == 'Start node is not exist in Graph'
def test_add_edge_more_nodes(): graph = Graph() a_vertex = graph.add_node('a') b_vertex = graph.add_node('b') c_vertex = graph.add_node('c') d_vertex = graph.add_node('d') graph.add_edge(a_vertex, b_vertex, 1) graph.add_edge(a_vertex, c_vertex, 2) graph.add_edge(a_vertex, d_vertex, 3) neighbors = graph.get_neighbors(a_vertex) assert neighbors[0][0].value == 'b' assert neighbors[0][1] == 1 assert isinstance(neighbors[0][0], Vertex) assert neighbors[1][0].value == 'c' assert neighbors[1][1] == 2 assert isinstance(neighbors[1][0], Vertex) assert neighbors[2][0].value == 'd' assert neighbors[2][1] == 3 assert isinstance(neighbors[2][0], Vertex)
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) vertices_lst = graph.depth_first(banana) assert vertices_lst == [ banana.value, apple.value, strawberry.value, tomato.value ]
def test_breadth_first_check_visited(): graph = Graph() banana = graph.add_node('banana') apple = graph.add_node('apple') strawberry = graph.add_node('strawberry') graph.add_edge(banana, apple) graph.add_edge(apple, strawberry) vertices_lst = graph.breadth_first(banana) assert vertices_lst == [banana, apple, strawberry] tomato = graph.add_node('tomato') graph.add_edge(apple, tomato) vertices_lst = graph.breadth_first(banana) assert vertices_lst == [banana, apple, strawberry, tomato]
def test_all_neighbors_can_be_retrived(): g = Graph() g.add_node('a') g.add_node('b') g.add_node('c') g.add_node('d') g.add_edge('a', 'b', 1) g.add_edge('a', 'd', 3) g.add_edge('b', 'd', 4) g.add_edge('d', 'd', 6) g.add_edge('d', 'a', 5) actual = g.get_nodes() expected = { 'a': [['b', 1], ['d', 3]], 'b': [['d', 4]], 'c': [], 'd': [['d', 6], ['a', 5]] } assert expected == actual