Bipartite Graph Check in DSA Typescript - 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.
function isBipartite(graph: number[][]): boolean {
const n = graph.length;
const colors = new Array(n).fill(-1);
function dfs(node: number, color: number): boolean {
if (colors[node] !== -1) return colors[node] === color;
colors[node] = color;
for (const neighbor of graph[node]) {
if (!dfs(neighbor, 1 - color)) return false;
}
return true;
}
for (let i = 0; i < n; i++) {
if (colors[i] === -1 && !dfs(i, 0)) return false;
}
return true;
}
This code checks if the graph is bipartite by coloring nodes using depth-first search.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The depth-first search (DFS) visits each node and its neighbors once.
- How many times: Each node and edge is checked once during the DFS calls.
As the graph grows, the time to check 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 total nodes plus edges, so bigger graphs take proportionally more time.
Time Complexity: O(n + e)
This means the time grows linearly with the number of nodes and edges in the graph.
[X] Wrong: "The DFS runs in O(n²) because it looks at all pairs of nodes."
[OK] Correct: The DFS only visits neighbors of each node, so it depends on actual edges, not all pairs.
Understanding this time complexity helps you explain how graph traversal scales and shows you can analyze recursive algorithms clearly.
"What if we used a queue and breadth-first search (BFS) instead of DFS? How would the time complexity change?"
Practice
bipartite?Solution
Step 1: Understand bipartite graph definition
A bipartite graph 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.Final Answer:
Its vertices can be divided into two groups with no edges inside the same group -> Option AQuick Check:
Bipartite = 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 for each vertex efficiently to check bipartite property.Step 2: Choose suitable data structure
An adjacency list using Map stores neighbors for each vertex clearly and is efficient.Final Answer:
Adjacency list using Map<number, number[]> -> Option AQuick Check:
Graph as adjacency list = Map [OK]
- Using simple arrays without edge info
- Using strings which don't represent graph
- Using single variable instead of structure
const graph = new Map([
[0, [1, 3]],
[1, [0, 2]],
[2, [1, 3]],
[3, [0, 2]]
]);
function isBipartite(graph: Map): boolean {
const color = new Map();
for (const node of graph.keys()) {
if (!color.has(node)) {
const queue = [node];
color.set(node, 0);
while (queue.length) {
const u = queue.shift()!;
for (const v of graph.get(u)!) {
if (!color.has(v)) {
color.set(v, 1 - color.get(u)!);
queue.push(v);
} else if (color.get(v) === color.get(u)) {
return false;
}
}
}
}
}
return true;
}
console.log(isBipartite(graph));Solution
Step 1: Analyze graph structure
The graph is a square cycle: 0-1-2-3-0, which is an even cycle and bipartite.Step 2: Trace BFS coloring
Starting from node 0 with color 0, neighbors get color 1, their neighbors color 0, no conflicts found.Final Answer:
true -> Option BQuick Check:
Even cycle graph is bipartite = true [OK]
- Assuming all cycles are not bipartite
- Confusing color assignment logic
- Missing BFS queue processing
function isBipartite(graph: Map): boolean {
const color = new Map();
for (const node of graph.keys()) {
if (!color.has(node)) {
const queue = [node];
color.set(node, 0);
while (queue.length) {
const u = queue.shift()!;
for (const v of graph.get(u)!) {
if (!color.has(v)) {
color.set(v, color.get(u)!);
queue.push(v);
} else if (color.get(v) === color.get(u)) {
return false;
}
}
}
}
}
return true;
}Solution
Step 1: Identify color assignment error
The code assigns the same color to neighbors, which breaks bipartite logic.Step 2: Correct color assignment
Neighbors must get opposite color: 1 - color of current node.Final Answer:
color.set(v, 1 - color.get(u)!) -> Option CQuick Check:
Neighbor color = opposite of current [OK]
- Assigning same color to neighbors
- Using pop instead of shift in BFS
- Incorrect graph access
const graph = new Map([ [0, [1, 2]], [1, [0, 3]], [2, [0, 3]], [3, [1, 2, 4]], [4, [3]] ]);Is this graph bipartite? Choose the correct reasoning.
Solution
Step 1: Identify cycles in the graph
There is a triangle cycle: nodes 1-3-2-1 (edges 1-3, 3-2, 2-1), which is an odd-length cycle.Step 2: Recall bipartite property about cycles
Graphs containing odd-length cycles are not bipartite.Final Answer:
No, because it contains a triangle (3-cycle) -> Option DQuick Check:
Odd cycle present = not bipartite [OK]
- Falsely identifying triangle in 1-2-3 (actually 1-3-2 forms triangle)
- Assuming even degree means bipartite
- Confusing connectivity with bipartite property
Ready to prove it?
Pass the test and this pattern moves unknown
