I was wondering whether I can solve the following problem by using a greedy strategy: Let's say that I have a set of containers with 2 dimensions (width and height) and a set of items also with 2 dimensions. Can a solution be found where each item is assigned to a container so that it fits inside? I can have containers left over; however, all of the items must be assigned. Assuming a solution exist, is there a greedy choice I can make for assigning an item to a container?
1 Answers
No, a greedy approach would not guarantee a solution for this problem (regardless of whether this solution is optimal or not). To see why, proceed by reduction. Suppose that you could actually find a solution for your problem in polynomial time using a greedy approach. Then, you could use your algorithm to solve in polynomial time the decision version of the Bin packing problem, which is known to be NP-complete (and, therefore, unlikely to be solvable in polynomial time).
How does this reduction work?
The decision version of the Bin packing problem involves deciding whether a certain number of bins (for example, 9) is optimal. If your hypothetical polynomial-time greedy algorithm finded a solution for 9 bins and failed to find a solution for 8 bins, then the answer for the decision problem would definitely be yes, and no otherwise.
Greedy is actually good.
All that being said, to obtain an approximate solution, a greedy approach is, probably, the best heuristic: sort the items in decreasing order of size and insert them one by one into the first bin that has room for it. This heuristic is called first-fit decreasing. The main appeal of this heuristic is that we pack the big items first and hope that the little ones fill up the spaces.
- 3,804
- 2
- 20
- 22