Skip to content

Commit 5835a46

Browse files
committed
Add doctest coverage to Graph.remove_pair
1 parent c5519cd commit 5835a46

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

graphs/directed_and_undirected_weighted_graph.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,35 @@ def add_pair(self, u, v, w=1) -> None:
315315

316316
# handles if the input does not exist
317317
def remove_pair(self, u, v):
318+
"""
319+
Removes the edge between u and v in an unidirected graph, if it exists
320+
321+
>>> g = Graph()
322+
>>> g.add_pair(1,2)
323+
>>> g.add_pair(1,3,5)
324+
>>> g.graph[1]
325+
[[1, 2], [5, 3]]
326+
>>> g.remove_pair(1, 2)
327+
>>> g.graph[1]
328+
[[5, 3]]
329+
>>> g.graph[2]
330+
[]
331+
>>> g.remove_pair(1,4) # node 4 does not exist
332+
>>> g.remove_pair(10, 11) # neither exists
333+
>>> g.add_pair(5,5)
334+
>>> g.graph[5]
335+
[[1, 5]]
336+
>>> g.remove_pair(5,5)
337+
>>> g.graph[5]
338+
[]
339+
>>> g.add_pair(6,7,2)
340+
>>> g.add_pair(6,7,3)
341+
>>> g.graph[6]
342+
[[2, 7], [3, 7]]
343+
>>> g.remove_pair(6,7)
344+
>>> g.graph[6]
345+
[[3, 7]]
346+
"""
318347
if self.graph.get(u):
319348
for _ in self.graph[u]:
320349
if _[1] == v:

0 commit comments

Comments
 (0)