JB Math Club 26-27 · Lecture notes
Semi-Invariants
Weighted counts, state graphs and lexicographic order
Study how weighted counts, finite state graphs, lexicographic order, and Palmer’s cycle-repair algorithm control what a sequence of moves can do.
Route through the lecture. Semi-invariants; two boxes and changing signs; Fibonacci state graphs; lexicographic order and the banker; Hamiltonian cycles, Ore’s theorem and Palmer’s algorithm.
1. Semi-invariants
A process consists of states and allowed moves between them. To understand whether the process must stop, we often look for a quantity that changes in the same direction at every move.
Definition. A semi-invariant is a function of the state that strictly increases at every allowed move, or strictly decreases at every allowed move.
Strict change alone does not prove termination: the sequence \(1,\tfrac12,\tfrac14,\ldots\) strictly decreases and stays positive, but has infinitely many terms. We also need to know that infinitely many such changes are impossible. In this lecture, we will use a nonnegative integer that strictly decreases, an integer that strictly increases but is bounded above, and a fixed-length tuple of nonnegative integers that decreases lexicographically.
Problem 1. Two boxes
Two boxes each contain 9 balls. In one move, one can remove 1 ball from any box, or remove 1 ball from the left box and add 9 balls to the right box. Prove that the moves will eventually terminate.
Idea. The total number of balls may increase. Give a ball in the left box more weight, since it can produce nine balls in the right box.
Solution. If the boxes contain \(x\) and \(y\) balls, consider
\[F(x,y)=10x+y.\]
There are three possible moves:
| Move | New state | Change in \(F\) |
|---|---|---|
| Remove from the left | \((x-1,y)\) | \(-10\) |
| Remove from the right | \((x,y-1)\) | \(-1\) |
| Exchange left for right | \((x-1,y+9)\) | \(-1\) |
In particular,
\[10(x-1)+(y+9)=10x+y-1.\]
Thus \(F\) is a nonnegative integer that decreases by at least 1 at each move. Initially, \(F(9,9)=99\), so at most 99 moves are possible. If either box is nonempty, a removal is still possible. Therefore the process, continued for as long as a legal move exists, ends with both boxes empty.
Problem 2. Changing signs
A \(20\times20\) square contains 400 nonzero integers. In one move, one can change the signs of all numbers in a single row or a single column. Prove that after a finite number of such operations, the sum of the numbers in each row and each column can be made nonnegative.
Idea. We choose which moves to make: flip only a row or column whose sum is negative. Track the sum of the whole table.
Solution. Let \(S\) be the total sum. If a chosen row or column has sum \(r<0\), its contribution changes from \(r\) to \(-r\). Hence
\[S'=S-2r>S.\]
Because \(r\) is an integer, each chosen move increases \(S\) by at least 2. The absolute values of all entries remain unchanged, so
\[S\leq M,\qquad M=\sum_{i=1}^{20}\sum_{j=1}^{20}|a_{ij}|.\]
An increasing integer sequence bounded above cannot continue indefinitely. Our procedure therefore stops after finitely many moves. At that point there is no negative row sum and no negative column sum, which is exactly the required conclusion. The procedure proves that a suitable sequence of moves exists; it does not claim that every possible choice of sign flips terminates.
2. State graphs
Definition. A state graph is a directed graph whose vertices represent the possible states of a system and whose directed edges represent allowed transitions. The state must contain enough information to determine the next move when the process is deterministic.
For a finite deterministic process whose transition is always defined, a state eventually repeats. This need not be the initial state: for example, \(A\to B\to C\to D\to E\to C\to\cdots\) never returns to \(A\). If every state also has exactly one predecessor, the graph is a disjoint union of directed cycles: no path can feed into a cycle from outside, because its entry vertex would then have two predecessors.
Problem 3. Fibonacci numbers
Prove that there exists a Fibonacci number divisible by 2025. Here \(F_1=F_2=1\) and \(F_{n+2}=F_{n+1}+F_n\) for \(n\geq1\).
Solution. For the proof, extend the sequence by setting \(F_0=0\); the same recurrence then also holds at index zero. We seek a positive index \(k\) such that \(2025\mid F_k\). The auxiliary term \(F_0\) is not the required answer.
Use pairs of consecutive residues as states:
\[X_k=(F_k,F_{k+1})\pmod{2025}.\]
Each coordinate is one of \(0,1,\ldots,2024\), so there are \(2025^2\) possible states. The transition is
\[T(a,b)=(b,a+b)\pmod{2025}.\]
It has a unique inverse:
\[T^{-1}(a,b)=(b-a,a)\pmod{2025}.\]
All residues here are taken modulo 2025. Thus every vertex has exactly one outgoing edge and exactly one incoming edge. The finite state graph is a union of cycles, and the initial state \((0,1)\) lies on one of them.
After some positive number \(N\) of moves we return to \((0,1)\). Consequently,
\[X_N=(0,1)\quad\Longrightarrow\quad F_N\equiv0\pmod{2025},\qquad N>0.\]
This gives the required Fibonacci number. Merely knowing that some state repeats would not by itself show that the initial state returns; reversibility supplies that step.
3. Lexicographic order
Sometimes one numerical quantity is inconvenient, but a short list of quantities works. The order of the list tells us which quantity has priority.
Ordered alphabets and words
An alphabet \(\Sigma\) is a set of symbols. For lexicographic comparison, it comes with a fixed linear order: any two distinct symbols are comparable, and the comparison is transitive. The usual alphabet has \(\mathrm A<\mathrm B<\cdots<\mathrm Z\). We may also use the digits \(0<1<\cdots<9\), or the infinite alphabet \(\mathbb Z_{\geq0}\) with its usual order.
A word over \(\Sigma\) is a finite sequence \(w=(a_1,\ldots,a_n)\) with \(a_i\in\Sigma\). Its length is \(n\). Thus \(\mathrm{CAT}\) is a word of length three over the letter alphabet. The tuple \((2,100,7)\) is a word of length three over \(\mathbb Z_{\geq0}\): here the whole number \(100\) is one symbol, not three decimal digits.
How the comparison works
Definition. Let \(A=(a_1,\ldots,a_n)\) and \(B=(b_1,\ldots,b_n)\) be words of equal length over the same ordered alphabet. We write \(A\succ B\) if, at the first position where they differ, the symbol of \(A\) is larger. Equivalently, for some \(k\),
\[a_1=b_1,\ldots,a_{k-1}=b_{k-1},\qquad a_k>b_k.\]
The later symbols do not affect the comparison. If every coordinate agrees, the words are equal. This is lexicographic order, the rule behind dictionary order. We use \(A\prec B\) for the reverse strict comparison, and \(A\preceq B\) to mean \(A\prec B\) or \(A=B\).
For words of different lengths, use the same first-difference rule whenever a differing position exists. If one word ends while all its symbols agree with the beginning of the other, it is a proper prefix of the other word; the shorter word comes first. For example, \(\mathrm{CAT}\prec\mathrm{CATS}\).
Examples.
- \(\mathrm{CAR}\prec\mathrm{CAT}\): the first two letters agree, and \(\mathrm R<\mathrm T\) at the third position.
- \((1,2,3,4)\succ(1,1,13,24)\): the first entries agree; \(2>1\) at the second position decides, regardless of \(13\) and \(24\) later.
- \((2,100)\prec(3,0)\): the first coordinate decides because \(2<3\). Comparing the sums would give the opposite answer, so a sum is not the relevant quantity.
Why the domain matters. Lexicographic order is defined more broadly than the termination theorem we will use. If lengths vary, even the finite alphabet \(\mathrm A<\mathrm B\) admits the infinite decreasing chain
\[\mathrm B\succ\mathrm{AB}\succ\mathrm{AAB}\succ\mathrm{AAAB}\succ\cdots.\]
At each step, a new \(\mathrm A\) precedes the final \(\mathrm B\); this is the first differing position. Similarly, allowing all integers would give the decreasing chain \((0)\succ(-1)\succ(-2)\succ\cdots\) even at length one. From now on, our termination statements concern a fixed finite length and nonnegative integer entries. The collection of such tuples is infinite, but its strictly decreasing chains are finite.
Three properties
For tuples of a fixed length \(n\) over \(\mathbb Z_{\geq0}\):
- Lexicographic order is transitive: \(A\preceq B\) and \(B\preceq C\) imply \(A\preceq C\).
- There is no infinite strictly decreasing sequence of tuples.
- Every nonempty set of tuples has a least element.
Proof of 1. If either comparison is equality, the result is immediate. Otherwise let \(k\) be the first position where \(A\) and \(B\) differ, and \(m\) the first position where \(B\) and \(C\) differ. Before position \(\min(k,m)\) all three tuples agree.
- If \(k<m\), then \(a_k<b_k=c_k\).
- If \(k=m\), then \(a_k<b_k<c_k\).
- If \(k>m\), then \(a_m=b_m<c_m\).
In every case, the first difference between \(A\) and \(C\) gives \(A\prec C\).
Proof of 2. Use induction on the length \(n\). For \(n=1\), a strictly decreasing sequence of nonnegative integers is finite.
Assume the claim holds for length \(n\), and suppose there is an infinite strictly decreasing sequence of tuples of length \(n+1\). Their first coordinates never increase. Since they are nonnegative integers, they can decrease only finitely many times, and eventually become constant. After that point, delete the common first coordinate. The remaining tails form an infinite strictly decreasing sequence of length \(n\), contradicting the induction hypothesis.
Proof of 3. If a nonempty set had no least element, we could choose \(A_1\) in it, then a smaller \(A_2\), then a smaller \(A_3\), and continue. This would give an infinite strictly decreasing sequence, contrary to property 2.
Problem 4. The banker and the genie
A banker with finitely many coins found a magic lamp, from which a genie appeared. The genie proposed a deal: every day, the banker would give him one coin and, in return, could receive any finite number of smaller-denomination coins of his choice. If the banker ever runs out of money, he must release the genie. The available denominations are 1 euro, 2 euros, 5 euros and 10 euros. Prove that the banker will inevitably have to release the genie.
Idea. Neither the total value nor the number of coins has to decrease. Give priority to the largest denomination, then the next largest, and so on.
Solution. Giving away a 1-euro coin returns no coins, since no smaller denomination is available. Encode the state as
\[A=(a_{10},a_5,a_2,a_1),\]
where \(a_d\) is the number of coins of denomination \(d\). Each entry is a nonnegative integer. When a coin of denomination \(d\) is exchanged, all coordinates for larger denominations remain unchanged, the coordinate \(a_d\) decreases by 1, and only coordinates to its right may increase. Therefore the new tuple \(A'\) satisfies
\[A\succ A'.\]
The successive states form a strictly decreasing sequence of tuples of fixed length four, so the process is finite. If any coin remains, the banker can still give it away. Thus the only terminal state is \((0,0,0,0)\), and the genie must be released.
4. Proving a theorem by starting a process
We can also use a semi-invariant to prove that a desired object exists: start with an imperfect object, repeatedly improve it, and show that the process must stop only when the object has the required property.
Hamiltonian paths and cycles
Throughout this section, graphs are finite, simple, and undirected. The degree \(\deg(v)\) is the number of vertices adjacent to \(v\).
A Hamiltonian path visits every vertex exactly once. A Hamiltonian cycle visits every vertex exactly once and then returns to the starting vertex. A graph containing such a cycle is called Hamiltonian.
Dirac’s theorem. If a graph has \(n\geq3\) vertices and every vertex has degree at least \(n/2\), then the graph is Hamiltonian.
Ore’s theorem. If a graph has \(n\geq3\) vertices and every pair of distinct nonadjacent vertices \(u,v\) satisfies
\[\deg(u)+\deg(v)\geq n,\]
then the graph is Hamiltonian.
Dirac’s degree condition implies Ore’s condition. It is therefore enough to prove Ore’s theorem.
Palmer’s algorithm: one repair
We prove Ore’s theorem constructively using Palmer’s algorithm. Start with any cyclic ordering of all the vertices. A consecutive pair is good if it is an edge of the graph and bad otherwise. Count the closing pair as well. Our aim is to repair bad pairs until every consecutive pair is an edge.
Consider the cyclic order
\[A,B,C,D,E,F,G,H,A.\]
In our example, the complete neighbour lists of \(A\) and \(B\) are
\[N(A)=\{C,D,E,H\},\qquad N(B)=\{C,F,G,H\}.\]
The other six vertices \(C,D,E,F,G,H\) are pairwise adjacent. Thus every vertex has degree at least \(4\), so this eight-vertex graph satisfies Ore’s condition. In the displayed ordering, only \(AB\) is bad: there are \(7\) good consecutive pairs.
We want to repair the gap \(A\mid B\). Look for a second cut \(X\mid Y\) for which both \(AX\) and \(BY\) are edges. Then the block starting at \(B\) and ending at \(X\) can be reversed, placing \(X\) next to \(A\) and \(B\) next to \(Y\).
There are seven possible second cuts: \(B\mid C\), \(C\mid D\), \(D\mid E\), \(E\mid F\), \(F\mid G\), \(G\mid H\), and \(H\mid A\). Make one column for each cut. In the top row, mark the column when \(A\) is adjacent to its left endpoint; in the bottom row, mark it when \(B\) is adjacent to its right endpoint.
Each row has four marks. Eight marks cannot fit into seven columns with at most one mark in each column. Hence some column has both marks. Here it is \(E\mid F\): we have found the two edges \(AE\) and \(BF\). We are looking for a common cut, not a common neighbour of \(A\) and \(B\).
Cut the two neighbour relations \(A\mid B\) and \(E\mid F\) in the ordering. Reverse the whole block \(B,C,D,E\) into \(E,D,C,B\), then reconnect using the edges \(AE\) and \(BF\):
\[A,\boxed{B,C,D,E},F,G,H \quad\longrightarrow\quad A,\boxed{E,D,C,B},F,G,H.\]
The vertices \(A,F,G,H\) stay in place. This is not just a swap of the endpoints \(B\) and \(E\): the order of \(C\) and \(D\) is reversed too. We change the cyclic ordering, not the graph; the two reconnecting edges already existed.
Why Ore’s condition guarantees a repair
Now use the same argument for an arbitrary cyclic ordering \((v_1,v_2,\ldots,v_n)\). If there is a bad pair, rotate the notation so that it is \(u=v_1\), \(v=v_2\). Set \(v_{n+1}=v_1=u\).
There are \(n-1\) possible second cuts \(v_j\mid v_{j+1}\), with \(j=2,\ldots,n\). For each cut, test the two required edges:
| Row | When to mark column \(j\) | Number of marks |
|---|---|---|
| Top | \(uv_j\) is an edge | \(\deg(u)\) |
| Bottom | \(vv_{j+1}\) is an edge | \(\deg(v)\) |
Why are these the degrees? The top row tests \(v_2,\ldots,v_n\), which is every vertex except \(u\). The bottom row tests \(v_3,\ldots,v_n,v_1\), which is every vertex except \(v\). Each possible neighbour is tested exactly once.
This is where we use Ore’s condition. The chosen vertices \(u\) and \(v\) are nonadjacent, so
\[\underbrace{\deg(u)+\deg(v)}_{\text{total marks}} \ \geq\ n\ >\ \underbrace{n-1}_{\text{columns}}.\]
If no column had both marks, there would be at most \(n-1\) marks. Therefore a shared column exists, giving both \(uv_j\) and \(vv_{j+1}\). It cannot be the first column, \(j=2\), because its top mark would require \(uv\); nor the last column, \(j=n\), because its bottom mark would require \(vu\). Thus \(3\leq j\leq n-1\), and the four boundary vertices are distinct.
Reverse the block \(v_2,\ldots,v_j\). The new cyclic order is
\[\bigl(u,v_j,v_{j-1},\ldots,v_2,v_{j+1},\ldots,v_n\bigr).\]
In the example, \(u=A\), \(v=B\), \(j=5\), \(v_j=E\) and \(v_{j+1}=F\).
Why the flip improves the order
In the example, the boundary pairs change as follows:
\[\underbrace{AB}_{\text{bad}},\ \underbrace{EF}_{\text{good}} \quad\longrightarrow\quad \underbrace{AE}_{\text{good}},\ \underbrace{BF}_{\text{good}}.\]
Inside the reversed block, \(BC,CD,DE\) become \(ED,DC,CB\). These are the same undirected edges, read in reverse. Outside the block, \(FG,GH,HA\) are unchanged. So the number \(Q\) of good consecutive pairs rises from \(7\) to \(8\), and \(A,E,D,C,B,F,G,H,A\) is a Hamiltonian cycle.
In general, only the two unordered boundary pairs change:
\[uv,\ v_jv_{j+1}\quad\longrightarrow\quad uv_j,\ vv_{j+1}.\]
The first old pair is bad. If the other old pair is good, one good pair is replaced by two, so \(Q\) increases by \(1\). If it is bad, \(Q\) increases by \(2\). Every internal pair remains the same unordered pair, and all external pairs remain unchanged.
Since \(0\leq Q\leq n\), only finitely many repairs are possible: at most \(n-Q_0\), where \(Q_0\) is the initial number of good pairs. One repair was enough for our example; in general, repeat the procedure. A bad pair always allows another repair by the counting argument, so the algorithm can stop only when all \(n\) pairs are good. They form a Hamiltonian cycle, proving Ore’s theorem and hence Dirac’s theorem.
With constant-time adjacency queries, finding a bad pair, scanning the possible cuts, and reversing a block each take \(O(n)\) time per repair. There are at most \(n\) repairs, so Palmer’s algorithm takes \(O(n^2)\) time.