Everything in one table. vertices, edges.
Traversal and connectivity
| Algorithm | Time | Space | Notes |
|---|---|---|---|
| DFS / BFS | BFS gives unweighted shortest paths | ||
| Connected components | repeated DFS, or DSU | ||
| Bipartite check | 2-colouring | ||
| Cycle detection | colours (directed), parent (undirected) | ||
| Topological sort | DAGs only | ||
| Bridges / articulation points | low-link DFS | ||
| SCC (Tarjan) | one DFS | ||
| SCC (Kosaraju) | two DFS + transpose | ||
| 2-SAT | SCC on the implication graph | ||
| DSU | amortized | path compression + union by size | |
| Offline dynamic connectivity | segment tree on time + rollback DSU |
Shortest paths
| Algorithm | Time | Negative weights | Notes |
|---|---|---|---|
| BFS | — | unweighted | |
| 0-1 BFS | no | weights in , deque | |
| Dial’s algorithm | no | small integer weights | |
| DAG topological DP | yes | DAG only; also gives longest path | |
| Dijkstra (binary heap) | no | the default | |
| Dijkstra (Fibonacci heap) | no | theory only | |
| Dijkstra (dense, array) | no | better when | |
| Bellman-Ford | yes | detects negative cycles | |
| SPFA | worst, fast avg | yes | can be hacked |
| Floyd-Warshall | yes | all pairs, | |
| Johnson | yes | all pairs, sparse | |
| A* | depends on heuristic | no | needs an admissible heuristic |
| Yen ( shortest, loopless) | no | ||
| Eppstein ( shortest, loops ok) | no |
Trees and spanning trees
| Algorithm | Time | Notes |
|---|---|---|
| Tree diameter | two BFS, or one DFS DP | |
| LCA (binary lifting) | / | also gives -th ancestor |
| LCA (Euler + sparse table) | / | best constant |
| LCA (Tarjan offline) | queries known in advance | |
| Kruskal MST | needs DSU | |
| Prim MST (heap) | ||
| Prim (dense, array) | better when | |
| Borůvka | parallelisable | |
| Second-best MST | max edge on tree paths | |
| Chu-Liu/Edmonds (arborescence) | or | directed MST |
| HLD + segment tree | per query | path queries |
| Centroid decomposition | build | path-counting problems |
| DSU on tree | subtree aggregate queries |
Flows and matching
| Algorithm | Time | Notes |
|---|---|---|
| Ford-Fulkerson | pseudo-polynomial | |
| Edmonds-Karp | BFS augmenting paths | |
| Dinic | ; unit caps | write this |
| Push-relabel (highest label) | best for dense graphs | |
| Kuhn (bipartite matching) | short and usually enough | |
| Hopcroft-Karp | ||
| Hungarian (assignment) | min-cost perfect matching, bipartite | |
| Blossom (general matching) | non-bipartite | |
| MCMF (SSP + potentials) | ||
| Stoer-Wagner (global min cut) | deterministic |
Hard problems
| Problem | Best exact | Feasible size |
|---|---|---|
| TSP (Held-Karp) | - | |
| Hamiltonian path | - | |
| Max clique (Bron-Kerbosch) | with bitsets | |
| Chromatic number | ||
| Steiner tree (Dreyfus-Wagner) | terminals | |
| Vertex cover | (FPT) |
Choosing a shortest-path algorithm
- Unweighted → BFS
- Weights → 0-1 BFS
- DAG → topological DP (fastest, handles negatives)
- Non-negative weights → Dijkstra
- Negative weights, or you need cycle detection → Bellman-Ford
- All pairs, → Floyd-Warshall
- All pairs, sparse, large → Johnson
See also: Modelling Patterns · Complexity Cheatsheet