Title: The Maths of Sorting Things Out
1John D Barrow
Gresham College
DAMTP Cambridge
The Maths of Sorting Things Out
2Complexity is All Around Us
3(No Transcript)
4389965026819938 5569 389965026814369
5Number of ways an even number can be written as
the sum of two primes (4 lt n lt 1000)
6Number of ways an even number can be written as
the sum of two primes (4 lt n lt 106)
7Computer-aided Mathematics Computer
Proofs Experimental mathematics Is Anything
Beyond Computers?
8Four Colours Suffice
9Tractability Easy problems N parts Calculation
time grows as N Hard problems N
parts Calculation time grows as 2N
10Monkey Puzzles
116-city Travelling Salesman
126-city Travelling Salesman
13(No Transcript)
148 years of parallel computing
15Some More Hard ProblemsWhere the best solution
cant be predicted by a general recipe
16Packing Problems
17Examples
- Fitting music tracks on CDs
- Fitting packages in boxes
- Scheduling computer or machine tasks
- Building shelves
- Adverts in TV breaks
- Data storage on disk
- Fitting your holiday stuff in a suitcase
18The Problem
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
19Strategy 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 wont fit. - Then start new disk.
- You cant go back and fill gaps in earlier disks.
- 14 disks used. 3 full.
- 32 out of 140 slots empty
- 108/140 77 filled
20Other Strategies to Try
- First Fit
- You can go back and put tracks on the first disk
in the line that still has room - Worst Fit
- Put next track on the disk that currently has
most space available - Best Fit
- Put next track on the disk that leaves the least
room on the disk after it is added
21Strategy 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,
- Then start new disk.
- You can go back and fill gaps in earlier disks.
- 12 disks used. 6 full.
- 14 slots empty
- 106/120 88 filled
22Sorting 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?
23Strategy 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 wont fit. - Then start new disk.
- You cant go back to fill gaps in earlier disks.
- 14 disks used. 4 full.
- 33 slots empty
- 107/140 76 filled
24Strategy 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
- Then start new disk.
- 11 disks used. 10 full.
- 4 slots empty. Cant do better
- 106/110 96 filled
- THIS IS THE BEST POSSIBLE PACKING
25Chicken McNugget Numbers
6, 9 and 20
26What is the Largest Non-McNugget Number
Any sufficiently large number can me written as
a linear sum of 6s, 9s and 20s
44 69920 45 99999 46 62020 47
99920 48 669999 49 92020
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
doesnt 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
!
The numbers 4,6,9,20 now allow only six
non-McNugget numbers 1,2,3,5,7 and 11
28Whats the smallest total stamp value that cant
fit on the envelope?
29The Postage Stamp Problem
An envelope can only hold a maximum number of
stamps ? h Stamps have values An a1,
a2,..an Sort in ascending order so that 1 a1
lt a2 lt.. lt an We want the smallest integer that
cant be written as N(h, An) F1a1 F2a2 ..
Fnan where F1 F2 .. Fn lt h
The number of consecutive possible postage
amounts that will fit on the envelope is n(h,
An) N(h, An) - 1
Exact solution only known for n 2 n(h, A2)
(h 3 a2)a2 2 for h ? a2 - 2 n(h,2) (h2
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) gt 2.008 ? (h/4)4 O(h3)
30The Spare Change Problem
St Gaudens 20 Gold Double Eagle
31(No Transcript)
32Smallest 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 didnt 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 (gt 2d) eg
with a groat 4d 8d 4 4 6 1 1
33Shallits 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 Best to use the 1, 5, 18, 25
values And replace the dime with a new 18c piece
18c
34Improved 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
137p
133p
35A Further Improvement?
- 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 - 1,3,4, and 10, 30, 40, and 100, 300, 400
- 68 50 10 5 2 1 40 10 10 4 4
- 134 100 20 10 2 2 100 30 4
- 243 200 20 20 2 2 200 40 3
- 279 200 50 20 5 2 2 200 40
30 3 3 3 - Greedy algorithm has one failure because 6 33
is better than 6 411 - Best to do away with the 1c or 1p coin though!