Boxing problem (NP problem)

Source: Internet
Author: User
Problem description

There are n items with a volume of v[i],i = 1..N. Now there are several identical boxes, Volume c,c > v[i], i = 1..N. Ask at least how many boxes are needed to load all the items.

Problem solving

This kind of problem is NP problem, that is, can verify a solution is OK in polynomial time, but can not obtain the solution in polynomial time.
Packing problem, there is an approximate solution.

First Fit (FF)

First adaptation solution. For item I, find the first in the box where the item is currently loaded, the remaining capacity can be lowered into the box of item I, if the box does not exist, then put into the new box. It's obvious that you need n boxes at most. The time complexity is O (n^2), of course, the time complexity will be O (n log n) by tournament tree.

Tournament tree, which is a fully-rooted, all-leaf node for chests, each internal node V, holds a property: V.max, which records the maximum remaining capacity in all leaf nodes that are root for that node.
So when you want to put the item I, its capacity is v[i], then for tournament tree root node start, if Root->left_child.max >= v[i], then recursive to left dial hand tree to solve the problem, put item I, otherwise recursive to the right subtree.
When a leaf node (box) is placed in item I, its V.max minus v[i]. Then the ancestor (ancestor node) of the leaf node is updated recursively.
As you can see, the time complexity is O (log n) when you place an item I, so the total time complexity is O (n log n).

FFD

Descending first-fit (FFD) algorithm. The first Fit above is an on-line algorithm. That is, each time you can directly accept the item I as input, and then according to a strategy to place the item I. The descending first adaptation algorithm (FFD) takes all items offline and sorts them by size from large to small. The FF algorithm is then used. The complexity is also O (n log n).

Boxing problem (NP problem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.