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 C# code that checks if the given graph is bipartite?
DSA C#
using System; using System.Collections.Generic; class Program { static bool IsBipartite(List<int>[] graph) { int n = graph.Length; int[] colors = new int[n]; for (int i = 0; i < n; i++) colors[i] = -1; for (int i = 0; i < n; i++) { if (colors[i] == -1) { Queue<int> queue = new Queue<int>(); queue.Enqueue(i); colors[i] = 0; while (queue.Count > 0) { int node = queue.Dequeue(); foreach (int neighbor in graph[node]) { if (colors[neighbor] == -1) { colors[neighbor] = 1 - colors[node]; queue.Enqueue(neighbor); } else if (colors[neighbor] == colors[node]) { return false; } } } } } return true; } static void Main() { List<int>[] graph = new List<int>[4]; for (int i = 0; i < 4; i++) graph[i] = new List<int>(); graph[0].Add(1); graph[1].Add(0); graph[1].Add(2); graph[2].Add(1); graph[2].Add(3); graph[3].Add(2); Console.WriteLine(IsBipartite(graph)); } }
Attempts:
2 left
💡 Hint
Think about whether the graph can be colored using two colors without adjacent nodes sharing the same color.
✗ Incorrect
The graph is a simple chain 0-1-2-3 which can be colored alternately with two colors, so it is bipartite.
❓ Predict Output
intermediateOutput of Bipartite Check on a Triangle Graph
What is the output of the following C# code that checks if the given graph is bipartite?
DSA C#
using System; using System.Collections.Generic; class Program { static bool IsBipartite(List<int>[] graph) { int n = graph.Length; int[] colors = new int[n]; for (int i = 0; i < n; i++) colors[i] = -1; for (int i = 0; i < n; i++) { if (colors[i] == -1) { Queue<int> queue = new Queue<int>(); queue.Enqueue(i); colors[i] = 0; while (queue.Count > 0) { int node = queue.Dequeue(); foreach (int neighbor in graph[node]) { if (colors[neighbor] == -1) { colors[neighbor] = 1 - colors[node]; queue.Enqueue(neighbor); } else if (colors[neighbor] == colors[node]) { return false; } } } } } return true; } static void Main() { List<int>[] graph = new List<int>[3]; for (int i = 0; i < 3; i++) graph[i] = new List<int>(); graph[0].Add(1); graph[1].Add(0); graph[1].Add(2); graph[2].Add(1); graph[2].Add(0); graph[0].Add(2); Console.WriteLine(IsBipartite(graph)); } }
Attempts:
2 left
💡 Hint
A triangle graph has an odd cycle. Think if it can be bipartite.
✗ Incorrect
A triangle (3-node cycle) cannot be bipartite because it has an odd cycle, so the function returns false.
🔧 Debug
advancedIdentify the Bug in Bipartite Check Code
The following code is intended to check if a graph is bipartite. It sometimes returns incorrect results. What is the bug?
DSA C#
using System; using System.Collections.Generic; class Program { static bool IsBipartite(List<int>[] graph) { int n = graph.Length; int[] colors = new int[n]; for (int i = 0; i < n; i++) colors[i] = 0; for (int i = 0; i < n; i++) { if (colors[i] == 0) { Queue<int> queue = new Queue<int>(); queue.Enqueue(i); colors[i] = 1; while (queue.Count > 0) { int node = queue.Dequeue(); foreach (int neighbor in graph[node]) { if (colors[neighbor] == 0) { colors[neighbor] = 3 - colors[node]; queue.Enqueue(neighbor); } else if (colors[neighbor] == colors[node]) { return false; } } } } } return true; } }
Attempts:
2 left
💡 Hint
Check how the colors array is initialized and used in the code.
✗ Incorrect
The code uses 0 to mean uncolored, but initializes colors with 0, so it treats all nodes as colored initially, skipping BFS.
🧠 Conceptual
advancedProperties of Bipartite Graphs
Which of the following statements is TRUE about bipartite graphs?
Attempts:
2 left
💡 Hint
Think about the relationship between cycles and bipartite property.
✗ Incorrect
A graph is bipartite if and only if it has no odd-length cycles. Even cycles are allowed.
🚀 Application
expertDetermine the Number of Connected Components That Are Bipartite
Given the following graph represented as adjacency lists, how many connected components are bipartite?
DSA C#
using System; using System.Collections.Generic; class Program { static bool IsBipartiteComponent(List<int>[] graph, int start, int[] colors) { Queue<int> queue = new Queue<int>(); queue.Enqueue(start); colors[start] = 0; while (queue.Count > 0) { int node = queue.Dequeue(); foreach (int neighbor in graph[node]) { if (colors[neighbor] == -1) { colors[neighbor] = 1 - colors[node]; queue.Enqueue(neighbor); } else if (colors[neighbor] == colors[node]) { return false; } } } return true; } static void Main() { int n = 7; List<int>[] graph = new List<int>[n]; for (int i = 0; i < n; i++) graph[i] = new List<int>(); // Component 1: 0-1-2 (chain) graph[0].Add(1); graph[1].Add(0); graph[1].Add(2); graph[2].Add(1); // Component 2: 3-4-5-3 (triangle with extra node) graph[3].Add(4); graph[4].Add(3); graph[4].Add(5); graph[5].Add(4); graph[5].Add(3); graph[3].Add(5); // Component 3: 6 (isolated node) int[] colors = new int[n]; for (int i = 0; i < n; i++) colors[i] = -1; int bipartiteCount = 0; for (int i = 0; i < n; i++) { if (colors[i] == -1) { if (IsBipartiteComponent(graph, i, colors)) { bipartiteCount++; } } } Console.WriteLine(bipartiteCount); } }
Attempts:
2 left
💡 Hint
Check each connected component for bipartite property separately.
✗ Incorrect
Component 1 is bipartite (chain), component 2 contains a triangle (odd cycle) so not bipartite, component 3 is a single node (bipartite). So total 2 bipartite components.
