@@ -355,7 +355,7 @@ def remove_pair(self, u, v) -> None:
355355 self .graph [v ].remove (_ )
356356
357357 # if no destination is meant the default value is -1
358- def dfs (self , s = - 2 , d = - 1 ) -> None :
358+ def dfs (self , s = - 2 , d = - 1 ) -> list [ int ] :
359359 """
360360 Performs a depth-first search starting from node s.
361361 If destination d is given, stops when d is found
@@ -459,7 +459,7 @@ def bfs(self, s=-2) -> list[int]:
459459 >>> g2.bfs()
460460 [10, 20, 30]
461461 """
462- d = deque ()
462+ d : deque = deque ()
463463 visited = []
464464 if s == - 2 :
465465 s = next (iter (self .graph ))
@@ -535,12 +535,15 @@ def has_cycle(self) -> bool:
535535 Detects whether the undirected graph contains a cycle.
536536
537537 Note:
538- - This function assumes the graph is connected and only traverses from the first node found in the graph.
538+ - This function assumes the graph is connected and only traverses from the
539+ first node found in the graph.
539540 - It does not detect cycles that exist in disconnected components.
540- - It also does not detect self-loops (e.g., an edge from a node to itself like 1-1).
541+ - It also does not detect self-loops
542+ (e.g., an edge from a node to itself like 1-1).
541543
542544 Returns:
543- bool: True if a cycle is detected in the connected component starting from the first node; False otherwise.
545+ bool: True if a cycle is detected in the connected component starting
546+ from the first node; False otherwise.
544547
545548 >>> g = Graph()
546549 >>> g.add_pair(1, 2)
@@ -562,7 +565,7 @@ def has_cycle(self) -> bool:
562565 >>> g4.add_pair(3, 4)
563566 >>> g4.add_pair(4, 5)
564567 >>> g4.add_pair(5, 3) # cycle in disconnected component
565- >>> g4.has_cycle() # Only checks the component reachable from the first node (1)
568+ >>> g4.has_cycle() # Only checks the component reachable from the first node 1
566569 False
567570 """
568571
@@ -572,7 +575,7 @@ def has_cycle(self) -> bool:
572575 stack .append (s )
573576 visited .append (s )
574577 parent = - 2
575- indirect_parents = []
578+ indirect_parents : list [ int ] = []
576579 ss = s
577580 on_the_way_back = False
578581 anticipating_nodes = set ()
0 commit comments