Library tutorials & articles
Bin Packing
By Mitch Dusina, published on 31 Mar 2006
Next Fit Algorithm
The basic algorithm is termed the
Next Fit Algorithm.
This algorithm starts at the beginning of the Elements array and steps
through each one. Once Bin 1 is full, it moves on and starts
placing elements into Bin 2, never looking back to see if an Element in
the future may fit inside Bin 1. This process continues until
there are no more Elements. This is least efficient of the
algorithms but it does have a practical benefit. Think of a
conveyor belt placing items in boxes to be shipped to a single customer. You wouldn't
want to have to reverse the conveyor belt to go back to a partially
empty box to fit in an item that the customer ordered.
After looking at the code you may be worried about the amount of ReDim Preserve statements. A better solution would be to make each Bin Array (the array that holds the Elements) as big as the Bin Height right at the declaration and then just chop of the unused (0) elements after the algorithm is done. An ArrayList could also be used as an alternative.
When the code above executes you will have in your array this structure (using BinHeight = 80, Elements = {26 57 18 8 45 16 22 29 5 11 8 27 54 13 17 21 63 14 16 45 6 32 57 24 18 27 54 35 12 43 36 72 14 28 3 11 46 27 42 59 26 41 15 41 68}):

How to read this graph: Bin Numbers along the bottom, Bin Height/Amount along the left side, Actual Fill Amount along the top, Individual Element Amount inside of bars.
Private Sub NextFit()
'Checks to make sure everything is initialized
If Elements Is Nothing Then Exit Sub
Dim ElementsCopy(Elements.GetUpperBound(0)) As Integer
ReDim Bins(0)
'Bin Number we are on, Bin Element we are on, Amount placed in the current Bin
Dim BinNumber, BinElement, BinCount As Integer
Dim i As Integer
'Make a copy of the array incase we need to sort it
DeepCopyArray(Elements, ElementsCopy)
'Sort in descending order if needed
If Me.Decreasing = True Then
Array.Sort(ElementsCopy)
Array.Reverse(ElementsCopy)
End If
'Declare the first element in the first Bin
ReDim Bins(0)(0)
'Loop through each Element and place in a Bin
For i = 0 To ElementsCopy.GetUpperBound(0)
'If True, move on to next Bin
If BinCount + ElementsCopy(i) > Me.BinHeight Then
'Remove extra, unused Element of this Bin
ReDim Preserve Bins(BinNumber)(BinElement - 1)
'Add another Bin
ReDim Preserve Bins(BinNumber + 1)
BinNumber += 1
'Initialize first element of new bin
ReDim Bins(BinNumber)(0)
BinElement = 0
BinCount = 0
End If
'Place task
Bins(BinNumber)(BinElement) = ElementsCopy(i)
'Keep track of how much is stored in this bin
BinCount += ElementsCopy(i)
'Add Element unless we are done
If i < ElementsCopy.GetUpperBound(0) Then
ReDim Preserve Bins(BinNumber)(BinElement + 1)
BinElement += 1
End If
Next
GC.Collect()
End Sub
After looking at the code you may be worried about the amount of ReDim Preserve statements. A better solution would be to make each Bin Array (the array that holds the Elements) as big as the Bin Height right at the declaration and then just chop of the unused (0) elements after the algorithm is done. An ArrayList could also be used as an alternative.
When the code above executes you will have in your array this structure (using BinHeight = 80, Elements = {26 57 18 8 45 16 22 29 5 11 8 27 54 13 17 21 63 14 16 45 6 32 57 24 18 27 54 35 12 43 36 72 14 28 3 11 46 27 42 59 26 41 15 41 68}):
How to read this graph: Bin Numbers along the bottom, Bin Height/Amount along the left side, Actual Fill Amount along the top, Individual Element Amount inside of bars.
Related articles
Related discussion
-
How to write the category attribut in a class dynamically
by converter2009 (1 replies)
-
VB.NET: Hide and show table using radio buttons
by converter2009 (1 replies)
-
VB.Net Button Problem
by pysdex (0 replies)
-
Unable to access AxInterop.AcoPdflib.dll on 64 bit OS
by Shaila14041981 (0 replies)
-
Very Urgent regarding deleting the images from a folder
by Nanosteps (6 replies)
Related podcasts
-
xpert to Expert: Inside Concurrent Basic (CB)
"Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...
keen to have a look the c++ code.
zxy901109@hotmail.com
cheers
!--removed tag-->Hi Nimpo
thank you for this fine sourcecode, it helps me in understanding the problem of bin packing a bit, a porblem i just want to solve in my access database
but i have a question and something i did not found in your programm.
you mentioned the possibility of bins with different heights, but i did not find this in the sourcecode or how to enter it
and anothe question, do you think that your solution will work in combination with an existing access database ?
thanks
Andreas
Hello,
I really want to thank you for for this great article. However, I am currently working on my last year project, which is about 2 dimensional sheet cutting optimizer. Unfortunalty, I did not find any article that explains how to implement it by code.
Please can any one help me, this is very important because I just have about 2 weeks to finish it.
If any one has some helpful resources please send it to my email mishoco@hotmail.com
Michel
Hi,
i urgently need the codes too before 19/09/07. pls help me. could you send me the C++ and java version of the codes. Tankyou. send to nuranastacia@yahoo.com
can u plz send me a 'C++' or 'C' program code for first fit algorithms....
i need it urgently.... i need to submit code as my project by saturday 09/15/07...
example output:
enter no. of process to be loaded: 4
Job # Job size Arrival time
1 40K 0 msec
2 15K 7 msec
3 10K 5 msec
4 45K 10 msec
press any key to continue...
note: Job size and Arrival time will be inputted by the user and as you press enter in the Job size, "K" will be automatically displayed as well as the "msec" in the arrival time.
I believe that the First Fit algorithm will always be better than or equivalent to the Next Fit algorithm.
I first learned of the Bin Packing problem from an Algebra 3 class in high school. I implemented all of the algorithms the textbook talked about. Doing some quick online research, the only other algorithm I found a mention of was the "Almost Worse Fit". It simply places the object in the second emptiest bin (which is slightly better than Worse Fit apparently).
I cannot myself convert my code into other languages (I do not have the time or the motivation). But the concepts used in my code are not very language specific. It shouldn't take much time to do the conversion. If you do not know the target language well then you will simply have to search online for the syntax for that particular structure (Arrays, sorting, and drawing is all there really is to it, and I would think that drawing isn't that important). Plus, that way you'd actually be learning the language you are supposed to learn =)
i am in the same situation can u plz send me a C or C++ code for best/first/worst fit algorithms....
i need it to complete my assignment
bion365@gmail.com
thanks
can u plz send me a 'C' program code for best/first/worst fit algorithms....
i need it urgently.... i need to submit code as assignment......PLZ HELP
rrookwood_jm@yahoo.com
hey... grt job buddy...
can u plz send me a 'C' program code for best/first/worst fit algorithms....
i need it urgently.... i need to submit code as assignment..... by sunday...
plz help....
ritusmart2001@yahoo.com
Hello,
I'm new here and maybe missing something. Everybody says 'Great article' - but where the article itself? Same for code - is there code example or algorithm available?
Thanks.
Have you provided all the algorithms that are available to solve Bin Packing ?
I would also like to see a solution for the 0-1 Knapsack problem where each article(File) have a weight(MB) as well as a value(priority). The sack capacity let's say 700MB.
Thank you.
Great article. Thanks.
Hi,
Excellent article. Would you know how or have a PHP version of this problem?
Regards, Michael
I have a quick question. Can there ever be more bins in a First Fit Algorithm than a Next Fit algorithm?
Well as for variable sized bins, all you would need to do is have an array of Integers that holds the size for each Bin. Then in my code, where ever there is a reference to BinHeight, you would simply reference BinHeightArray(ThisBinNumber) instead. You could incorporate all this code into its own Class as it should be an make the array a property so you can do bounds checking, check for valid values, and all that good stuff.
As for 2-dimensional Bin Packing... I'd have to get back to you on that one. I understand what you mean but I don't think these algorithms are easily translatable into another dimension... Try to formulate an algorithm that starts filling in the lower left to the lower right, and then starts moving up. I can't vouch for the efficiency of this idea but its my only lead...
Great article, I was looking for something exactly like this! How difficult would it be to modify the code to allow for varible size bin widths, and then pack each element either horizontal or vertial depending on height and width. If you can figure that out let me know. Thanks and keep up the good work.![Smiley Face [:)]](/emoticons/emotion-1a.gif)
This thread is for discussions of Bin Packing.