39
Card Tricks Phillip James & Tom Davies

The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Embed Size (px)

Citation preview

Page 1: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Card Tricks

Phillip James & Tom Davies

Page 2: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

The aim of today’s magic tricks is to:

• Teach concepts from Computer Science without the computers!

• Show how topics from Computer Science apply within the magic world.

• Make Computer Science fun!

Page 3: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Overview of Computer Science.

The Magic: 21 cards.

Carrying on conjuring.

The remote control brain.

Out of body experiences.

Page 4: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick
Page 5: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Classical Examples

Page 6: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Classical Examples

Industrial Examples

Page 7: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick
Page 8: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Embedded systems count towards a large percentage of all computer systems.

How many have you interacted with today?

Page 9: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

1. Explore 4 magic card tricks.

2. Explain the computer science behind each trick.

Page 10: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

1. Explore 4 magic card tricks.

2. Explain the computer science behind each trick.

Page 11: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

And now Tom will present the 21 card trick.

Page 12: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Let’s look at the ‘mechanics’ of the trick:

how do you make it work?

Put the pile your volunteer selects carefully between the other two piles.

Deal the pack down then across.

Repeat...

After the fourth deal : chosen card in middle.

Page 13: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

The stepwise approach is a simple “algorithm”.

Think of it as being like baking a cake!

Page 14: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

The stepwise approach is a simple “algorithm”“algorithm”.

Think of it as being like baking a cake!

Some questions:

Does it always work? (Testing/Correctness)

Can we do it in less steps? (Efficiency)

How to describe the whole process? (Specification)

Page 15: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Question: will our algorithm always work?

First Solution: Check all possibilities – i.e. “Testing”“Testing”.

Problem: How many tests are needed?

Every possible combination of 21 cards.

Every card could be the chosen one.

Leads to: Tom being occupied for a very long time...

Page 16: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Second Solution: Mathematically “prove” “prove” correctness.

Lets consider the steps:

After Deal Number 1: Cards split into three piles.

The seven-card pile holding the chosen card is put in the middle of the other piles.

There are now only seven places it could be.

Page 17: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

7 cards arranged in above positions

after deal. Chosen card in 3rd, 4th or 5th position of any

column.

Then middle column dealt as

above. Now choosing column

gives card.

Finally, specific dealing leads to chosen card in

center.

Page 18: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Example “AlgorithmAlgorithm”.

“TestingTesting” useful but time consuming.

“ProvingProving” tricky but ensures correctness.

Page 19: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Tom will now predict the future (we hope)!

Page 20: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Need to ensure:

Before the trick you know the 16th card.

The participant cuts the cards between the 16th and 32nd cards.

The rest is up to “Binary ArithmeticBinary Arithmetic”.

Page 21: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick
Page 22: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

““Binary NumbersBinary Numbers” ” are used to represent data.

Binary properties often form basis of algorithms.

For example: “Binary Search” (Telephone Directory)Binary Search” (Telephone Directory)

Page 23: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Tom will now control your brains...

Page 24: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Phillip James & Tom Davies

Page 25: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Based on abstract “algebra”“algebra”! (Yes! abstract mathematics and computer science are closely linked.)

Page 26: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Based on abstract “algebra”“algebra”! (Yes! abstract mathematics and computer science are closely linked.)

Firstly you picked: 8 + 7 + 6 + 5 = 26 cards.

Now Consider the following:

•We know 26 Reds and 26 BlacksHence:

• R1 + R3 + R4 = 26 (*)• B2 + B3 + B4 = 26 (**)

• We also know:• R1 = R3 + B3• B2 = R4 + B4

Page 27: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Performing some substitutions:

(R3 + B3) + R3 + R4 = 26 (1) substitute R1 in * by R3+B3.

(R4 + B4) + B3 + B4 = 26 (2) substitute B2 in * by R4+B4.

Combining 1 and 2:

(R3 + B3) + R3 + R4 = 26 = (R4 + B4) + B3 + B4

Group and Simplyfy:

2*R3 + B3 + R4 = R4 + 2*B4 + B3 group similar terms.

2 x R3 = 2 x B4 subtract B3 + R4.

Finally dividing by 2 gives:

R3 = B4

(Magic!)

Page 28: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

This trick always works – it’s based on mathematics.

We proved this through “abstractionsabstractions”:

e.g. We let R3 represent red cards in pile 3.

Page 29: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

This trick always works – it’s based on mathematics.

We proved this through “abstractionsabstractions”:

e.g. We let R3 represent red cards in pile 3.

“AbstractionAbstraction” is also used to prove programs correct.

This ensures programs always work as expected!

Page 30: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Now I will become the magician and

“float into the room”.

1. I will leave the room.

2. A volunteer will pick a card from a selection.

3. I will come back and tell you which card you picked!

So goodbye for now!

Page 31: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Notice that Tom added extra cards?

Page 32: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Notice that Tom added extra cards?

Make each row have even number of cards facing down.

Page 33: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Then after a flip, detection is easy.

Page 34: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Consider sending a message across a network.

We assign a number to each letter.

Then encode each number in binary.

(Remember everything is eventually represented in binary)

Page 35: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Suddenly, a message like:

He is fit

01000 00101 00000 001001 10011 00000 00110 001001 10100

Page 36: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Suddenly, a message like:

He is fit

01000 00101 00000 001001 10011 00000 00110 001001 10100

becomes:

He is fat

01000 00101 00000 001001 10011 00000 00110 000001 10100

Detection of flipped using same algorithm as in our card trick.

Page 37: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Today we have introduced: “Algorithms”“Algorithms”.

“Testing”“Testing”.

“Correctness Proofs”“Correctness Proofs”.

“Binary Arithmetic”“Binary Arithmetic”.

“Binary Search”“Binary Search”.

“Abstraction”“Abstraction”.

“Parity”“Parity”.

And all through card tricks...

Page 38: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

We have taught computer science in a fun way!

...and we didn’t use a computer.

For more details please visit:

Page 39: The Magic of Computer Science - Swanseacs.swansea.ac.uk/~csmarkus/ProcessesAndData/Slides/2011_Magic_Of...Explore 4 magic card tricks. 2. Explain the computer science behind each trick

Any Questions?