Today we shall celebrate Children’s Day (with the painful observation that I no longer qualify as a child) and the two-year anniversary of this blog (time has flown…) by looking at an extremely useful idea which comes up repeatedly in problems involving algorithms or processes.
A state graph of an operation is a labeled directed graph that can be used to indicate the directed connectivity between the set of possible states under
. The concept of the state graph can be utilized in two main ways: firstly, we may bound the number of operations that can be performed before we reach a certain state (usually an end or neutral state) and secondly, we may use it to show that a certain state can be reached from some other state (and maybe bound the number of operations it takes to do so).
One of the most important things to check right away on problems of this sort is whether the edges are directed, i.e whether the operation is reversible. If it is, showing state can be reached from state
is equivalent to the converse.
Unless stated otherwise, all distances below are taken in a connected component of an undirected state graph (which shall usually be the case in olympiad contexts). When an operation is reversible, we replace the two opposite directed edges by a single undirected edge.
Before moving on, we need to review several key terms from graph theory. The distance
between two vertices and
is the number of edges in the shortest path connecting these two vertices. The diameter
of a graph is the maximum shortest distance between any pair of vertices in a graph. The eccentricity
is the greatest distance from a specific vertex
to any other vertex in the graph. The radius
is the minimum value of all eccentricities in the graph.
We shall informally call a particularly convenient intermediate configuration a home state.1 In graph-theoretic language, we are selecting a vertex through which we can construct short paths between many pairs of states.
A very important result that we shall use time and time again is the relationship between the diameter and radius of a graph.
Theorem 1 (Diameter-Radius)
The radius of a graph is at least half its diameter.
Proof. For the other inequality, choose a vertex whose eccentricity is minimal, and let
be vertices satisfying
By the triangle inequality,
Therefore, as required.
The basic recipe for many problems here is almost repetitive:
Step 1 Pick a suitable state graph and identify the vertex and edge set (usually states and states connected by a single operation). Check whether edges are directed.
Step 2A Determine the home state(s), based on a rough intuition of ‘distance’.
Step 2B Rigorously define distance between states. This parameter will almost always depend on which home state you are looking at, and is usually extreme at the home state, so expect to deal with several definitions concurrently.
Step 3 Bound! Bound! Bound!
Steps 2A and 2B can be performed in either order, but you can always use one of them to motivate the other. This motif will make a lot more sense looking at a contest example.
Problem 2 (IMO Shortlist 2017 C2)
Let be a positive integer. A chameleon is a sequence of
letters with exactly
occurrences of each of the letters
,
,
. Define a swap to be the transposition of two adjacent letters in the chameleon. Prove that for any chameleon
there exists a chameleon
such that
cannot be changed to
using fewer than
swaps.
Swaps are clearly reversible, and the state graph2 is connected since adjacent swaps can ‘sort’ every chameleon.
Here, since we are working with swapping pairs of adjacent letters, we can look at the number of inversions (pairs of letters whose order in the chameleon is opposite to their alphabetical order) of our chameleon.
Each swap switches two adjacent letters, so the number of inversions of chameleon changes by one (if the letters are distinct) or zero (if the letters are identical) in each move.
Thus, in order to find two chameleons which are far apart from each other, we need to maximize the difference in the number of inversions. This naturally leads us to consider the pair of chameleons and
.
Clearly, the difference in the number of inversions is , so the diameter of the state graph is at least
. Moreover, our theorem above implies that the radius of the graph is at least
, and we win.
This framework also appears in some approaches for China 2024/6 and IMO Shortlist 2018 C3.
A common trick involves using a vertex with low eccentricity as a ‘home state’. Hence, an algorithm will involve moving to and from this home state, which cuts down the number of operations required.
One of the shortcomings of this approach is that it requires the state graph of the desired process to be in some sense compact. In fact, in the case where there exist states which are ‘further away’ from the home state, it might be efficient to travel through a different path. To counter this challenge, it is often convenient to define multiple home states and use the path which is closest to the starting/ending state.
We shall look at this generalization in action, with the following two beautiful problems, given at the Iran Team Selection Tests in consecutive years.
Problem 3 (Iran TST 2025/11)
There are cups labeled
, where the
-th cup has capacity
liters. In total, there are
liters of water distributed among these cups such that each cup contains an integer amount of water. In each step, we may transfer water from one cup to another. The process continues until either the source cup becomes empty or the destination cup becomes full.
- Prove that from any configuration where each cup contains an integer amount of water, it is possible to reach a configuration in which each cup contains exactly 1 liter of water in at most
steps.
- Prove that in at most
steps, one can go from any configuration with integer water amounts to any other configuration with the same property.
Let denote the number of non-empty cups in the starting state. We first look at how this parameter relates to the distance to the equidistributed (all cups have
liter of water) state.
Algorithm 1. Pour all the non-empty cups into the th cup. Now, pour the
th cup into the
-th cup for all
in descending order starting with
.
Observe that the first part takes at most steps and the second takes
steps, for a total of less than
steps. The second part ensures that each cup contains
liter of water.
Algorithm 2A. Until all the cups contain water, select a cup with at least 2 liters of water, and pour it into the -st cup (if the
-st cup is not empty pour it into any empty cup). Then, pour the water in the
st cup into any empty cup.
Clearly this takes at most steps to pour 1 liter of water into all the empty cups.
Now, it is clear that if , then algorithm 1 ensures that in less than
steps each cup contains 1 liter of water. Further, if
then algorithm 2A ensures that in at most
steps each cup contains 1 liter of water.
For the second part, our first question is regarding the choice of home state. The most natural option is of course to simply use the one given to us in part (a). We can try using the equidistributed state (or any other single home state), but some playing around with that should convince you that the bounds obtained as such are far too weak.
As before, this is due to the presence of two clearly antipodal states: the equidistributed and extremal (the th cup has
liters of water) ones. We shall have to deal with this by selecting both of them to be home states.
Naturally, this introduces the additional parameter of non-empty cups int he final configuration. We strengthen algorithm 2A slightly.
Algorithm 2B. Pour all the cups with water into the th cup. Now, say the final configuration includes non-empty cups (other than the
th) whose indices are
and desired amounts of water are
liters. Now, pour the
th cup into the
th cup until it becomes full and then pour the
th cup into the
th cup for all
, in descending order starting from
.
The difference is that this does not waste the interim moves which pour water into cups which are supposed to be empty in the final state.
The first part takes at most steps, and the second part takes at most
steps for a total of
steps. Most importantly, this algorithm can be executed (please don’t forget to check this each time you introduce an algorithm) since the only cups other than the
th cup that will be non-empty after round
are
. Since
, previous rounds of the algorithm do not interfere with later ones.
This time, we have three possibilities.
- If we perform algorithm 1 followed by pouring water from any cup which is empty in the final configuration, into any cup which has less water in it than required, then
steps suffice.
- If we perform algorithm 2A followed by pouring water from any cup which is empty in the final configuration, into any cup which has less water in it than required, then
steps suffice.
- If we perform algorithm 2B then
steps suffices.
It is not hard to see that at least one of these choices requires at most moves, which finishes the proof.
Problem 4 (Iran TST 2026/2)
A trick is defined on a vertex tree as follows. Starting on a tree
, first add an edge which forms a triangle (cycle of length three). Then, a different edge from that cycle is deleted, producing a new tree. The new tree is the result of a trick on
.
Prove that there exists a positive integer
such that for all integers
and any two
vertex trees
and
, we can reach
from
by using at most
tricks.
Again, we scout for our choice of home states. The most natural pair of antipodal states with respect to this operation are the path and
star, simple enough graphs, so we shall use them. What about defining distance?
We can let denote the length of the longest path
of
, and use that to relate to the
path. Think about this as how smoothed out (and hence close to being a path) the graph is.
We can let denote the maximum degree
of
, and use that to relate to the
star. Think about this as how dense (and hence close to being a star) the graph is.
It is clear that tricks are reversible.

Clearly, any vertex which is a neighbour of a vertex in can be added to
using a trick, increasing
by one, and any vertex which is a neighbour of a vertex adjacent to
can be connected to
using a trick, increasing
by one.
By repeatedly applying these two tricks, we can thus transform any tree on
vertices into either the
path using at most
tricks or into the
star using at most
tricks.
Thus, at most
tricks are required to move between and
, by passing through one of the common states.
What remains now is to show that for all sufficiently large , any
vertex tree
must satisfy at least one of
or
. This is a simple exercise in graph theory, and is left as an exercise3.
- This terminology is by no means standard, and you should always explain what you mean by ‘home state’ during an actual contest. ↩︎
- The state graph of such a ‘swapping’ process is commonly referred to as an adjacent-transposition graph. ↩︎
- Hint: think of the tree as rooted. How many generations can we have? How many vertices in each generation? ↩︎









