Graph Implementierung für die Aufgabe OK
| | |
| | | if not isinstance(vertex, int): |
| | | raise TypeError(f"Argument {vertex} is not a valid vertex") |
| | | self._adjacent[vertex] = set() |
| | | |
| | | pass |
| | | |
| | | def add_edge(self, u, v): |
| | |
| | | visited_edges: dict[int, set] = {} |
| | | for start in self.vertices(): |
| | | for end in self.adjacent_of(start): |
| | | (first, second) = (start, end) if (start > end) else (end, start) |
| | | (first, second) = (start, end) if (start >= end) else (end, start) |
| | | if first in visited_edges: |
| | | visited_adjacent = visited_edges.get(first) |
| | | if second not in visited_adjacent: |
| | |
| | | self.assertTrue(edge in edges) |
| | | g.for_each_edges(action) |
| | | |
| | | def test_iterate_all_vertices(self): |
| | | g = Graph() |
| | | g.add_edge(1, 2) |
| | | g.add_edge(1, 3) |
| | | g.add_edge(2, 3) |
| | | g.add_edge(2, 4) |
| | | vertices = [] |
| | | for v in g.vertices(): |
| | | vertices.append(v) |
| | | expected = [1, 2, 3, 4] |
| | | self.assertListEqual(sorted(vertices), expected) |
| | | pass |
| | | |
| | | def test_iterate_neighbor_vertex(self): |
| | | g = Graph() |
| | | g.add_edge(1, 2) |
| | | g.add_edge(1, 3) |
| | | g.add_edge(2, 3) |
| | | g.add_edge(2, 4) |
| | | g.add_edge(4, 1) |
| | | vertices = [] |
| | | for v in g.adjacent_of(1): |
| | | vertices.append(v) |
| | | expected = [2, 3, 4] |
| | | self.assertListEqual(sorted(vertices), expected) |
| | | pass |
| | | |
| | | def test_not_add_float_to_vertex(self): |
| | | g = Graph() |
| | | try: |
| | | g.add_vertex(1.2) |
| | | pass |
| | | except TypeError as ex: |
| | | expected_msg = "Argument 1.2 is not a valid vertex" |
| | | self.assertEqual(str(ex), expected_msg) |
| | | pass |
| | | pass |
| | | |
| | | def test_not_add_string_to_vertex(self): |
| | | g = Graph() |
| | | try: |
| | | g.add_vertex("add") |
| | | except TypeError as ex: |
| | | expected_msg = 'Argument add is not a valid vertex' |
| | | self.assertEqual(str(ex), expected_msg) |
| | | pass |
| | | pass |
| | | |
| | | |
| | | if __name__ == '__main__': |
| | | unittest.main() |