06.03.2017 Views

Mathematics for Computer Science

e9ck2Ar

e9ck2Ar

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

“mcs” — 2017/3/3 — 11:21 — page 183 — #191<br />

6.4. The Stable Marriage Problem 183<br />

right-hand and the bottom into your left. Then you shuffle the two halves together<br />

so that the cards are perfectly interlaced; that is, the shuffled deck consists of one<br />

card from the left, one from the right, one from the left, one from the right, etc. The<br />

top card in the shuffled deck comes from the right-hand in an outshuffle and from<br />

the left-hand in an inshuffle.<br />

(a) Model this problem as a state machine.<br />

(b) Use the Invariant Principle to prove that you cannot make the entire first half<br />

of the deck black through a sequence of inshuffles and outshuffles.<br />

Note: Discovering a suitable invariant can be difficult! This is the part of a<br />

correctness proof that generally requires some insight, and there is no simple recipe<br />

<strong>for</strong> finding invariants. A standard initial approach is to identify a bunch of reachable<br />

states and then look <strong>for</strong> a pattern—some feature that they all share.<br />

Problem 6.6.<br />

Prove that the fast exponentiation state machine of Section 6.3.1 will halt after<br />

dlog 2 ne C 1 (6.3)<br />

transitions starting from any state where the value of z is n 2 Z C .<br />

Hint: Strong induction.<br />

Problem 6.7.<br />

Nim is a two-person game that starts with some piles of stones. A player’s move<br />

consists of removing one or more stones from a single pile. Players alternate moves,<br />

and the loser is the one who is left with no stones to remove.<br />

It turns out there is a winning strategy <strong>for</strong> one of the players that is easy to carry<br />

out but is not so obvious.<br />

To explain the winning strategy, we need to think of a number in two ways: as<br />

a nonnegative integer and as the bit string equal to the binary representation of the<br />

number—possibly with leading zeroes.<br />

For example, the XOR of numbers r; s; ::: is defined in terms of their binary representations:<br />

combine the corresponding bits of the binary representations of r; s; :::<br />

using XOR, and then interpret the resulting bit-string as a number. For example,<br />

2 XOR 7 XOR 9 D 12

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!