From f0b09084305e41667316fed1ee956059b6901143 Mon Sep 17 00:00:00 2001 From: tanishqraikwar54-blip Date: Mon, 31 Aug 2026 01:07:29 +0530 Subject: [PATCH] fix: Remove defensive checks in bipartite graph functions to allow natural KeyError/TypeError exceptions The is_bipartite_dfs and is_bipartite_bfs functions contained defensive checks that prevented natural exceptions from occurring when given invalid graph inputs. According to FIXME comments in the docstrings, these functions should allow KeyError to be raised when a graph contains neighbors that are not keys in the graph dictionary, and TypeError when non-integer keys are used where integers are expected. This commit removes the defensive checks: - Removed 'if node not in graph: return True' from depth_first_search - Removed 'if curr_node not in graph: continue' from the BFS loop This allows the functions to raise KeyError when accessing graph[node] for a node that is not a key in the graph, which is the expected behavior for invalid inputs as documented in the FIXME comments. --- graphs/check_bipatrite.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/graphs/check_bipatrite.py b/graphs/check_bipatrite.py index 897c78850d58..34bbedfa59fa 100644 --- a/graphs/check_bipatrite.py +++ b/graphs/check_bipatrite.py @@ -67,8 +67,6 @@ def depth_first_search(node: int, color: int) -> bool: """ if visited[node] == -1: visited[node] = color - if node not in graph: - return True for neighbor in graph[node]: if not depth_first_search(neighbor, 1 - color): return False @@ -140,8 +138,6 @@ def is_bipartite_bfs(graph: dict[int, list[int]]) -> bool: visited[node] = 0 while queue: curr_node = queue.popleft() - if curr_node not in graph: - continue for neighbor in graph[curr_node]: if visited[neighbor] == -1: visited[neighbor] = 1 - visited[curr_node]