Skip to content

Commit 16be736

Browse files
fix: Handle missing nodes in bipartite graph checks (DFS and BFS)
1 parent 6c6902d commit 16be736

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

‎graphs/check_bipatrite.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ 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
7072
for neighbor in graph[node]:
7173
if not depth_first_search(neighbor, 1 - color):
7274
return False
@@ -138,6 +140,8 @@ def is_bipartite_bfs(graph: dict[int, list[int]]) -> bool:
138140
visited[node] = 0
139141
while queue:
140142
curr_node = queue.popleft()
143+
if curr_node not in graph:
144+
continue
141145
for neighbor in graph[curr_node]:
142146
if visited[neighbor] == -1:
143147
visited[neighbor] = 1 - visited[curr_node]

0 commit comments

Comments
 (0)