Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Maths of Sorting Things Out John D Barrow DAMTPCambridge Gresham College.

Similar presentations


Presentation on theme: "The Maths of Sorting Things Out John D Barrow DAMTPCambridge Gresham College."— Presentation transcript:

1 The Maths of Sorting Things Out John D Barrow DAMTPCambridge Gresham College

2 Complexity is All Around Us

3

4 3899650268199 38 =5569+ 3899650268143 69

5 Number of ways an even number can be written as the sum of two primes (4 < n < 1000)

6 Number of ways an even number can be written as the sum of two primes (4 < n < 10 6 )

7 Computer-aided Mathematics Computer Proofs Experimental mathematics Is Anything Beyond Computers?

8 Four Colours Suffice

9 Tractability ‘Easy’ problems N parts Calculation time grows as N ‘Hard’ problems N parts Calculation time grows as 2 N

10 Monkey Puzzles

11 6-city Travelling Salesman

12

13

14 8 years of parallel computing

15 Some More ‘Hard’ Problems Where the best solution can’t be predicted by a general recipe

16 Packing Problems

17 Examples Fitting music tracks on CDs Fitting music tracks on CDs Fitting packages in boxes Fitting packages in boxes Scheduling computer or machine tasks Scheduling computer or machine tasks Building shelves Building shelves Adverts in TV breaks Adverts in TV breaks Data storage on disk Data storage on disk Fitting your holiday stuff in a suitcase Fitting your holiday stuff in a suitcase

18 25 data files to fit onto disks that each hold a maximum of 10 GB What is the smallest number of disks you could use? How do you pack these files to do it? 6,6,5,5,5,5,4,3,2,2,3,7,6,5,4,3,2,2,4,4,5,8,2,7,1 Total volume to be fitted on disks is 106 The Problem

19 Strategy 1: ‘Next Fit’ Strategy 1: ‘Next Fit’ 6,6,5,5,5,5,4,3,2,2,3,7,6,5,4,3,2,2,4,4,5,8,2,7,1 Put first track on first disk, then next in the same disk until it won’t fit. Put first track on first disk, then next in the same disk until it won’t fit. Then start new disk. Then start new disk. You can’t go back and fill gaps in earlier disks. You can’t go back and fill gaps in earlier disks. 14 disks used. 3 full. 14 disks used. 3 full. 32 out of 140 slots empty 32 out of 140 slots empty 108/140 = 77% filled 108/140 = 77% filled 552 55242 5524481 553742487 665537642487 66553376524587 66554376524587 66554376534587 66554276534587 66554276534587

20 Other Strategies to Try ‘First Fit’ ‘First Fit’ You can go back and put tracks on the first disk in the line that still has room ‘Worst Fit’ ‘Worst Fit’ Put next track on the disk that currently has most space available ‘Best Fit’ ‘Best Fit’ Put next track on the disk that leaves the least room on the disk after it is added

21 Strategy 2 : ‘First Fit’ Strategy 2 : ‘First Fit’ 6,6,5,5,5,5,4,3,2,2,3,7,6,5,4,3,2,2,4,4,5,8,2,7,1 Put first track on first disk which has space, Put first track on first disk which has space, Then start new disk. Then start new disk. You can go back and fill gaps in earlier disks. You can go back and fill gaps in earlier disks. 12 disks used. 6 full. 12 disks used. 6 full. 14 slots empty 14 slots empty 106/120 = 88% filled 106/120 = 88% filled 415534 43553242 4355324248 43553742487 66553762487 665537654587 665527654587 665527654587 665527654587 665527654587

22 Sorting Helps Sort the files into descending size order 8,7,7,6,6,6,5,5,5,5,5,5,4,4,4,4,3,3,3,2,2,2,2,2,1 Now try sorted ‘Next Fit’ and ‘First Fit’ strategies What was the best strategy? Is it the best possible for this problem?

23 Strategy 3: ‘Sorted Next Fit’ 8,7,7,6,6,6,5,5,5,5,5,5,4,4,4,4,3,3,3,2,2,2,2,2,1 Put first track on first disk, then next in the same disk until it won’t fit. Put first track on first disk, then next in the same disk until it won’t fit. Then start new disk. Then start new disk. You can’t go back to fill gaps in earlier disks. You can’t go back to fill gaps in earlier disks. 14 disks used. 4 full. 14 disks used. 4 full. 33 slots empty 33 slots empty 107/140 = 76% filled 107/140 = 76% filled 5552 55532 85554432 8775554432 8776665554432 8776665554432 8776665554432 8776665554432 8776665554432 87766655544321

24 Strategy 4: ‘Sorted First Fit’ 8,7,7,6,6,6,5,5,5,5,5,5,4,4,4,4,3,3,3,2,2,2,2,2,1 Put first track on first with disk with space Put first track on first with disk with space Then start new disk. Then start new disk. 11 disks used. 10 full. 11 disks used. 10 full. 4 slots empty. Can’t do better 4 slots empty. Can’t do better 106/110 = 96% filled 106/110 = 96% filled THIS IS THE BEST POSSIBLE PACKING THIS IS THE BEST POSSIBLE PACKING 2334445551 2334445552 8334445552 8774445553 87766655532 87766655532 87766655542 87766655542 87766655542 87766655542

25 Chicken McNugget Numbers 6, 9 and 20

26 Any sufficiently large number can me written as a linear sum of 6s, 9s and 20s as a linear sum of 6s, 9s and 20s What is the Largest Non-McNugget Number 44 = 6+9+9+20 45 = 9+9+9+9+9 46 = 6+20+20 47 = 9+9+9+20 48 = 6+6+9+9+9+9 49 = 9+20+20 Make all larger numbers by adding a 6 to any one of these and so on The largest non-McNugget number is 43 6s and 9s only make multiples of 3. Using a 20 or two doesn’t help. All the non-McNugget numbers are 1,2,3,4,5,7,8,10,11,13,14,16,17,19,22,23,25,28,31,34,37,43.

27 The Happy Meal 4-McNugget Box (1979) makes it mathematically far less interesting makes it mathematically far less interesting The numbers {4,6,9,20} now allow only six non-McNugget numbers 1,2,3,5,7 and 11 !

28 What’s the smallest total stamp value that can’t fit on the envelope?

29 An envelope can only hold a maximum number of stamps  h Stamps have values A n = {a 1, a 2,…..a n } Sort in ascending order so that 1 = a 1 < a 2 <….. < a n We want the smallest integer that can’t be written as N(h, A n ) = F 1 a 1 + F 2 a 2 + …..+ F n a n where F 1 + F 2 + …..+ F n < h The number of consecutive possible postage amounts that will fit on the envelope is n(h, A n ) = N(h, A n ) - 1 Exact solution only known for n = 2: n(h, A 2 ) = (h + 3 – a 2 )a 2 – 2 for h  a 2 - 2 n(h,2) = [(h 2 + 6h +1)/4] […] is integer part, so [3.14] = 3 The first values are 2,4,7,10,14,18,23,28,34,40,…. N(h,4) > 2.008  (h/4) 4 + O(h 3 ) The Postage Stamp Problem

30 The Spare Change Problem St Gaudens $20 Gold Double Eagle

31

32 Smallest set of coins that will make all amounts of change from 1 to 100 Europe: 1, 2, 5, 10, 20, 50 denominations USA: 1, 5, 10, 25 Minimum number of coins given by the handy ‘Greedy Algorithm’: Use as many of the largest value at each step 76 pence = 50 + 20 + 5 + 1 (4 coins) 76 cents = 50 + 25 + 1 (3 coins) ‘Old money’ in the UK didn’t have the handy property ½, 1, 3, 6, 12, 24, 30 old pence 4 shillings = 48d = 30 + 12 + 6 = 24 + 24 This happens when we double a coin value (> 2d) eg with a groat 4d 8d = 4 + 4 = 6 +1 + 1

33 Shallit’s Improved American System Average number of coins needed to make 1 to 100 cents with 1, 5, 10, 25 denominations is 4.7 Worst case is with only a 1 cent coin: need 99 coins to make 99c etc Average number needed is 49.5 What is the best case using only 4 values? 1, 5, 18, 29 and 1, 5, 18, 25 are both better! They need an average of 3.89 coins to make all 1  100 cent amounts all 1  100 cent amounts Best to use the 1, 5, 18, 25 values And replace the dime with a new 18c piece 18 c

34 Improved UK or Euro system ? 1, 2, 5, 10, 20, 50, 100, 200p coins Average number of coins to make all amounts up to 500 is 4.6 Add a 133p or 137p coin and the average number falls to 3.92 133 p 137 p

35 A Further Improvement? Lots of currencies use the pattern of denominations Lots of currencies use the pattern of denominations 1,2,5, and 10,20, 50, and 100, 200, 500 They would do (about 6%) better and use less change with They would do (about 6%) better and use less change with 1,3,4, and 10, 30, 40, and 100, 300, 400 68 = 50 + 10 + 5 + 2 +1 = 40 +10 + 10 + 4 + 4 68 = 50 + 10 + 5 + 2 +1 = 40 +10 + 10 + 4 + 4 134 = 100 + 20 +10 +2 + 2 = 100 + 30 + 4 134 = 100 + 20 +10 +2 + 2 = 100 + 30 + 4 243 = 200 + 20 + 20 + 2 +2 = 200 + 40 + 3 243 = 200 + 20 + 20 + 2 +2 = 200 + 40 + 3 279 = 200 + 50 +20 + 5 + 2 +2 = 200 + 40 + 30 + 3 + 3 +3 279 = 200 + 50 +20 + 5 + 2 +2 = 200 + 40 + 30 + 3 + 3 +3 Greedy algorithm has one failure because 6 = 3+3 is better than 6= 4+1+1 Best to do away with the 1c or 1p coin though!


Download ppt "The Maths of Sorting Things Out John D Barrow DAMTPCambridge Gresham College."

Similar presentations


Ads by Google