Bin Packing Algorithms (Edexcel A Level Further Maths: Decision Maths 1)

Revision Note

Test Yourself
Paul

Author

Paul

Expertise

Maths

Introduction to Bin Packing Algorithms

  • Bin packing algorithms organise objects into as few containers as possible
  • The containers are called bins
    • In problems all bins will be of equal size
      • Size may refer to length, width, capacity (volume), weight, etc
  • 'Packing' is a loose term that could mean 'separating', 'cutting' or similar
  • Examples of bin packing problems include
    • loading pallets (objects) of varying weights into lorries (bins) without exceeding their weight capacity
      • with the aim of minimising the number of lorries required
    • cutting short strips of cable (objects) from large reels of cable (bins)
      • with the aim of minimising wastage at the end of a reel
  • There are two bin packing algorithms covered
    • the first-fit bin packing algorithm
    • the first-fit decreasing bin packing algorithm
    • Both are heuristic algorithms - they will provide a (good) solution
      • but not necessarily an optimal solution
      • and not necessarily the only solution

First-Fit Bin Packing Algorithm

What is the first-fit bin packing algorithm?

  • The first fit bin packing algorithm packs objects into bins in the order in which they are presented (listed)
    • e.g.  organising scattered boxes in the order they are encountered instead of organising them first
  • The algorithm finds the number of bins required to pack all the objects
    • ideally this would be the minimum number of bins required
    • but the algorithm will not necessarily provide the optimal solution
  • An advantage of using the first-fit bin packing algorithm is speed
    • the objects do not have to be sorted (into size or any other criteria) first
    • in many cases, the solution obtained, will be sufficient for business or practical purposes
      • i.e.  speed (efficiency) is of more importance than optimisation

How do I apply the first-fit bin packing algorithm?

  • Each object, in turn, is placed in the first available bin that has enough capacity (room) for it
    • Object 1 (the first on the list) will be placed into bin 1
      • if bin 1 then has enough spare capacity, object 2 (the second on the list) will also go into bin 1
      • if not, object 2 will need a new bin, bin 2
      • object 3 would be considered for bin 1 first, then bin 2, and failing both, it would require a new bin, bin 3
  • For each object, if any existing bin does not have enough capacity remaining, a new bin will be required
    • For example,
      • if a bin has capacity 10 and the first two objects are of capacity 6 and 3, the 6 will go into bin 1, then the 3 will go into bin 1 too
        (6 + 3 = 9 ≤ 10)
      • if the first two objects are of capacity 5 and 9, the 5 would go in bin 1, then the 9 would go into bin 2
        (5 + 9 = 14 > 10)
  • The algorithm is complete when all objects have been placed in a bin

How do I find the lower bound for the number of bins?

  • The lower bound (LB) for the number of bins required in the first-fit bin packing problem is the smallest integer that satisfies

 LB greater or equal than fraction numerator Total space capacity space of space all space objects over denominator Size space of space each space bin end fraction

  • As it is a number of bins, the lower bound will be an integer
    • even if the calculation gave the value 3.01, say, more than 3 bins would be required, and so the lower bound will be 4
    • i.e.  always round up (unless the value happens to be an exact integer)
  • Note that this is exactly the same as for the first-fit decreasing bin packing algorithm

Exam Tip

  • As you place values into bins, you may find it helpful to jot down either the total capacity used in each bin or the remaining capacity in each bin next to it
    • However, do not include these as part of your final answer

Worked example

A gym worker is tidying up by stacking weights on shelves.  The weights, given in kilograms, are stated below.

12,  16,  18,  8,  16,  12,  10,  4,  6,  10,  12, 16,  20,  10,  24  

Each shelf has a load capacity of 40 kg.

a)
Find a lower bound for the number of shelves needed so all the weights can be stacked on the shelves.

The lower bound will be (greater than or equal to) the total of the weights divided by the capacity (load) of one shelf

table row LB greater or equal than cell fraction numerator 12 plus 16 plus 18 plus 8 plus 16 plus 12 plus 10 plus 4 plus 6 plus 10 plus 12 plus 16 plus 20 plus 10 plus 24 over denominator 40 end fraction end cell row LB greater or equal than cell 194 over 40 end cell row LB greater or equal than cell 4.85 end cell end table

The number of shelves (bins) will need to be an integer (hence the inequality), more than four shelves are needed so round up to five 

Lower bound for the number of shelves is 5

b)
Use the first-fit bin packing algorithm to find the number of shelves needed to stack all of the weights.

In this case, a 'shelf' will be a 'bin'
In the first-fit bin packing algorithm, we deal with each weight in turn as they are listed
(To help, we have written the remaining capacity in brackets next to each shelf in the working area, this is optional)
The first weight, 12 kg, will go on the first shelf

Shelf 1: 12         [28]

The second weight, 16 kg will also fit on the first shelf

Shelf 1: 12 16       [12]

Next, the 18 kg will need to go on to a new shelf (shelf 2)

Shelf 1: 12 16       [12]
Shelf 2: 18         [22]

The 8 kg weight will go on the first shelf

Shelf 1: 12 16 8     [4]
Shelf 2: 18         [22]

Continuing in this manner, the 16 kg weight can join shelf 2;  the second 12 kg weight will need a third shelf

Shelf 1: 12 16 8     [4]
Shelf 2: 18 16       [6]
Shelf 3: 12         [28]

Continuing in order - a fourth shelf is needed for the third 12 kg weight and the 20 kg and 24 kg weights require a shelf of their own
When finished, double check each shelf does not exceed 40 kg in total and do not include the spare capacities with the final answer

Shelf 1: 12 16 8 4
Shelf 2: 18 16 6  
Shelf 3: 12 10 10  
Shelf 4: 12 16 10  
Shelf 5: 20      
Shelf 6: 24      

Notice that, whilst the lower bound was 5 shelves, the algorithm used 6 shelves

First-Fit Decreasing Bin Packing Algorithm

What is the first-fit decreasing bin packing algorithm?

  • The first fit decreasing bin packing algorithm packs objects into bins once they have been arranged into decreasing (descending) order
    • e.g.  arranging scattered boxes in order of size/weight first, then organising them, in descending order, starting with the biggest/heaviest
  • The algorithm finds the number of bins required to pack all the objects
    • ideally this would be the minimum number of bins required
    • but the algorithm will not necessarily provide the optimal solution
  • An advantage of using the first-fit decreasing bin packing algorithm is that it usually gets close to the optimal solution
    • this would be where minimising the number of bins is more desirable than speed

How do I apply the first-fit decreasing bin packing algorithm?

  • Each object, in turn, is placed in the first available bin that has enough capacity (room) for it
    • Object 1 (the biggest) will be placed into bin 1
      • if bin 1 then has enough spare capacity, object 2 (the second biggest) will also go into bin 1
      • if not, object 2 will need a new bin, bin 2
      • object 3 would be considered for bin 1 first, then bin 2, and failing both, it would require a new bin, bin 3
    • For each object, if any existing bin does not have enough capacity remaining, a new bin will be required
    • For example,
      • if a bin has capacity 15 and the first two objects are of capacity 13 and 10, the 13 will go into bin 1, then the 10 will go into bin 2
        (as 13 + 10 = 23 > 15)
      • if the first two objects are of capacity 8 and 6, the 8 would go in bin 1, then the 6 would go into bin 1 too
        (as 8 + 6 = 14 ≤ 15)
  • The algorithm is complete when all objects have been placed in a bin

How do I find the lower bound for the number of bins?

  • The lower bound (LB) for the number of bins required in the first-fit decreasing bin packing problem is the smallest integer that satisfies

 LB greater or equal than fraction numerator Total space capacity space of space all space objects over denominator Size space of space each space bin end fraction

  • As it is a number of bins, the lower bound will be an integer
    • even if the calculation gave the value 3.01, say, more than 3 bins would be required, and so the lower bound will be 4
    • i.e.  always round up (unless the value happens to be an exact integer)
  • Note that this is exactly the same as for the first-fit bin packing algorithm

Exam Tip

  • A question may have a previous part that asks you to use a bubble sort or a quick sort to arrange values into descending order
  • If not, you may be asked to name a suitable algorithm that would sort values into descending order
      • in which case 'bubble sort' or 'quick sort' can be stated
      • you may be asked to briefly explain these algorithms
  • As you place values into bins, you may find it helpful to jot down either the total capacity used in each bin or the remaining capacity in each bin next to it
    • However, do not include these as part of your final answer

Worked example

A gym worker is tidying up by stacking weights on shelves.  A list of the weights at the gym, given in kilograms, in descending order is stated below.

24,  20,  18,  16,  16,  16,  12,  12,  12,  10,  10,  10,  8,  6,  4

Each shelf has a load capacity of 40 kg.  The lower bound for the number of shelves required to stack all of the weights is 5.
Use the first-fit decreasing bin packing algorithm to stack the weights on to as few shelves as possible, explaining how you know your answer is optimal.

A 'shelf' will be a 'bin'
For the decreasing first-fit bin packing algorithm, assign the weights starting with the heaviest, in descending order
The first weight, 24 kg, will go on the first shelf

Shelf 1: 24         [16]

The second weight, 20 kg will not fit on the first shelf so a new shelf is needed

Shelf 1: 24         [16]
Shelf 2: 20         [20]

Shelf 2 has enough capacity to take the next, 18 kg weight

Shelf 1: 24         [16]
Shelf 2: 20 18       [2]

The first of the 16 kg weights can fill shelf 1

Shelf 1: 24 16       [0]
Shelf 2: 20 18       [2]

Thinking ahead we can now see that the lowest weight is 4 kg, but there is only 2 kg of spare capacity in the first two shelves
Therefore we know we can no longer use shelves 1 or 2
The other two 16 kg weights can both go on to shelf 3

Shelf 1: 24 16       [0]
Shelf 2: 20 18       [2]
Shelf 3: 16 16       [8]

Continuing in order - a fourth shelf is needed for the three 12 kg weights (leaving 4 kg spare) and so a fifth shelf is needed for the three 10 kg weights (leaving 10 kg spare)
There is then spare capacity on shelves 3, 5 and 4 respectively for the 8 kg, 6 kg and 4 kg weights
Double check your final answer and do not include the spare capacities

Shelf 1: 24 16    
Shelf 2: 20 18    
Shelf 3: 16 16 8  
Shelf 4: 12 12 12 4
Shelf 5: 10 10 10 6

This uses the same number of bins as the lower bound stated in the question

This solution is optimal as the number of bins required is the same as the lower bound given

Other arrangements on 5 shelves may be possible

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Paul

Author: Paul

Paul has taught mathematics for 20 years and has been an examiner for Edexcel for over a decade. GCSE, A level, pure, mechanics, statistics, discrete – if it’s in a Maths exam, Paul will know about it. Paul is a passionate fan of clear and colourful notes with fascinating diagrams – one of the many reasons he is excited to be a member of the SME team.