0% found this document useful (0 votes)
2 views

0 1 knapsack using class notes DP

The document discusses the 0/1 Knapsack Problem and its solution using dynamic programming. It outlines the problem's definition, the steps to solve it, and provides a practice problem with a detailed example. Additionally, it highlights the applications of the knapsack problem in various real-life scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

0 1 knapsack using class notes DP

The document discusses the 0/1 Knapsack Problem and its solution using dynamic programming. It outlines the problem's definition, the steps to solve it, and provides a practice problem with a detailed example. Additionally, it highlights the applications of the knapsack problem in various real-life scenarios.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Unit 2

0/1 Knapsack Problem using


Dynamic Programming
• Previous lecture:- Discussion on Dynamic programming on bin
packing problem.
• Discussed about Dynamic programming approach.
• Discussed about how to solve bin packing using DP approach.

• Today’s objective:- Discussion on Dynamic programming on 0/1


Knapsack problem.
• Knapsack problem
• How to solve knapsack using DP approach.
Connection with Specific CO
• Course Outcomes:
• CO1:-Analyse worst case running time of algorithms.
• CO2:-Study of various algorithmic design paradigms.
• CO3:-Tree and graph based algorithms.
• CO4:-Ways to analyse approximation/randomized algorithms.
• CO5:-Necessity for NP class based problems.

• Topic Related to CO:


• CO2:-Study of various algorithmic design paradigms.
KNAPSACK PROBLEM
You are given the following-
• A knapsack (kind of shoulder bag) with limited weight capacity.
• Few items each having some weight and value.
The problem states-
“Which items should be placed into the knapsack such that-”
• The value or profit obtained by putting the items into the knapsack is maximum.
• And the weight limit of the knapsack does not exceed.
Knapsack Problem Variants-
Knapsack problem has the following two variants-
1. Fractional Knapsack Problem
2. 0/1 Knapsack Problem

• DP solves 0/1 knapsack problem. For fractional knapsack we may use


greedy approach.
0/1 Knapsack Problem-
In 0/1 Knapsack Problem,

• As the name suggests, items are indivisible here.


• We can not take the fraction of any item.
• We have to either take an item completely or leave it completely.
• It is solved using dynamic programming approach.
0/1 Knapsack Problem Using Dynamic Programming-

Consider-
• Knapsack weight capacity = w
• Number of items each having some weight and value = n

0/1 knapsack problem is solved using dynamic programming in the


following steps-
Step-01:

• Draw a table say ‘T’ with (n+1) number of rows and (w+1) number of
columns.(n represents number of items and w represents knapsack
capacity)
• Fill all the boxes of 0th row and 0th column with zeroes as shown-
Step-02:

• Start filling the table row wise top to bottom from left to right.
• Use the following formula-
• T (i , j) = max { T ( i-1 , j ) , valuei + T( i-1 , j – weighti ) }
Here, T(i , j) = maximum value of the selected items if we can take items 1 to i and
have weight restrictions of j.

• This step leads to completely filling the table.


• Then, value of the last box represents the maximum possible value
that can be put into the knapsack.
Step-03:
To identify the items that must be put into the knapsack to obtain that
maximum profit,
• Consider the last column of the table.
• Start scanning the entries from bottom to top.
• On encountering an entry whose value is not same as the value stored in
the entry immediately above it, mark the row label of that entry.
• After all the entries are scanned, the marked labels represent the items
that must be put into the knapsack.
PRACTICE PROBLEM BASED ON 0/1 KNAPSACK
PROBLEM-

• Problem- For the given set of items and knapsack capacity = 5 kg, find
the optimal solution for the 0/1 knapsack problem making use of
dynamic programming approach
Item Weight Value

1 2 3
2 3 4
3 4 5
4 5 6
Given-
• Knapsack capacity (w) = 5 kg
• Number of items (n) = 4
• Step-01:
• Draw a table say ‘T’ with (n+1) = 4 + 1 = 5 number of rows and (w+1) = 5 + 1 = 6
number of columns.
• Fill all the boxes of 0th row and 0th column with 0.
Step-02:
• Start filling the table row wise top to bottom from left to right using the formula-
• T (i , j) = max { T ( i-1 , j ) , valuei + T( i-1 , j – weighti )
Weigh
Item Value ($)
Finding T(1,1)- t (kg)

1 2 3
We have,
2 3 4
• i=1 3 4 5
• j=1 4 5 6
• (value)i = (value)1 = 3
• (weight)i = (weight)1 = 2
Substituting the values, we get -
• T(1,1) = max { T(1-1 , 1) , 3 + T(1-1 , 1-2) }
• T(1,1) = max { T(0,1) , 3 + T(0,-1) }
• T(1,1) = T(0,1) { Ignore T(0,-1) }
• T(1,1) = 0
Finding T(1,2)- Finding T(1,3)-
We have, • We have,
• i=1 • i=1
• j=3
• j=2
• (value)i = (value)1 = 3
• (value)i = (value)1 = 3
• (weight)i = (weight)1 = 2
• (weight)i = (weight)1 = 2
Substituting the values, we get-
Substituting the values, we get-
• T(1,3) = max { T(1-1 , 3) , 3 + T(1-1 , 3-2) }
• T(1,2) = max { T(1-1 , 2) , 3 + T(1-1 , 2-2) }
• T(1,3) = max { T(0,3) , 3 + T(0,1) }
• T(1,2) = max { T(0,2) , 3 + T(0,0) } • T(1,3) = max {0 , 3+0}
• T(1,2) = max {0 , 3+0} • T(1,3) = 3
• T(1,2) = 3 Item
Weigh
t (kg) Value ($)

1 2 3

2 3 4

3 4 5

4 5 6
Finding T(1,4)- Finding T(1,5)-
We have, We have,
• i=1 • i=1
• j=4 • j=5
• (value)i = (value)1 = 3 • (value)i = (value)1 = 3
• (weight)i = (weight)1 = 2 • (weight)i = (weight)1 = 2

Substituting the values, we get- Substituting the values, we get-


• T(1,4) = max { T(1-1 , 4) , 3 + T(1-1 , 4-2) } • T(1,5) = max { T(1-1 , 5) , 3 + T(1-1 , 5-2) }
• T(1,4) = max { T(0,4) , 3 + T(0,2) } • T(1,5) = max { T(0,5) , 3 + T(0,3) }
• T(1,4) = max {0 , 3+0} • T(1,5) = max {0 , 3+0}
• T(1,4) = 3 • T(1,5) = 3
• Finding T(2,2)-
• Finding T(2,1)-
• Finding T(2,3)-
We have, • Finding T(2,4)-
• i=2 • Finding T(2,5)-
• j=1 • Similarly, compute all the entries.
• (value)i = (value)2 = 4 • After all the entries are computed and filled in the table, we
• (weight)i = (weight)2 = 3 get the following table-

Substituting the values, we get-


• T(2,1) = max { T(2-1 , 1) , 4 + T(2-1 , 1-3) }
• T(2,1) = max { T(1,1) , 4 + T(1,-2) }
• T(2,1) = T(1,1) { Ignore T(1,-2) }
• T(2,1) = 0
Weigh
Item Value ($)
t (kg)

1 2 3 •The last entry represents the maximum possible value that can
2 3 4 be put into the knapsack.
3 4 5 • maximum possible value that can be put into the knapsack = 7.
4 5 6
Identifying Items To Be Put Into Knapsack-

Following Step-03,
• Consider the last column of the table.
• Start scanning the entries from bottom to top.
• On encountering an entry whose value is not same as the
value stored in the entry immediately above it, mark the row
label of that entry.
• After all the entries are scanned, the marked labels represent
the items that must be put into the knapsack.

• We mark the rows labelled “1” and “2”.


• Thus, items that must be put into the knapsack to obtain the Item
Weigh
t (kg)
Value ($)
maximum value 7 are-
1 2 3
• Item-1 and Item-2
2 3 4
3 4 5
4 5 6
APPLICATION OF KNAPSACK PROBLEMS

• The knapsack problems have a variety of real life applications


including financial modeling, production and inventory management
systems, design of queuing network models in manufacturing, and
control of traffic overload in telecommunication systems.

You might also like