Professional Documents
Culture Documents
of used Bins)
Given n items of different weights and bins each of capacity c, assign each item
to a bin such that number of total used bins is minimized. It may be assumed
that all items have weights smaller than bin capacity.
Example:
Input: weight[] = {4, 8, 1, 4, 2, 1}
Bin Capacity c = 10
Output: 2
We need minimum 2 bins to accommodate all items
First bin contains {4, 4, 2} and second bin {8, 1, 1}
The above implementation of First Fit requires O(n 2) 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 the smallest empty space is left.
Best Fit can also be implemented in O(n Log n) time using Self-Balancing
Binary Search Trees.
If M is the optimal number of bins, then Best Fit never uses more than 1.7M
bins. So Best Fit is same as First Fit and better than Next Fit in terms of upper
bound on number of bins.
4. Worst Fit:
The idea is to places the next item in the least tight spot to even out the bins.
That is, put it in the bin so that most empty space is left.
Worst Fit can also be implemented in O(n Log n) time using Self-Balancing
Binary Search Trees.
If M is the optimal number of bins, then Best Fit never uses more than 2M-2
bins. So Worst Fit is same as Next Fit in terms of upper bound on number of
bins.
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 analogues of online First Fit and
Best Fit.
First Fit decreasing produces the best result for the sample input because items
are sorted first.
First Fit Decreasing can also be implemented in O(n Log n) time using Self-
Balancing Binary Search Trees.