Bipartite Graph Check in DSA C# - Time & Space Complexity
Checking if a graph is bipartite means dividing nodes into two groups without edges inside the same group.
We want to know how the time needed grows as the graph gets bigger.
Analyze the time complexity of the following code snippet.
// Check if graph is bipartite using BFS
bool IsBipartite(List<int>[] graph) {
int n = graph.Length;
int[] colors = new int[n];
Array.Fill(colors, -1);
for (int i = 0; i < n; i++) {
if (colors[i] == -1) {
if (!BFSCheck(graph, i, colors)) return false;
}
}
return true;
}
bool BFSCheck(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;
}
This code checks if a graph can be colored using two colors without neighbors sharing the same color.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Visiting each node and its neighbors once using BFS.
- How many times: Each node and edge is checked at most once during BFS traversals.
As the graph grows, the time grows with the number of nodes and edges.
| Input Size (n nodes, m edges) | Approx. Operations |
|---|---|
| 10 nodes, 15 edges | About 25 checks (nodes + edges) |
| 100 nodes, 200 edges | About 300 checks |
| 1000 nodes, 3000 edges | About 4000 checks |
Pattern observation: The operations grow roughly in a straight line with nodes plus edges combined.
Time Complexity: O(n + m)
This means the time needed grows linearly with the total number of nodes and edges in the graph.
[X] Wrong: "The BFS inside the check runs for every node fully, so time is O(n * m)."
[OK] Correct: Each node and edge is visited only once overall, not repeatedly, so the total work is linear, not multiplied.
Understanding this time complexity helps you explain how graph traversal scales and shows you can analyze common graph algorithms clearly.
"What if we used DFS instead of BFS for the bipartite check? How would the time complexity change?"
Practice
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 DQuick Check:
Bipartite graph = two groups, no internal edges [OK]
- Confusing bipartite with equal vertices and edges
- Thinking bipartite means no cycles
- Assuming all vertices have same degree
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 BQuick Check:
Dictionary adjacency list = best for flexible graphs [OK]
- Using HashSet only stores vertices, no edges
- Adjacency matrix wastes space for sparse graphs
- List lacks vertex key mapping
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));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 AQuick Check:
Even cycle graph is bipartite = True [OK]
- Assuming all cycles are not bipartite
- Confusing color assignment logic
- Ignoring queue BFS traversal
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;
}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 AQuick Check:
Neighbor colors must differ [OK]
- Assigning same color to neighbors
- Confusing stack vs queue (both work but BFS preferred)
- Forgetting to initialize colors
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 CQuick Check:
Odd cycle means not bipartite [OK]
- Miscounting cycle lengths
- Assuming any cycle means not bipartite
- Ignoring extra edges creating odd cycles
Ready to prove it?
Pass the test and this pattern moves unknown
