📖 Scenario: You are working with a social network where people are connected by friendships. You want to check if the network can be split into two groups so that no one in the same group is friends with each other. This is called checking if the network is a bipartite graph.
🎯 Goal: Build a program that represents the friendship network as a graph and checks if it is bipartite using colors to separate groups.
📋 What You'll Learn
Create a graph using an adjacency list representation with exact nodes and edges
Create a color array to track group assignments
Implement a function to check if the graph is bipartite using BFS
Print the result as 'True' or 'False'
💡 Why This Matters
🌍 Real World
Checking if a network can be divided into two groups without internal connections helps in social network analysis, scheduling problems, and resource allocation.
💼 Career
Understanding bipartite graphs is important for software engineers working on graph algorithms, network design, and optimization problems.
Progress0 / 4 steps
1
Create the graph adjacency list
Create a dictionary called graph representing the graph adjacency list with these exact entries: 0: new List {1, 3}, 1: new List {0, 2}, 2: new List {1, 3}, 3: new List {0, 2}.
DSA C#
Hint
Use a Dictionary<int, List<int>> to represent the graph. Each key is a node, and the value is a list of connected nodes.
2
Create the color array
Create an integer array called color with length 4 and initialize all elements to -1 to represent uncolored nodes.
DSA C#
Hint
Use int[] color = new int[4] and set all values to -1 to mark nodes as uncolored.
3
Implement the bipartite check function
Add a static method called IsBipartite that takes Dictionary<int, List<int>> graph and int[] color as parameters and returns a bool. Use a queue to perform BFS starting from node 0, assign colors 0 and 1 to nodes, and return false if a conflict is found; otherwise, return true. Call IsBipartite(graph, color) inside Main and store the result in a variable called result.
DSA C#
Hint
Use BFS with a queue. Assign color 0 to the start node, then assign opposite colors to neighbors. If a neighbor has the same color as the current node, return false.
4
Print the bipartite check result
Print the value of the variable result using Console.WriteLine(result).
DSA C#
Hint
Use Console.WriteLine(result) to print the boolean result.
Practice
(1/5)
1. What does it mean for a graph to be bipartite?
easy
A. All vertices have the same degree
B. It has an equal number of vertices and edges
C. It contains at least one cycle
D. Its vertices can be divided into two groups with no edges inside the same group
Solution
Step 1: Understand bipartite graph definition
A bipartite graph is one where vertices can be split into two sets with edges only between sets, not inside a set.
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.
Final Answer:
Its vertices can be divided into two groups with no edges inside the same group -> Option D
Quick Check:
Bipartite graph = 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 means no cycles
Assuming all vertices have same degree
2. Which C# data structure is best to represent a graph for bipartite check?
easy
A. List adjacency list
B. Dictionary adjacency list
C. int[,] adjacency matrix
D. HashSet<int> for vertices only
Solution
Step 1: Identify graph representation needs
We need to store edges efficiently and access neighbors quickly for bipartite check.
Step 2: Evaluate options
Dictionary allows flexible vertex keys and fast neighbor lookup, better than fixed-size arrays or sets.
Final Answer:
Dictionary adjacency list -> Option B
Quick Check:
Dictionary adjacency list = best for flexible graphs [OK]
Hint: Use Dictionary for flexible graph edges [OK]
Common Mistakes:
Using HashSet only stores vertices, no edges
Adjacency matrix wastes space for sparse graphs
List lacks vertex key mapping
3. What is the output of this C# code snippet checking bipartite for a graph with edges: 0-1, 1-2, 2-3, 3-0?
var graph = new Dictionary {
{0, new List<int>{1,3}},
{1, new List<int>{0,2}},
{2, new List<int>{1,3}},
{3, new List<int>{2,0}}
};
bool IsBipartite(Dictionary g) {
var color = new Dictionary();
foreach(var node in g.Keys) {
if(!color.ContainsKey(node)) {
var queue = new Queue<int>();
queue.Enqueue(node);
color[node] = 0;
while(queue.Count > 0) {
var u = queue.Dequeue();
foreach(var v in g[u]) {
if(!color.ContainsKey(v)) {
color[v] = 1 - color[u];
queue.Enqueue(v);
} else if(color[v] == color[u]) {
return false;
}
}
}
}
}
return true;
}
Console.WriteLine(IsBipartite(graph));
medium
A. True
B. False
C. Compilation error
D. Runtime exception
Solution
Step 1: Analyze the graph structure
The graph forms a cycle of length 4: 0-1-2-3-0.
Step 2: Check bipartite condition on even cycle
Even cycles are bipartite, but here the cycle is length 4 (even), so it should be bipartite. However, the code returns false if adjacent nodes have same color.
Step 3: Trace coloring
Coloring alternates 0 and 1 around the cycle, no conflict occurs, so function returns true.
Final Answer:
True -> Option A
Quick Check:
Even cycle graph is bipartite = True [OK]
Hint: Even cycles are bipartite; odd cycles are not [OK]
Common Mistakes:
Assuming all cycles are not bipartite
Confusing color assignment logic
Ignoring queue BFS traversal
4. Identify the bug in this bipartite check code snippet:
bool CheckBipartite(Dictionary graph) {
var color = new Dictionary();
foreach(var node in graph.Keys) {
if(!color.ContainsKey(node)) {
color[node] = 0;
var stack = new Stack<int>();
stack.Push(node);
while(stack.Count > 0) {
var u = stack.Pop();
foreach(var v in graph[u]) {
if(!color.ContainsKey(v)) {
color[v] = color[u];
stack.Push(v);
} else if(color[v] == color[u]) {
return false;
}
}
}
}
}
return true;
}
medium
A. Colors assigned to neighbors are same as current node, should be opposite
B. Stack should be replaced with queue for BFS
C. Missing initialization of color dictionary
D. No return statement for true case
Solution
Step 1: Review color assignment logic
Neighbors must have opposite colors to current node for bipartite check.
Step 2: Identify incorrect assignment
Code assigns neighbor color same as current node, causing false negatives.
Final Answer:
Colors assigned to neighbors are same as current node, should be opposite -> Option A
Quick Check:
Neighbor colors must differ [OK]
Hint: Neighbors must have opposite colors, not same [OK]
Common Mistakes:
Assigning same color to neighbors
Confusing stack vs queue (both work but BFS preferred)
Forgetting to initialize colors
5. Given a graph with vertices 0 to 5 and edges: 0-1, 1-2, 2-3, 3-4, 4-5, 5-0, and 1-4, is the graph bipartite? Explain why.
hard
A. Yes, because all cycles are even length
B. No, because it has a self-loop
C. No, because it contains an odd cycle including edges 1-2-3-4-1
D. Yes, because it has no cycles
Solution
Step 1: Identify cycles in the graph
The graph has a large cycle 0-1-2-3-4-5-0 (length 6, even), and with edge 1-4, cycles 1-2-3-4-1 (length 4, even) and 0-1-4-5-0 (length 4, even).
Step 2: Check for odd-length cycles
No odd-length cycles exist. A graph is bipartite if and only if it has no odd-length cycles.
Final Answer:
No, because it contains an odd cycle including edges 1-2-3-4-1 -> Option C
Quick Check:
Odd cycle means not bipartite [OK]
Hint: Check for odd cycles to decide bipartite [OK]
Common Mistakes:
Miscounting cycle lengths
Assuming any cycle means not bipartite
Ignoring extra edges creating odd cycles
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.