Bird
Raised Fist0
DSA C++programming~20 mins

Bipartite Graph Check in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bipartite Graph Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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;
}
A"Yes"
B"No"
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Think about whether the graph can be colored with two colors without adjacent nodes sharing the same color.
❓ Predict Output
intermediate
2:00remaining
Output 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;
}
ARuntime error
B"Yes"
C"No"
DCompilation error
Attempts:
2 left
💡 Hint
A triangle (3-node cycle) is not bipartite because it has an odd cycle.
🧠 Conceptual
advanced
1:30remaining
Why BFS is used in Bipartite Check?
Why is BFS (Breadth-First Search) commonly used to check if a graph is bipartite?
ABecause BFS always finds the shortest path between nodes.
BBecause BFS can detect cycles faster than DFS.
CBecause BFS uses less memory than DFS.
DBecause BFS colors nodes level by level, ensuring adjacent nodes get alternate colors.
Attempts:
2 left
💡 Hint
Think about how BFS explores nodes in layers.
🔧 Debug
advanced
2:00remaining
Identify 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;
}
Acolor[v] should be assigned 1 - color[u] instead of color[u]
BQueue should be replaced with stack for correct traversal
CThe initial color vector should be initialized with 0 instead of -1
DThe function should return true immediately after coloring the first node
Attempts:
2 left
💡 Hint
Check how colors are assigned to neighbors.
🚀 Application
expert
2:30remaining
Number 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?
A3
B1
C2
D0
Attempts:
2 left
💡 Hint
Check bipartite property for each connected component separately.

Practice

(1/5)
1. What does it mean if a graph is bipartite?
easy
A. It contains at least one cycle of odd length
B. It has an equal number of vertices and edges
C. Its vertices can be divided into two groups with no edges inside the same group
D. All vertices have the same degree

Solution

  1. Step 1: Understand bipartite graph definition

    A bipartite graph means vertices can be split into two sets where no edge connects vertices within the same set.
  2. Step 2: Compare options with definition

    Its vertices can be divided into two groups with no edges inside the same group matches this definition exactly, while others describe unrelated properties.
  3. Final Answer:

    Its vertices can be divided into two groups with no edges inside the same group -> Option C
  4. Quick Check:

    Bipartite = Two groups, no internal edges [OK]
Hint: Bipartite means two groups with no edges inside groups [OK]
Common Mistakes:
  • Confusing bipartite with equal vertices and edges
  • Thinking bipartite graphs must have cycles
  • Assuming all vertices have same degree
2. Which of the following C++ code snippets correctly initializes a graph as an adjacency list for bipartite check?
easy
A. vector graph(n);
B. vector<int> graph(n);
C. int graph[n][n];
D. map graph;

Solution

  1. Step 1: Identify adjacency list structure

    An adjacency list for a graph with n vertices is best represented as vector of vectors: vector graph(n);
  2. Step 2: Check other options

    vector<int> graph(n); is a vector of ints, not suitable for adjacency lists. int graph[n][n]; is a 2D array but less flexible. map graph; is a map of int to int, not adjacency list.
  3. Final Answer:

    vector graph(n); -> Option A
  4. Quick Check:

    Adjacency list = vector of vectors [OK]
Hint: Use vector of vectors for adjacency list in C++ [OK]
Common Mistakes:
  • Using vector<int> instead of vector
  • Using fixed size arrays limiting flexibility
  • Using map which is not adjacency list
3. What is the output of the following code snippet that checks if a graph is bipartite?
int n = 4;
vector graph = {{1,3}, {0,2}, {1,3}, {0,2}};
vector<int> color(n, -1);

function dfs = [&](int node, int c) {
    color[node] = c;
    for (int neigh : graph[node]) {
        if (color[neigh] == -1) {
            if (!dfs(neigh, 1 - c)) return false;
        } else if (color[neigh] == c) {
            return false;
        }
    }
    return true;
};

bool result = true;
for (int i = 0; i < n; i++) {
    if (color[i] == -1) {
        if (!dfs(i, 0)) {
            result = false;
            break;
        }
    }
}
cout << (result ? "YES" : "NO") << endl;
medium
A. Runtime Error
B. NO
C. Compilation Error
D. YES

Solution

  1. Step 1: Analyze graph structure

    The graph edges connect vertices 0-1, 0-3, 1-2, 2-3 forming a square (even cycle), which is bipartite.
  2. Step 2: Trace DFS coloring

    DFS colors nodes alternatingly. No adjacent nodes share the same color, so dfs returns true for all nodes.
  3. Final Answer:

    YES -> Option D
  4. Quick Check:

    Even cycle graph is bipartite = YES [OK]
Hint: Even cycles are bipartite, odd cycles are not [OK]
Common Mistakes:
  • Assuming all cycles are non-bipartite
  • Confusing color assignment logic
  • Ignoring disconnected components
4. Identify the bug in this bipartite check code snippet:
bool dfs(int node, int c, vector& graph, vector<int>& color) {
    color[node] = c;
    for (int neigh : graph[node]) {
        if (color[neigh] == -1) {
            dfs(neigh, 1 - c, graph, color);
        } else if (color[neigh] == c) {
            return false;
        }
    }
    return true;
}
medium
A. Missing return value check for recursive dfs call
B. Wrong color assignment to nodes
C. Using vector<int> instead of vector<bool>
D. Incorrect graph representation

Solution

  1. Step 1: Check recursive call handling

    The recursive dfs call result is not checked. If it returns false, the function should also return false immediately.
  2. Step 2: Explain fix

    Adding 'if (!dfs(...)) return false;' ensures early exit on conflict detection.
  3. Final Answer:

    Missing return value check for recursive dfs call -> Option A
  4. Quick Check:

    Always check recursive call results in DFS [OK]
Hint: Return false immediately if recursive DFS fails [OK]
Common Mistakes:
  • Ignoring recursive call return values
  • Assigning wrong colors
  • Using wrong data structures for graph
5. Given a graph with 5 vertices and edges: (0-1), (1-2), (2-3), (3-4), (4-1), which statement is true about its bipartite property?
hard
A. The graph is bipartite because it has no odd cycles
B. The graph is not bipartite because it contains an odd cycle
C. The graph is bipartite because all vertices have degree 2
D. The graph is not bipartite because it is disconnected

Solution

  1. Step 1: Identify cycles in the graph

    The edges form a cycle: 1-2-3-4-1 which has 4 edges (even length). However, the full cycle including vertices 0-1-4-3-2-1 forms a cycle of length 5 (odd cycle): 0-1-4-3-2-1 (0-1,1-4,4-3,3-2,2-1).
  2. Step 2: Check bipartite condition

    Since the graph contains an odd cycle of length 5, it is not bipartite.
  3. Step 3: Re-examine options

    The graph is bipartite because it has no odd cycles is false. The graph is not bipartite because it contains an odd cycle is true. The graph is bipartite because all vertices have degree 2 is incorrect reasoning. The graph is not bipartite because it is disconnected is false because the graph is connected.
  4. Final Answer:

    The graph is not bipartite because it contains an odd cycle -> Option B
  5. Quick Check:

    Odd cycle means not bipartite [OK]
Hint: Check cycle lengths: odd cycle means not bipartite [OK]
Common Mistakes:
  • Miscounting cycle length
  • Confusing connectivity with bipartite property
  • Assuming degree affects bipartite
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard