Creates an edge from source vertex to destination vertex. If any given vertex doesn't exist or the edge already exists, a ValueError will be thrown.
(self, source_vertex: T, destination_vertex: T)
| 77 | self.adj_list[vertex] = [] |
| 78 | |
| 79 | def add_edge(self, source_vertex: T, destination_vertex: T) -> None: |
| 80 | """ |
| 81 | Creates an edge from source vertex to destination vertex. If any |
| 82 | given vertex doesn't exist or the edge already exists, a ValueError |
| 83 | will be thrown. |
| 84 | """ |
| 85 | if not ( |
| 86 | self.contains_vertex(source_vertex) |
| 87 | and self.contains_vertex(destination_vertex) |
| 88 | ): |
| 89 | msg = ( |
| 90 | f"Incorrect input: Either {source_vertex} or " |
| 91 | f"{destination_vertex} does not exist" |
| 92 | ) |
| 93 | raise ValueError(msg) |
| 94 | if self.contains_edge(source_vertex, destination_vertex): |
| 95 | msg = ( |
| 96 | "Incorrect input: The edge already exists between " |
| 97 | f"{source_vertex} and {destination_vertex}" |
| 98 | ) |
| 99 | raise ValueError(msg) |
| 100 | |
| 101 | # add the destination vertex to the list associated with the source vertex |
| 102 | # and vice versa if not directed |
| 103 | self.adj_list[source_vertex].append(destination_vertex) |
| 104 | if not self.directed: |
| 105 | self.adj_list[destination_vertex].append(source_vertex) |
| 106 | |
| 107 | def remove_vertex(self, vertex: T) -> None: |
| 108 | """ |