Skip to content

Commit 931386d

Browse files
fix: Remove defensive checks in bipartite graph functions
This commit removes defensive checks in is_bipartite_dfs and is_bipartite_bfs functions that were preventing natural KeyError and TypeError exceptions from occurring for invalid graph inputs. The functions contained checks like 'if node not in graph_node not in graph: return True' and 'if curr_node not in graph: continue' which would return early or skip processing when encountering invalid graph structures, instead of allowing the natural exceptions to occur. According to FIXME comments in the docstrings, the expected behavior is: - KeyError should be raised when a graph contains neighbors that are not keys in the graph dictionary - TypeError should be raised when non-integer keys are used where integers are expected By removing these defensive checks, the functions now properly raise KeyError when accessing graph[node] for a node that is not a key in the graph, which aligns with the documented expected behavior. Fixes #15127
1 parent 181175e commit 931386d

1 file changed

Lines changed: 0 additions & 4 deletions

File tree

graphs/check_bipatrite.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ def depth_first_search(node: int, color: int) -> bool:
6767
"""
6868
if visited[node] == -1:
6969
visited[node] = color
70-
if node not in graph:
71-
return True
7270
for neighbor in graph[node]:
7371
if not depth_first_search(neighbor, 1 - color):
7472
return False
@@ -140,8 +138,6 @@ def is_bipartite_bfs(graph: dict[int, list[int]]) -> bool:
140138
visited[node] = 0
141139
while queue:
142140
curr_node = queue.popleft()
143-
if curr_node not in graph:
144-
continue
145141
for neighbor in graph[curr_node]:
146142
if visited[neighbor] == -1:
147143
visited[neighbor] = 1 - visited[curr_node]

0 commit comments

Comments
 (0)