GTOIgraph theory, redesigned

Appendix

Complexity cheat sheet

What runs in time, what to write when two techniques share a row, and which page has the proof and the code.

Traversal & components

TechniqueTimeNotePage
DFS / BFS on an adjacency listO(n + m)O(n + m)Depth-First SearchBreadth-First Search
BFS shortest paths (unweighted)O(n + m)O(n)Breadth-First Search
Bridges / articulation pointsO(n + m)low-link valuesConnectivity, Bridges, Articulation Points
Tarjan / Kosaraju SCCO(n + m)condensation DAGStrongly Connected Components
Topological order (Kahn, DFS)O(n + m)DAG onlyDAGs and Topological Order
Cycle detection in a digraphO(n + m)three-colour DFSCycles: Detection, Extraction, Feedback Sets

Trees

TechniqueTimeNotePage
Diameter (two BFS)O(n)works with weightsTree Diameter in Two Passes
Tree DP, two statesO(n)take / not-takeIndependent Sets, Cliques, and Treewidth
RerootingO(n)answer for every rootDistance, Radius, Eccentricity
Subtree = interval (tin/tout)O(n) pre, O(1) queryEuler tourEntry/Exit Times and the Euler Tour
Binary lifting LCApre O(n log n), q O(log n)O(n log n) memoryLCA by Binary Lifting
Euler tour + sparse table LCApre O(n log n), q O(1)RMQ reductionLCA via Range Minimum Query
Tarjan offline LCAO((n+q) α(n))offline, DSU onlyTarjan's Offline LCA
HLD path queriesO(log² n) per querysegment tree over chainsHeavy-Light Decomposition
Centroid decompositionO(n log n)pairs with a distance boundCentroid Decomposition
Small-to-large mergingO(n log² n)per-subtree setsSmall-to-Large Merging
Virtual tree on k marksO(k log k)closure has ≤ 2k−1 nodesVirtual Trees

Data structures

TechniqueTimeNotePage
Binary heap / priority_queuebuild O(n), ops O(log n)comparator is invertedBinary Heap and priority_queue
DSU (size + compression)α(n) amortisedno compression with rollbackDisjoint Set Union (Union–Find)
Fenwick (BIT)add/sum O(log n)kth by bit liftingFenwick Tree (Binary Indexed Tree)
Segment tree, iterativebuild O(n), query O(log n)2n nodesSegment Tree
Segment tree with lazyO(log n)push before descendingSegment Tree
Sparse tablepre O(n log n), q O(1)idempotent ops onlySparse Table and RMQ
Trie / xor-trieO(total length)max-xor pair in O(B)Trie (Prefix Tree) and Its Relatives

Shortest paths

TechniqueTimeNotePage
Dijkstra + binary heapO((n + m) log n)non-negative weightsDijkstra's Algorithm
0–1 BFS (deque)O(n + m)weights in {0, 1}0-1 BFS, Dial, Potentials, Johnson
Dial / bucket DijkstraO(nC + m)small integer weights0-1 BFS, Dial, Potentials, Johnson
Bellman–FordO(nm)detects negative cyclesBellman–Ford and Negative Weights
Floyd–WarshallO(n³)all pairs, k outermostFloyd–Warshall

Counting & matrices

TechniqueTimeNotePage
Adjacency powersO(n³ log k)walks of length kCounting Walks with Matrix Powers
Matrix–Tree theoremO(n³)spanning trees mod pThe Matrix–Tree Theorem
Linear recurrences (Kitamasa)O(k² log n)k-th term of a DPLinear Recurrences from Graphs and Matrices
Bitmask DP (Held–Karp)O(2ⁿ n²)n ≤ 20–22Held–Karp: Hamilton in O(2ⁿn²)

Flows & matching

TechniqueTimeNotePage
Kruskal / Prim / Borůvka MSTO(m log m)cut propertyMinimum Spanning Tree
Dinic max flowO(n²m), O(m√n) unitmin cut from the residual graphMax Flow: Ford–Fulkerson, Dinic, Push–Relabel
Push–relabelO(n²√m)dense graphsMax Flow: Ford–Fulkerson, Dinic, Push–Relabel
Min-cost max-flowO(F · m log n)potentials keep Dijkstra validMin-Cost Max-Flow
Min-cut models (closure)flowproject selection, bipartite coverMin-Cut Models
Kuhn bipartite matchingO(nm)greedy pre-pass helpsBipartite Matching (Kuhn's Algorithm)
Hopcroft–KarpO(m√n)blocking flow per layerHopcroft–Karp
Kőnig cover / independent setO(m√n)min cover = max matchingKőnig's Theorem and Minimum Covers
Hungarian assignmentO(n³)labels + equality subgraphThe Hungarian Algorithm
Blossom (general matching)O(n³)odd cycles need contractionGeneral Matching and Blossoms

Cycles, colouring, hard problems

TechniqueTimeNotePage
Hierholzer Euler tourO(n + m)emit then reverseHierholzer's Linear Algorithm
de Bruijn sequenceO(kⁿ)Euler on (k−1)-mersDe Bruijn Sequences
Dirac / Ore HamiltonicityO(n²) checksufficient conditionsWhen Hamiltonian Cycles Must Exist
2-SATO(n + m)implication graph SCCs2-SAT
Greedy colouringO(n + m)degeneracy + 1Graph Colouring
Planarity boundsm ≤ 3n − 6Euler's formulaPlanarity and Euler's Formula
Degree sequence (Havel–Hakimi)O(n²)constructs the graphDegree Sequences and Graphical Sequences
Random-walk commute timeO(1) after solve2m · effective resistanceRandom Walks and Cover Times

n = vertices, m = edges, α = inverse Ackermann (≤ 4 for any real input). Bounds are worst-case unless marked expected. "Fine" means roughly 10⁸ simple operations per second on a judge machine — see P, NP, and Reductions — What You Actually Need for the reasoning behind the numbers.