Skip to content

Add to doctests #12790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix code style issues flagged by CI
  • Loading branch information
tioBlachi committed Jun 9, 2025
commit 0daf7356649e6918f7b9e7d41ee7a428c0b77021
17 changes: 10 additions & 7 deletions graphs/directed_and_undirected_weighted_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def remove_pair(self, u, v) -> None:
self.graph[v].remove(_)

# if no destination is meant the default value is -1
def dfs(self, s=-2, d=-1) -> None:
def dfs(self, s=-2, d=-1) -> list[int]:
"""
Performs a depth-first search starting from node s.
If destination d is given, stops when d is found
Expand Down Expand Up @@ -459,7 +459,7 @@ def bfs(self, s=-2) -> list[int]:
>>> g2.bfs()
[10, 20, 30]
"""
d = deque()
d: deque = deque()
visited = []
if s == -2:
s = next(iter(self.graph))
Expand Down Expand Up @@ -535,12 +535,15 @@ def has_cycle(self) -> bool:
Detects whether the undirected graph contains a cycle.

Note:
- This function assumes the graph is connected and only traverses from the first node found in the graph.
- This function assumes the graph is connected and only traverses from the
first node found in the graph.
- It does not detect cycles that exist in disconnected components.
- It also does not detect self-loops (e.g., an edge from a node to itself like 1-1).
- It also does not detect self-loops
(e.g., an edge from a node to itself like 1-1).

Returns:
bool: True if a cycle is detected in the connected component starting from the first node; False otherwise.
bool: True if a cycle is detected in the connected component starting
from the first node; False otherwise.

>>> g = Graph()
>>> g.add_pair(1, 2)
Expand All @@ -562,7 +565,7 @@ def has_cycle(self) -> bool:
>>> g4.add_pair(3, 4)
>>> g4.add_pair(4, 5)
>>> g4.add_pair(5, 3) # cycle in disconnected component
>>> g4.has_cycle() # Only checks the component reachable from the first node (1)
>>> g4.has_cycle() # Only checks the component reachable from the first node 1
False
"""

Expand All @@ -572,7 +575,7 @@ def has_cycle(self) -> bool:
stack.append(s)
visited.append(s)
parent = -2
indirect_parents = []
indirect_parents: list[int] = []
ss = s
on_the_way_back = False
anticipating_nodes = set()
Expand Down