Continuous knapsack problem

Algorithmic problem in computer science From Wikipedia, the free encyclopedia

In theoretical computer science, the continuous knapsack problem (also known as the fractional knapsack problem) is an algorithmic problem in combinatorial optimization in which the goal is to fill a container (with a fixed capacity) fractional amounts of different materials chosen to maximize the value of the selected materials.[1][2] It is a variation of the classic knapsack problem, in which the items to be placed in the container are indivisible; however, the continuous knapsack problem may be solved in polynomial time whereas the classic knapsack problem is NP-hard.[1] It is a classic example of how a seemingly small change in the formulation of a problem can have a large impact on its computational complexity.

Problem definition

An instance of either the continuous or classic knapsack problems may be specified by the numerical capacity W of the knapsack, together with a collection of materials, each of which has two numbers associated with it: the weight wi of material that is available to be selected and the total value vi of that material. The goal is to choose an amount xiwi of each material, subject to the capacity constraint and maximizing the total benefit In the classic knapsack problem, each of the amounts xi must be either zero or wi; the continuous knapsack problem differs by allowing xi to range continuously from zero to wi.[1]

Some formulations of this problem rescale the variables xi to be in the range from 0 to 1. In this case the capacity constraint becomes and the goal is to maximize the total benefit

Algorithm

The continuous knapsack problem admits a greedy solution first published in 1957 by George Dantzig.[3][4] Each material is assigned the ratio , and then the materials are sorted in descending order of this ratio.

While the total weight of materials chosen is less than W, the algorithm takes the largest possible fraction of the first item in the sorted list. For example, suppose we have 4 items with weights , values , and that the capacity of the knapsack is . The value-to-weight ratio of each item is computed, giving . In this case the items are already sorted in descending order of ratio. The algorithm then takes all of item 1, all of item 2, and of item 3.

If comparison-based sorting is used, this algorithm runs in with respect to materials.[1][2] However, by adapting an algorithm for finding weighted medians, it is possible to solve the problem in time .[2]

The proof of correctness follows by a standard exchange argument. Suppose that we have a solution which is not the greedy solution. Take the first instance where the new solution and the greedy solution differ. For example, if the greedy solution takes fractions of 4 items (sorted by the value-to-weight ratio) and the new solution takes fractions of the same 4 items, then they differ in the third entry.

In general, the greedy solution takes weights of items sorted by ratio, so the greedy solution has assigned more weight to a higher-ratio item. Since both solutions assign the same total weights across items, the new solution is forced to assign weight to items of a lower ratio. So the new assignment has a value no greater than the value given by the greedy assignment.

References

Related Articles

Wikiwand AI