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

Slides:



Advertisements
Similar presentations
22C:19 Discrete Math Algorithms and Complexity
Advertisements

Math for Liberal Studies. There is a list of numbers called weights These numbers represent objects that need to be packed into bins with a particular.
MATH 224 – Discrete Mathematics
MCS 312: NP Completeness and Approximation algorithms Instructor Neelima Gupta
Greedy Algorithms.
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Algorithm Design Techniques: Greedy Algorithms. Introduction Algorithm Design Techniques –Design of algorithms –Algorithms commonly used to solve problems.
The Efficiency of Algorithms
ICS-171:Notes 4: 1 Notes 4: Optimal Search ICS 171 Summer 1999.
KS3 Divisibility Dr J Frost Last modified: 30 th November 2014.
Searching and Sorting Topics  Sequential Search on an Unordered File  Sequential Search on an Ordered File  Binary Search  Bubble Sort  Insertion.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
The Brick Wall: NP-Completeness Christopher King Joshua Greenspan.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Informationsteknologi Friday, November 16, 2007Computer Architecture I - Class 121 Today’s class Operating System Machine Level.
CS 206 Introduction to Computer Science II 10 / 16 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
10/31/02CSE Greedy Algorithms CSE Algorithms Greedy Algorithms.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
10/31/02CSE Greedy Algorithms CSE Algorithms Greedy Algorithms.
MAKEATABLE ORCHART We make a table or chart when we have information that needs to be organized before we can move on and find the solution to a problem.
Lecture 23. Greedy Algorithms
SPANNING TREES Lecture 21 CS2110 – Spring
Pictures Matter Don’t let them get away. Photos are important – people run back into burning homes for them.
1 Chapter 1 Analysis Basics. 2 Chapter Outline What is analysis? What to count and consider Mathematical background Rates of growth Tournament method.
Numerical Methods Applications of Loops: The power of MATLAB Mathematics + Coding 1.
The puzzle is to put the numbers from 1 to 7 into the boxes, so that the two straight lines of numbers add up to the same total. This total is known as.
Creating Mathematical Conversations using Open Questions Marian Small Sydney August, 2015 #LLCAus
22C:19 Discrete Structures Algorithms and Complexity Fall 2014 Sukumar Ghosh.
© 2010 AT&T Intellectual Property. All rights reserved. AT&T and the AT&T logo are trademarks of AT&T Intellectual Property. Case Studies: Bin Packing.
Maths on the move Cookie Count Room 13 – Year 2 Class 2007.
The Fundamentals: Algorithms, the Integers & Matrices.
Greedy Algorithms. Surprisingly, many important and practical computational problems can be solved this way. Every two year old knows the greedy algorithm.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
March 23 & 28, Csci 2111: Data and File Structures Week 10, Lectures 1 & 2 Hashing.
March 23 & 28, Hashing. 2 What is Hashing? A Hash function is a function h(K) which transforms a key K into an address. Hashing is like indexing.
Georgia Institute of Technology Speed part 6 Barb Ericson Georgia Institute of Technology May 2006.
Operational Research & ManagementOperations Scheduling Economic Lot Scheduling 1.Summary Machine Scheduling 2.ELSP (one item, multiple items) 3.Arbitrary.
Exam Technique Read the question Highlight the important information! Read the question again Then answer the question Mark a Minute.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
22C:19 Discrete Math Algorithms and Integers Fall 2010 Sukumar Ghosh.
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
The bin packing problem. For n objects with sizes s 1, …, s n where 0 < s i ≤1, find the smallest number of bins with capacity one, such that n objects.
Computability NP complete problems. Space complexity. Homework: [Post proposal]. Find PSPACE- Complete problems. Work on presentations.
Numbers Properties Learning Objective: Recognise and use multiples, factors, common factor, highest common factor, lowest common multiple and primes; find.
CS 361 – Chapter 10 “Greedy algorithms” It’s a strategy of solving some problems –Need to make a series of choices –Each choice is made to maximize current.
Copyright © 2014 Curt Hill Algorithm Analysis How Do We Determine the Complexity of Algorithms.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Greedy Algorithms.
Big-O notation.
Bin Packing Optimization
CS 2210 Discrete Structures Algorithms and Complexity
Greedy Algorithms.
Searching and Sorting Topics Sequential Search on an Unordered File
CS 2210 Discrete Structures Algorithms and Complexity
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
UMBC CMSC 104 – Section 01, Fall 2016
Searching and Sorting Topics Sequential Search on an Unordered File
B-Trees CSE 373 Data Structures CSE AU B-Trees.
CSE 373, Copyright S. Tanimoto, 2002 B-Trees -
B-Trees CSE 373 Data Structures CSE AU B-Trees.
B-Trees.
B-Trees.
What do you already know?
CS 2210 Discrete Structures Algorithms and Complexity
Presentation transcript:

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

Complexity is All Around Us

=

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 < 10 6 )

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

Four Colours Suffice

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

Monkey Puzzles

6-city Travelling Salesman

8 years of parallel computing

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

Packing Problems

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

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

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

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

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

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?

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

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

Chicken McNugget Numbers 6, 9 and 20

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 = = = = = = 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.

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 !

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

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 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) >  (h/4) 4 + O(h 3 ) The Postage Stamp Problem

The Spare Change Problem St Gaudens $20 Gold Double Eagle

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 = (4 coins) 76 cents = (3 coins) ‘Old money’ in the UK didn’t have the handy property ½, 1, 3, 6, 12, 24, 30 old pence 4 shillings = 48d = = This happens when we double a coin value (> 2d) eg with a groat 4d 8d = =

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

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 p 137 p

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, = = = = = = = = = = = = = = = = Greedy algorithm has one failure because 6 = 3+3 is better than 6= Best to do away with the 1c or 1p coin though!