Wikipedia / Continuous knapsack problem
Wikipedia / Continuous knapsack problem
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. 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. 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 of the knapsack, together with a collection of materials, each of which has two numbers associated with it: the weight of material that is available to be selected and the total value of that material. The goal is to choose an amount In the classic knapsack problem, each of the amounts must be either zero or ; the continuous knapsack problem differs by allowing to range continuously from zero to .
Some formulations of this problem rescale the variables
Algorithm
The continuous knapsack problem admits a greedy solution first published in 1957 by George Dantzig., and then the materials are sorted in descending order of this ratio.
While the total weight of materials chosen is less than W of item 3.
If comparison-based sorting materials. However, by adapting an algorithm for finding weighted medians.
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.