We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 24,597 times

Contents

Downloads

Related Categories

Bin Packing - First Fit Algorithm

Nimpo

First Fit Algorithm

  Now let's talk about an algorithm with a little more "intelligence".  The First Fit Algorithm does exactly as its name implies.  It steps through the Elements sticking them into the first Bin it can, if there aren't any Bins that it will fit into, a new Bin is added.  You will get pretty efficient use of Bins with this algorithm.

   Private Sub FirstFit()
'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, j, k 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)
Dim bPlaced As Boolean = False

'Loops through each Bin to find the first available spot
For j = 0 To BinNumber
BinElement = Bins(j).GetUpperBound(0)

'Count the amount placed in this Bin
BinCount = 0
For k = 0 To BinElement
BinCount += Bins(j)(k)
Next

If BinCount + ElementsCopy(i) <= Me.BinHeight Then
'There's room for this Element
ReDim Preserve Bins(j)(BinElement + 1)
Bins(j)(BinElement) = ElementsCopy(i)

bPlaced = True
Exit For
Else
'There's not room for this Element in this Bin
End If
Next


'There wasn't room for the Element in any existing Bin
'Create a new Bin
If bPlaced = False Then
'Add another Bin
ReDim Preserve Bins(BinNumber + 1)
BinNumber += 1

'Initialize first element of new bin
ReDim Bins(BinNumber)(1)
BinElement = 0

Bins(BinNumber)(BinElement) = ElementsCopy(i)

End If
Next

'All Elements have been place, now we go back and remove unused Elements
For i = 0 To BinNumber
For j = 0 To Bins(i).GetUpperBound(0)
If Bins(i)(j) = 0 Then
ReDim Preserve Bins(i)(j - 1)
End If
Next
Next

GC.Collect()
End Sub

With the same set of data as the last example, this algorithm will lay out something like this in memory:


Languages I know (from most comfortable with, to least comfortable with): VB.NET, C#, VB6, and C/C++

I'm 18 years old and pursuing a BS in Computer Science at the Colorado School of Mines. My dad introduced me to programming (with Visual Basic 6) when I was about 10. When I was 13 I was accepted and enrolled at Arapahoe Community College for a programming class and continued there during the summers while in high school.


Check me out!

Comments

  • Re: [5540] Bin Packing

    Posted by Yak on 22 Jun 2008

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

  • Re: [5540] Bin Packing

    Posted by mishoco on 17 Apr 2008

    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&nbs...

  • Re: [5540] Bin Packing

    Posted by Roque on 16 Mar 2008

     hallo, i'm from poland.

    can u please send me a C program code for the best, next and first fit algorithms?

     

    my email: roque@poczta.onet.pl 

    thank you:) ...

  • Re: [5540] Bin Packing

    Posted by MisSya on 16 Sep 2007

    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

  • Re: [5540] Bin Packing

    Posted by kirby on 12 Sep 2007

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