The three arguments behind most ad hoc and constructive problems:
- An invariant proves something is impossible.
- A monovariant proves a process terminates and bounds the number of moves.
- An extremal object gives the first step of a proof or construction.
Invariants
An invariant is a quantity that no allowed move changes. If start and target differ in it, the target is unreachable.
| Move | Preserved |
|---|---|
| swap two elements | permutation parity flips, so it’s preserved under pairs of swaps |
| replace by | total sum, gcd of all |
| replace by | gcd of all, parity of the sum |
| add to two elements | pairwise differences mod |
| flip a cell and its neighbours | XOR of the cells in a fixed “test set” orthogonal to every move (linear algebra over ) |
| reverse a subarray of length | multiset; parity of the permutation changes by |
| adjacent swap of different values | inversion count changes by exactly |
| slide a tile in the 15-puzzle | permutation parity + row of the blank, mod 2 |
| rotate a cube face | corner orientation sum mod 3, edge orientation mod 2, permutation parities equal |
Candidates to try in order: sum, sum mod (try first, then 3), XOR, gcd, the multiset or sorted order, inversions, a colouring count, a weighted sum with weights chosen so every move preserves it.
Linear invariants over . When every move adds (XORs) a fixed vector to the state (lights out, toggling a row/column, flipping a path), the reachable set is the start plus the span of the move vectors. Reachability is a linear system, and the invariants are the vectors orthogonal to all moves. Solve with Gaussian elimination or an XOR basis.
Necessary and sufficient. An invariant only proves impossibility. The standard solution shape is:
- compute the invariant and output “impossible” if it differs;
- otherwise construct a solution, usually by induction (fix one element, recurse on the rest).
Often the answer is that the invariant is the only obstruction. Brute-force tiny cases to check that before proving it — this step is not optional: a brute force over answers “which cases are possible” in seconds and makes the invariant visible, instead of guessing at a pattern from nothing.
Monovariants and termination
A monovariant only increases or only decreases. A non-negative integer that strictly decreases at each step proves termination, and its initial value bounds the number of steps.
| Process | Monovariant |
|---|---|
| bubble sort / adjacent swaps | inversions: exactly swaps are necessary and sufficient |
| Euclid’s algorithm | ; at least halves every two steps |
| local improvement of a cut or a matching | objective value (bounded integer) |
| augmenting paths | flow value |
| ”if two neighbours are out of order, fix them” | of squares, or the lexicographic order of the sorted state |
| chip-firing | a potential such as |
| Stable matching (Gale-Shapley) | each proposer moves down their list |
A real number decreasing toward 0 need not terminate; you need a well-founded measure (bounded integers, or lexicographic tuples of them).
Bounding the answer: “minimum number of operations” problems are often: show each operation decreases potential by at most , so the answer is at least , then construct a sequence achieving it.
Parity and colouring
| Fact | Use |
|---|---|
| , so the number of odd-degree vertices is even (in any subgraph too) | Euler paths, Chinese postman, Christofides, -joins; Sperner’s lemma and “handshake on a hypergraph” generalise this and give a combinatorial proof of Brouwer’s fixed-point theorem |
| bipartite no odd cycle | 2-colour by BFS |
| a permutation with cycles on elements has sign | sorting by swaps needs swaps |
| odd bitwise | Lucas mod 2, Sierpinski patterns |
| parity of sums | |
| a closed walk in a bipartite graph has even length | grid walks: flips every step |
| knight moves change square colour | closed knight tours need an even number of squares |
Colouring arguments. Colour the board so every piece covers a fixed count of each colour:
- Chessboard with opposite corners removed has two more of one colour, so no domino tiling.
- bars on an board: colour cell with . Each bar covers one of each colour, so every colour count must be equal.
- four-colouring for -ish pieces and L-trominoes.
- Conway’s soldiers: weight a cell at Manhattan distance from the target by with (so ). Every jump keeps the total weight from increasing, and the infinite half-plane has total weight exactly 1 relative to row 5, so row 5 is unreachable.
The rule: when stuck on “is it possible”, tabulate small cases and look at them mod 2, then mod 3, then under a colouring, then as a weighted sum.
Try mod 2 first, always
It is the cheapest hypothesis and correct more often than any other single guess. If mod 2 fails, try mod 3, then a colouring, then a weighted sum. A potential function (strictly monotone rather than invariant) is the fallback once no fixed quantity works — that’s a monovariant, not an invariant.
More parity facts, and where they decide an answer outright rather than just narrowing the search:
| Fact | |
|---|---|
| , parity has period 4 in | odd for , even for |
| Eulerian circuit iff every degree even; Eulerian path iff exactly two odd | Euler Tours |
| Chinese postman: pair up the (even number of) odd-degree vertices by shortest paths and duplicate those paths | makes every degree even, so an Eulerian circuit exists on the duplicated multigraph |
| misère Nim’s deviation from normal Nim depends on the parity of the number of heaps of size 1 | Nim |
| ”take 1 or 2 stones” has Grundy value | parity generalised to mod 3 |
| -trominoes tile a board with any one cell removed | classic induction: quarter the board, the removed cell’s quadrant needs one more tromino at the centre, which also covers one cell in each of the other three quadrants, recursing |
| triangular or toroidal boards | the same colouring idea re-indexes: 3-colour a triangular grid by ; a toroidal colouring just wraps the indices mod the board’s period |
The extremal principle
Consider the largest, smallest, first, last, or otherwise extreme object. It has properties nothing else has.
Minimal counterexample: assume the claim fails, take the smallest counterexample, and build a smaller one. This is strong induction in disguise.
Extremal element of an optimal solution: take an optimal solution and look at its extreme part (heaviest edge, last job, leftmost point). Show it can be exchanged or removed without loss. That’s the core of exchange arguments.
| Claim | Extremal object |
|---|---|
| every tree with vertices has leaves | endpoints of a longest path |
| min degree implies a cycle | longest path: its endpoint’s other neighbour lies on the path |
| min degree implies a path of length and a cycle of length | longest path again |
| every DAG has a source | start of a longest path |
| every tournament has a Hamiltonian path | insert vertices one by one, or take a longest path |
| every tournament has a king (reaches all in steps) | vertex of maximum out-degree |
| the heaviest edge of a cycle isn’t in some MST | cycle property via exchange |
| Sylvester-Gallai | smallest positive distance from a point to a line through two others |
| irrational | minimal denominator |
Where to look:
| Setting | Extreme |
|---|---|
| graphs | longest path, max/min degree, heaviest edge, largest component, deepest DFS vertex |
| trees | leaves, centroid, diameter endpoints, deepest node |
| sequences | maximum (split the array at it), first violation, longest run |
| geometry | leftmost/lowest point (on the hull), closest pair, extreme point in a direction |
| constructions | most constrained element first; the largest value placed first |
Proof: every tree with vertices has leaves
Take the longest path in the tree, ( since ). Its endpoint has degree 1: any neighbour other than would either extend the path (if not already on it) or close a cycle (if it were some , , contradicting that trees are acyclic) — both impossible for a longest path. The same argument applies to , and since , giving two distinct leaves.
Proof: min degree implies a cycle
Take the longest path . Since , it has a neighbour other than ; that neighbour cannot be a new vertex (it would extend the path, contradicting maximality), so it must be some on the path with — giving the cycle .
Proof: every DAG has a source
Take the longest directed path (exists and is finite since the graph is acyclic and finite). If had an incoming edge from some : if is not on the path, prepending it extends the path, contradicting maximality; if for some on the path, the edge together with the path segment closes a cycle , contradicting acyclicity. So has in-degree 0.
Proof: the heaviest edge of a cycle isn't in some MST
Suppose it were. Removing it from the MST splits the tree into two components; the cycle (minus that edge) is a path between the same two endpoints, so it must cross the cut again at some other edge — which, since the removed edge was strictly heaviest on the cycle, is strictly lighter. Swapping the heavy edge for that lighter crossing edge keeps the result a spanning tree (still connected, still edges, no cycle reintroduced) with strictly smaller total weight, contradicting that the original was an MST.
Proof: Sylvester-Gallai
Among all (point, line-through-two-other-points) pairs with the point not on the line, take the one minimising the perpendicular distance from the point to the line (finitely many pairs, so a minimum exists, and at least one pair exists since the points are not all collinear). Call the point , foot of the perpendicular , and the line through at least three of the points, (assume for contradiction has 3 or more). Among , two lie on the same side of along (pigeonhole: 3 points split by into left/right); say is strictly between and . The perpendicular distance from to the line is then strictly less than : lies strictly inside the right triangle (on segment , with off the line), and the distance from any point on a leg of a right triangle to the hypotenuse is less than the triangle’s height from the right-angle vertex — here that height is exactly . So is a pair with strictly smaller distance than , contradicting minimality. The minimising line therefore has exactly 2 points.
Proof: is irrational (via the extremal principle)
Suppose with positive integers and minimal among all such representations (exists by well-ordering). Then . Since , taking square roots gives , i.e. . Set and ; both are positive integers (; ) with (a smaller denominator). Direct algebra, using only : , so , i.e. too — a representation with a strictly smaller denominator, contradicting minimality of . (This avoids the usual parity/infinite-descent argument entirely, using “minimal counterexample” instead.)
Splitting at the maximum is a great general tool: many problems on arrays decompose recursively around the position of the maximum (Cartesian tree, “divide at the max” with small-to-large to get ).
A linear function on an interval is extremised at an endpoint, and a convex function on a polytope at a vertex. Only test the boundary values.
Pigeonhole
items in boxes force some box to hold at least .
| Application | |
|---|---|
| some non-empty subarray has sum divisible by | prefix sums mod ; find two equal in |
| among numbers in two are coprime, and one divides another | consecutive pairs; odd parts |
| iterating on a set of size repeats within steps | cycle detection, period finding |
| a string of length repeats a letter | answer bounded by alphabet size |
| bounded answer means brute force | ”if the answer is always yes”, so only search when |
| Erdős-Szekeres: distinct numbers contain an increasing run of length or a decreasing one of length | LIS/LDS bounds; |
| distinct prefix gcds/ORs/ANDs ending at a position | at most |
| birthday bound: random samples from collide with probability | hashing collision estimates |
Pigeonhole proves existence. You still have to scan for the pair.
Dilworth and Mirsky. In a partial order, the size of the largest antichain equals the minimum number of chains covering it (Dilworth), and the length of the longest chain equals the minimum number of antichains (Mirsky). For sequences: the minimum number of non-increasing subsequences covering equals the length of the longest strictly increasing subsequence. Computing a minimum chain cover in a general DAG is a bipartite matching (path cover).
Proof: Mirsky's theorem (the easy direction of the pair)
Longest chain minimum antichain cover is immediate: a chain of length has one element in each of at least different antichains of any cover (no two elements of a chain can share an antichain, since a chain’s elements are pairwise comparable and an antichain’s are not), so any cover needs antichains, in particular the longest chain’s length.
Equality (a cover achieving exactly the longest chain length exists): let be the length of the longest chain ending at . Each level set is an antichain (if with , then extending the chain ending at by gives , contradiction), and there are exactly non-empty level sets, covering every element.
(Dilworth’s theorem, the antichain/chain-cover dual, is substantially harder — the standard proof is by induction using König’s theorem or a direct exchange argument; it is the one genuinely deep result of the pair.)
Proof: Erdős-Szekeres
Among distinct numbers, assign to each element the pair where is the length of the longest increasing run ending at and the length of the longest decreasing run ending at . If (in position) and the value at is less than at , (extend the increasing run); if the value at exceeds the value at , . So for , (the elements are distinct values, so one of the two cases applies, incrementing one coordinate). If no increasing run has length and no decreasing run has length , then and for every , giving only possible distinct pairs — but there are elements, each with a distinct pair, a contradiction (pigeonhole).