Loop Invariant Case Study: Jar of Red/Blue Marbles Game

do while (# of marbles in jar ≠ 1)
   Remove two (randomly-chosen) marbles from the jar;

   if (both are BLUE)
      discard both marbles;
      put a RED one into the jar;

   else if (both are RED)
      discard one of them;
      put the other one back into the jar;

   else  // chosen marbles are of different colors
      put the BLUE one back into the jar;
      discard the RED one;
   fi
od
You are given a jar containing M blue marbles and N red marbles, where M+N > 0. You also have a separate (and inexhaustible) pile of red marbles to the side. (At most ⌊M/2⌋ will be needed.)

You then execute the non-deterministic procedure shown to the right, which, as is clear from its loop guard, terminates when there is a single marble left in the jar.

We have two questions. One is whether this procedure is guaranteed to terminate, regardless of the values of M and N (i.e., the numbers of blue and red marbles, respectively, initially occupying the jar). The second is whether we can know in advance, given the values of M and N, the color of the last marble remaining in the jar. A more mathematical way to express the second question is to ask whether there exists a function f : ℕ×ℕ ⟶ { BLUE, RED } such that, for all M and N, f(M,N) = the color of the last marble remaining when this procedure is applied to a jar initially containing M blue marbles and N red marbles.

Due to the fact that the pair of marbles removed from the jar during each iteration of the loop is chosen randomly, this procedure is non-deterministic, meaning that it is quite conceivable that, if we were to apply it to the same jar of marbles two times, the color of the last marble could be blue one time and red the other time.

Termination: First let's address the question of termination, which is pretty easy to answer. By inspecting the procedure, we see that, during each iteration, the number of marbles in the jar decreases by exactly one. Of course, to make that argument, we must consider each of the three branches of the if-else statement separately. In the first branch, the jar loses two blue marbles but gains one red marble. In both the second and third branches, the jar loses one red marble and the number of blue marbles remains the same.

Given that the number of marbles decreases by exactly one during each iteration, it is clear that, regardless of the values of M and N, eventually the jar will contain exactly one marble, which is the condition needed for the procedure to terminate. (Indeed, it will take exactly M+N−1 iterations to reach that condition.)

Is the color of the last marble determined by M and N? This question is more difficult. To gain some insight into the answer, we could choose particular values for M and N and map out all possible paths that a game could take, starting with a jar containing M blue and N red marbles. If we find a path leading to a blue marble and another one leading to a red marble, then clearly the answer to the question is no. If, on the other hand, all paths lead to the same result, it tells us only that, for the values of M and N that we chose (and all others reachable therefrom), the result is uniquely determined. To the right is shown the transition graph resulting from doing this analysis for a jar that begins with four blue and two red marbles. Notice that all paths lead to the last marble being red.

If we were to run this experiment for several different choices of M and N, each time we would find that the result is uniquely determined. That would lead us to conjecture that, for all values of M and N, the result is uniquely determined. But how could we prove it? We cannot exhaustively run the experiment for all possible values of M and N, of course, as there are infinitely many.

Insight: Loop Invariant
The key to answering the question definitively is to recognize a particular invariant of the loop (i.e., a condition that is true immediately before and immediately after each iteration). This condition pertains to a relationship between the number of blue and red marbles initially in the jar (M and N, respectively) and the number "currently" in the jar. You are encouraged to pause now to think about it....the answer will be revealed below.

THINK!

The relevant invariant condition is that the parity (i.e., oddness/evenness) of the number of blue marbles "currently" in the jar is equal to the parity of M (the number of blue marbles in the jar initially).

This is because, during each iteration, the number of blue marbles either decreases by two (in the first branch) or stays the same (in both the 2nd and 3rd branches). (Of course, you know that any two integers that differ by two have the same parity.)

Observe that if a condition is true immediately after each iteration of a loop, it must be true after the final iteration. Which here means that we can conclude that, when the procedure terminates, the number of blue marbles remaining in the jar will be odd if, and only if, M is odd. Now, the number of marbles remaining in the jar after the last iteration is exactly one. (Indeed, this is precisely the condition under which the loop terminates.) Thus, the number of blue marbles in the jar at that time must be either zero (which is even) or one (which is odd). It follows that it will be one if, and only if, M is odd.

The answer to our question about the existence of function f can thus be answered in the affirmative, and we can describe it as follows:

f(M,N) = { BLUE   if M is odd
RED   if M is even

Interestingly, the color of the last marble depends upon only M, and not upon N.


Exercise: Daisy Petal Game

Imagine a daisy having 16 petals. Two players take alternating moves. A move involves removing either one petal or two adjacent petals from the daisy, at the player's choice. The game is won by whichever player removes the last petal. (Note: By "adjacent" petals, we mean two petals that were adjacent to one another before any petals were removed.)

Question: Does either player have a winning strategy? If so, which player, and what is that strategy?

A winning strategy in a game is a rule that, if followed in taking each move, guarantees that a player will win.

Hint: Think of the game as being modeled by this loop:

while (petals remain on daisy) {
   Player 1 removes one petal or two adjacent petals;
   if (petals remain on daisy) {
      Player 2 removes one petal or two adjacent petals;
   }
}

Try to identify a condition that is true initially and that, if true at the end of each iteration, can be exploited by Player 2 to win. Of course, Player 2 needs to be able to "force" that condition to be true by reacting appropriately to whatever move Player 1 makes. The means by which Player 2 does that is the winning strategy. End of hint.

Generalize the problem from 16 petals to N petals. Does the same winning strategy exist for all possible values of N? For some of them? Which ones?


Exercise: Covering a Chessboard with Dominoes

To the right is an image of a chessboard. Suppose that we had a collection of dominoes, each one having exactly the dimensions of a pair of squares on the chessboard.

Question: Is there a way to place the dominoes on the chessboard so that every square is covered, except for the squares at two opposite corners (e.g., the northeast and southwest corners)?

The answer is no. Think of the process of covering the chessboard as a loop in which, during each iteration, one more domino is placed upon the board, covering two more squares. Identify a condition that is initially true (i.e., before any dominoes are placed onto the board) and that remains true as a result of each iteration, such that that condition is inconsistent with all squares of the board (except for the two chosen corners) being covered.

Hint: What are the colors of the two chosen corner squares?