def test_add_vertex(): g = Graph() g.add_vertex(1) assert g.get_vertex(1).data == 1 assert g.get_vertex(1).connected_to == {}
def test_is_two_colors_returns_true(): g = Graph() for i in range(1, 7): g.add_vertex(i) g.add_edge(1, 2) g.add_edge(1, 6) g.add_edge(2, 5) g.add_edge(2, 3) g.add_edge(3, 4) g.add_edge(4, 1) assert is_two_colors(g)
def test_add_edge(): g = Graph() g.add_vertex(1) g.add_vertex(2) g.add_edge(1, 2) v1_connections = g.get_vertex(1).get_connections() assert v1_connections[0].data == 2 v2_connections = g.get_vertex(2).get_connections() assert v2_connections[0].data == 1
def test_is_route_returns_true(): g = Graph() for i in range(1, 7): g.add_vertex(i) g.add_edge(1, 2) g.add_edge(1, 5) g.add_edge(1, 6) g.add_edge(2, 5) g.add_edge(2, 3) g.add_edge(5, 4) g.add_edge(3, 4) v1 = g.get_vertex(1) v2 = g.get_vertex(3) assert is_route(g, v1, v2)
def test_connected_components(): g = Graph() for i in range(1, 7): g.add_vertex(i) g.add_edge(1, 2) g.add_edge(1, 5) g.add_edge(1, 6) g.add_edge(2, 5) g.add_edge(2, 3) g.add_edge(5, 4) g.add_edge(3, 4) for i in range(7, 10): g.add_vertex(i) g.add_edge(7, 8) g.add_edge(8, 9) result = connected_components(g) assert sorted(result[1]) == [1, 2, 3, 4, 5, 6] assert sorted(result[2]) == [7, 8, 9]
def test_is_route_returns_false(): g = Graph() for i in range(1, 7): g.add_vertex(i) g.add_edge(1, 2) g.add_edge(1, 5) g.add_edge(1, 6) g.add_edge(2, 5) g.add_edge(2, 3) g.add_edge(5, 4) g.add_edge(3, 4) v1 = g.get_vertex(6) v2 = Vertex(7) assert not is_route(g, v1, v2)
def test_dfs(): g = Graph() for i in range(1, 7): g.add_vertex(i) g.add_edge(1, 2) g.add_edge(1, 5) g.add_edge(1, 6) g.add_edge(2, 5) g.add_edge(2, 3) g.add_edge(5, 4) g.add_edge(3, 4) v1 = g.get_vertex(1) g.dfs(v1) v4 = g.get_vertex(4) assert g.entry_time[v4] == 4 assert g.exit_time[v4] == 7 v5 = g.get_vertex(5) assert g.parent[v4] == v5
def test_bfs(): g = Graph() for i in range(1, 7): g.add_vertex(i) g.add_edge(1, 2) g.add_edge(1, 5) g.add_edge(1, 6) g.add_edge(2, 5) g.add_edge(2, 3) g.add_edge(5, 4) g.add_edge(3, 4) v1 = g.get_vertex(1) assert g.bfs(v1) == [1, 2, 5, 6, 3, 4]