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 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.)
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:
|
Interestingly, the color of the last marble depends upon only M, and not upon N.
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?
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?