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

Post on 29-Dec-2015

216 views 2 download

Tags:

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

The Maths of The Maths of Sorting Things OutSorting Things Out

John D BarrowJohn D Barrow

DAMTPDAMTPCambridgeCambridge

Gresham Gresham CollegeCollege

Complexity is All Complexity is All Around UsAround Us

Impossible to Prove(so far)

Goldbach-Euler 7th June 1742

Every even number can bewritten as the sum of

2 prime numbers

4 = 2+2, 6 = 3+3, 8 = 3+510 = 3+7, 12 = 5+7, 14 =7+7,…..

…36 = 31+5…etc

known to be true for even numbers up to

61016

389965026819389965026819938938==

55695569++

389965026814389965026814369369

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

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

Computer-aided MathematicsComputer-aided Mathematics

Computer ProofsComputer ProofsExperimental mathematicsExperimental mathematics

Is Anything Beyond Computers?Is Anything Beyond Computers?

Four Colours SufficeFour Colours Suffice

TractabilityTractability

‘‘Easy’ problemsEasy’ problemsN partsN parts

Calculation time grows as NCalculation time grows as N

‘‘Hard’ problemsHard’ problemsN partsN parts

Calculation time grows as 2Calculation time grows as 2NN

Monkey PuzzlesMonkey Puzzles

6-city Travelling Salesman

6-city Travelling Salesman

‘Travelling Salesman’ visiting3038 sites

(Applegate et al 1990)

18 months of computer time

8 years of parallel computing8 years of parallel computing

Some More ‘Hard’ Some More ‘Hard’ ProblemsProblems

Where the best solution can’t Where the best solution can’t be predictedbe predicted by a general by a general

reciperecipe

Packing ProblemsPacking Problems

ExamplesExamples

Fitting music tracks on Fitting music tracks on CDsCDs

Fitting packages in Fitting packages in boxesboxes

Scheduling computer Scheduling computer or machine tasksor machine tasks

Building shelvesBuilding shelves Adverts in TV breaksAdverts in TV breaks Data storage on diskData storage on disk Fitting your holiday Fitting your holiday

stuff in a suitcasestuff in a suitcase

25 data files to fit onto disks that each hold a maximum of 10 GB25 data files to fit onto disks that each hold a maximum of 10 GBWhat is the smallest number of disks you could use?What is the smallest number of disks you could use?

How do you pack these files to do it?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,16,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 isTotal volume to be fitted on disks is

106106

The ProblemThe Problem

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 Put first track on first disk, then next in the disk, then next in the same disk until it won’t same disk until it won’t fit. fit.

Then start new disk. Then start new disk. You can’t go back and You can’t go back and

fill gaps in earlier disks.fill gaps in earlier disks. 14 disks used. 3 full.14 disks used. 3 full. 32 out of 140 slots 32 out of 140 slots

emptyempty 108/140 = 77% filled108/140 = 77% filled

55 55 22

55 55 22 44 22

55 55 22 44 44 88 11

55 55 33 77 44 22 44 88 77

66 66 55 55 33 77 66 44 22 44 88 77

66 66 55 55 33 33 77 66 55 22 44 55 88 77

66 66 55 55 44 33 77 66 55 22 44 55 88 77

66 66 55 55 44 33 77 66 55 33 44 55 88 77

66 66 55 55 44 22 77 66 55 33 44 55 88 77

66 66 55 55 44 22 77 66 55 33 44 55 88 77

Other Strategies to TryOther Strategies to Try

‘‘First Fit’ First Fit’ You can go back and put tracks on the first You can go back and put tracks on the first

disk in the line that still has roomdisk in the line that still has room ‘‘Worst Fit’ Worst Fit’ Put next track on the disk that currently Put next track on the disk that currently

has most space availablehas most space available ‘‘Best Fit’Best Fit’Put next track on the disk that leaves the Put next track on the disk that leaves the

least room on the disk after it is addedleast room on the disk after it is added

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 Put first track on first disk which has space, disk which has space,

Then start new disk. Then start new disk. You can go back and You can go back and

fill gaps in earlier fill gaps in earlier disks.disks.

12 disks used. 6 full.12 disks used. 6 full. 14 slots empty14 slots empty 106/120 = 88% filled106/120 = 88% filled

44 11 55 55 33 44

44 33 55 55 33 22 44 22

44 33 55 55 33 22 44 22 44 88

44 33 55 55 33 77 44 22 44 88 77

66 66 55 55 33 77 66 22 44 88 77

66 66 55 55 33 77 66 55 44 55 88 77

66 66 55 55 22 77 66 55 44 55 88 77

66 66 55 55 22 77 66 55 44 55 88 77

66 66 55 55 22 77 66 55 44 55 88 77

66 66 55 55 22 77 66 55 44 55 88 77

Sorting HelpsSorting HelpsSort the files into descending size orderSort 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,18,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’ Now try sorted ‘Next Fit’ and ‘First Fit’ strategiesstrategies

What was the best strategy?What was the best strategy?

Is it the best possible for this Is it the best possible for this problem?problem?

Strategy 3: ‘Sorted Next Fit’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,18,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 Put first track on first disk, then next in the disk, then next in the same disk until it same disk until it won’t fit. won’t fit.

Then start new disk. Then start new disk. You can’t go back to You can’t go back to

fill gaps in earlier fill gaps in earlier disks.disks.

14 disks used. 4 full.14 disks used. 4 full. 33 slots empty33 slots empty 107/140 = 76% filled107/140 = 76% filled

55 55 55 22

55 55 55 33 22

88 55 55 55 44 44 33 22

88 77 77 55 55 55 44 44 33 22

88 77 7766 66 66 55 55 55 44 44 33 22

88 77 7766 66 66 55 55 55 44 44 33 22

88 77 7766 66 66 55 55 55 44 44 33 22

88 77 7766 66 66 55 55 55 44 44 33 22

88 77 7766 66 66 55 55 55 44 44 33 22

88 77 7766 66 66 55 55 55 44 44 33 22 11

Strategy 4: ‘Sorted First Fit’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,18,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 Put first track on first with disk with spacewith 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 4 slots empty. Can’t do

betterbetter 106/110 = 96% filled106/110 = 96% filled THIS IS THE BEST THIS IS THE BEST

POSSIBLE PACKINGPOSSIBLE PACKING

22 33 3344 44 44 55 55 55 11

22 33 3344 44 44 55 55 55 22

88 33 3344 44 44 55 55 55 22

88 77 7744 44 44 55 55 55 33

88 77 7766 66 66 55 55 55 33 22

88 77 7766 66 66 55 55 55 33 22

88 77 7766 66 66 55 55 55 44 22

88 77 7766 66 66 55 55 55 44 22

88 77 7766 66 66 55 55 55 44 22

88 77 7766 66 66 55 55 55 44 22

Chicken McNugget NumbersChicken McNugget Numbers

6, 9 and 206, 9 and 20

Any sufficiently large number can me writtenAny sufficiently large number can me written as a linear sum of 6s, 9s and 20sas a linear sum of 6s, 9s and 20s

What is the Largest Non-McNugget NumberWhat is the Largest Non-McNugget Number

44 = 6+9+9+2044 = 6+9+9+2045 = 9+9+9+9+945 = 9+9+9+9+946 = 6+20+2046 = 6+20+2047 = 9+9+9+2047 = 9+9+9+2048 = 6+6+9+9+9+948 = 6+6+9+9+9+949 = 9+20+2049 = 9+20+20

Make all larger numbers by adding a 6Make all larger numbers by adding a 6to any one of these and so onto any one of these and so on

The largest non-McNugget number is 43The largest non-McNugget number is 43

6s and 9s only make multiples of 3. 6s and 9s only make multiples of 3. Using a 20 or two doesn’t help.Using a 20 or two doesn’t help.

All the non-McNugget numbers areAll the non-McNugget numbers are1,2,3,4,5,7,8,10,11,13,14,16,17,19,22,23,25,28,31,34,37,43.1,2,3,4,5,7,8,10,11,13,14,16,17,19,22,23,25,28,31,34,37,43.

The Happy Meal 4-McNugget Box (1979)The Happy Meal 4-McNugget Box (1979) makes it mathematically far less interestingmakes it mathematically far less interesting

The numbers {4,6,9,20} now allow only six non-McNugget numbers The numbers {4,6,9,20} now allow only six non-McNugget numbers

1,2,3,5,7 and 111,2,3,5,7 and 11

!

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

An envelope can only hold a maximum number of An envelope can only hold a maximum number of stamps stamps h h

Stamps have values AStamps have values Ann = {a = {a11, a, a22,…..a,…..ann}}Sort in ascending order so that 1 = aSort in ascending order so that 1 = a11 < a < a22 <….. < a <….. < ann

We want the smallest integer that can’t be written asWe want the smallest integer that can’t be written asN(h, AN(h, Ann) = F) = F11aa11 + F + F22aa22 + …..+ F + …..+ Fnnaann

where Fwhere F11 + F + F22 + …..+ F + …..+ Fnn < h < hThe number of consecutive possible postage The number of consecutive possible postage

amounts that amounts that will fit on the envelope is n(h, Awill fit on the envelope is n(h, Ann) = N(h, A) = N(h, Ann) - 1) - 1

Exact solution only known for n = 2: Exact solution only known for n = 2: n(h, An(h, A22) = (h + 3 – a) = (h + 3 – a22)a)a22 – 2 for h – 2 for h a a22 - 2 - 2

n(h,2) = [(hn(h,2) = [(h22 + 6h +1)/4] […] is integer part, so [3.14] = 3 + 6h +1)/4] […] is integer part, so [3.14] = 3

The first values are 2,4,7,10,14,18,23,28,34,40,….The first values are 2,4,7,10,14,18,23,28,34,40,….N(h,4) > 2.008 N(h,4) > 2.008 (h/4) (h/4)44 + O(h + O(h33))

The Postage Stamp ProblemThe Postage Stamp Problem

The Spare Change ProblemThe Spare Change Problem

St Gaudens $20 Gold Double Eagle

Smallest set of coins that will make all Smallest set of coins that will make all amounts of change from 1 to 100amounts of change from 1 to 100

Europe: 1, 2, 5, 10, 20, 50 denominationsEurope: 1, 2, 5, 10, 20, 50 denominations

USA: 1, 5, 10, 25USA: 1, 5, 10, 25

Minimum number of coins given by the handy ‘Greedy Algorithm’:Minimum number of coins given by the handy ‘Greedy Algorithm’:Use as many of the largest value at each stepUse as many of the largest value at each step

76 pence = 50 + 20 + 5 + 1 (4 coins)76 pence = 50 + 20 + 5 + 1 (4 coins)76 cents = 50 + 25 + 1 (3 coins)76 cents = 50 + 25 + 1 (3 coins)

‘‘Old money’ in theOld money’ in the UK didn’t have the handy propertyUK didn’t have the handy property½ , 1 , 3 , 6 , 12 , 24 , 30 old pence½ , 1 , 3 , 6 , 12 , 24 , 30 old pence

4 shillings = 48d = 30 + 12 + 6 = 4 shillings = 48d = 30 + 12 + 6 = 24 + 2424 + 24This happens when we double a coin value (> 2d) eg with a groat 4dThis happens when we double a coin value (> 2d) eg with a groat 4d

8d = 4 + 4 = 6 +1 + 18d = 4 + 4 = 6 +1 + 1

Shallit’s Improved American SystemShallit’s Improved American System

Average number of coins needed to make 1 to 100 cents Average number of coins needed to make 1 to 100 cents with with

1, 5, 10, 25 denominations is 1, 5, 10, 25 denominations is 4.74.7

Worst case is with only a 1 cent coin: need 99 coins to Worst case is with only a 1 cent coin: need 99 coins to make 99c etcmake 99c etc

Average number needed is Average number needed is 49.549.5What is the best case using only 4 values?What is the best case using only 4 values?1, 5, 18, 291, 5, 18, 29 and and 1, 5, 18, 251, 5, 18, 25 are both better! are both better!They need an average of They need an average of 3.893.89 coins to make coins to make

all 1 all 1 100 cent amounts 100 cent amountsBest to use the Best to use the 1, 5, 18, 251, 5, 18, 25 valuesvalues

And replace the dime with a new 18c pieceAnd replace the dime with a new 18c piece

18c

Improved UK or Euro system ?Improved UK or Euro system ?1, 2, 5, 10, 20, 50, 100, 200p coins1, 2, 5, 10, 20, 50, 100, 200p coins

Average number of coins to make all amounts up to 500 Average number of coins to make all amounts up to 500 is 4.6is 4.6

Add a 133p or 137p coin and the average number falls to Add a 133p or 137p coin and the average number falls to 3.923.92

133p 137p

A Further Improvement?A Further Improvement? Lots of currencies use the pattern of denominationsLots of currencies use the pattern of denominations

1,2,5, and 10,20, 50, and 100, 200, 5001,2,5, and 10,20, 50, and 100, 200, 500

They would do (about 6%) better and use less change withThey would do (about 6%) better and use less change with1,3,4, and 10, 30, 40, and 100, 300, 4001,3,4, and 10, 30, 40, and 100, 300, 400

68 = 50 + 10 + 5 + 2 +1 68 = 50 + 10 + 5 + 2 +1 = 40 +10 + 10 + 4 + 4= 40 +10 + 10 + 4 + 4 134 = 100 + 20 +10 +2 + 2 134 = 100 + 20 +10 +2 + 2 = 100 + 30 + 4= 100 + 30 + 4 243 = 200 + 20 + 20 + 2 +2 243 = 200 + 20 + 20 + 2 +2 = 200 + 40 + 3= 200 + 40 + 3 279 = 200 + 50 +20 + 5 + 2 +2 279 = 200 + 50 +20 + 5 + 2 +2 = 200 + 40 + 30 + 3 + 3 +3= 200 + 40 + 30 + 3 + 3 +3Greedy algorithm has one failure because 6 = 3+3 is better than 6= 4+1+1Greedy 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!Best to do away with the 1c or 1p coin though!