The hard part of most graph problems is building the right graph: what is a vertex (often a state), what is an edge (a move), which weights. This note collects representations, standard modelling tricks, reductions to known problems, the basic counting facts with proofs, and a selection table for algorithms in this chapter.

What makes a model correct. A graph model (or a reduction to a known graph problem) is correct exactly when there is a structure-preserving correspondence between the objects the original problem asks about and the objects the graph problem answers: every valid solution of the original problem maps to a walk/path/cut/matching etc. of the same cost in the graph, and conversely every such structure in the graph maps back to a valid solution of the same cost. Both directions matter — a construction that only shows “every original solution gives a graph solution” can still be wrong if some graph solution corresponds to nothing valid in the original problem (a classic bug: forgetting a constraint when building the graph lets the algorithm find a cheaper-looking but infeasible answer). Every row in the modelling and reductions tables below is this same correspondence argument specialised to one shape; when building a new model, state both directions explicitly rather than checking only that the construction “looks right” on the sample input.

Representations

RepresentationMemoryEdge lookupIterate neighboursGood for
adjacency list vector<vector<int>>default: DFS, BFS, Dijkstra, flows
CSR (offsets + flat array)2–3× faster traversals on + edges, no pointer chase
adjacency matrixFloyd–Warshall, dense Prim/Dijkstra ,
bitset<N> rows bytesneighbourhood intersections: triangles, cliques, transitive closure in
edge listKruskal, Bellman–Ford, offline processing
implicit (neighbour function)—grids, puzzles, number states — the vertices are states, the edges are moves, and the adjacency list is never built; recognise it whenever a problem asks for the minimum number of (reversible, cheaply-enumerable) operations to transform one state into another
// CSR from an edge list (directed u -> v)
vector<int> start(n + 1), to(m);
for (auto [u, v] : edges) start[u + 1]++;
for (int i = 0; i < n; i++) start[i + 1] += start[i];
vector<int> pos(start.begin(), start.end() - 1);
for (auto [u, v] : edges) to[pos[u]++] = v;               // neighbours of u: to[start[u] .. start[u+1])

Modelling tricks

SituationConstruction
movement depends on carried state (keys, fuel, parity, time mod , “used the coupon”)vertices : layered / product graph
vertex capacities, vertex costs, vertex-disjoint pathssplit into
“minimum number of edge reversals”forward edge weight 0, reversed edge weight 1, 0-1 BFS
teleports between all cells of a colour / all multiples / all vertices in a rangeone virtual vertex per group (edges in 0 and out with the cost), or a segment tree of virtual vertices (Shortest Path Techniques)
dense graph whose complement is sparseBFS on the complement with an unvisited list (BFS and DFS)
constraints difference constraints, shortest paths
two choices per item, pairwise conflicts2-SAT (2-SAT)
“after time the edge disappears”process edges in time order, reverse time, or segment tree over time
huge number line with few interesting pointscompress to the interesting points plus edges between consecutive ones
cost depends on the previous edge (turn penalties)vertices = directed edges (line graph)
path must visit a small set of special vertices in some ordershortest paths between specials + bitmask DP
who must come before whomtopological order on a DAG (Topological Sort)
cost grows with how much of an edge’s capacity you use, convexlysubdivide the edge into several parallel edges of increasing cost; min-cost flow picks the cheapest first (Min-Cost Flow)

Common carried states for the layered-graph row above: keys collected ; fuel remaining ; moves used mod ; one-time teleport used ; current speed or direction . The graph grows by the number of states; the algorithm is unchanged.

Reductions worth knowing

ProblemBecomes
min path cover of a DAG (vertex-disjoint) − max bipartite matching (out-copies × in-copies)
min path cover allowing shared verticestransitive closure, then the above
max antichain of a posetDilworth: min chain cover (Matching)
bipartite min vertex cover / max independent setKönig: matching / − matching
max weight closure (project selection)min cut
binary labelling with pairwise penalties (submodular)min cut
edge-disjoint / vertex-disjoint pathsmax flow (split vertices)
shortest pair of disjoint pathsmin-cost flow of 2 (Suurballe)
traverse every edge at least onceChinese postman: matching on odd vertices
bottleneck path (maximise the minimum edge)path in the maximum spanning tree, or binary search + BFS
reachability with cyclescondense SCCs, then DAG DP
minimum mean cycleKarp’s algorithm or binary search + negative cycle

Taxonomy and named graphs

AxisValues
directionundirected ( implies ) vs. directed (digraph)
weightsunweighted (every edge costs 1) vs. weighted
edge rulessimple (no loops/parallel edges) vs. multigraph (parallel edges) vs. pseudograph (loops and parallel edges)
connectivityconnected vs. disconnected (components); a component is a maximal connected subgraph
rootingout-tree / arborescence (edges point away from the root) vs. in-tree / anti-arborescence (edges point toward it)

Named graphs worth recognising on sight: DAG (no directed cycle, has a topological order, at least one source and sink); (complete bipartite, edges); (path, two degree-1 vertices); (star); wheel (cycle plus a universal centre); hypercube (-bit strings, adjacent iff Hamming distance 1, vertices, edges, -regular); tournament (an orientation of , exactly one directed edge per pair — see Special Graph Classes for the king-vertex theorem); complement (same vertices, adjacency inverted); line graph (one vertex per edge of , adjacent iff they share an endpoint); caterpillar (a tree that becomes a path once leaves are removed); chordal (every cycle of length has a chord); planar (drawable with no edge crossings, see the Euler’s-formula bounds below); Eulerian (a closed walk using every edge exactly once; connected with all degrees even, see Eulerian Paths); semi-Eulerian (Eulerian path but not cycle: exactly two odd-degree vertices); Hamiltonian (a cycle visiting every vertex once — no known polynomial characterisation, deciding it is NP-complete); semi-Hamiltonian (Hamiltonian path but perhaps no cycle).

Recognising the graph in a statement

A more basic pass than the modelling table above — the phrasing alone often names the problem type directly:

PhrasingVerticesEdges / problem
”minimum number of operations to transform X into Y”statesone operation BFS
”can we reach this configuration”configurationsone move reachability
”these two cannot both be chosen”choicesconflict 2-SAT or bipartite
”assign each A to a B”A’s and B’scompatibility matching
”minimum cost to separate / block”as givenmin cut
”maximum number of disjoint routes”as givenmax flow
”connect everything cheaply”as givenMST
”cheapest route”as givenshortest path

Basic facts

FactStatement
handshake lemma; the number of odd-degree vertices is even
treesfor vertices, any two of {connected, acyclic, } imply the third
forests for components
bipartiteiff no odd cycle (Bipartite)
Euler’s formulaconnected plane graph: (with components: )
planar density for ; if triangle-free; so , are not planar
planar degeneracyevery planar graph has a vertex of degree (so 6-colourable greedily; 5 by Kempe chains, 4 by the four colour theorem)
Mantel / Turántriangle-free graphs have edges; -free at most
trianglesat most triangles, all listed in by orienting edges by degree (Sqrt)

Choosing an algorithm

TaskDefaultNote
unweighted shortest pathsBFS
0/1 weights0-1 BFSdeque
nonnegative weightsDijkstra with a binary heap; dense: array version
negative weightsBellman–Ford / SPFA; detect negative cycles
DAGtopological order DPnegatives fine, also longest paths
all pairsFloyd–Warshall , or Johnson
connectivity under insertionsDSU
connectivity with deletionsoffline segment tree over time + rollback DSU; online HDT
strongly connected / 2-SATTarjan or Kosaraju
bridges, articulation points, BCClowlink DFS
MSTKruskal ; Prim dense; Borůvka for implicit graphs
max flowDinicpush–relabel for dense/hard cases
bipartite matchingHopcroft–Karp (or Dinic)
general matchingblossom / Tutte matrix rank
min-cost flowSSP with potentials
NP-hard (Hamiltonian, clique, colouring)bitmask DP / branch and bound for –

Before coding: directed or undirected? self-loops and multi-edges? connected? negative weights? the sentinel for “unreachable”? is recursion depth safe (paths of length )? does actually fit the algorithm ( needs , needs )?

Terminology

TermMeaning
Adjacent verticesjoined by an edge
Incident edgean edge touching a given vertex
Path / path lengtha sequence of vertices with consecutive ones adjacent / its number of edges
Simple pathno repeated vertex
Cyclea path returning to its start with no repeated interior vertex
Leaf / pendantdegree 1
Isolated vertexdegree 0
Bridge (cut edge)removing it increases the component count
Articulation point (cut vertex)removing it increases the component count
Subgraph / spanning subgrapha subset of vertices and edges / one containing all vertices
Spanning treea spanning subgraph that is a tree