From 6c6902dc25b8b1efdf0f63803d5af8ac5b0d0172 Mon Sep 17 00:00:00 2001 From: tanishqraikwar54-blip Date: Sun, 6 Sep 2026 22:39:16 +0530 Subject: [PATCH 1/2] fix: Remove defensive checks in bipartite graph functions --- 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] From 16be736f0b58c940c575d087a8f47f35bae6727c Mon Sep 17 00:00:00 2001 From: tanishqraikwar54-blip Date: Thu, 10 Sep 2026 23:48:05 +0530 Subject: [PATCH 2/2] fix: Handle missing nodes in bipartite graph checks (DFS and BFS) --- graphs/check_bipatrite.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/graphs/check_bipatrite.py b/graphs/check_bipatrite.py index 34bbedfa59fa..897c78850d58 100644 --- a/graphs/check_bipatrite.py +++ b/graphs/check_bipatrite.py @@ -67,6 +67,8 @@ 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 @@ -138,6 +140,8 @@ 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]