Note: Since writing this, I've found a rather complete account of the same trick (with history!) at: http://blue.butler.edu/~phenders/sigcse2005/niftyworkshop/Handouts/cardTrick.pdf. Also, I implemented most pieces of the magic trick described below in card_trick.py (Python).

Note: Since first running this in class, a few students have pointed out a better (i.e., more human-friendly) way to communicate the secret card. It's a spoiler; so, see below or just read on.

A Discrete Mathematics Magic Trick

Shuffle a deck of cards and deal five of them out to MOE (Magician OnE). Moe sets one of the cards aside, puts the remaining four into a neat pile, and hands them off to MAgGIe (MAGIcian two). After investigating them for a few seconds, Maggie announces the identity of Moe's hidden card, to the audience's eternal delight. (The audience, being computer scientists, are easily amused.)

How did Maggie and Moe do it? Does their trick always work? How big a deck does their trick work for? Is there some other strategy that works for bigger decks? What's the largest deck that any strategy can work for?

This page is designed to walk you through questions like these using the concepts of discrete mathematics. The underlying goal is to show you how a simple problem can become a delightful playground for theoretical investigation, and how the discrete mathematics concepts you've been learning can help support your curiosity as you explore these problems.

A Note to Students and Instructors

This page describes a detailed progression in the exploration of a magic trick using discrete mathematics concepts, including permutations, combinations, the pigeonhole principle, functions, bijections, and bipartite graphs.

Although not all of these concepts will be mentioned directly, and a student might understand this page without having learned all of these concepts, the student who has seen each concept will be better prepared for the exploration below, and the exploration will in turn reinforce these concepts for the student.

As you read, play with the concepts and ideas expressed. Try to answer the questions before reading our answers. Relish differences in approach or results that you discover!

The Basic Trick

Setting up the problem

Moe gets five cards at random and must find a way to select a secret card and order the remaining four cards so that Maggie can always identify the secret card. To figure out the trick, let's start by figuring out some of the constraints on the trick.

How many different cards could be the secret card?

Maggie sees the four cards Moe hands her; so, the secret card cannot be any of them. There are 52 cards in the deck. That leaves 52 - 4 = 48 cards that could be the secret card.

Moe can provide information to Maggie just by ordering the cards. How many different ways can Moe order the four cards?

To start with, let's pick some way to order cards. How about we assume suits are ordered alphabetically so that Spades > Hearts > Diamonds > Clubs. Then, one card can be greater than another card if the first card's rank is greater or if the ranks are the same but the first card's suit is greater. We'll let Aces be low cards; so, the smallest cards in order are: AC, AD, AH, AS, 2C, 2D, 2H, 2S, 3C, 3D, ...

Now, Maggie can think of the four cards she sees as the "lowest, 2nd lowest, 3rd lowest, and 4th lowest" cards (or "1, 2, 3, and 4"), regardless of what the actual values of the cards are. Our previous question reduces to "How many different ways can Moe order the numbers 1, 2, 3, and 4?"

That's just a question of permutations. How many different permutations are there of four objects? Well, there are 4 objects that could be chosen first, and then 3 left to be chosen second, 2 left to be chosen third, and only 1 to be chosen last: 4 * 3 * 2 * 1 = 4! = 24.

Finally, Moe chooses the secret card from among five possibilities. Clearly, he can pass some information by this choice, but how that works is not clear.. yet.

How can Maggie and Moe use orderings to pass information?

Using just the ordering of the 4 cards Moe passes to Maggie, Maggie can distinguish among 24 options. To make the trick work, however, both participants need a clear way to translate between orderings and numbers between 1 and 24. That amounts to is picking an ordering of the arrangements of the numbers 1, 2, 3, and 4 (just like we picked an ordering of the 52 cards above). One natural ordering is lexicographic order. For example, 2134 comes before 2314 (since the second character of the first arrangement '1' comes before the second character of the second arrangement '3').

How can Maggie and Moe perform the translation? Well, say Moe wants to pick an arrangement representing 17. He first decides which of the four cards to arrange in the first slot. There are four possibilities, and for each possibility there are 3! = 6 arrangement of the remaining cards. So, the arrangements starting with card #1 are arrangements #1–6. The arrangements starting with #2 are arrangements #7–12, those starting with #3 are #13–18, and those starting with #4 are #19–24.

Moe wants to express 17; so, he'll pick card #3 (the third lowest card) to be first in the arrangement. There are three possible cards to be next in the arrangement; for each one, there are two arrangements of the remaining two cards. So, card #1 is the next card in arrangements #13 and 14, card #2 is for arrangements #15 and 16, and card #4 is for arrangements #17 and 18. Moe picks card #4 as the second card. Finally, putting the last cards in order finishes arrangement #17, while reversing them finishes arrangement #18. So, Moe puts card #1 next and card #2 last.

Moe expresses 17 using the arrangement 3412. As a side note, notice that once Moe picks the first card, arranging the rest of the cards is actually a recursive step: arrange three cards (which can be renumbered 1, 2, and 3) to represent a number between 1 and 6.

In short, to represent a number, find which group of 6 it falls in to select the first card. Then, use the group of 2 within those 6 the number falls in to select the second card. Then, use which of the remaining 2 is the number to select the order of the remaining two cards.

Given an arrangement, Maggie does the reverse of Moe's steps to find the number it represents. So, given the arrangement 2431, Maggie notes that the arrangement is in the second group of 6: 7–12. She then notes that the arrangement is in the last group of 2 within those 6: 11 and 12. Finally, the last two cards are out of order; so, the arrangement is the larger of the two: #12.

Of course, this is just one of many possible ways to translate between arrangements and numbers. In fact, any bijection (one-to-one, invertible function) between arrangements and the numbers 24 works. However, with some practice, humans can calculate this particular bijection in their heads without too much trouble.

Where do Maggie and Moe get their extra bit?

With a total of 48 cards that can be the secret card and 24 orderings of the 4 non-secret cards, Maggie's only a factor of 2 away from a successful trick. How will we narrow down the options that one bit more?

Take some time to think about how to solve this. (Avoid the temptation to encode that extra bit in the physical placement of the cards! "If those first two cards are overlapping, that's one set of 24; if they don't touch, that's a different set of 24.") In the process, you might discover some interesting properties of the selection of cards Moe and Maggie can see. For example, can all of Moe's cards be the same rank?

There are actually many strategies that solve this problem. The variety of strategies is an interesting point that we'll return to later. For now, let's develop one particular strategy that is fairly "clean", meaning a human can do the trick quickly without a cheat sheet!

The extra information Moe passes to Maggie must come from Moe's choice of the secret card. Moe gets to choose among five possibilities when he selects the secret card. (That's 5C1 or equivalently 5C4 possibilities in combinatoric terms.) So far, we've ignored that choice. Let's consider it now. How can Moe's choice of the secret card pass sufficient information to Maggie for her to complete the trick?

It's tempting, with just one bit to go, to divide the cards in half into red cards (hearts and diamonds) and black cards (spades and clubs). Unfortunately, this doesn't work out well. Can Maggie always assume that the secret card is black? No, because all five original cards could have been red. Can Maggie assume that the colour of the hidden card will be a kind of parity bit for the other colours: red if an even number of the other cards are black and black if an odd number of the other cards are black? No, because all the original cards could be black.

As a side note, the division of cards into red and black cards is just one of many ways to partition the cards evenly into two sets. As with our ordering of the cards and our bijection between the numbers 1–24 and arrangements of four cards above, any such division is arbitrary from an algorithmic perspective. From a human perspective, however, it's easier to consider the set of red cards and black cards rather than sets like "all red cards except the 7H, 4D, and AH but including the 8C, 9C, and KC". Sometimes we want to devise strategies that make sense to humans, and sometimes we want to prove the existence or absence of any strategy, regardless of its complexity.

Back to Moe's problem. How should he choose the secret card? Well, let's pick a simple but broken strategy and then try to repair it. Moe can pick the highest card of the five he sees as the secret card. Then, he can take the "number" of the highest card (its place in the ordering of cards), subtract the number of the second highest card, and encode that as an ordering of the remaining cards.

For example, say Moe sees the 3C, 5H, 5S, 6D, and 9S. We can translate these cards into numbers between 1 and 52 (recalling that Aces are low, and the suits are ordered alphabetically: Clubs, Diamonds, Hearts, and then Spades). Then, these cards are: 3C = 9, 5H = 19, 5S = 20, 6D = 22 and 9S = 36. The 9S is the high card, and 9S - 6D = 14. So, Moe would set 9S aside as the secret card and arrange the other cards to represent 14. That's arrangement 3142 or <5S, 3C, 6D, 5H>.

When Maggie sees the arrangement <5S, 3C, 6D, 5H>, she immediately knows to start counting from the highest (remaining) card: 6D. From the arrangement 3142, she knows to count 14 cards up from 6D. That's 3 ranks (12) plus 2 suits (2): 6D → 7D → 8D → 9D → 9H → 9S. Maggie announces that the secret card is the 9S.

Great!

Now, what does Moe do if his original 5 cards are instead {3C, 5H, 5S, 6D, KC}? KC = 49 and 6D = 22; so, KC - 6D = 25. That's too big to represent with our strategy so far! So, Moe cannot set aside KC and complete the trick. Does he have some alternative?

Take a look at the 3C and KC. 3C = 9, and KC = 49. Imagine counting up from KC and "wrapping around": 49 → 50 → 51 → 52 → 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9. That's 12 steps.

Moe could set aside the 3C as the secret card and arrange the other cards to indicate 12: <5S, KC, 6D, 5H>. Maggie can then count from the highest card she sees, just like in the last example, but wrap around when she goes above 52. (In mathematical terms, she's essentially counting modulo 52, except for the minor detail that we're using the numbers 1–52 rather than 0–51.)

That's it! Moe's strategy is to select the highest card as the secret unless it's more than 24 places away from the second highest card, in which case he selects the lowest card. Either way he finds the (wraparound) distance between the secret card and the highest remaining card and encodes that value in the arrangement of the remaining four cards.

Maggie's strategy is to find the highest card of the four she's given, find the number encoded in the order of those cards, and count up (wrapping around) from the highest card to the secret card.

Try the trick out for a few times to confirm that it works. Write a program to execute the trick. Play with the trick. For example, keep statistics in your program on how often Moe must choose the low card as the secret card rather than the high card.

Proving the Basic Trick Works

Do you believe that Maggie and Moe's trick always works? Why?

How could you prove their trick works? Could you enumerate every five card hand and show that Moe can find a four card ordering to give Maggie? Would that be enough? How many five card hands are there?

I'll just answer the last question here. There are 52C5 five card hands. That's 2598960 hands. You could probably write a program to work through each of those hands, but you certainly couldn't do it.. ahem.. by hand. Let's try a different approach.

We know that our translation between arrangements and numbers 1–24 is a bijection; so, Moe can always create an arrangement given a number 1–24, and Maggie can always find the number Moe intended given an arrangement. Similarly, we know our translation from cards to numbers 1–52 is a bijection.

Put these facts together with our algorithm, and we know that as long as the secret card is 24 or fewer (wraparound) places away from the highest remaining card, Maggie and Moe's strategies work. Can we prove that Moe never chooses a secret card more than 24 places away from the highest remaining card?

Let's assume that Moe picks a secret card that's 25 or more places away from the highest remaining card. If the highest card is ≤24 places away from the second highest card, Moe would select the highest card as the secret card and the secret card would be within 25 places of the highest remaining card. So, the highest card must be ≥25 places away from the second highest card, and Moe must have selected the low card as the secret card.

Now, if we count from a card all the way back around to itself, we must go exactly 52 places. That's because there are 52 cards in the deck. So, let's count from the highest card all the way back to itself. There are ≥25 places from the high card to the secret card. There are ≥1 places from that card to the second card, ≥1 places from that card to the third card, and ≥1 places from to the fourth card. So far, we've gone ≥28 places. From the discussion above, we know that there are ≥25 places between the second highest and highest cards. So, to get all the way back to the highest card takes ≥53 places.

Wait! There are only 52 places total. It cannot take 53 or more places to get from a card back around to itself. To reach such a contradiction, we must have assumed something impossible. Our only assumption was that Moe picks a secret card that's 25 or more places away from the highest remaining card. Therefore, Moe never picks a secret card that's 25 or more places away from the highest remaining card; the secret card is always 24 or fewer places away from the highest remaining card.

Along with our discussion above, that proves that Moe and Maggie's trick works! (This is a pigeonhole proof: we develop a contradiction by showing that Moe can count more cards than there are spots for.)

Dealing with a Joker

What happens if Moe starts with a deck that contains a Joker? Does the trick still work? Can you prove whether it works or not?

If Moe starts with a deck of 53 cards, our proof above no longer works. We can still show that there are ≥53 places to get from the high card back around to itself. Unfortunately, that fact no longer causes a contradiction since it should take 53 places to get back around to the high card. Does that prove that the trick doesn't work?

Actually, that does not prove that the trick doesn't work. It's incredibly important to understand why not.

(Invalidating a proof of a concept X does not prove that X is false. It just shows that a particular proof of X doesn't work. For example, here's a proof that 35 > 21: "The sum of the digits in 35 is 8. The sum of the digits in 21 is 3. 8 is greater than 3. Therefore, 35 > 21." That proof is completely bogus. Nonetheless, 35 really is greater than 21!)

That leaves us with the same question: does Moe and Maggie's strategy work on a deck with a Joker? Let's assume without loss of generality that Maggie and Moe number the Joker 53. (The "without loss of generality" part essentially means that this assumption doesn't count against us. Numbering the Joker as 53 doesn't affect the proof. Maggie and Moe must number the Joker as something, and whatever they choose, the rest of the proof would still work, although perhaps with minor but obvious changes.)

What happens if Moe faces the five card hand {AC, AD, AH, AS, 8C}?

8C - AS = 29 - 4 = 25, and AC - 8C = 1 - 29 which wraps around to 25. In other words, regardless of whether Moe chooses the 8C or the AC as the secret card, Moe's strategy will fail. Does this prove that Moe and Maggie's strategy is not guaranteed to succeed for a 53 card deck?

Sure! We've shown an example for which the strategy fails; so, it's not guaranteed to succeed. (By the way, on how many five card hands does their strategy fail? What's the probability of the strategy failing on a random hand? How big would the deck have to get before Maggie and Moe would even worry about their strategy failing?)

Does this prove that no strategy can possibly work for a 53 card deck?

Proving that No Strategy Can Work

Our work so far just shows that Maggie and Moe's strategy doesn't work for a 53 card deck. We haven't proven that no strategy exists for a 53 card deck. Let's see how big we have to make the deck before we know how to put a proof together that no strategy will work. (Note: this section gets a bit more technical than the previous sections.)

A strategy for Moe must map any possible five card hand to a four card ordering. Moreover, each of those four card orderings need to be distinct. Otherwise, Maggie could get a four card ordering and not know which of two (or more) possible hands led Moe to construct that ordering.

So, Moe's strategy is a one-to-one, total function from five card hands to four card orderings. (As a side note, there's no particular reason that Moe's strategy needs to be "onto", i.e., that every four card ordering must be the image of a five card hand under Moe's strategy. If such a four card ordering exists and Maggie gets it, she'll have no idea what five card hand it came from. However, Moe would never construct that ordering; so, Maggie will never see it anyway.)

What's an example condition under which no strategy exists for Moe?

Let's say, arbitrarily, there were 1,000,000,000 five card hands and 999,999,999 four card orderings. In that case, Moe couldn't possibly construct a one-to-one, total function from five card hands to four card orderings. By the pigeonhole principle, some pair of those billion five card hands must map to the same four card ordering. (If that doesn't make sense, try it with 4 five card hands and 3 four card orderings. The first 3 five card hands use up all the available four card orderings. The last five card hand has nowhere to go except an already used ordering.)

Therefore, no strategy exists for Moe if there are more five card hands than there are four card orderings.

How many five card hands and four card orderings are there for a deck of size n?

This is a combinatorial problem. There are nC5 five card hands. There are nC4 four card hands and 4! orderings of each such hand; so, there are 4! * nC4 four card orderings.

For what deck sizes are there more five card hands than four card orderings?

Well, first let's figure out which one grows faster. nC5 = n!/((n-5)!5!) = (n * (n - 1) * (n - 2) * (n - 3) * (n - 4))/120. 4! * nC4 = 4! * n!/((n-4)!4!) = n!/(n-4)! = n * (n - 1) * (n - 2) * (n - 3). In asymptotic terms, nC5 grows as Θ(n^5) while 4! * nC4 grows as Θ(n^4) (i.e., nC5 has one more n). So, nC5 asymptotically dominates 4! * nC4.

That means that there's some deck size after which the number of five card hands is always bigger than the number of four card orderings. (Note: I should actually prove that once these cross, the first is always bigger than the second!!!)

At what deck size is the number of five card hands first larger than the number of four card orderings?

We can find that by finding the n for which the number of five card hands is equal to the number of four card orderings. For all larger decks, the number of five card hands will be bigger:

nC5 = 4! * nC4
(n * (n - 1) * (n - 2) * (n - 3) * (n - 4))/120 = n * (n - 1) * (n - 2) * (n - 3)
(n - 4)/120 = 1
n - 4 = 120
n = 124

Therefore, any deck with 125 cards or more has more five card hands than four card orderings, and we know for sure that no strategy will guarantee a successful trick.

Narrowing the Bounds

For decks of size 52 or fewer, Maggie and Moe's strategy will work. Based on this "witness" of the feasibility of a 52 card deck, we know that some strategy is guaranteed to work. We can easily show that the same strategy scales to smaller decks down to five cards. For decks of size 125 or more, we know that no strategy is guaranteed to work. What about decks of size 53 to 124?

There's a few strategies we could take to narrow these bounds. Here are three: we could try to prove that no strategy exists for a smaller deck size than 125; we could try to find a strategy for a larger deck size than 52; or we could try to prove that a strategy exists for a larger deck size than 52 (without actually supplying the strategy). Think about which of these seems the most promising. Try them out.

Based on a couple of clues, we'll work on proving that a strategy does exist for a deck larger than 52 cards. One clue is that, for the deck of 124 cards, the number of five card hands is exactly equal to the number of four card orderings. That equivalence suggests that, perhaps, there's a strategy that "just fits" for a 124 card deck.

The second clue is that Maggie and Moe's strategy only very rarely requires Moe to select the lowest card as the secret card. (If you haven't already worked out the probability of Moe selecting the lowest card as the secret card, now's a good time!) That asymmetry suggests that Maggie and Moe's strategy is somehow "overkill" for the problem. That is, their strategy has extra information potential that they throw away on just a few special cases. We know their strategy doesn't work as it stands on 53 cards, but maybe if it did a better job of utilizing the available information passing mechanisms, it could.

Both clues suggest that the trick could work on decks larger than 52 cards. Two natural sizes to try at this point would be 53 card decks and 124 card decks. The two deck sizes suggest very different approaches. Give them both a shot!

With the benefit of hindsight, we'll focus only on the 124 card deck. Is there a successful strategy for a 124 card deck? What does it mean for such a strategy to exist?

As above, we'll consider a strategy to be a function mapping five card hands to four card orderings. The function must be one-to-one and total. Because a 124 card deck has exactly the same number of five card hands and four card orderings, the function must also be onto, meaning that it is a bijection.

Let's consider the space of possible functions. Given a five card hand, Moe can't just pick any four card ordering. He must pick an ordering whose cards are drawn from the five card hand he's given. How many four card orderings can be drawn from a given five card hand?

How many four card hands can be drawn from a five card hand? 5C4 = 5. How many four card orderings can be created from each of these four card hands? 4! = 24. So, the number of four card orderings drawn from a given five card hand is 5C4 * 4! = 5 * 4! = 5! = 120. Every five card hand can map to one of 120 different four card orderings.

Maggie, in turn, guesses a secret card given a four card ordering. Another way of thinking of Maggie's task is to reconstruct the five card hand Moe was given by adding the secret card back in. Given a four card ordering, then, she might guess any five card hand that contains all four cards in the ordering. How many five card hands contain a given set of four cards?

With a deck of 124 and a given set of 4 cards, there are 120 cards remaining that could be the fifth card in the hand. So, each four card ordering could come from 120 different five card hands.

Now we know the constraints on the possible 124 card strategies. How can we picture the set of strategies? One convenient representation is a bipartite graph. On the left are 124C5 nodes, each representing one five card hand. On the right are 124C5 nodes, each representing a four card ordering. Each node on the left has a degree of 120; that is, 120 edges depart each node leading to the four card orderings Moe could choose from that node on the right. Each node on the right has a degree of 120; that is, 120 edges depart each node to the five card hands that Maggie could reconstruct from a given four card ordering. (Of course, these two sets are the same edges considered from opposite sides!)

We can rephrase our central question in terms of this bipartite graph. Our original question was: Does a strategy exist for Moe and Maggie to successfully complete the magic trick with a 124 card deck? Our new question is: Is there a way to select 124 edges in this bipartite graph such that each node in the graph is incident to exactly one of the selected edges?

If such a set exists, then Moe's strategy given a five card hand is to find the node for that hand, follow its edge across to the matching four card ordering on the right, and construct that ordering for Maggie. Maggie finds the four card ordering she's given on the right, follows its edge across to a five card hand, and identifies the secret card as the extra card in that hand.

(As a side note, such a set is called a perfect matching. The marriage theorem establishes the conditions under which a perfect matching exists, and we could proceed from this point using principles proven for matchings. You should give it a shot! However, in this discussion, we will prove our result directly using techniques that can also be used in proving the marriage theorem.)

Our bipartite graph of the space of possible strategies is immense and fiercely complex. So, it's not immediately clear whether a perfect matching exists. We'll try to prove that a matching exists by imagining a (not very good) way to select a strategy and then proving that we can always improve that strategy until it's perfect. Step 1 is to pick an easy way to select an initial strategy. How could we do that?

We'll use a greedy approach. Start by marking every node as uncovered. Now, starting from the top node on the left, work your way down the graph. At each node, find the edge incident on that node that leads to the highest uncovered node on the right. If such an edge exists, add it to our strategy and mark both nodes as covered. If no such edge exists, just continue on to the next node down.

If this yields a perfect matching, we're done. If not, there's k nodes on the left and k nodes on the right that are uncovered. Let's say we were to prove that, for any imperfect matching, we can find a way to alter (or "augment") the matching to cover one extra pair of nodes. In that case, we could just keep augmenting our imperfect matching, one step after another, until there were no more uncovered pairs. That would be a perfect strategy! So, if we can prove that these augmentations always exist for imperfect matchings, then we've proven that a perfect matching exists.

What would an augmentation of an imperfect strategy look like?

Well, we can say for sure that there won't be any trivial fixes. That is, there won't be any uncovered pair with an edge between them. If there were, then our greedy algorithm would have chosen that edge and covered the pair.

Let's imagine an uncovered node on the left. It has 120 edges leading to nodes on the right, but every one of those nodes is itself covered. In fact, if we wanted to "turn on" one of those 120 edges to cover one of the 120 right-hand neighbours, we'd have to "turn off" the edge that was currently being used to cover that neighbour. Doing that would, in turn, uncover the node the right-hand neighbour used to be paired with. So, our original uncovered node is now covered; its right-hand neighbour is now covered but was covered before the change as well; and a new node on the left is now uncovered. It looks like we haven't made any progress, right?

Well.. no, but we might now be able to make progress. Unlike the original uncovered node on the left, the newly uncovered node on the left might be connected directly by an edge to an uncovered node on the right. Nothing in our original algorithm rules that possibility out.

Furthermore, even if our newly uncovered node isn't directly connected to another uncovered node, we can keep adding to this sequence of edges to "toggle". Each time we add another pair of edges leading to the right and coming back, we turn one edge on and turn another off, and each time we "move" the uncovered node on the left. If we eventually uncover a node connected directly to an uncovered node on the right, we can add the edge connecting them as the last in the sequence of edges, and we'll have covered an extra pair of nodes.

Can we always find a sequence of edges of this sort (which we will call an "augmenting path") that leads from an uncovered node on the left to an uncovered node on the right?

Well, let's assume that there's at least one uncovered pair but we cannot find an augmenting path. Then, if we can derive a contradiction, we'll know that we can always find an augmenting path.

Imagine building a set of nodes and edges on both sides of the graph. Start by adding an arbitrary uncovered node on the left to the set. Then, add all 120 nodes on the right that are adjacent to that node to the set. Either every one of those 120 nodes is covered or else we've found an augmenting path. Since we assumed there is no augmenting path, they must all be covered. So, add the 120 nodes on the left that are paired with those 120 nodes on the right. Now, continue adding nodes in this fashion — add all nodes adjacent to a node on the left and add the node paired with each covered node on the right — until no new nodes are added to the set.

Again, by our assumption that there's no augmenting path, this set must reach a fixed point (i.e., stop growing) before any uncovered node is added on the right. Furthermore, every node we added on the left except the first was added because it was paired with a node on the right. Therefore, every node on the left but the first is covered, every node on the right is covered, and there is one more node on the left than on the right.

Now, imagine dragging that whole set down to the bottom of the bipartite graph and drawing a line separating those nodes from the rest of the graph. We'll call that line the "separator". Can any edge from a node on the left cross the separator?

No, no edge from the left can possibly cross the separator. If it did, it would be an edge from a node on the left that's in the set to a node on the right that's not in the set. But, if such an edge existed, we would have added the node on the right to the set back when we were building the set up!

(As a side note, edges from the right can cross the separator. That's because only edges incident on nodes on the right that are part of the matching drag nodes on the left into the set. One other unused edge must have been the one that caused the node on the right to be added to the set. The other 118 edges incident on each node on the right can potentially cross the separator.)

How many edges depart from the left side of the set?

Every node on the left has degree 120. So, if there are k + 1 nodes on the left, then (k + 1) * 120 edges depart from the left side of the set.

How many of those edges are incident on nodes on the right side of the set?

Since we already know that no edge from the left can cross the separator, every one of the (k + 1) * 120 edges must lead to a node on the right side of the set.

How many edges depart from the right side of the set?

Like the left-hand nodes, every right-hand node has a degree of 120. If there are k + 1 nodes on the left, there are k nodes on the right (i.e., one fewer as mentioned above). So, there are exactly k * 120 edges incident on nodes in the right side of the set.

Wait!! We've just shown that there are exactly (k + 1) * 120 edges incident on the right-hand side of the set, but we have also shown that there are exactly k * 120 edges incidint on the right-hand side of the set. (k + 1) * 120 is not equal to k * 120; so, that's a contradiction!

(Intuitively, that says that there are just too many edges coming from the left to fit into the set on the right. That will be true unless there are as many nodes on the right as on the left, but the right side can't "catch up" in size to the left unless we add a node to the right that's uncovered and therefore unpaired with a node on the left.)

Therefore, our assumption was incorrect, and any time there is at least one uncovered pair, we can always find an augmenting path. Once we find the augmenting path, we can "flip" the state of each edge on the path: turning on edges that weren't part of the matching before and turning off edges that were part of the matching before. The new matching has one fewer pairs of uncovered nodes. If we keep using these augmenting paths, we'll eventually have a perfect matching!

We have proven that a strategy exists to do the magic trick on a 120 card deck. In a rather nifty twist, we did it without ever actually finding a particular working strategy! We just showed that such a strategy must exist. As it happens, you could actually implement the construction in our proof to generate a strategy (and I encourage you to do so!). Sometimes, however, we prove the existence of solutions in CS without even being able to construct the solution.

A Better Signal

A group of high school students proposed this better (in terms of human usability) signalling mechanism to me:

Moe is guaranteed to see two cards of the same suit (by the pigeonhole principle: there are only four suits and five cards). Moe picks as the secret card the higher of those two cards as long as it is no more than 6 "places away" from the lower card. If the high card is too far away, Moe picks the low card. Moe then places the card whose suit matches the secret card first followed by the remaining three cards in an ordering that signals the number of "places" from the first card to the secret card.

Because there are only 13 cards in a suit, we're guaranteed to be able to signal the secret card using only the numbers 1 through 6. Because we have three ordered cards to signal a number, we can signal the numbers 1 through 6.

Way easier to do with much less practice :)

Wrap-Up

That's as far as I've explored this particular magic trick. You can go farther in several ways. You could read up on some related topics (like the marriage theorem and maximum matching). You could explore other variants of the problem (like a generalized version of the trick where Moe is given a k card hand and selects m secret cards). You could explore other facets of the existing problems (like how big a deck can be solved with a human-usable strategy and, for that matter, what about a strategy makes it "human usable"). You could explore programs, visualizations, and descriptions that do a better job than this text at explaining the existing problems.

Finally, you can find other interesting problems or puzzles and push their boundaries using your own ingenuity and knowledge of computer science.

Have fun :)


Steve Wolfman
UBC CS