Bin Packing Algorithms
Bin Packing Algorithms
Date: 2009.1.15
Project12 by Group05
Content
Chapter 1: Introduction 3
Chapter 2: Data Structure / Algorithm Specification....4
Next Fit ..4
First Fit 5
Best Fit.8
First Fit Decreasing.9
Chapter 3: Testing Results...10
Time Complexity10
Bin number...15
Chapter 4: Analysis and Comments16
Appendix: Source Code.........23
References..46
Athor list.....47
Declaration....47
Signatures..............................................47
Project12 by Group05
Chapter 1:
Introduction
Project12 by Group05
create a new bin if not, and is significantly faster than the first-fit. Of
course, it uses more bins. Best fit is the algorithm places objects in the
tightest spot among all bins. It perform better for random inputs and
needs O( NlogN) time.
Chapter 2:
Algorithm 1:
Project12 by Group05
Project12 by Group05
Project12 by Group05
Operation:
- Creation(n): to create a winner tree with at most n players;
- Initialize(player[]): to insert the players in the external nodes
and play the the whole game.
- Replay: when the value of one player has changed, the game
has to been replayed.
- Winner(a,b): to decide the winner of player a and player b;
Advantages:
When the value of one player has changed, it is easy and fast to
replay the game in O(logN) time. More detailed analysis of the time
complexity is shown in Chapter 4.
Pseudo Code of First Fit:
Int FirsttFit (int Num, int Item[], int C)
{
CreateAWinnerTree(Num);
Initialize(bin[]=C); //the bins are the players and the available space is the value
of each player
for(Item[i]){
start from the leftchild of the root T[Q];
do {
if (Item[i] can be fit in bin[T[Q]])
go to the leftchild of T[Q] and go on checking;
else (Item[i] cannot be fit in bin[T[Q]]){
7
Project12 by Group05
Project12 by Group05
SplayTree
for(each Item[i]){
if(Tree==NULL){//no bins in the tree
Create a bin in the Tree and pack Item[i] in it;
bin_num++;
continue; //go on packing the next bin
}
find the bin in the tree whose space is closest to Item[i];
if(Item[i] cannot fit in the bin found){
Create a bin in the Tree and pack Item[i] in it;
bin_num++;
continue; //go on packing the next Item[i]
}
else if (Item[i] exactly fits the bin found)
Delete the bin from the Tree ; //since the bin is full
else if (the bin found is a little larger the Item[i]){
pack the Item[i] in the bin found and Update the Tree;
continue; //go to the next item;
}
}
return bin_num;
}
Project12 by Group05
10
Chapter 3:
Testing Results
In this chapter, we use several test cases to test the running time and
bin number of the four different algorithms. Whats more, we also test
the bin contain level of each algorithm, the bin contain level refers to
how full the bin is. Here, the specific testing input data is not included in
this report but will be contained in a file called Test.
Project12 by Group05
Figure 1
11
( unit: second)
TOTAL NUMBER
100
500
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
11000
12000
13000
14000
NEXTFIT
FIRSTFIT
0.000005
0.000019
0.000034
0.000066
0.000103
0.000131
0.000164
0.000206
0.000240
0.000274
0.000295
0.000328
0.000378
0.000394
0.000446
0.000459
0.000045
0.000273
0.000594
0.001314
0.002066
0.002812
0.003670
0.004422
0.005176
0.006000
0.006901
0.007970
0.008678
0.009602
0.010289
0.011225
BESTFIT
FIRSTFITDECREASING
0.000048
0.000266
0.000547
0.001062
0.001643
0.002188
0.002735
0.003295
0.003852
0.004376
0.004928
0.005470
0.005900
0.006590
0.007184
0.007704
0.000052
0.000304
0.000656
0.001438
0.002297
0.003060
0.004140
0.004898
0.005718
0.006504
0.007604
0.008590
0.009900
0.010542
0.011513
0.012324
Case 2:
When the input is in decreasing order, we test the running time of
the four algorithms in different total number.
The testing result is shown in Figure 2 as follows:
Figure 2
TOTAL NUMBER
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
( unit: second)
NEXTFIT
0.000028
0.000053
0.000075
0.000106
0.000125
0.000150
0.000197
0.000212
0.000239
0.000266
FIRSTFIT
0.000594
0.001282
0.002066
0.002752
0.003675
0.004428
0.005282
0.005880
0.006892
0.00797
BESTFIT
0.002765
0.009218
0.022192
0.038748
0.065855
0.094880
0.125662
0.176000
0.226919
0.29109
FIRSTFITDECREASING
0.000656
0.001406
0.002300
0.003000
0.004065
0.004892
0.005831
0.006496
0.007604
0.00859
11
Project12 by Group05
12
Case 3:
When the input is in increasing order, we test the running time of the
four algorithms in different total number.
The testing result is shown in Figure 3 as follows:
Figure 3
( unit: second)
TOTALNUMBER
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
NEXTFIT
FIRSTFIT
BESTFIT
0.000030
0.000053
0.000080
0.000100
0.000133
0.000150
0.000186
0.000200
0.000239
0.000265
0.000563
0.001220
0.001970
0.002624
0.003515
0.004235
0.005063
0.005752
0.006622
0.007500
0.000656
0.001624
0.003003
0.005252
0.007345
0.010169
0.013535
0.016000
0.021676
0.025780
FIRSTFITDECREASING
0.000641
0.001406
0.002300
0.003000
0.004140
0.004988
0.005718
0.006496
0.007459
0.008600
Case 4:
When the input is in different order, we test the running time of the
Next Fit algorithm in different total number.
The testing result is shown in Figure 4 as follows:
Case 5:
When the input is in different order, we test the running time of the First
Fit algorithm in different total number.
The testing result is shown in Figure 5 as follows:
12
Project12 by Group05
Figure 4
TOTAL NUMBER
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
(unit: second)
NEXTFIT
INCREASING
0.000030
0.000053
0.000080
0.000100
0.000133
0.000150
0.000186
0.000200
0.000239
0.000265
NEXTFIT
DECREASING
0.000028
0.000053
0.000075
0.000106
0.000125
0.000150
0.000197
0.000212
0.000239
0.000266
Figure 5
TOTAL NUMBER
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
13
NEXT FIT
RANDOM
0.000034
0.000066
0.000103
0.000131
0.000164
0.000206
0.000240
0.000274
0.000295
0.000328
( unit: second)
FIRSTFIT
INCREASING
0.000563
0.001220
0.001970
0.002624
0.003515
0.004235
0.005063
0.005752
0.006622
0.007500
FIRSTFIT
INCREASING
0.000594
0.001282
0.002066
0.002752
0.003675
0.004428
0.005282
0.005880
0.006892
0.00797
FIRSTFIT
RANDOM
0.000594
0.001314
0.002066
0.002812
0.003670
0.004422
0.005176
0.006000
0.006901
0.007970
Case 6:
When the input is in different order, we test the running time of the Best
Fit algorithm in different total number.
The testing result is shown in Figure 6 as follows:
13
Project12 by Group05
Figure 6
14
( unit: second)
TOTAL NUMBER
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
BESTFIT
INCREASING
0.000656
0.001624
0.003003
0.005252
0.007345
0.010169
0.013535
0.016000
0.021676
0.025780
BESTFIT
DECREASING
0.002765
0.009218
0.022192
0.038748
0.065855
0.094880
0.125662
0.176000
0.226919
0.29109
BESTFIT
RANDOM
0.000547
0.001062
0.001643
0.002188
0.002735
0.003295
0.003852
0.004376
0.004928
0.005470
Case 7:
According to our previous analysis, we thought that the size of the
bin may affect the running time of the Best Fit algorithm. Here, we
provide a test for Best Fit, to compare the running time of Best Fit when
bin size is 10e7 and 10, respectively.
The testing result is shown in Figure 7 as follows:
Figure 7
(unit: second)
TOTALNUMBER
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
BESTFIT
1E7
0.000547
0.001062
0.001643
0.002188
0.002735
0.003295
0.003852
0.004376
0.004928
0.00547
BESTFIT
10
0.000203
0.000374
0.000517
0.000752
0.00094
0.001133
0.001324
0.001496
0.001685
0.00188
14
Project12 by Group05
15
ORDER
Random
Increasing
NEXT FIT
FIRSTFIT
66890
64598
50970
64598
BESTFIT
50709
64598
FIRSTFITDEC
REASING
50426
50519
IDEAL
50401
50444
Case 9:
In different total numbers, we test the bin number of the four
algorithms and also compare with the idea number of bin needed.
Furthermore, we also test the bin contain level of each algorithm, that is,
how full is each bin on average.
The testing result is shown is Figure 9 as follows:
In the following Figure, the data with % refers to the bin contain level,
and data beside is the bin number.
15
Project12 by Group05
16
Figure 9
TOTAL NUMBER
10
100
1000
10000
100000
NEXTFIT
88.80%
5
75.65%
62
75.68% 684
75.31% 6711
75.31% 66815
Chapter 4:
FIRSTFIT
88.80%
5
91.96%
51
97.46% 555
98.89% 5211
98.89% 50880
BESTFIT
88.80%
5
91.96%
51
98.29% 547
99.29% 5167
99.29% 50673
FIRSTFITDECREASING IDEAL
88.80%
5
5
97.71%
48
47
99.62%
543
523
99.78%
5098
5079
99.78%
50428
50316
0.014000
0.012000
0.010000
NextFit
0.008000
FirstFit
0.006000
BestFit
0.004000
FirstFitDecreasing
0.002000
0.000000
16
Project12 by Group05
17
0.350000
0.300000
0.250000
NextFit
0.200000
FirstFit
0.150000
BestFit
0.100000
FirstFitDecreasing
0.050000
0.000000
0.010000
0.009000
0.008000
0.007000
0.006000
0.005000
0.004000
0.003000
0.002000
0.001000
0.000000
NextFit
FirstFit
FirstFitDecreasing
0.030000
0.025000
0.020000
0.015000
0.010000
NextFit
FirstFit
BestFit
FirstFitDecreasing
0.005000
0.000000
17
Project12 by Group05
18
0.000350
0.000300
0.000250
0.000200
0.000150
NextFit increasing
NextFit decreasing
Next fit random
0.000100
0.000050
0.000000
From Diagram 5 above, we see that Next Fit runs almost the same
when the input is in increasing order and decreasing order, but runs
much more slowly when the input is in random order.
18
Project12 by Group05
19
0.009000
0.008000
0.007000
0.006000
FirstFit increasing
0.005000
FirstFit decreasing
0.004000
firstfit random
0.003000
0.002000
0.001000
0.000000
From the Diagram 6 above, we see that First Fit runs almost the same
no matter what order in the input.
Diagram 7 : Best Fit
0.350000
0.300000
BestFit increasing
0.250000
0.200000
BestFit decreasing
0.150000
BestFit random
0.100000
0.050000
10000
9000
8000
7000
6000
5000
4000
3000
2000
1000
0.000000
Form Diagram 7 above, we see that Best Fit runs the fastest when the
input is in random and runs a little slowly when the input is in increasing
order. And when the input is in decreasing order, Best Fit runs much
really slowly.
19
Project12 by Group05
20
Analysis of Case 7:
According to the testing result in Figure 7, we get the following
Diagram 8, which is showing the running time of Best Fit when the bin
size is 10e7 and 10 respectively.
Diagram 8 : Best Fit with different bin size
0.006
0.005
0.004
0.003
BestFit 1e7
BestFit 10
0.002
0.001
0
As what we have argued before, how fast the Best Fit runs is related
to the bin size. From the Diagram 8 above, we see that , the larger the
bin size is ,the more slowly Best Fit runs.
20
Project12 by Group05
21
120.00%
100.00%
80.00%
NextFit
60.00%
FirstFit
BestFit
40.00%
FirstFitDecreasing
20.00%
0.00%
10
100
1000
10000
100000
From the Diagram 9, we see that when the total number is small, the
average bin contain level of the four algorithms is almost the same. But
when the total number get larger, the difference is more apparent, with
that First Fit Decreasing is the best and Next Fit is the worst. The results
also reflect that, with the same total weight of all the items, the First Fit
Decreasing use the fewest number of bins and the second fewest is Best
Fit, and then the First Fit. The Next Fit use the most bins.
Conclusion:
Next Fit
In the next fit program, we use two for loops. And when all the
objects are put in the bins and z, which we use to count the number of
those objects in the bins, increases to n. In this case, we can never come
into the second for loop again, so we can see the results of our test
21
Project12 by Group05
22
) if the sizes of
the elements are all different from each other. When the total number
of sizes kinds are far larger than the number of the elements, time
complexity of this program is O(
size kinds are far smaller than the number of the elements, time
complexity of this program is near O(N).
22
Project12 by Group05
23
So when the total number of size kinds is small, Best fit performs
much better than First fit on time.
As it is seen in the figure, best fit use less bins than first fit.
First Fit Decreasing
First fit decreasing is similar to first fit algorithm. But the difference is ,
the objects must be sorted firstly. So, it uses more time, but because the
quick sort does not affect the complexity, it still just needs O(NlogN)
time. And because the objects are sorted firstly, it needs the fewest bins.
Appendix:
1. WinnerTree.c
#include<stdio.h>
#include<stdlib.h>
struct WinnerTree;
typedef struct WinnerTree* WTree;
int
struct WinnerTree
23
Project12 by Group05
46
fdr=FirstFitDecreasing(Total,S,BinSize);
stop = clock();
tFD=((double)(stop - start))/CLK_TCK;
tFD/=FFD_repeat;
AVG:%lf
AVG:%lf
AVG:%lf
printf("FirstFitDecreasing: %d
Total time:%lf\n",nr,dstN,tN);
Total time:%lf\n",fr,dstF,tF);
Total time:%lf\n",br,dstB,tB);
AVG:%lf Total time:%lf\n",fdr,dstFD,tFD);
return 1;
}
References:
[1]Mark Allen Weiss, Data Structures and Algorithm Analysis in C,
POSTS&TELECOM PRESS, 2005.
[2] http://en.wikipedia.org/wiki/Winner_tree
[3] http://en.wikipedia.org/wiki/Splay_tree
46
Project12 by Group05
47
Author Lists:
Programmer:
Report Writer:
Tester
PPT maker
Declaration:
We hereby declare that all the work done in this project titled
"Bin Packing Heuristics" is of our independent effort as a
group.
Signatures:
47