0% found this document useful (0 votes)
5 views4 pages

0-1knapsack

The document discusses the 0-1 Knapsack problem, which involves selecting items with given weights and profits to maximize total profit without exceeding a maximum weight limit. It explains that the problem cannot be solved using a Greedy approach and instead utilizes dynamic programming to break it down into sub-problems. An example is provided to illustrate the algorithm, including a formula for calculating profits and a code snippet for implementation.

Uploaded by

suganya.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

0-1knapsack

The document discusses the 0-1 Knapsack problem, which involves selecting items with given weights and profits to maximize total profit without exceeding a maximum weight limit. It explains that the problem cannot be solved using a Greedy approach and instead utilizes dynamic programming to break it down into sub-problems. An example is provided to illustrate the algorithm, including a formula for calculating profits and a code snippet for implementation.

Uploaded by

suganya.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Its either the item is added to the knapsack or not.

That is why, this method is known as the 0-1


Knapsack problem.

0-1 Knapsack cannot be solved by Greedy approach. Greedy approach does not ensure an optimal
solution in this method. In many instances, Greedy approach may give an optimal solution.

In dynamic programming approach, the complicated problem is divided into sub-problems, then we
find the solution of a sub-problem and the solution of the sub-problem will be used to find the
solution of a complex problem.

0-1 Knapsack Algorithm

Problem Statement − A thief is robbing a store and can carry a maximal weight of W into his
knapsack. There are n items and weight of ith item is wi and the profit of selecting this item is pi.
What items should the thief take?

Let i be the highest-numbered item in an optimal solution S for W dollars. Then S’ = S − {i} is an
optimal solution for W – wi dollars and the value to the solution S is Vi plus the value of the sub-
problem.

We can express this fact in the following formula: define c[i, w] to be the solution for items 1,2, … ,
i and the maximum weight w.

The algorithm takes the following inputs

 The maximum weight W

 The number of items n

 The two sequences v = <v1, v2, …, vn> and w = <w1, w2, …, wn>

The set of items to take can be deduced from the table, starting at c[n, w] and tracing backwards
where the optimal values came from.

If c[i, w] = c[i-1, w], then item i is not part of the solution, and we continue tracing with c[i-1, w].
Otherwise, item i is part of the solution, and we continue tracing with c [i-1, w-W].

Example

Let us consider that the capacity of the knapsack is W = 8 and the items are as shown in the following
table.

Item A B C D

Profit 2 4 7 10

Weight 1 3 5 7

The remaining values are filled with the maximum profit achievable with respect to the items and
weight per column that can be stored in the knapsack.

The formula to store the profit values is −

c[i,w]=max{c[i−1,w−w[i]]+P[i]}
Step 1

Construct an adjacency table with maximum weight of knapsack as rows and items with respective
weights and profits as columns.

Values to be stored in the table are cumulative profits of the items whose weights do not exceed the
maximum weight of the knapsack (designated values of each row)

So we add zeroes to the 0th row and 0th column because if the weight of item is 0, then it weighs
nothing; if the maximum weight of knapsack is 0, then no item can be added into the knapsack.

The remaining values are filled with the maximum profit achievable with respect to the items and
weight per column that can be stored in the knapsack.

The formula to store the profit values is −

c[i,w]=max{c[i−1,w−w[i]]+P[i]}

By computing all the values using the formula, the table obtained would be −
To find the items to be added in the knapsack, recognize the maximum profit from the table and
identify the items that make up the profit, in this example, its {1, 7}.

The optimal solution is {1, 7} with the maximum profit is 12.

Analysis

This algorithm takes Ɵ(n.w) times as table c has (n+1).(w+1) entries, where each entry requires Ɵ(1)
time to compute.
int knapsack(int W, int wt[], int val[], int n){

int K[n+1][W+1];

for(int i = 0; i<=n; i++) {

for(int w = 0; w<=W; w++) {

if(i == 0 || w == 0) {

K[i][w] = 0;

} else if(wt[i-1] <= w) {

K[i][w] = findMax(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);

} else {

K[i][w] = K[i-1][w];

return K[n][W];

Example of 0/1 knapsack problem.

Consider the problem having weights and profits are:

Weights: {3, 4, 6, 5}

Profits: {2, 3, 1, 4}

The weight of the knapsack is 8 kg

You might also like