These algorithms are for Bin Packing problems where items arrive one at a time (in unknown order), each must be put in a bin, before considering the next item.

1. Next Fit:When processing next item, check if it fits in the same bin as the last item. Use a new bin only if it does not.

ntnextFit(intweight[], intn, intc)

{

// Initialize result (Count of bins) and remaining

// capacity in current bin.

intres = 0, bin_rem = c;

// Place items one by one

for(inti=0; i<n; i++)

{

// If this item can't fit in current bin

if(weight[i] > bin_rem)

{

res++; // Use a new bin

bin_rem = c - weight[i];

}

else

bin_rem -= weight[i];

}

returnres;

}

Next Fit is a simple algorithm. It requires only O(n) time and O(1) extra space to process n items.

Next Fit is 2 approximate, i.e., the number of bins used by this algorithm is bounded by twice of optimal. Consider any two adjacent bins. The sum of items in these two bins must be > c; otherwise, NextFit would have put all the items of second bin into the first. The same holds for all other bins. Thus, at most half the space is wasted, and so Next Fit uses at most 2M bins if M is optimal.

2. First Fit:When processing the next item, see if it fits in the same bin as the last item. Start a new bin only if it does not.

intfirstFit(intweight[], intn, intc)

{

// Initialize result (Count of bins)

intres = 0;

// Create an array to store remaining space in bins

// there can be at most n bins

intbin_rem[n];

// Place items one by one

for(inti=0; i<n; i++)

{

// Find the first bin that can accommodate

// weight[i]

intj;

for(j=0; j<res; j++)

{

if(bin_rem[j] >= weight[i])

{

bin_rem[j] = bin_rem[j] - weight[i];

break;

}

}

// If no bin could accommodate weight[i]

if(j==res)

{

bin_rem[res] = c - weight[i];

res++;

}

}

returnres;

}

The above implementation of First Fit requires O(n2) time, but First Fit can be implemented in O(n Log n) time using Self-Balancing Binary Search Trees.

If M is the optimal number of bins, then First Fit never uses more than 1.7M bins. So First Fit is better than Next Fit in terms of upper bound on number of bins.

3. Best Fit:The idea is to places the next item in the *tightest* spot. That is, put it in the bin so that smallest empty space is left.

intbestFit(intweight[], intn, intc)

{

// Initialize result (Count of bins)

intres = 0;

// Create an array to store remaining space in bins

// there can be at most n bins

intbin_rem[n];

// Place items one by one

for(inti=0; i<n; i++)

{

// Find the best bin that ca\n accomodate

// weight[i]

intj;

// Initialize minimum space left and index

// of best bin

intmin = c+1, bi = 0;

for(j=0; j<res; j++)

{

if(bin_rem[j] >= weight[i] &&

bin_rem[j] - weight[i] < min)

{

bi = j;

min = bin_rem[j] - weight[i];

}

}

// If no bin could accommodate weight[i],

// create a new bin

if(min==c+1)

{

bin_rem[res] = c - weight[i];

res++;

}

else// Assign the item to best bin

bin_rem[bi] -= weight[i];

}

returnres;

}

Offline Algorithms

In the offline version, we have all items upfront. Unfortunately offline version is also NP Complete, but we have a better approximate algorithm for it. First Fit Decreasing uses at most (4M + 1)/3 bins if the optimal is M.

4. First Fit Decreasing:A trouble with online algorithms is that packing large items is difficult, especially if they occur late in the sequence. We can circumvent this by *sorting* the input sequence, and placing the large items first. With sorting, we get First Fit Decreasing and Best Fit Decreasing, as offline analogs of online First Fit and Best Fit.