Best known practical bounds for the algorithms in these notes: what each solves, its time and its extra space. = elements / vertices, = edges, = queries, = value range, = word size (64), = inverse Ackermann. Amortized bounds are marked (am.), expected ones (exp.).

What fits in about one second (≈ – simple operations): : · –: · : · : · : · –: , up to · –: · larger: , , .

Searching, sorting, arrays

AlgorithmSolvesTimeSpace
binary search / on answermonotone predicate evaluations
ternary / golden-section searchunimodal function
merge sort / heap sort / introsortsorting / /
counting / radix sortsorting small keys /
quickselect / median of medians-th element exp. /
prefix sums, difference arraysstatic range sums, offline range adds build, query
two pointers / sliding windowmonotone windows
monotonic stack / dequenext greater, window min
Kadanemax subarray
Boyer–Moore votingmajority
inversions (merge sort / Fenwick)count inversions

Data structures

StructureOperationsTimeSpace
hash map (custom hash)insert / find exp.
std::set / map, treap, splayordered set, split / merge (am. / exp.)
order-statistics tree (pbds)-th, rank
binary heap / pairing heappush, pop min / meld / meld am.
DSU (union by size + compression)union, find am.
DSU with rollbackundoable union
sparse tableidempotent range query build, query
Fenwick treeprefix sums, point update
segment tree (lazy)range update / query
persistent segment treeversions, -th in range
segment tree beatsrange chmin + sum am.
Li Chao treeinsert line, min at
wavelet tree / matrix-th, rank in range
sqrt decompositionflexible range ops
Mo’s algorithmoffline range queries
binary triemax xor, xor--th
xor basisspan, max xor subset per insert
van Emde Boas / fast set (64-ary)successor on integers / /
k-d treenearest neighbour, range in 2D range, NN avg
bitsetset ops, subset-sum DP

Dynamic programming

TechniqueSolvesTimeSpace
0/1 knapsackcapacity
bounded knapsack (monotone deque)multiplicities
LIS (patience)longest increasing subsequence
LCS / edit distancealignment ( bit-parallel) with Hirschberg
interval DP (+ Knuth)merging, optimal BST ()
bitmask DP / Held–Karpsubsets, TSP
SOS DPsums over subsets
profile DPtilings on
digit DPcounting numbers
tree DP, rerootingsubtree / all-roots answers
tree knapsacksize-limited subtree DP
convex hull trick / Li Chao /
divide and conquer optimizationmonotone argmin, layers
Aliens (WQS)exactly items, concave cost innerinner
slope trickconvex piecewise-linear DP
SMAWK / LARSCHtotally monotone row minima
matrix exponentiationlinear recurrences of order
dynamic DPtree DP under updates per update

Graphs

AlgorithmSolvesTimeSpace
BFS / DFS / 0-1 BFSreachability, unweighted / 0-1 shortest paths
Dijkstra (binary heap)SSSP,
Dijkstra (array)dense SSSP
Bellman–Ford / SPFAnegative weights, negative cycles
Floyd–WarshallAPSP, transitive closure ( closure)
JohnsonAPSP sparse with negatives
topological sort (Kahn / DFS)DAG order, DAG DP
Tarjan / Kosaraju SCC, 2-SATcondensation, satisfiability
bridges, articulation points, BCClowlink
dominator tree (Lengauer–Tarjan)immediate dominators
Kruskal / Prim / BorůvkaMST / /
directed MST (Edmonds)min arborescence
HierholzerEuler path / circuit
Held–KarpHamiltonian path / TSP
chromatic number (IE)graph colouring
Yen / Eppstein shortest paths /
offline dynamic connectivityedge insert / delete
matrix-tree theoremcount spanning trees

Flows and matching

AlgorithmSolvesTimeSpace
Dinicmax flow; unit networks
push–relabel (HLPP + gap)max flow
Stoer–Wagnerglobal min cut /
Karger–Steinglobal min cut w.h.p.
Gomory–Huall-pairs min cut max flow
SSP with potentialsmin-cost flow
capacity / cost scaling, network simplexmin-cost flow, any costs / / fast in practice
Hungarianassignment
Kuhn / Hopcroft–Karpbipartite matching /
Edmonds’ blossomgeneral matching
weighted blossommax-weight general matching
matroid intersectionmax common independent set oracle calls
Gale–Shapley / Irvingstable marriage / roommates

Trees

AlgorithmSolvesTimeSpace
binary liftingLCA, -th ancestor, path aggregates /
Euler tour + sparse tableLCA /
Schieber–VishkinLCA /
ladder algorithmlevel ancestor /
HLD + segment treepath update / query
centroid decompositionall-paths counting, distance index (+ per query )
DSU on tree / small to largesubtree queries offline
virtual treeDP on marked vertices
link-cut tree, top treedynamic forests am.
Euler tour treelink / cut with subtree sums exp.
AHU / tree hashingtree isomorphism /
Prüfer encode / decodelabelled tree sequence

Number theory and math

AlgorithmSolvesTimeSpace
binary exponentiation
Euclid / extended Euclidgcd, Bézout, inverse
CRT / Garnercombine congruences /
Eratosthenes / linear sieveprimes, spf, multiplicative functions /
segmented sieveprimes in
Miller–Rabin64-bit primality mulmods
Pollard–Brent rho64-bit factorization exp.
harmonic lemma / floor-sum blocks
floor sum (AC library)
Du’s sieve / Min_25 / Lucyprefix sums of , , primes / /
baby-step giant-stepdiscrete log
Tonelli–Shanks / Cipollamodular square root /
Lucas / Granville
Gaussian eliminationsystems, rank, determinant, inverse ( over GF(2))
characteristic polynomial (Hessenberg)
Berlekamp–Masseyminimal recurrence
Kitamasa / Bostan–Mori-th term of order- recurrence /

Polynomials

AlgorithmSolvesTimeSpace
FFT / NTTconvolution
Karatsubamultiplication without roots of unity
arbitrary-mod convolution (3 NTT + CRT / split FFT)convolution mod any
Walsh–Hadamard / subset convolutionxor / and / or, /
online (relaxed) convolution depends on its own prefix
Newton iterationinverse, log, exp, sqrt, power of series
multipoint evaluation / interpolationvalues at points
Taylor shift
composition / compositional inverse (Kinoshita–Li)
half-GCDpolynomial gcd, resultant

Strings

AlgorithmSolvesTimeSpace
prefix function (KMP) / Z-functionmatching, borders, periods
polynomial hashingsubstring equality build, query
Aho–Corasickmany patterns
suffix array (SA-IS / doubling) + Kasaisorted suffixes, LCP /
suffix automatondistinct substrings, occurrences or
suffix tree (Ukkonen)substring structure
Manacherall palindromes
eertreedistinct palindromes online
Duval / BoothLyndon factorization / min rotation
bitset matching (shift-and)wildcard / small alphabet
FFT wildcard matchingpatterns with ?

Geometry

AlgorithmSolvesTimeSpace
Andrew monotone chainconvex hull
rotating calipersdiameter, width, antipodal pairs after hull
point in convex polygoncontainment
half-plane intersectionfeasible region
Minkowski sumconvex polygons
closest pairnearest pair of points
Welzlminimum enclosing circle exp.
Bentley–Ottmannall segment intersections
Delaunay (Bowyer–Watson / divide and conquer)triangulation, Voronoi
3D convex hullhull of points in space exp. ( simple)

Games, search, randomized, numerical

AlgorithmSolvesTimeSpace
Sprague–Grundyimpartial game sums
retrograde analysisgames with cycles
minimax + alpha–betagame trees best case
backtracking with pruningconstraint searchexponential
meet in the middlesubset sums, 4-sum
dancing links (Algorithm X)exact coverexponential, fast in practice
Bron–Kerboschmaximal cliques
Dreyfus–WagnerSteiner tree on terminals
simulated annealing / local searchheuristic optimisationuser-chosen
Freivaldsverify per round
Newton / bisection / Simpsonroots, integrals /
simplexlinear programmingexponential worst, fast in practice