Overview
Graph algorithms solve problems involving nodes and edges. Master these core algorithms to tackle most graph problems.When to Use
Shortest Path
Dijkstra, Bellman-Ford, Floyd-Warshall
MST
Kruskal, Prim for minimum spanning tree
Topological Sort
Task ordering, course prerequisites
Cycle Detection
DFS colors or Union Find
Key Algorithms
1. Dijkstra’s Algorithm
2. Bellman-Ford
3. Topological Sort (Kahn’s BFS)
4. Kruskal’s MST
Complexity Comparison
| Algorithm | Time | Space | Use Case |
|---|---|---|---|
| Dijkstra | O((V+E) log V) | O(V) | Non-negative weights |
| Bellman-Ford | O(VE) | O(V) | Negative weights |
| Floyd-Warshall | O(V^3) | O(V^2) | All pairs shortest |
| Kruskal | O(E log E) | O(V) | Sparse graphs MST |
| Prim | O(E log V) | O(V) | Dense graphs MST |