Adds a vertex to the graph. If the given vertex already exists, a ValueError will be thrown. >>> g = GraphAdjacencyList(vertices=[], edges=[], directed=False) >>> g.add_vertex("A") >>> g.adj_list {'A': []} >>> g.add_vertex("A") Traceb
(self, vertex: T)
| 58 | self.add_edge(edge[0], edge[1]) |
| 59 | |
| 60 | def add_vertex(self, vertex: T) -> None: |
| 61 | """ |
| 62 | Adds a vertex to the graph. If the given vertex already exists, |
| 63 | a ValueError will be thrown. |
| 64 | |
| 65 | >>> g = GraphAdjacencyList(vertices=[], edges=[], directed=False) |
| 66 | >>> g.add_vertex("A") |
| 67 | >>> g.adj_list |
| 68 | {'A': []} |
| 69 | >>> g.add_vertex("A") |
| 70 | Traceback (most recent call last): |
| 71 | ... |
| 72 | ValueError: Incorrect input: A is already in the graph. |
| 73 | """ |
| 74 | if self.contains_vertex(vertex): |
| 75 | msg = f"Incorrect input: {vertex} is already in the graph." |
| 76 | raise ValueError(msg) |
| 77 | self.adj_list[vertex] = [] |
| 78 | |
| 79 | def add_edge(self, source_vertex: T, destination_vertex: T) -> None: |
| 80 | """ |