MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / remove_edge

Method remove_edge

graphs/graph_adjacency_list.py:132–157  ·  view source on GitHub ↗

Removes the edge between the two vertices. If any given vertex doesn't exist or the edge does not exist, a ValueError will be thrown.

(self, source_vertex: T, destination_vertex: T)

Source from the content-addressed store, hash-verified

130 self.adj_list.pop(vertex)
131
132 def remove_edge(self, source_vertex: T, destination_vertex: T) -> None:
133 """
134 Removes the edge between the two vertices. If any given vertex
135 doesn't exist or the edge does not exist, a ValueError will be thrown.
136 """
137 if not (
138 self.contains_vertex(source_vertex)
139 and self.contains_vertex(destination_vertex)
140 ):
141 msg = (
142 f"Incorrect input: Either {source_vertex} or "
143 f"{destination_vertex} does not exist"
144 )
145 raise ValueError(msg)
146 if not self.contains_edge(source_vertex, destination_vertex):
147 msg = (
148 "Incorrect input: The edge does NOT exist between "
149 f"{source_vertex} and {destination_vertex}"
150 )
151 raise ValueError(msg)
152
153 # remove the destination vertex from the list associated with the source
154 # vertex and vice versa if not directed
155 self.adj_list[source_vertex].remove(destination_vertex)
156 if not self.directed:
157 self.adj_list[destination_vertex].remove(source_vertex)
158
159 def contains_vertex(self, vertex: T) -> bool:
160 """

Calls 3

contains_vertexMethod · 0.95
contains_edgeMethod · 0.95
removeMethod · 0.45