Bipartite Graph Check in DSA Java - Time & Space Complexity
We want to know how long it takes to check if a graph can be split into two groups without edges inside the same group.
How does the time needed grow as the graph gets bigger?
Analyze the time complexity of the following code snippet.
public boolean isBipartite(int[][] graph) {
int n = graph.length;
int[] colors = new int[n];
for (int i = 0; i < n; i++) {
if (colors[i] == 0 && !dfs(graph, colors, i, 1)) {
return false;
}
}
return true;
}
private boolean dfs(int[][] graph, int[] colors, int node, int color) {
if (colors[node] != 0) {
return colors[node] == color;
}
colors[node] = color;
for (int neighbor : graph[node]) {
if (!dfs(graph, colors, neighbor, -color)) {
return false;
}
}
return true;
}
This code uses depth-first search to color nodes and check if the graph is bipartite.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: DFS visits each node and its neighbors once.
- How many times: Each node and edge is checked once during DFS calls.
As the graph grows, the time to check colors grows with the number of nodes and edges.
| Input Size (n nodes, e edges) | Approx. Operations |
|---|---|
| 10 nodes, 15 edges | About 25 checks (nodes + edges) |
| 100 nodes, 200 edges | About 300 checks |
| 1000 nodes, 5000 edges | About 6000 checks |
Pattern observation: The work grows roughly with the sum of nodes and edges.
Time Complexity: O(n + e)
This means the time grows linearly with the number of nodes plus edges in the graph.
[X] Wrong: "The time depends only on the number of nodes, not edges."
[OK] Correct: Checking neighbors means edges matter too, so edges add to the work done.
Understanding this helps you explain how graph traversal scales and why both nodes and edges matter in real problems.
"What if we used a queue and breadth-first search instead of recursion? 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 groups so that no edge connects vertices within the same group.Step 2: Compare options with definition
Only Its vertices can be divided into two sets with no edges inside the same set matches this definition exactly. Other options describe unrelated properties.Final Answer:
Its vertices can be divided into two sets with no edges inside the same set -> Option AQuick Check:
Bipartite = Two sets, 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 adjacency list structure
An adjacency list for a graph is usually a list of lists, where each inner list holds neighbors of a vertex.Step 2: Match code snippets to adjacency list
List<List<Integer>> graph = new ArrayList<>(); uses List of Lists, suitable for adjacency list. int graph = new int[5][5]; is a 2D array (adjacency matrix), not list. Options A and C use Set and Map, which are not standard adjacency lists.Final Answer:
List<List<Integer>> graph = new ArrayList<>(); -> Option AQuick Check:
Adjacency list = List of Lists [OK]
- Using 2D arrays instead of lists
- Using Map or Set incorrectly for adjacency
- Not initializing the list properly
List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < 4; i++) graph.add(new ArrayList<>()); graph.get(0).add(1); graph.get(1).add(0); graph.get(1).add(2); graph.get(2).add(1); graph.get(2).add(3); graph.get(3).add(2); graph.get(3).add(0); graph.get(0).add(3); boolean result = isBipartite(graph); System.out.println(result);Assuming
isBipartite correctly checks bipartite property.Solution
Step 1: Analyze the graph structure
The graph has edges forming a cycle of length 4: 0-1-2-3-0.Step 2: Check bipartite condition for even cycle
Even length cycles (4) are bipartite, so the graph should be bipartite. But the edges connect 0-3 and 3-2, forming a cycle of length 4.Step 3: Re-examine edges carefully
The cycle is 0-1-2-3-0, which is even length, so graph is bipartite. So output should be true.Final Answer:
true -> Option BQuick Check:
Even cycle graph is bipartite = true [OK]
- Thinking all cycles are not bipartite
- Confusing cycle length parity
- Assuming runtime errors without cause
boolean isBipartite(List<List<Integer>> graph) {
int n = graph.size();
int[] colors = new int[n];
Arrays.fill(colors, -1);
for (int i = 0; i < n; i++) {
if (colors[i] == -1) {
if (!dfsCheck(graph, i, colors, 0)) return false;
}
}
return true;
}
boolean dfsCheck(List<List<Integer>> graph, int node, int[] colors, int color) {
colors[node] = color;
for (int neighbor : graph.get(node)) {
if (colors[neighbor] == -1) {
if (!dfsCheck(graph, neighbor, colors, 1 - color)) return false;
} else if (colors[neighbor] == color) {
return false;
}
}
return true;
}Solution
Step 1: Review color initialization
Colors array is initialized with -1, meaning uncolored, which is correct.Step 2: Check DFS color assignment and recursion
DFS assigns current node color, then recursively assigns opposite color (1 - color) to neighbors. If neighbor has same color, returns false.Step 3: Confirm logic correctness
The logic correctly detects bipartite property using DFS and color flipping. No bug found.Final Answer:
DFS color assignment logic is correct, no bug -> Option DQuick Check:
DFS color flip and checks are correct [OK]
- Forgetting to flip color in recursion
- Not initializing colors array
- Returning true too early
Solution
Step 1: Identify cycles in the graph
The graph edges form a 6-node cycle: 0-1-2-3-4-5-0 and an extra edge 1-4.Step 2: Check for odd length cycles
There exists an odd length cycle: 1-4-5-0-1 (length 4 is even, but 1-4-5-0-1 is actually length 4, so check carefully). Actually, the cycle 1-4-5-0-1 is length 4 (even). But consider cycle 1-2-3-4-1: edges 1-2,2-3,3-4,4-1 is length 4 (even). However, the presence of edge 1-4 creates a triangle cycle 1-4-5-0-1? No, 1-4-5-0-1 is length 4. But check for odd cycles: 1-4-5-0-1 is length 4, 0-1-4 is length 3? No edge 0-4. But edge 1-4 exists, 4-5, 5-0, 0-1. So cycle 1-4-5-0-1 is length 4. No odd cycles. But check cycle 1-4-5-0-1 is length 4. So no odd cycles. So graph is bipartite.Step 3: Determine bipartite property
Absence of odd length cycles means the graph is bipartite.Final Answer:
Yes, because it contains only even length cycles -> Option CQuick Check:
No odd cycles = bipartite [OK]
- Falsely assuming extra edge creates odd cycle (e.g., nonexistent 1-5)
- Miscalculating cycle lengths
- Confusing isolated vertices with bipartite condition
Ready to prove it?
Pass the test and this pattern moves unknown
