The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very...
Transcript of The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very...
![Page 1: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/1.jpg)
Paul Curzon Queen Mary University of London
The Magic of Computer Science (Part 2)
teachinglondoncomputing.org/magicofcs/
![Page 2: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/2.jpg)
Computer Science is about computation (not computers)
Magic = Computation
2
![Page 3: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/3.jpg)
Fast Sorting and Searching
• We are going to explore how divide and conquer leads to fast algorithms
• Focus on search and sort algorithms
• Through Magic
3
![Page 4: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/4.jpg)
On to the magic … Please keep the secrets
• I’m going to teach you how to do the tricks • Some are actually in the shows of professional
magicians • If you do perform them later for friends don’t
break the magician’s code • Keep the secrets! • If you do know then don’t shout out let others
puzzle it out first!
4
![Page 5: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/5.jpg)
The Australian magician’s
dream
5
![Page 6: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/6.jpg)
ALERT: A video message has arrived for you …
![Page 7: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/7.jpg)
![Page 8: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/8.jpg)
Ponder break
How on earth do they do
that?
8
![Page 9: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/9.jpg)
The computer science?It is a self-working trick: an algorithm
1. Place the chosen card in position ??? 2. Hide a prediction of that card (in an envelope) 3. Discard roughly the bottom half 4. Repeat 4 times: Discard the first and then every second card thereafter 5. Reveal the card is the one predicted.
Computers pull the same trick (algorithm) to search for data
9
![Page 10: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/10.jpg)
Does it always work?
I set my force card 16 from the
top
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,...
2, 4, 6, 8,10,12,14,16,18,20,...
4, 8,12,16,20,...
8, 16
1610
![Page 11: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/11.jpg)
Try it yourselfThe algorithm
1. Place the chosen card in position 16 2. Hide a prediction of that card (in an envelope) 3. Discard roughly the bottom half 4. Repeat 4 times: Discard the first and then every second card thereafter 5. Reveal the card is the one predicted.
11
![Page 12: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/12.jpg)
It is a real search algorithm• Early computers used punch cards as memory
• This trick is actually a real search algorithm – the algorithm was used by early computers to find data stored
on punch cards
• Used to pull out a particular punch card based on numbers encoded in binary as holes and slots along the top of the card – A slot = 1 – A hole = 0
12
![Page 13: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/13.jpg)
We need some binary maths16 in Binary is 10000 16 8 4 2 1 x 1 0 0 0 0 = 16 + 0 + 0 + 0 + 0 5 in Binary is 00101 16 8 4 2 1 x 0 0 1 0 1 = 0 + 0 + 4 + 0 + 1
0 means DISCARD the “down” pile 1 means KEEP the “down” pile
APPLY THIS RULE WITH PUNCH CARDS TO FIND ANY CARD
13
![Page 14: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/14.jpg)
Searching for punch card 1616 in Binary is 10000
1 : 0 DISCARD the “down” pile 2 : 0 DISCARD the “down” pile 4 : 0 DISCARD the “down” pile 8 : 0 DISCARD the “down” pile 16 : 1 KEEP the “down” pile
14
![Page 15: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/15.jpg)
Searching for punch card 55 in Binary is 00101
1 : 1 KEEP the “down” pile 2 : 0 DISCARD the “down” pile 4 : 1 KEEP the “down” pile 8 : 0 DISCARD the “down” pile 16 : 0 DISCARD the “down” pile
15
![Page 16: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/16.jpg)
Why is it fast? Divide and Conquer
• Divide and Conquer algorithm • Halve the size of the problem with each step • Shake out half the cards • Based on 1 or 0 in next binary position • But left with the same problem (just fewer
cards) • Stop when only 1 card left
• This is very very fast
16
![Page 17: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/17.jpg)
Why is it fast? Divide and Conquer
• This is very very fast • Divide and conquer requires O(log n)
operations
• log n is just the number of times we divide a number in half before getting down to 1 • In this case 1 card left • So to search a million cards we would need
only 20 operations • One for each bit in the binary
17
![Page 18: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/18.jpg)
… leads to a sort algorithm• On each pass
• Instead of discarding the cards that drop • Place the discard cards at the back of the pile
• Leads to a lightning fast sort algorithm • Divide and conquer gives O(n log n) sorting
• eg Quicksort and Mergesort • see the separate video explanation of heap
sort
18
![Page 19: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/19.jpg)
How does that work?
19
![Page 20: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/20.jpg)
Why is it fast? Parallelism
• It is even faster than that though because it uses parallelism too • based on the physicality of the cards • do all the comparisons to check if it is a 1 or a
0 in parallel • would need a processor per card to get this extra
speedup in a computer • Makes it ridiculously fast
• O(log n) • So (in theory)
• can sort a million cards with 20 operations20
![Page 21: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/21.jpg)
The big picture?
Magic = secret method + presentation
Software = algorithm + interaction design
Both involve the same kind of Computational Thinking
www.cs4fn.org/magic/
21
![Page 22: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/22.jpg)
In summary• Computation happens in the
world (including in magic tricks)
• Divide and Conquer leads to fast algorithms
• Binary representations are one basis for divide and conquer halving
• Parallelism also leads to fast algorithms
• Sorting and searching are good for exploring algorithms and efficiency
22
![Page 23: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/23.jpg)
Dedicated to the memory of
Peter McOwan
23
![Page 24: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/24.jpg)
Thank you
Resources on magic and computer science at: teachinglondoncomputing.org/magicofcs/
24
Thanks to Richard Garriott
for his part in the trick
![Page 25: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/25.jpg)
![Page 26: The Magic of Computer Science (Part 2)...2020/07/02 · Divide and Conquer • This is very very fast • Divide and conquer requires O(log n) operations • log n is just the number](https://reader034.fdocuments.in/reader034/viewer/2022052012/6028785a5b4c8b49110c7ea6/html5/thumbnails/26.jpg)