BEST - Fractional Knapsack - Tutorialspoint
BEST - Fractional Knapsack - Tutorialspoint
The Greedy algorithm could be understood very well with a well-known problem referred to as Knapsack problem. Although the same
problem could be solved by employing other algorithmic approaches, Greedy approach solves Fractional Knapsack problem
reasonably in a good time. Let us discuss the Knapsack problem in detail.
Knapsack Problem
Given a set of items, each with a weight and a value, determine a subset of items to include in a collection so that the total weight is
less than or equal to a given limit and the total value is as large as possible.
The knapsack problem is in combinatorial optimization problem. It appears as a subproblem in many, more complex mathematical
models of real-world problems. One general approach to difficult problems is to identify the most restrictive constraint, ignore the
others, solve a knapsack problem, and somehow adjust the solution to satisfy the ignored constraints.
Applications
In many cases of resource allocation along with some constraint, the problem can be derived in a similar way of Knapsack problem.
Following is a set of example.
Finding the least wasteful way to cut raw materials
portfolio optimization
Cutting stock problems
Problem Scenario
A thief is robbing a store and can carry a maximal weight of W into his knapsack. There are n items available in the store and weight
of ith item is wi and its profit is pi. What items should the thief take?
In this context, the items should be selected in such a way that the thief will carry those items for which he will gain maximum profit.
Hence, the objective of the thief is to maximize the profit.
Fractional Knapsack
In this case, items can be broken into smaller pieces, hence the thief can select fractions of items.
According to the problem statement,
There are n items in the store
0
0 ⩽
⩽ x
xii ⩽
⩽ 1
1
n
n
m
maax
xiim
miiz
zee ∑
∑((x
xii .. p
pii)
)
n
n==1
1
subject to constraint,
n
n
∑
∑((x
xii .. w
wii)
) ⩽
⩽ W
W
n
n==1
1
It is clear that an optimal solution must fill the knapsack exactly, otherwise we could add a fraction of one of the remaining items and
increase the overall profit.
Thus, an optimal solution can be obtained by
n
n
∑
∑((x
xii .. w
wii)
) =
= W
W
n
n==1
1
p
pii p
pii+
+11 p
pii
In this context, first we need to sort those items according to the value of w
, so that w
≤ . Here, x is an array to store
wii wii+
+11 w
wii
Analysis
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_fractional_knapsack.htm#:~:text=problem in detail.-,Knapsack Problem,is in combinatorial optimi… 3/5
1/28/2021 DAA - Fractional Knapsack - Tutorialspoint
p
pii
If the provided items are already sorted into a decreasing order of w
, then the whileloop takes a time in O(n); Therefore, the total
wii
Example
Let us consider that the capacity of the knapsack W = 60 and the list of provided items are shown in the following table −
Item A B C D
Weight 40 10 20 24
p
pii
Ratio (
(
w
)
)
7 10 6 5
wii
p
pii
As the provided items are not sorted based on w
. After sorting, the items are as shown in the following table.
wii
Item B A C D
Weight 10 40 20 24
p
pii
Ratio (
(
w
)
)
10 7 6 5
wii
Solution
p
pii
After sorting all the items according to w
wii
. First all of B is chosen as weight of B is less than the capacity of the knapsack. Next,
item A is chosen, as the available capacity of the knapsack is greater than the weight of A. Now, C is chosen as the next item.
However, the whole item cannot be chosen as the remaining capacity of the knapsack is less than the weight of C.
This is the optimal solution. We cannot gain more profit selecting any different combination of items.