Everything in one table. vertices, edges.

Traversal and connectivity

AlgorithmTimeSpaceNotes
DFS / BFSBFS gives unweighted shortest paths
Connected componentsrepeated DFS, or DSU
Bipartite check2-colouring
Cycle detectioncolours (directed), parent (undirected)
Topological sortDAGs only
Bridges / articulation pointslow-link DFS
SCC (Tarjan)one DFS
SCC (Kosaraju)two DFS + transpose
2-SATSCC on the implication graph
DSU amortizedpath compression + union by size
Offline dynamic connectivitysegment tree on time + rollback DSU

Shortest paths

AlgorithmTimeNegative weightsNotes
BFSunweighted
0-1 BFSnoweights in , deque
Dial’s algorithmnosmall integer weights
DAG topological DPyesDAG only; also gives longest path
Dijkstra (binary heap)nothe default
Dijkstra (Fibonacci heap)notheory only
Dijkstra (dense, array)nobetter when
Bellman-Fordyesdetects negative cycles
SPFA worst, fast avgyescan be hacked
Floyd-Warshallyesall pairs,
Johnsonyesall pairs, sparse
A*depends on heuristicnoneeds an admissible heuristic
Yen ( shortest, loopless)no
Eppstein ( shortest, loops ok)no

Trees and spanning trees

AlgorithmTimeNotes
Tree diametertwo 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 MSTneeds DSU
Prim MST (heap)
Prim (dense, array)better when
Borůvkaparallelisable
Second-best MSTmax edge on tree paths
Chu-Liu/Edmonds (arborescence) or directed MST
HLD + segment tree per querypath queries
Centroid decomposition buildpath-counting problems
DSU on treesubtree aggregate queries

Flows and matching

AlgorithmTimeNotes
Ford-Fulkersonpseudo-polynomial
Edmonds-KarpBFS augmenting paths
Dinic; unit capswrite 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

ProblemBest exactFeasible 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

  1. Unweighted → BFS
  2. Weights 0-1 BFS
  3. DAG → topological DP (fastest, handles negatives)
  4. Non-negative weights → Dijkstra
  5. Negative weights, or you need cycle detection → Bellman-Ford
  6. All pairs, Floyd-Warshall
  7. All pairs, sparse, large → Johnson

See also: Modelling Patterns · Complexity Cheatsheet