Adds a vertex to the graph. If the given vertex already exists, a ValueError will be thrown.
(self, vertex: T)
| 116 | self.adj_matrix[v][u] = 0 |
| 117 | |
| 118 | def add_vertex(self, vertex: T) -> None: |
| 119 | """ |
| 120 | Adds a vertex to the graph. If the given vertex already exists, |
| 121 | a ValueError will be thrown. |
| 122 | """ |
| 123 | if self.contains_vertex(vertex): |
| 124 | msg = f"Incorrect input: {vertex} already exists in this graph." |
| 125 | raise ValueError(msg) |
| 126 | |
| 127 | # build column for vertex |
| 128 | for row in self.adj_matrix: |
| 129 | row.append(0) |
| 130 | |
| 131 | # build row for vertex and update other data structures |
| 132 | self.adj_matrix.append([0] * (len(self.adj_matrix) + 1)) |
| 133 | self.vertex_to_index[vertex] = len(self.adj_matrix) - 1 |
| 134 | |
| 135 | def remove_vertex(self, vertex: T) -> None: |
| 136 | """ |