Challenge - 5 Problems
Bipartite Graph Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Bipartite Check on a Simple Graph
What is the output of the following code that checks if the given graph is bipartite?
DSA Python
def is_bipartite(graph): color = {} def dfs(node, c): if node in color: return color[node] == c color[node] = c return all(dfs(nei, 1 - c) for nei in graph[node]) return all(dfs(node, 0) for node in graph if node not in color) graph = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]} print(is_bipartite(graph))
Attempts:
2 left
💡 Hint
Think about whether the graph can be colored with two colors without conflicts.
✗ Incorrect
The graph is a square cycle (0-1-2-3-0), which is bipartite. The code colors nodes alternatingly and returns True.
❓ Predict Output
intermediateOutput of Bipartite Check on a Triangle Graph
What is the output of the following code that checks if the given graph is bipartite?
DSA Python
def is_bipartite(graph): color = {} def dfs(node, c): if node in color: return color[node] == c color[node] = c return all(dfs(nei, 1 - c) for nei in graph[node]) return all(dfs(node, 0) for node in graph if node not in color) graph = {0: [1, 2], 1: [0, 2], 2: [0, 1]} print(is_bipartite(graph))
Attempts:
2 left
💡 Hint
A triangle graph cannot be colored with two colors without conflict.
✗ Incorrect
The graph is a triangle (3 nodes all connected), which is not bipartite. The code detects a conflict and returns False.
🔧 Debug
advancedIdentify the Error in Bipartite Check Code
What error will this code raise when checking bipartiteness of the graph?
DSA Python
def is_bipartite(graph): color = {} def dfs(node, c): if node in color: return color[node] == c color[node] = c for nei in graph[node]: if not dfs(nei, 1 - c): return False return True return all(dfs(node, 0) for node in graph if node not in color) graph = {0: [1], 1: [0, 2], 2: [1, 3], 3: [2, 4], 4: [3, 5], 5: [4, 6], 6: [5, 7], 7: [6, 8], 8: [7, 9], 9: [8]} print(is_bipartite(graph))
Attempts:
2 left
💡 Hint
Check if the graph is connected and if the recursion depth is reasonable.
✗ Incorrect
The graph is a simple chain of 10 nodes, which is bipartite. The code runs without error and returns True.
🚀 Application
advancedNumber of Connected Components That Are Bipartite
Given the graph below, how many connected components are bipartite?
DSA Python
graph = {
0: [1],
1: [0],
2: [3, 4],
3: [2, 4],
4: [2, 3],
5: []
}
# Use the bipartite check function from previous examples.Attempts:
2 left
💡 Hint
Check each connected component separately for bipartiteness.
✗ Incorrect
There are three components: {0,1} bipartite, {2,3,4} not bipartite (triangle), {5} single node bipartite. So 2 bipartite components.
🧠 Conceptual
expertWhy Odd Cycles Prevent Bipartiteness?
Why does the presence of an odd-length cycle in a graph mean it cannot be bipartite?
Attempts:
2 left
💡 Hint
Think about coloring nodes alternately along the cycle.
✗ Incorrect
In an odd cycle, alternating colors will cause the first and last node to have the same color but are connected, causing a conflict.
