Bipartite Graph Check in DSA Javascript - Time & Space Complexity
Checking if a graph is bipartite means deciding if we can split 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.
function isBipartite(graph) {
const n = graph.length;
const colors = new Array(n).fill(-1);
for (let i = 0; i < n; i++) {
if (colors[i] === -1) {
const queue = [i];
colors[i] = 0;
while (queue.length) {
const node = queue.shift();
for (const neighbor of graph[node]) {
if (colors[neighbor] === -1) {
colors[neighbor] = 1 - colors[node];
queue.push(neighbor);
} else if (colors[neighbor] === colors[node]) {
return false;
}
}
}
}
}
return true;
}
This code uses a breadth-first search to color nodes and check if the graph can be split into two groups without conflicts.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop with queue processes each node and its neighbors once.
- How many times: Each node is visited once, and each edge is checked once or twice depending on direction.
As the number of nodes and edges grows, the time to check colors grows roughly with the total nodes plus edges.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 nodes, 15 edges | About 25 operations (visiting nodes and edges) |
| 100 nodes, 200 edges | About 300 operations |
| 1000 nodes, 3000 edges | About 4000 operations |
Pattern observation: Operations grow roughly in a straight line with the sum of nodes and edges.
Time Complexity: O(V + E)
This means the time grows linearly with the number of nodes plus the number of edges in the graph.
[X] Wrong: "The time depends only on the number of nodes, not edges."
[OK] Correct: Checking neighbors means looking at edges too, so edges affect the time as much as nodes.
Understanding this time complexity helps you explain how graph traversal works efficiently and shows you can analyze real problems involving networks or connections.
"What if we used depth-first search (DFS) instead of breadth-first search (BFS)? How would the time complexity change?"
Practice
bipartite?Solution
Step 1: Understand bipartite graph definition
A bipartite graph means vertices can be split into two sets where no edges connect vertices within the same set.Step 2: Compare options with definition
Only Its vertices can be divided into two groups with no edges inside the same group matches this definition exactly. Other options 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 = vertices split into two groups with 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: Understand array initialization with fill
To create an array of length n filled with -1, use new Array(n).fill(-1).Step 2: Check each option for correctness
Options A and B call map without a callback function, causing TypeError. const color = Array.fill(n, -1); uses non-existent Array.fill static method. Only C uses correct syntax.Final Answer:
const color = new Array(n).fill(-1); -> Option AQuick Check:
Use new Array(n).fill(value) to initialize array [OK]
- Calling map without callback function
- Wrong syntax for fill method
- Confusing Array.fill with fill method
const graph = [[1,3],[0,2],[1,3],[0,2]];
function isBipartite(graph) {
const n = graph.length;
const color = new Array(n).fill(-1);
for (let i = 0; i < n; i++) {
if (color[i] === -1) {
const queue = [i];
color[i] = 0;
while (queue.length) {
const node = queue.shift();
for (const neighbor of graph[node]) {
if (color[neighbor] === -1) {
color[neighbor] = 1 - color[node];
queue.push(neighbor);
} else if (color[neighbor] === color[node]) {
return false;
}
}
}
}
}
return true;
}
console.log(isBipartite(graph));Solution
Step 1: Analyze graph structure and BFS coloring
The graph is a square cycle: 0-1-2-3-0. The BFS colors nodes alternately. Since it's an even cycle, it is bipartite.Step 2: Check BFS color assignment and conflict detection
No two adjacent nodes have the same color, so the function returns true.Final Answer:
true -> Option CQuick Check:
Even cycle graph is bipartite = true [OK]
- Assuming all cycles are not bipartite
- Confusing color assignment logic
- Forgetting to check all connected components
function checkBipartite(graph) {
const n = graph.length;
const color = new Array(n).fill(-1);
for (let i = 0; i < n; i++) {
if (color[i] === -1) {
const stack = [i];
color[i] = 0;
while (stack.length) {
const node = stack.pop();
for (const neighbor of graph[node]) {
if (color[neighbor] === -1) {
color[neighbor] = color[node];
stack.push(neighbor);
} else if (color[neighbor] === color[node]) {
return false;
}
}
}
}
}
return true;
}Solution
Step 1: Review color assignment logic
The code assigns color[neighbor] = color[node], which means neighbors get the same color, violating bipartite rules.Step 2: Correct color assignment for bipartite check
Neighbors must have opposite colors, so color[neighbor] = 1 - color[node] is correct.Final Answer:
color[neighbor] should be set to 1 - color[node] instead of color[node] -> Option BQuick Check:
Neighbors must have opposite colors [OK]
- Assigning same color to neighbors
- Confusing stack and queue usage
- Wrong initial color array values
const graph = [[1,2],[0,3],[0,3],[1,2,4],[3]];
Which of the following statements is true about this graph's bipartite property?
Solution
Step 1: Identify cycles in the graph
The graph edges are 0->1,2; 1->0,3; 2->0,3; 3->1,2,4; 4->3. Cycles like 0-1-3-2-0 are even length (4). No odd-length cycles.Step 2: Understand bipartite condition with odd cycles
Graphs without odd cycles are bipartite.Final Answer:
The graph is bipartite because it contains no odd cycles -> Option AQuick Check:
No odd cycles means bipartite [OK]
- Misidentifying paths like 1-3-4 as odd cycles
- Assuming single connections make it non-bipartite
- Confusing node degree with bipartite property
Ready to prove it?
Pass the test and this pattern moves unknown
