Quant Out of Water
Group Theory, Symmetry, and a Brain Teaser

Group Theory, Symmetry, and a Brain Teaser

I review some elementary discrete math concepts, use them to describe and solve a neat brain-teaser, and generalize the solution into a form of robust breadth first search.

Introduction

Recently, a friend introduced me to the following brain teaser:

Problem Statement: Suppose you are kidnapped, blindfolded, and placed into a room. Your kidnappers hand you a plate with four coins on it, arranged in a 2×22 \times 2 square. They will let you leave if you can flip all of the coins so that they are face up, in a finite amount of time (they ain’t got all day!). Since you can’t see, you can ask the interrogator if you’ve successfully placed all the coins heads up (if you’d like to increase the immersion, you can imagine you are actually blind, otherwise you could just take off the blindfold). The interrogator won’t lie to you, but will try to mess with you: any time the answer is “no”, they will rotate the plate before giving it back to you. The amount by which it is rotated is arbitrary – it could be random, follow a fixed pattern, or be adversarial.

The first things coming to mind with this puzzle is that you could just iterate through every possible sequence of flips, asking the interrogator whether they’re all heads up after each flip. This possibility is immediately shut down by their adversarial rotations – the interrogator can easily foil this plan.

Another possibility is through random flips. Randomization is an effective tool because the interrogator cannot determine your next move by finding a pattern in your behaviour (if you randomize effectively, even you don’t know your next move!). If you simply pick up all the coins at random, flip them randomly, and then ask if you’ve solved the puzzle, it is guaranteed (by the Borell-Cantelli Lemma ) that you will eventually stumble upon the solution. However, this is where the caveat about finite time comes in. In probability theory, it is sometimes a point of confusion what is actually meant by a random variable being finite, so let’s spell it out. If N(ω)N(\omega) is the number of flips taken, given a state of the universe ω\omega, the Borell-Cantelli Lemma tells us that N(ω)<N(\omega) < \infty almost surely, so NN is bounded, but it is not uniformly bounded (over all possible events ω\omega). What we want is that there is some real number Nmax<N_{\text{max}} < \infty such that N(ω)NmaxN(\omega) \le N_{\text{max}} almost-surely, and this cannot be done by the random flipping strategy.

That all being said, the solution I arrived upon does not include any randomization. Before explaining the solution, I’m going to take a detour through the ideas that came to my mind, and some of the formalisms that can be used to understand the puzzle, its solution, and potential generalization.

A Mathematical Preamble

The purpose of this section is to review some mathematical formalism and terminology used in reasoning about the coins-on-a-plate puzzle. I have a limited background in discrete math or abstract algebra, so even though these concepts are rather elementary, it has been good for me to write things down. Indeed, reviewing elementary concepts facilitates Chunking (an essential part of learning ). If all one is interested in is the solution to the puzzle, or if they are already well familiar with these concepts, they can skip to Solving the Puzzle .

Equivalence Classes

In mathematics, an equivalence relation \sim on a set XX is a binary relation satisfying the following properties:

  1. Reflexivity: xX xx\forall x \in X\ x \sim x
  2. Symmetry: x,yX xy    yx\forall x, y \in X\ x \sim y \iff y \sim x
  3. Transitivity: x,y,zX xy,yz    xz.∀ x, y, z ∈ X\ x ∼ y, y ∼ z \implies x ∼ z.

The most famous equivalence relation is “ordinary” equality: ‘==’, but there are various more exotic equivalence relations: equivalence up to rotations in geometry, equivalence almost surely in probability, equivalence except on measure-zero sets in the theory of LpL_p spaces, etc.

When dealing with a finite number of objects (finiteness is not necessary in some/many of the cases I talk about, but I keep the assumption to avoid accidentally writing something that is technically wrong for infinite sets), equivalence classes easily arise from partitions of the set. That is, if XX is a finite set, and X=i=1MXiX = \bigcup_{i = 1}^M X_i for subsets XiXX_i \subseteq X which are all disjoint ij    XiXj=i \ne j \implies X_i \cap X_j = \emptyset (i.e., the XiX_i are a partition XX) we can define an equivalence relation on XX by xy    (i:xXiyXi),x \sim y \iff (\exists i: x \in X_i \wedge y \in X_i), where \wedge indicates “and”. Let us quickly check that this is a bona-fide equivalence relation: Let xXx \in X. There is an XiX_i in the partition such that xXix \in X_i and hence “also” xXix \in X_i verifying it is reflexive. That it is symmetric arises since (xXiyXi)    (yXixXi)(x \in X_i \wedge y \in X_i) \iff (y \in X_i \wedge x \in X_i). Finally, it is transitive since the XiX_i are a partition without any overlap: if x,yx, y are in the same set XiX_i and y,zy, z are both in another set XjX_j, it must be that Xi=XjX_i = X_j (and hence x,zXix, z \in X_i) since yy must be in exactly one of the XiX_i. Conversely, the reader can check that any set partition also induces an equivalence relation. Thus, set partitions and equivalence relations are equivalent.

Given an equivalence relation \sim on a set XX, the corresponding partition (i.e., the partition induced by \sim) is commonly denoted as X/X / \sim as the quotient set. We then say that elements of a set in the partition (that is, elements xXix \in X_i for some XiX/X_i \in X / \sim, which is a subset XiXX_i \subseteq X) are equivalent up to \sim.

Functions, Permutations, and Groups

Any finite set with NN elements can be represented simply as the set of integers from 11 to NN as in X={1,2,,N}X = \{1, 2, \ldots, N\} (a set which is commonly denoted [N][N]). It does not matter what the elements of the set actually are, you can simply refer to the first, second, etc., in an arbitrary order.

Now, what are the functions on finite sets? Well, a function f:XXf: X \rightarrow X is just a mapping from some element of xXx \in X (without loss, an integer between 11 and NN) to another element f(x)Xf(x) \in X. If the function is a one-to-one function (i.e., it is injective) whose domain is all of XX, then it must also (why?) be an onto function (i.e., a surjection) and thus it is a bijection and in this context such functions are called permutations . These are nothing but re-orderings of the integers, usually denoted by σ\sigma (or other Greek letter, instead of the more generic ff) as in “σ\sigma re-orders the sequence (1,2,,N)(1, 2, \ldots, N) into (σ(1),σ(2),,σ(N))(\sigma(1), \sigma(2), \ldots, \sigma(N))”.

Since permutations are bijections, they have an inverse. Moreover, the composition operation amongst permutations is associative (why?): for permutations σ,π,τ\sigma, \pi, \tau on XX, it is the case that (σπ)τ=σ(πτ)(\sigma \circ \pi) \circ \tau = \sigma \circ (\pi \circ \tau), where, e.g., (σπ)(x)=σ(π(x))(\sigma \circ \pi)(x) = \sigma(\pi(x)). This means that (with the trivial “doing nothing” permutation Id(x)=xId(x) = x serving as the identity) the set of all permutations of XX, call it GX={σ:XX  σ is a bijection},G_X = \{\sigma: X \rightarrow X\ | \ \sigma \text{ is a bijection}\}, along with the composition operation constitutes a (finite) Group (GX,)(G_X, \circ). This particular group GXG_X of all bijections is particularly special, and is referred to as the Symmetric Group SNS_N on NN elements – it is worthwhile noting that the size of this group grows rather quickly: there are N!N! bijections in SNS_N.

Cycles and Decomposition

Given a permutation σSN\sigma \in S_N, an rr-cycle is a sequence of r1r \ge 1 numbers i1,,iri_1, \ldots, i_r such that ik+1=σ(ik)i_{k + 1} = \sigma(i_k) and ir=i1i_r = i_1. Given an rr-cycle of σ\sigma, we can collect each i1,,iri_1, \ldots, i_r into a set C[N]C \subseteq [N] called an orbit and restrict σ\sigma to this cycle so that σC\sigma_C is equal to σ\sigma in CC, and the identity outside of CC as in xC: σC(x)=σ(x)\forall x \in C:\ \sigma_C(x) = \sigma(x) and xCc: σC(x)=x\forall x \in C^{\mathsf{c}}:\ \sigma_C(x) = x. Remarkably, permutations can be decomposed into a composition of their cycles:

Theorem (Cyclic Decomposition): Let σSN\sigma \in S_N. Then, there exists a partition C1,,CmC_1, \ldots, C_m of [N][N] such that σ=σC1σCm.\sigma = \sigma_{C_1} \circ \cdots \circ \sigma_{C_m}. Moreover, each CiC_i is an orbit of a cycle of σ\sigma, and for any cycle of σ\sigma there is a corresponding orbit CiC_i.

proof (sketch): Since σ\sigma acts on a finite set, there exists at least one cycle, call its orbit C1C_1. We have the partition [N]=C1cC1[N] = C_1^{\mathsf{c}} \cup C_1 and the decomposition σ=σC1cσC1\sigma = \sigma_{C_1^{\mathsf{c}}} \circ \sigma_{C_1} since σC1(x)=x\sigma_{C_1}(x) = x on C1cC_1^{\mathsf{c}}. Now, C1C_1 contains at least one element, and σC1C\sigma_{C_1^{\mathsf{C}}} is another permutation when restricted to C1cC_1^{\mathsf{c}}, so the theorem follows by induction. \square

From the partitioning of [N][N] by the orbits of a permutation, we see that any permutation also induces a collection of equivalence classes (exactly the orbits of the permutation)! Within each of these equivalence classes, we can say that the elements are identical up to the operation of σ\sigma. As well, we say that any set CC such that xC    σ(x)Cx \in C \implies \sigma(x) \in C is invariant to σ\sigma – clearly, the partitions induced by σ\sigma are all invariant sets. These formalisms can be used to understand the structure of the coins-on-a-plate puzzle.

Solving the Puzzle

The main idea for solving the puzzle revolves around identifying particular partitions of the set of all states of the game, thus identifying equivalence relations. I then define some natural operations that one can apply to the coins on the plate and reason about how those operations map elements of one equivalence class into another. It turns out that we can apply these operations in a particular order such that we are guaranteed to eventually reach the solution.

Representation and Rotations

Abstractly, we can represent the state of each coin as either being heads HH or tails TT. Since the coins are arranged in a square, we can think of the state of the whole puzzle as being the sequence of states of each individual coin following a left-to-right and top-to-bottom ordering. Notice that we’ve already encountered an equivalence class: we naturally think of aligning the coins to look as though they are on a “right-angle” when facing us – following this convention, a whole set of angles of the plate spanning 9090^\circ (4545^\circ in either direction) all represent equivalent configurations. If the interrogator rotates the plate by, say, 3737^\circ before handing it back to us, we should be able to feel around the coins to rotate this back to 00^\circ. Thus, when we speak of an arrangement of coins on the plate, we are already really speaking of a whole equivalence class, all of which are equivalent up to rotation back to the nearest right-angle alignment of the coins. The upshot here is that listing the coins in this order results in a finite representation of the set of all possible states (of which there are 24=162^4 = 16) as elements of the set S={HHHH,THHH,HTHH,,TTTH,TTTT},S = \Bigl\{{HH \atop HH}, {TH \atop HH}, {HT \atop HH}, \ldots, {TT \atop TH}, {TT \atop TT}\Bigr\}, which is naturally isomorphic (i.e., equivalent) to the set [16]={1,,16}[16] = \{1, \ldots, 16\}.

Any such rotation operation can now naturally be thought of as a permutation rotaten\texttt{rotate}^n, where nn indicates the number of 9090^\circ angles the plate is clockwise rotated. For example, rotate(HTHH)=HHHT\texttt{rotate} \Bigl({HT \atop HH}\Bigr) = {HH \atop HT} and rotate2(HTHH)=HHTH\texttt{rotate}^2 \Bigl({HT \atop HH}\Bigr) = {HH \atop TH}, etc. The power notation with nn is naturally suggestive since nn rotations by 9090^\circ is equivalent to applying the single rotate\texttt{rotate} permutation nn times. Since we don’t know how many times the interrogator will apply rotate\texttt{rotate} before giving us the plate back, we need to work with equivalence classes of states which are equivalent up to the action of this permutation.

Remark: It is an exercise for the reader to enumerate all of the equivalence classes induced by rotate\texttt{rotate}. I should note at this point though that I stumbled upon the appropriate sets and operations for solving the puzzle through intuition, and only went backwards afterwards to add some formalism. In my experience, this is typical of mathematical problem solving and research – you begin by understanding and solving particular problems and special cases ad-hoc through intuition, and then go back to formalize the ideas, which opens up new ideas and generalizations, which you begin by solving ad-hoc, and then later formalize, which leads to more questions…

Solved States

The next key observation involves another permutation and collection of equivalence classes. Consider a flip_all\texttt{flip\_all} operation – you can apply this operator to the coins on the plate by flipping every coin onto its other face. Since this flip_all\texttt{flip\_all} operation is just a permutation, it decomposes the space SS into equivalence classes (whose elements are all equivalent up to the operation of flip_all\texttt{flip\_all}). Examples include {HTHT,THTH},{HTTH,THHT}\Bigl\{ {HT \atop HT}, {TH \atop TH} \Bigr\}, \Bigl\{ {HT \atop TH}, {TH \atop HT} \Bigr\}, etc, which are all orbits containing just two elements. Another important example is S={HHHH,TTTT},S_\star = \Bigl\{{HH \atop HH}, {TT \atop TT}\Bigr\}, which is the equivalence class (of flip_all\texttt{flip\_all}) containing the solved state HHHH{HH \atop HH}. The importance of this class arises as follows.

Consider the following query, which I’ll refer to as solved?(s)\texttt{solved?}(s): Ask the interrogator if the puzzle in state ss is solved. If it is, you’re done. If not, the plate is returned to you after application of some number of rotate\texttt{rotate} permutations. Now, consider the composite query (solved?(s)\texttt{solved?}(s) is not formally a permutation, so I use the word “query”): solved?flip_all\texttt{solved?} \circ \texttt{flip\_all}, which in words describes: first flip all the coins onto their other side and then ask the interrogator if the puzzle is solved. Therefore, since rotate\texttt{rotate} does not change the number of heads or tails in the configuration (i.e., both elements of SS_\star are fixed-points of rotate\texttt{rotate}) if you reach a state sSs \in S_\star then you can first query solved?\texttt{solved?} and if the plate is returned to you (which means you must have asked about the configuration TTTT{TT \atop TT}) then you know that the state ss has not changed and you can apply the composite query solved?flip_all\texttt{solved?} \circ \texttt{flip\_all} which is now guaranteed to result in successfully solving the puzzle.

Thus, if you do this every time you want to know if the puzzle is solved, then you have effectively solved the puzzle anytime all of the coins are all face up, or all face down – if they are face down, then they will be face up the second time you ask. Thus, the two states in SS_\starcoins all face up” and “coins all face down” form an equivalence class of “solved states” according to the composite query described above – I’ll call this composite query solved?\texttt{solved?}^\star. We can now frame our problem as reaching any state in SS_\star.

Three More Equivalence Classes

Consider the following three sets: S1={HHHT,HHTH,HTHH,THHH,TTTH,TTHT,THTT,HTTT},S_1 = \Bigl\{{HH \atop HT}, {HH \atop TH}, {HT \atop HH}, {TH \atop HH}, {TT \atop TH}, {TT \atop HT}, {TH \atop TT}, {HT \atop TT}\Bigr\}, which has either a single head or a single tail; S2(d)={HTTH,THHT},S_2^{(d)} = \Bigl\{{HT \atop TH}, {TH \atop HT}\Bigr\}, with two heads and two tails arranged diagonally from one and other; and S2(s)={HHTT,HTHT,TTHH,THTH},S_2^{(s)} = \Bigl\{{HH \atop TT}, {HT \atop HT}, {TT \atop HH}, {TH \atop TH}\Bigr\}, consisting again of two heads and two tails, but now sitting side-by-side. Critically, the sets we have so far defined form a partition of the space: S=SS1S2(s)S2(d),S = S_\star \cup S_1 \cup S_2^{(s)} \cup S_2^{(d)}, and thus more equivalence relations given by membership in these sets. Moreover, each of these sets is invariant to both permutations rotate\texttt{rotate} and flip_all\texttt{flip\_all}. Indeed, S2(d)S_2^{(d)} is exactly one of the equivalence classes induced by both, S2(s)S_2^{(s)} is an equivalence class induced by rotate\texttt{rotate} and is the union of two equivalence classes of flip_all\texttt{flip\_all}, and S1S_1 is the union of two equivalence classes of rotate\texttt{rotate} along with the corresponding ones of flip_all\texttt{flip\_all}. Since the elements of the sets are all equivalent to one and other (in the sense of being members of a partition, and in the sense of rotational symmetry) we can treat all members of the class simultaneously. These sets, along with three more operations, will allow us to formalize a solution to the puzzle.

Due to the invariance of these sets to the rotate\texttt{rotate} permutation, the action of the rotation introduced by the interrogator when you query solved?\texttt{solved?} does not have any affect on the membership of ss in one of these sets. This is, of course, not true of any arbitrary subset of SS, and hence the choice of sets above is in some sense “cunning” (and now we see, well motivated by the consideration of equivalence classes!). Indeed, rotations applied to S~={HHTT,TTHH}\tilde{S} = \{{HH \atop TT}, {TT \atop HH}\} could produce something in S~\tilde{S}, but it could also produce something in {HTHT,THTH}\{{HT \atop HT}, {TH \atop TH}\}!

More Permutations on SS

The additional permutations we need to solve the puzzle are: flip_one\texttt{flip\_one}, which flips over an arbitrary single coin; flip_side\texttt{flip\_side}, which flips over two side-by-side coins; and flip_diag\texttt{flip\_diag}, which flips over arbitrary cross-diagonal coins. These functions are not uniquely defined (e.g., which coin do you flip over for flip_one\texttt{flip\_one}) but the choice of which particular permutation you use does not matter, so I’m glossing over this.

We can reason through the effects of how these map between our equivalence classes as follows (I skip the consideration of how SS_\star is affected since we will always query solved?\texttt{solved?}^\star between each operation, so if we hit SS_\star we are done):

flip_one:S1S2(d)S2(s)Sflip_one:S2(d)S2(s)S1,\begin{equation} \begin{aligned} \texttt{flip\_one}: S_1 \rightarrow S_2^{(d)} \cup S_2^{(s)} \cup S_\star\\ \texttt{flip\_one}: S_2^{(d)} \cup S_2^{(s)} \rightarrow S_1, \end{aligned} \end{equation}

since if sS1s \in S_1 and we flip over a single coin, then we must now have either two heads and two tails, or we happened to flip over the “right” coin and reach a solved state. Alternatively, if we begin with two HHs and two TTs, then flipping one coin over necessarily puts us back in S1S_1 with either three HHs and one TT, or three TTs and one HH. For the side-by-side flip operator:

flip_side:S1S1flip_side:S2(d)S2(s)flip_side:S2(s)SS2(d),\begin{equation} \begin{aligned} \texttt{flip\_side}: S_1 \rightarrow S_1\\ \texttt{flip\_side}: S_2^{(d)} \rightarrow S_2^{(s)}\\ \texttt{flip\_side}: S_2^{(s)} \rightarrow S_\star \cup S_2^{(d)}, \end{aligned} \end{equation}

since if there is one head (or one tail), application of flip_side\texttt{flip\_side} must necessarily result in three heads or tails; if there are heads diagonal from one and other (i.e., sS2(d)s \in S_2^{(d)}) then flip_side\texttt{flip\_side} must necessarily flip one head and one tail, and thus result in side-by-side heads and tails; and if sS2(s)s \in S_2^{(s)} then we either get lucky and flip the “right” coins into a solved state, or we flip the “wrong” coins into a diagonal state S2(d)S_2^{(d)}. Finally, for the diagonal flipping operator:

flip_diag:S1S1flip_diag:S2(d)Sflip_diag:S2(s)S2(s),\begin{equation} \begin{aligned} \texttt{flip\_diag}: S_1 \rightarrow S_1\\ \texttt{flip\_diag}: S_2^{(d)} \rightarrow S_\star\\ \texttt{flip\_diag}: S_2^{(s)} \rightarrow S_2^{(s)}, \end{aligned} \end{equation}

by similar reasoning. This is the key to the puzzle. If we are faced with some sS2(d)s \in S_2^{(d)}, then application of flip_diag\texttt{flip\_diag} is guaranteed to place us into the solved state SS_\star. The only difficulty now is that we cannot know whether or not sS2(d)s \in S_2^{(d)}! Regardless, this feels like progress.

Transition Diagramming

The simple enumeration of the action of the operators flip_one,flip_side,flip_diag\texttt{flip\_one}, \texttt{flip\_side}, \texttt{flip\_diag} on the sets S1,S2(s),S2(d)S_1, S_2^{(s)}, S_2^{(d)} described in the previous section can be difficult to visualize. Our intuition is that we should try to find some appropriate order in which to apply these operations which will guarantee that we eventually reach the state SS_\star, but how? We can make progress towards this task by drawing a diagram of all the possible transitions:

Figure 1: Transition Diagram for each Function and Set

Figure 1: Transition Diagram for each Function and Set

One thing we can notice from this diagram is that some operations map from a set and back into itself, i.e., some of the sets described above are also invariant to flip_diag\texttt{flip\_diag} or flip_side\texttt{flip\_side}. For example, flip_diag\texttt{flip\_diag} maps from S2(s)S_2^{(s)} back into itself, as well as from S1S_1 and back into itself. Recall that there is also an implicit self-loop corresponding to the rotate\texttt{rotate} permutation for each set, i.e., we can safely ask if the puzzle is solved (following the logic described in Solved States ) without the interrogator being able to effect the state we are in.

A Solution

Using the diagram above, we can deduce a solution to the puzzle. Start in any arbitrary state sSs \in S: first, make the solved?\texttt{solved?}^\star query, if sSs \in S_\star then we are done (that was easy!), otherwise, we are in a state sSS=S1S2(d)S2(s).s \in S \setminus S_\star = S_1 \cup S_2^{(d)} \cup S_2^{(s)}. Apply the operator flip_diag\texttt{flip\_diag} and then query solved?\texttt{solved?}^\star again. If we happened to be in S2(d)S_2^{(d)}, then the puzzle is solved, otherwise (notice the self-loops) we must be in sS2(s)S1s \in S_2^{(s)} \cup S_1. Now, apply flip_side\texttt{flip\_side}, followed by solved?\texttt{solved?}^\star, flip_diag\texttt{flip\_diag}, and solved?\texttt{solved?}^\star again. If we were in S2(s)S_2^{(s)}, then flip_side\texttt{flip\_side} transitioned us into S2(d)S_2^{(d)}, and flip_diag\texttt{flip\_diag} brought us into SS_\star. If we were in S1S_1, then we are still in S1S_1, since this set is invariant to both flip_diag\texttt{flip\_diag} and flip_side\texttt{flip\_side}. To solve the puzzle from here, we apply flip_one\texttt{flip\_one} and query solved?\texttt{solved?}^\star. If that query failed, then we must be in S2(d)S2(s)S_2^{(d)} \cup S_2^{(s)} in which case we can apply flip_diag\texttt{flip\_diag} and query solved?\texttt{solved}?^\star, which will finish the puzzle if we were in S2(d)S_2^{(d)}. Otherwise, we are in S2(s)S_2^{(s)} and applying flip_side\texttt{flip\_side} followed by solved?\texttt{solved?}^\star and then again flip_diag\texttt{flip\_diag} which is finally guaranteed to place us into SS_\star, wherein a final solved?\texttt{solved?}^\star query is the end.

We have solved the puzzle – regardless of our starting position, and regardless of how random or adversarial are the rotations to the plate of coins done by the interrogator (they can even know our exact plan!), the above policy will eventually move us into the solved state SS_\star. To summarize the policy, we apply the following order of operations, querying solved?\texttt{solved?}^\star between each operator: flip_diag,flip_side,flip_diag,flip_one,flip_diag,flip_side,flip_diag\texttt{flip\_diag}, \texttt{flip\_side}, \texttt{flip\_diag}, \texttt{flip\_one}, \texttt{flip\_diag}, \texttt{flip\_side}, \texttt{flip\_diag}.

Computational Generalizations

Solving this particular brain-teaser is satisfying, but a solution is not a good solution to me unless it is illustrative or indicative of a more general pattern. Indeed, I wrote out the formalism above exactly for the purpose of trying to reason through a more general idea. To introduce the idea, suppose we have NN states V=[N]=Δ{1,2,,N}V = [N] \overset{\Delta}{=} \{1, 2, \ldots, N\} and KK functions F={fk:VV  k[K]}\mathcal{F} = \{f_k: V \rightarrow V\ |\ k \in [K]\} on VV. Given this data, we can define a graph G=(V,E)\mathcal{G} = (V, \mathcal{E}) where the set of vertices is naturally VV and the edges are tuples E{(i,j,f)  i,jV,fF}\mathcal{E} \subseteq \{(i, j, f)\ |\ i, j \in V, f \in \mathcal{F}\} where the edges (i,j,f)E(i, j, f) \in \mathcal{E} is present in the graph if there is a function fFf \in \mathcal{F} which maps from node ii to node jj. Each node will have KK edges emanating from it – one for each function. Let’s suppose as well that there is a fixed target state sSs^\star \in S and that this state is invariant to every function application fk(s)=sf_k(s^\star) = s^\star, i.e., when we reach the target state, we know it. This graph is analogous to the graph I drew for the coin problem.

Now, the interesting part of this comes in when we suppose we do not know what state we are in. The new question is: can we find a search algorithm which will find a sequence of functions leading to a goal state, but which does not require knowledge of what vertex it is currently visiting? Abstractly, given a target state ss^\star, can we find a finite (ideally minimal) sequence of functions f1,f2,,fMFf_1, f_2, \ldots, f_M \in \mathcal{F} which when applied from an arbitrary starting point, are guaranteed to eventually reach the target state ss^\star?

There may exist a better algorithm, but this problem can be solved by means of breadth first search. However, we cannot just apply BFS directly upon the graph G\mathcal{G}, since we don’t know our starting point. Instead, we’ll apply BFS on a graph over the power set 2V2^V of VV (i.e., the set of all subsets of VV). We generalize the application of fFf \in \mathcal{F} to act on 2V2^V by the definition WV: f(W)={f(w)  wW}, W\subseteq V:\ f(W) = \{f(w)\ |\ w \in W\}, i.e., we apply ff to every element of the subset. This gives us an edge relation for a graph G=(2V,E)\mathcal{G}^\star = (2^V, \mathcal{E}^\star): we have (U,W)E    (fF: f(U)=W).(U, W) \in \mathcal{E}^\star \iff \bigl(\exists f \in \mathcal{F}:\ f(U) = W\bigr). The goal is to find a path through this “power-graph” leading to the state S={s}S_\star = \{s^\star\}, the singleton set containing only the goal state.

A completely hacked-together implementation of this idea for the coins-on-a-plate problem is given here:

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import typing as ty

from functools import reduce
from collections import deque

# Some nicer names for the nodes in the graph
Node = str
S1: Node = "S1"
S2d: Node = "S2d"
S2s: Node = "S2s"
S_star: Node = "S_star"

TransitionFunction = ty.Callable[[Node], ty.Set[Node]]

# To easily keep track of what has been visited
def _hash(s: ty.Set[Node]):
    return hash("".join(str(hash(n)) for n in s))

# Computes the set of next nodes obtained by applying `op` from some uncertainty set
def _next_nodes(nodes: ty.Set[Node], op: TransitionFunction):
    return reduce(lambda acc, new_nodes: acc | new_nodes, (op(w) for w in nodes), set())

# The robust implementation of BFS
def uncertain_bfs(nodes: ty.Set[Node], ops: ty.Dict[str, TransitionFunction]):
    visited = {_hash(nodes)}
    queue = deque([(("START",), nodes)])
    while queue:
        path, nodes = queue.pop()
        for name, op in ops.items():
            new_nodes = _next_nodes(nodes, op)
            if (hsh := _hash(new_nodes)) not in visited:
                visited.add(hsh)
                queue.append(((*path, name), new_nodes))
            if new_nodes == {S_star}:
                print(f"SUCCESS: {(*path, name)}")
                return
    print("FAILURE!")

# Helper to make transition functions with S_star being an absorbing state
def _make_op(mapping: ty.Dict[Node, Node]) -> TransitionFunction:
    def op(v: Node) -> ty.Set[Node]:
        if v == S_star:
            return {S_star}
        return mapping[v]
    return op

# The operations on the coins
flip_diag = _make_op({S1: {S1}, S2d: {S_star}, S2s: {S2s}})
flip_side = _make_op({S1: {S1}, S2d: {S2s}, S2s: {S2d, S_star}})
flip_one = _make_op({S1: {S2d, S_star, S2s}, S2d: {S1}, S2s: {S1}})

# The possible starting positions
nodes = {S1, S2d, S2s, S_star}

# And names for the transition functions
transition_functions = {"flip_diag": flip_diag, "flip_one": flip_one, "flip_side": flip_side}

# Which we finally run the algorithm on
uncertain_bfs(nodes, transition_functions)

SUCCESS: (‘START’, ‘flip_diag’, ‘flip_side’, ‘flip_diag’, ‘flip_one’, ‘flip_diag’, ‘flip_side’, ‘flip_diag’)\texttt{SUCCESS: (‘START’, ‘flip\_diag’, ‘flip\_side’, ‘flip\_diag’, ‘flip\_one’, ‘flip\_diag’, ‘flip\_side’, ‘flip\_diag’)}

And indeed, this code outputs the same policy I came up with ad-hoc in the previous section.

Conclusion

I thought that the puzzle described at the beginning of this article was very neat and wanted to write down a complete solution. We saw how the idea of symmetries were crucial for solving the puzzle, and that these symmetries can be described through the language of equivalence classes. Moreover, the remarkable cyclic decomposition theorem for permutations (nothing but bijections on finite sets) tell us that we can partition all the possible states of the coins on the plate into a collection of cycles and equivalence classes. Some of these sets are invariant to certain operations on the coins, and therefore create self-loops in a graph describing how the state of the puzzle evolves. Diagramming this graph allowed us to write down an ad-hoc solution to the coins-on-a-plate puzzle.

The idea of searching in a graph when you don’t know your starting point generalized to a form of robust breadth-first search. In this algorithm, we keep track of an entire uncertainty set of nodes that we can plausibly be, and stop once that set contains nothing but the goal state. I have few ideas for what such an algorithm might be practically useful for, but there it is!