Skip to main content

ProofsPath · 11 min

Invariants and Monovariants

Important
For:GeneralResearch

Invariants and Monovariants

Why This Matters

Many olympiad problems describe a process: a game, a configuration, a move-by-move transformation: and ask whether some target state is reachable, or whether the process terminates. The two go-to techniques:

  • Invariant: a quantity preserved by every allowed move. If start and target have different invariant values, the target is unreachable.
  • Monovariant (a.k.a. potential function, Lyapunov function): a quantity that strictly decreases (or strictly increases) with each move. If the quantity is bounded below (above), the process must terminate.

The combination handles both configuration questions ("can we reach state TT from state SS?") via invariants and termination questions ("does the process always halt?") via monovariants. The technique generalizes far beyond olympiads: loop-termination proofs in software verification, Markov-chain mixing arguments, simulated annealing, and reinforcement learning all use the same pattern.

The Setup

A process has:

  • A state space S\mathcal{S}.
  • A set of moves (operations) MS×SM \subseteq \mathcal{S} \times \mathcal{S}.
  • A start state s0s_0 and (often) a target state sTs_T or a halting condition.

Invariant. A function I:SXI: \mathcal{S} \to X is an invariant if I(s)=I(s)I(s) = I(s') for every move (s,s)M(s, s') \in M.

Monovariant. A function ϕ:SR\phi: \mathcal{S} \to \mathbb{R} is a monovariant if ϕ(s)>ϕ(s)\phi(s) > \phi(s') for every move (i.e., strictly decreasing), or symmetrically, strictly increasing. We say ϕ\phi is strict on MM.

Conclusions.

  • Invariant \Rightarrow I(s0)=I(s)I(s_0) = I(s) for every reachable ss. So if I(s0)I(sT)I(s_0) \ne I(s_T), sTs_T is unreachable.
  • Monovariant (strict) on a well-ordered XX \Rightarrow the process must halt. The number of moves is bounded by ϕ(s0)ϕmin|\phi(s_0) - \phi_{\min}| in the discrete case.

Worked Example: The 15-Puzzle Parity Invariant

Example

The 15-puzzle (Sam Loyd, 1880)

Problem. The 15-puzzle has tiles numbered 1-15 in a 4x4 grid with one empty cell. A move slides a tile into the empty cell. Show that no sequence of moves can swap tiles 14 and 15 while leaving all other tiles in their starting positions.

Solution. The state is a permutation σ\sigma of {1,,15}\{1, \ldots, 15\} plus the position of the empty cell (r,c){1,2,3,4}2(r, c) \in \{1, 2, 3, 4\}^2. Define the invariant:

I(σ,r,c)=sign(σ)(1)r+c{+1,1}.I(\sigma, r, c) = \text{sign}(\sigma) \cdot (-1)^{r + c} \in \{+1, -1\}.

Check. Each move slides one tile, swapping it with the empty cell. The permutation σ\sigma gets a single transposition (sign flip), and the empty cell's (r+c)(r + c) parity also flips. So both factors flip, and II is preserved.

Apply. In the start state, σ=id\sigma = \text{id} has sign +1+1, and the empty cell is at (4,4)(4, 4) giving (1)4+4=+1(-1)^{4+4} = +1, so I=+1I = +1.

The target state has σ=(14,15)\sigma = (14, 15) a transposition with sign 1-1, and the empty cell back at (4,4)(4, 4) giving (1)8=+1(-1)^8 = +1, so I=1I = -1.

Different invariant values: target unreachable.

This argument was first made precise by Johnson (1879) and Story (1879), independently of the toy: it underlies the classification of solvable / unsolvable configurations.

The 15-puzzle parity argument is the canonical illustration: the invariant is not obvious (you have to think to see that empty-cell parity must couple with permutation sign), but once chosen, the proof is one line.

Worked Example: The Chocolate-Bar Problem

Example

The chocolate-bar problem

Problem. A chocolate bar is a 6x8 grid of squares. Two players alternate breaking the bar along grid lines (the broken pieces stay in play). The player who cannot move loses. (Each move strictly increases the number of pieces.) Show that the game terminates; if both play perfectly, who wins?

Solution: termination via monovariant. Every move turns one piece into two pieces. Let ϕ(s)=68#pieces(s)\phi(s) = 6 \cdot 8 - \#\text{pieces}(s). At the start, ϕ=481=47\phi = 48 - 1 = 47; the game ends when ϕ=0\phi = 0 (every piece is a single square). Each move decreases ϕ\phi by 1. So the game lasts exactly 47 moves.

Solution: who wins. Since the game lasts an odd number of moves and the first player makes odd-numbered moves, the first player makes the 47th move; the second player has no move and loses. So the first player always wins.

The cleanness. The monovariant ϕ\phi doesn't depend on which moves are chosen: it just counts pieces. This makes the analysis trivial and game-theory-free.

This is the invariant counting technique: count something that changes deterministically per move, regardless of strategy.

Choosing the Invariant or Monovariant

The hard part is finding the right quantity. Useful heuristics:

CueTry as invariant/monovariant
Game with allowed swapsSign of permutation, parity of inversions
Game on a grid with reflection movesParity of r+cr + c for moved cell
Sum / product / coloring constraintSum modulo kk for some kk
Numbers replaced by combinationsSum, max, min, GCD, LCM
Configuration of pointsCentroid, diameter, total distance
Process with shrinking piecesNumber of pieces, total area
Recursive / fractal structureMultinomial / characteristic polynomial
Game treeSprague-Grundy number (advanced)

When in doubt, compute the candidate quantity on a few moves and see if it's preserved or strictly decreasing.

Worked Example: Numbers on a Blackboard

Example

Replacing numbers on a blackboard

Problem. The numbers 1,2,,1001, 2, \ldots, 100 are written on a blackboard. In each move, you erase two numbers aa and bb and write ab|a - b| in their place. After 99 moves, one number remains. What can it be?

Solution. The total parity: a+ba + b has the same parity as ab|a - b| (since a+bab=2min(a,b)a + b - |a - b| = 2 \min(a, b) is even). So the parity of the sum is invariant.

Initial sum: 1+2++100=50501 + 2 + \cdots + 100 = 5050, which is even. So the final number must be even.

The final number can be 0 (if at some point a=ba = b are erased and the difference is 0), 2, 4, etc. Constructions exist for many even values. The question "what can it be?" asks for the range; the invariant pins down "even" but nothing more.

Note: the same problem appears in Engel and Larson with slight variations.

This is the invariant modulo 2 pattern: when moves combine two numbers via a function, often only the parity of some statistic survives.

Termination via Monovariants

For termination, the monovariant must be:

  • Real-valued (or take values in a well-ordered set).
  • Bounded below (or above) by some fixed constant.
  • Strictly decreasing (or increasing) per move.

Then by well-ordering on N\mathbb{N} or by the completeness of R\mathbb{R} (with a discrete-step hypothesis), the process halts.

A classical example: bubble sort terminates because the number of inversions is a monovariant: each swap decreases it by 1, and it's bounded below by 0. The number of moves equals the initial number of inversions.

In ML, gradient descent on a μ\mu-strongly convex function has f(xk)f(x_k) as a monovariant (modulo the step-size constant) and converges by exactly this argument.

Common Mistakes

Watch Out

Confusing 'invariant' and 'monovariant'

Invariant: preserved by every move. Used to prove non-reachability. Monovariant: strictly changes per move. Used to prove termination. Mixing them up gives wrong proofs.

Watch Out

Strictness of monovariants

A monovariant must change strictly on every move. If some moves leave it constant, you have an invariant on the restricted move set, but termination is no longer guaranteed on its own: you need a secondary monovariant for the unchanged-quantity moves.

Watch Out

Non-bounded monovariants don't give termination

ϕ(sk)=ϕ(s0)k\phi(s_k) = \phi(s_0) - k is monotone but unbounded below on R\mathbb{R}. For termination, you need ϕ\phi bounded below on the state space. In olympiad problems, this is usually ϕ:SN\phi: \mathcal{S} \to \mathbb{N}, where well-ordering forces termination.

Watch Out

Forgetting non-uniqueness

Multiple invariants can hold at once; finding one is enough to prove unreachability. A finer-grained invariant gives a sharper non-reachability claim. The 15-puzzle invariant splits states into two equivalence classes; finer invariants might split into more, but for the 14-15 swap problem, parity suffices.

Exercises

ExerciseCore

Problem

Five 1's and five 2's are written in a row. In each move, you erase two adjacent identical numbers and replace them with one. (E.g., 1,111, 1 \to 1 or 2,222, 2 \to 2.) Can the row end with one of each (1, 2)?

ExerciseAdvanced

Problem

On a 9x9 grid, we place pieces such that each row and column contains an even number of pieces. Show that the total number of pieces is even.

ML and Production Connections

  • Loop termination in formal verification. Proving a loop terminates often uses a ranking function — monovariant: that maps the state space into a well-ordered set. Coq's Function keyword and Isabelle's function command require an explicit ranking function.

  • Lyapunov functions in control theory. The Lyapunov function for a dynamical system x˙=f(x)\dot{x} = f(x) is exactly a monovariant on a continuous state space. Used in reinforcement learning for stability analysis (Berkenkamp et al. 2017).

  • Convergence proofs in optimization. Gradient descent on convex ff: the objective f(xk)f(x_k) is a monovariant. In stochastic settings, E[f(xk)]\mathbb{E}[f(x_k)] is the monovariant.

  • Markov chain mixing and detailed balance. The KL divergence to stationary distribution is a monovariant (the H-theorem). Used in MCMC convergence analysis.

Cross-Network Links

References

See structured references block. Primary entry points: Engel Ch 2 for the technique treatment; Larson Ch 4 for worked examples; Johnson and Story (1879) for the original 15-puzzle analysis; Aigner-Ziegler for elegant invariant proofs across mathematics.