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 Simple Graph
What is the output of the following C++ code that checks if the graph is bipartite?
DSA C++
#include <iostream> #include <vector> #include <queue> using namespace std; bool isBipartite(int V, vector<vector<int>>& adj) { vector<int> color(V, -1); for (int i = 0; i < V; i++) { if (color[i] == -1) { queue<int> q; q.push(i); color[i] = 0; while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (color[v] == -1) { color[v] = 1 - color[u]; q.push(v); } else if (color[v] == color[u]) { return false; } } } } } return true; } int main() { int V = 4; vector<vector<int>> adj = { {1, 3}, {0, 2}, {1, 3}, {0, 2} }; cout << (isBipartite(V, adj) ? "Yes" : "No") << endl; return 0; }
Attempts:
2 left
💡 Hint
Think about whether the graph can be colored with two colors without adjacent nodes sharing the same color.
✗ Incorrect
The graph is a square cycle (4 nodes connected in a cycle). Such a cycle with even length is bipartite. The code colors nodes alternately and finds no conflict, so returns true, printing "Yes".
❓ Predict Output
intermediateOutput of Bipartite Check on Odd Cycle Graph
What is the output of this C++ code checking bipartite property on a triangle graph?
DSA C++
#include <iostream> #include <vector> #include <queue> using namespace std; bool isBipartite(int V, vector<vector<int>>& adj) { vector<int> color(V, -1); for (int i = 0; i < V; i++) { if (color[i] == -1) { queue<int> q; q.push(i); color[i] = 0; while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (color[v] == -1) { color[v] = 1 - color[u]; q.push(v); } else if (color[v] == color[u]) { return false; } } } } } return true; } int main() { int V = 3; vector<vector<int>> adj = { {1, 2}, {0, 2}, {0, 1} }; cout << (isBipartite(V, adj) ? "Yes" : "No") << endl; return 0; }
Attempts:
2 left
💡 Hint
A triangle (3-node cycle) is not bipartite because it has an odd cycle.
✗ Incorrect
The graph is a triangle (3 nodes all connected). Odd cycles cannot be bipartite. The code detects a conflict in coloring and returns false, printing "No".
🧠 Conceptual
advancedWhy BFS is used in Bipartite Check?
Why is BFS (Breadth-First Search) commonly used to check if a graph is bipartite?
Attempts:
2 left
💡 Hint
Think about how BFS explores nodes in layers.
✗ Incorrect
BFS explores nodes in layers or levels. Assigning colors alternately per level ensures adjacent nodes have different colors, which is key to bipartite checking.
🔧 Debug
advancedIdentify the Bug in Bipartite Check Code
What is the bug in this bipartite check code snippet?
DSA C++
bool isBipartite(int V, vector<vector<int>>& adj) { vector<int> color(V, -1); for (int i = 0; i < V; i++) { if (color[i] == -1) { queue<int> q; q.push(i); color[i] = 0; while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (color[v] == -1) { color[v] = color[u]; q.push(v); } else if (color[v] == color[u]) { return false; } } } } } return true; }
Attempts:
2 left
💡 Hint
Check how colors are assigned to neighbors.
✗ Incorrect
Assigning color[v] = color[u] means neighbors get the same color, which breaks bipartite property. It should be color[v] = 1 - color[u] to alternate colors.
🚀 Application
expertNumber of Connected Components in Bipartite Graph
Given a graph with 6 nodes and edges: (0-1), (1-2), (3-4), (4-5), (5-3), how many connected components are bipartite?
Attempts:
2 left
💡 Hint
Check bipartite property for each connected component separately.
✗ Incorrect
There are two connected components: one with nodes {0,1,2} which is a chain (bipartite), and one with nodes {3,4,5} forming a triangle (not bipartite). So 1 component is bipartite, 1 is not. Total bipartite components = 1.
