Bipartite Graph Check
Determine if graph is 2-colorable. Use BFS/DFS with alternating colors. No odd cycles means bipartite.
Visualization
Interactive visualization for Bipartite Graph Check
Bipartite Graph Check
• Time: O(V + E)
• Uses BFS with 2-coloring
• Bipartite ⟺ No odd cycles
Interactive visualization with step-by-step execution
Implementation
1function isBipartite(graph: Map<number, number[]>, V: number): boolean {
2 const color = new Array(V).fill(-1);
3
4 for (let start = 0; start < V; start++) {
5 if (color[start] !== -1) continue;
6
7 const queue = [start];
8 color[start] = 0;
9
10 while (queue.length > 0) {
11 const u = queue.shift()!;
12
13 for (const v of graph.get(u) || []) {
14 if (color[v] === -1) {
15 color[v] = 1 - color[u];
16 queue.push(v);
17 } else if (color[v] === color[u]) {
18 return false;
19 }
20 }
21 }
22 }
23 return true;
24}Deep Dive
Theoretical Foundation
Graph is bipartite iff no odd-length cycles. Color vertices alternately. If neighbor has same color: not bipartite. Applications: matching, scheduling.
Complexity
Time
O(V+E)
O(V+E)
O(V+E)
Space
O(V)
Applications
Industry Use
Job assignment with constraints
Social network separation (two groups)
Scheduling problems without conflicts
Graph 2-coloring in compilers (register allocation variants)
Checking if a graph is bipartite before matching
Detecting odd-length dependency cycles
Use Cases
Related Algorithms
Depth-First Search (DFS)
Graph traversal exploring as deep as possible before backtracking. DFS is a fundamental algorithm that uses a stack (either implicitly through recursion or explicitly) to explore graph vertices. It's essential for cycle detection, topological sorting, and pathfinding problems.
Breadth-First Search (BFS)
Level-by-level graph traversal guaranteeing shortest paths in unweighted graphs. BFS uses a queue to explore vertices level by level, making it optimal for finding shortest paths and solving problems that require exploring nearest neighbors first.
Dijkstra's Algorithm
Finds shortest path from source to all vertices in weighted graph with non-negative edges. Uses greedy approach with priority queue.
Floyd-Warshall Algorithm
Floyd-Warshall is an all-pairs shortest path algorithm that finds shortest distances between every pair of vertices in a weighted graph. Unlike Dijkstra (single-source), it computes shortest paths from all vertices to all other vertices simultaneously. The algorithm can handle negative edge weights but not negative cycles. Developed independently by Robert Floyd, Bernard Roy, and Stephen Warshall in the early 1960s.