πΊοΈ Graphs
One-line summary: Nodes connected by edges β BFS for shortest path, DFS for connectivity/cycle, topological sort for dependencies, Union-Find for dynamic connectivity.
Enhanced Visualizationβ
Interactive demonstration of BFS, DFS, and Dijkstra's algorithm with path finding and performance analysis
Core Graph Conceptsβ
The enhanced animation above demonstrates three fundamental graph algorithms:
π΅ Breadth-First Search (BFS)β
- Strategy: Explores nodes level by level using a queue
- Path: A β B β C β F (shortest in unweighted graphs)
- Time: O(V + E) | Space: O(V)
- Use Cases: Shortest path, social networks, web crawling
π£ Depth-First Search (DFS)β
- Strategy: Explores as far as possible using recursion/stack
- Path: A β D β E β F (explores deeply first)
- Time: O(V + E) | Space: O(V)
- Use Cases: Topological sorting, cycle detection, connectivity
π‘ Dijkstra's Algorithmβ
- Strategy: Uses priority queue for weighted shortest paths
- Path: A β B β E β F (Distance: 7 - optimal weighted path)
- Time: O((V + E) log V) | Space: O(V)
- Use Cases: GPS navigation, network routing, flight planning
Graph Fundamentalsβ
G = (V, E). Types: directed, undirected, weighted, DAG.
Representations:
- Adjacency List:
Map<node, neighbors[]>β O(V+E) space β preferred. - Adjacency Matrix:
matrix[u][v]β O(VΒ²) β dense graphs.
Also covers: BFS pattern, DFS pattern, topological sort, Union-Find, Dijkstra, Bellman-Ford.
Time & Space Complexityβ
| Algorithm | Time | Space |
|---|---|---|
| BFS / DFS | O(V + E) | O(V) |
| Dijkstra (min-heap) | O((V+E) log V) | O(V) |
| Topological Sort (Kahn) | O(V + E) | O(V) |
| Union-Find (with compression) | O(Ξ±(n)) per op | O(n) |
Common Patternsβ
BFS (Shortest Path)β
function bfs(graph, start) {
const visited = new Set([start]),
queue = [start];
while (queue.length) {
const node = queue.shift();
for (const nb of graph.get(node) || [])
if (!visited.has(nb)) {
visited.add(nb);
queue.push(nb);
}
}
}
DFS (Connected Components)β
function dfs(graph, node, visited = new Set()) {
visited.add(node);
for (const nb of graph.get(node) || []) if (!visited.has(nb)) dfs(graph, nb, visited);
}
Topological Sort (Kahn's BFS)β
function topoSort(n, edges) {
const inDeg = new Array(n).fill(0),
adj = Array.from({ length: n }, () => []);
for (const [a, b] of edges) {
adj[b].push(a);
inDeg[a]++;
}
const queue = [],
order = [];
for (let i = 0; i < n; i++) if (!inDeg[i]) queue.push(i);
while (queue.length) {
const node = queue.shift();
order.push(node);
for (const nb of adj[node]) if (--inDeg[nb] === 0) queue.push(nb);
}
return order.length === n ? order : []; // empty = cycle
}
Union-Findβ
const parent = Array.from({ length: n }, (_, i) => i);
function find(x) {
return parent[x] === x ? x : (parent[x] = find(parent[x]));
}
function union(x, y) {
parent[find(x)] = find(y);
}
Pitfallsβ
- Forgetting to mark visited before enqueuing (BFS) β causes duplicates
- Cycle detection: in undirected graph pass parent to DFS to avoid false positives
- Topological sort only valid on DAG β if cycle exists, output length < n
Practice Problemsβ
Related Topicsβ
- Trees β trees are acyclic connected graphs
- Heap β Dijkstra requires min-heap
- Dynamic Programming β DP on DAGs
β Back to Home Β· Β© sparshjaswal