Challenge - 5 Problems
Bipartite Graph Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Bipartite Check on Simple Graph
What is the output of the following C code that checks if a graph is bipartite?
DSA C
#include <stdio.h> #include <stdbool.h> #define V 4 int graph[V][V] = { {0, 1, 0, 1}, {1, 0, 1, 0}, {0, 1, 0, 1}, {1, 0, 1, 0} }; bool isBipartiteUtil(int v, int colorArr[], int c) { colorArr[v] = c; for (int i = 0; i < V; i++) { if (graph[v][i]) { if (colorArr[i] == -1) { if (!isBipartiteUtil(i, colorArr, 1 - c)) return false; } else if (colorArr[i] == c) { return false; } } } return true; } bool isBipartite() { int colorArr[V]; for (int i = 0; i < V; i++) colorArr[i] = -1; for (int i = 0; i < V; i++) { if (colorArr[i] == -1) { if (!isBipartiteUtil(i, colorArr, 0)) return false; } } return true; } int main() { if (isBipartite()) printf("Graph is Bipartite\n"); else printf("Graph is NOT Bipartite\n"); return 0; }
Attempts:
2 left
💡 Hint
Check if the graph can be colored using two colors without adjacent nodes sharing the same color.
✗ Incorrect
The given graph is a square cycle (4 nodes connected in a cycle). Such even-length cycles are bipartite. The code colors nodes alternately and finds no conflict, so it prints 'Graph is Bipartite'.
❓ Predict Output
intermediate2:00remaining
Output of Bipartite Check on Odd Cycle Graph
What is the output of this C code checking bipartiteness on a triangle graph?
DSA C
#include <stdio.h> #include <stdbool.h> #define V 3 int graph[V][V] = { {0, 1, 1}, {1, 0, 1}, {1, 1, 0} }; bool isBipartiteUtil(int v, int colorArr[], int c) { colorArr[v] = c; for (int i = 0; i < V; i++) { if (graph[v][i]) { if (colorArr[i] == -1) { if (!isBipartiteUtil(i, colorArr, 1 - c)) return false; } else if (colorArr[i] == c) { return false; } } } return true; } bool isBipartite() { int colorArr[V]; for (int i = 0; i < V; i++) colorArr[i] = -1; for (int i = 0; i < V; i++) { if (colorArr[i] == -1) { if (!isBipartiteUtil(i, colorArr, 0)) return false; } } return true; } int main() { if (isBipartite()) printf("Graph is Bipartite\n"); else printf("Graph is NOT Bipartite\n"); return 0; }
Attempts:
2 left
💡 Hint
Odd cycles cannot be bipartite.
✗ Incorrect
The graph is a triangle (3 nodes all connected). Odd cycles are not bipartite because you cannot color nodes with two colors without adjacent nodes sharing the same color.
🔧 Debug
advanced2:30remaining
Identify the bug causing incorrect bipartite detection
This code is intended to check if a graph is bipartite. What is the bug causing it to always return true, even for non-bipartite graphs?
DSA C
#include <stdio.h> #include <stdbool.h> #define V 3 int graph[V][V] = { {0, 1, 1}, {1, 0, 1}, {1, 1, 0} }; bool isBipartiteUtil(int v, int colorArr[], int c) { colorArr[v] = c; for (int i = 0; i < V; i++) { if (graph[v][i]) { if (colorArr[i] == -1) { if (!isBipartiteUtil(i, colorArr, 1 - c)) return false; } else if (colorArr[i] == c) { return false; } } } return true; } bool isBipartite() { int colorArr[V]; for (int i = 0; i < V; i++) colorArr[i] = -1; for (int i = 0; i < V; i++) { if (colorArr[i] == -1) { if (!isBipartiteUtil(i, colorArr, 0)) return false; } } return true; } int main() { if (isBipartite()) printf("Graph is Bipartite\n"); else printf("Graph is NOT Bipartite\n"); return 0; }
Attempts:
2 left
💡 Hint
Check if the recursive call's return value is used properly.
✗ Incorrect
The recursive call to isBipartiteUtil does not return its result. So even if a false is found deeper, it is ignored and the function returns true.
🧠 Conceptual
advanced1:30remaining
Which graph property guarantees bipartiteness?
Which of the following properties guarantees that a graph is bipartite?
Attempts:
2 left
💡 Hint
Think about cycles and coloring.
✗ Incorrect
A graph is bipartite if and only if it contains no odd-length cycles. Other properties do not guarantee bipartiteness.
🚀 Application
expert2:30remaining
Result of Bipartite Check on Disconnected Graph
Consider a graph with two disconnected components: one is a triangle (3 nodes all connected), the other is a line of two nodes connected. What will the bipartite check output be?
Attempts:
2 left
💡 Hint
Check bipartiteness on all components.
✗ Incorrect
The graph is disconnected but bipartite check runs on all components. The triangle component is not bipartite, so overall graph is not bipartite.