SlideShare a Scribd company logo
0-1 KNAPSACK
USING
DYNAMIC PROGRAMMING
MADE BY:-
FENIL SHAH
15CE121
CHARUSAT UNIVERSITY
What is Dynamic Programming ?
• Dynamic programming is a method for solving
optimization problems.
The idea: Compute the solutions to the sub-
problems once and store the solutions in a table,
so that they can be reused (repeatedly) later.
• It is a Bottom-Up approach.
• Coined by Richard Bellman who described
dynamic programming as the way of solving
problems where you need to find the best
decisions one after another
Properties
• Two main properties of a problem suggest
that the given problem can be solved using
Dynamic Programming.
• These properties are overlapping sub-
problems and optimal substructure.
Steps of Dynamic Programming Approach
• Dynamic Programming algorithm is designed
using the following four steps −
1. Characterize the structure of an optimal
solution.
2. Recursively define the value of an optimal
solution.
3. Compute the value of an optimal solution,
typically in a bottom-up fashion.
4. Construct an optimal solution from the
computed information.
Applications of Dynamic Programming
Approach
• Matrix Chain Multiplication
• Longest Common Subsequence
• Travelling Salesman Problem
• Knapsack Problem
The 0-1 Knapsack Problem
(Rucksack Problem)
• Given: A set S of n items, with each item i having
– wi - a positive weight
– bi - a positive benefit (value)
• Goal: Choose items with maximum total benefit but with
weight at most W (weight of Knapsack).
– In this case, we let Tdenote the set of items we take
– Objective: maximize
– Constraint:
Ti
ib


Ti
i Ww
Why not Greedy?
• Greedy approach provides the optimal
solution to the fractional knapsack.
• 0-1 Knapsack cannot be solved by Greedy
approach.
• It does not ensure an optimal solution to the
0-1 Knapsack
EXAMPLE:
• Capacity 200
• Items :
X1 : v1/w1 = 12, weight : 50
X2 : v2/w2 = 10, weight : 55
X3 : v3/w3 = 8, weight : 10
X4 : v4/w4 = 6, weight : 100
 Value of Greedy : 50 * 12 + 55 *10 + 8* 10= 1230
 Optimal : 8 * 100 + 55*10 + 8*10= 1430
 SO, Greedy Approach does not ensure an optimal solution
to the 0-1 Knapsack
To solve 0-1 Knapsack, Dynamic Programming approach is
required.
Recursive formula for sub-problems:
3 cases:
1. There are no items in the knapsack, or the weight of the knapsack is 0 -
the benefit is 0
2. The weight of itemi exceeds the weight w of the knapsack - itemi cannot be
included in the knapsack and the maximum benefit is V[i-1, w]
3. Otherwise, the benefit is the maximum achieved by either not including
itemi ( i.e., V[i-1, w]),
or by including itemi (i.e., V[i-1, w-wi]+bi)
otherwise}],1[],,1[max{
wwif],1[
0or0for0
],[ i









ii bwwiVwiV
wiV
wi
wiV
0-1 Knapsack Algorithm
for w = 0 to W // Initialize 1st row to 0’s
V[0,w] = 0
for i = 1 to n // Initialize 1st column to 0’s
V[i,0] = 0
for i = 1 to n
for w = 0 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Let’s run our algorithm on the
following data:
n = 4 (# of elements)
W = 5 (max weight)
Elements (weight, benefit):
(2,3), (3,4), (4,5), (5,6)
Example
Example (1)
0
1
2
3
4 50 1 2 3
4
iW
for w = 0 to W
V[0,w] = 0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
Example (2)
for i = 1 to n
V[i,0] = 0
0
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
Example (3)
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
0
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
0
i=1
bi=3
wi=2
w=1
w-wi =-1
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
0
0
0
Example (4)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
300
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=1
bi=3
wi=2
w=2
w-wi =0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Example (5)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
300
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=1
bi=3
wi=2
w=3
w-wi =1
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
3
Example (6)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
300
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=1
bi=3
wi=2
w=4
w-wi =2
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
3 3
Example (7)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
300
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=1
bi=3
wi=2
w=5
w-wi =3
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
3 3 3
Example (8)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=2
bi=4
wi=3
w=1
w-wi =-2
3 3 3 3
0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Example (9)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=2
bi=4
wi=3
w=2
w-wi =-1
3 3 3 3
3
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
0
Example (10)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=2
bi=4
wi=3
w=3
w-wi =0
3 3 3 3
0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
43
Example (11)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=2
bi=4
wi=3
w=4
w-wi =1
3 3 3 3
0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
43 4
Example (12)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=2
bi=4
wi=3
w=5
w-wi =2
3 3 3 3
0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
73 4 4
Example (13)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=3
bi=5
wi=4
w= 1..3
3 3 3 3
0 3 4 4
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
7
3 40
Example (14)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=3
bi=5
wi=4
w= 4
w- wi=0
3 3 3 3
0 3 4 4 7
0 3 4 5
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
Example (15)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=3
bi=5
wi=4
w= 5
w- wi=1
3 3 3 3
0 3 4 4 7
0 3 4
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
5 7
Example (16)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=4
bi=6
wi=5
w= 1..4
3 3 3 3
0 3 4 4
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
7
3 40
70 3 4 5
5
Example (17)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=4
bi=6
wi=5
w= 5
w- wi=0
3 3 3 3
0 3 4 4 7
0 3 4
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
5
7
7
0 3 4 5
Example (18)
We’re DONE!!
The max possible value that can be carried in
this knapsack is $7
• This algorithm only finds the max possible
value that can be carried in the knapsack
– i.e., the value in V[n,W]
• To know the items that make this maximum
value, we need to trace back through the
table.
How to find actual Knapsack Items
• All of the information we need is in the table.
• V[n,W] is the maximal value of items that can
be placed in the Knapsack.
• Let i=n and k=W
if V[i,k]  V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
i = i1 // Assume the ith item is not in the
knapsack
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=4
k= 5
bi=6
wi=5
V[i,k] = 7
V[i1,k] =7
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k]  V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
i = i1
5 7
0 3 4 5 7
Finding the Items
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=4
k= 5
bi=6
wi=5
V[i,k] = 7
V[i1,k] =7
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k]  V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
i = i1
5 7
0 3 4 5 7
Finding the Items (2)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=3
k= 5
bi=5
wi=4
V[i,k] = 7
V[i1,k] =7
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k]  V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
i = i1
5 7
0 3 4 5 7
Finding the Items (3)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=2
k= 5
bi=4
wi=3
V[i,k] = 7
V[i1,k] =3
k  wi=2
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k]  V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
i = i1
5 7
0 3 4 5 7
7
Finding the Items (4)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW i=1
k= 2
bi=3
wi=2
V[i,k] = 3
V[i1,k] =0
k  wi=0
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k]  V[i1,k] then
mark the ith item as in the knapsack
i = i1, k = k-wi
else
i = i1
5 7
0 3 4 5 7
3
Finding the Items (5)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k]  V[i1,k] then
mark the nth item as in the knapsack
i = i1, k = k-wi
else
i = i1
5 7
0 3 4 5 7
i=0
k= 0
The optimal
knapsack
should contain
{1, 2}
Finding the Items (6)
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
3 3 3 3
0 3 4 4 7
0 3 4
i=n, k=W
while i,k > 0
if V[i,k]  V[i1,k] then
mark the nth item as in the knapsack
i = i1, k = k-wi
else
i = i1
5 7
0 3 4 5 7
The optimal
knapsack
should contain
{1, 2}
7
3
Finding the Items (7)
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
for i = 1 to n
for w = 0 to W
< the rest of the code >
What is the running time of this algorithm?
O(W)
O(W)
Repeat n times
O(n*W)
Running time
Applications of Knapsack Problem
• Resourse allocation with financial constraints
• Construction and Scoring of Heterogenous
test
• Selection of capital investments
ANY QUESTIONS ???
THANK YOU
Ad

More Related Content

What's hot (20)

0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
Amin Omi
 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and bound
Abhishek Singh
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
i i
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
Sahil Kumar
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
padmeshagrekar
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
harsh kothari
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
Vikas Sharma
 
Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programming
khush_boo31
 
Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
Tech_MX
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
Amrinder Arora
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
Protap Mondal
 
Backtracking
BacktrackingBacktracking
Backtracking
subhradeep mitra
 
Backtracking Algorithm.ppt
Backtracking Algorithm.pptBacktracking Algorithm.ppt
Backtracking Algorithm.ppt
SalmIbrahimIlyas
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
InteX Research Lab
 
Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
Dr. C.V. Suresh Babu
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
Saga Valsalan
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
Amin Omi
 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and bound
Abhishek Singh
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
i i
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
Sahil Kumar
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
padmeshagrekar
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
harsh kothari
 
Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programming
khush_boo31
 
Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking Sum of subsets problem by backtracking 
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
Tech_MX
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
Amrinder Arora
 
Backtracking Algorithm.ppt
Backtracking Algorithm.pptBacktracking Algorithm.ppt
Backtracking Algorithm.ppt
SalmIbrahimIlyas
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
Saga Valsalan
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 

Similar to 01 Knapsack using Dynamic Programming (17)

lecture 25
lecture 25lecture 25
lecture 25
sajinsc
 
DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.ppt
Ruchika Sinha
 
0-1Knapsack.pptx greedy Algorithm, Maximize profit
0-1Knapsack.pptx greedy Algorithm, Maximize profit0-1Knapsack.pptx greedy Algorithm, Maximize profit
0-1Knapsack.pptx greedy Algorithm, Maximize profit
zeeshanmubeen1
 
0-1 knapsack.ppt
0-1 knapsack.ppt0-1 knapsack.ppt
0-1 knapsack.ppt
AyushJaiswal513854
 
Knapsack problem and Memory Function
Knapsack problem and Memory FunctionKnapsack problem and Memory Function
Knapsack problem and Memory Function
Barani Tharan
 
Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1
Amit Kumar Rathi
 
Longest common sub sequence & 0/1 Knapsack
Longest common sub sequence & 0/1 KnapsackLongest common sub sequence & 0/1 Knapsack
Longest common sub sequence & 0/1 Knapsack
Asif Shahriar
 
Knapsack Problem Analysis of Algorithm.ppt
Knapsack Problem Analysis of Algorithm.pptKnapsack Problem Analysis of Algorithm.ppt
Knapsack Problem Analysis of Algorithm.ppt
SharjeelFaisal4
 
Knapsack problem dynamicprogramming
Knapsack problem dynamicprogrammingKnapsack problem dynamicprogramming
Knapsack problem dynamicprogramming
rowntu
 
Presentation of knapsack
Presentation of knapsackPresentation of knapsack
Presentation of knapsack
Gaurav Dubey
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
RacksaviR
 
267 3 dot product-angles-projection-n
267 3 dot product-angles-projection-n267 3 dot product-angles-projection-n
267 3 dot product-angles-projection-n
math260
 
16 dot product angles-projection
16 dot product angles-projection16 dot product angles-projection
16 dot product angles-projection
math260
 
16 dot product angles-projection
16 dot product angles-projection16 dot product angles-projection
16 dot product angles-projection
math260
 
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programmingComparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Editor IJMTER
 
Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack Dynamic
Paras Patel
 
Re call basic operations in mathematics
Re call basic operations in mathematics Re call basic operations in mathematics
Re call basic operations in mathematics
Nadeem Uddin
 
lecture 25
lecture 25lecture 25
lecture 25
sajinsc
 
DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.ppt
Ruchika Sinha
 
0-1Knapsack.pptx greedy Algorithm, Maximize profit
0-1Knapsack.pptx greedy Algorithm, Maximize profit0-1Knapsack.pptx greedy Algorithm, Maximize profit
0-1Knapsack.pptx greedy Algorithm, Maximize profit
zeeshanmubeen1
 
Knapsack problem and Memory Function
Knapsack problem and Memory FunctionKnapsack problem and Memory Function
Knapsack problem and Memory Function
Barani Tharan
 
Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1
Amit Kumar Rathi
 
Longest common sub sequence & 0/1 Knapsack
Longest common sub sequence & 0/1 KnapsackLongest common sub sequence & 0/1 Knapsack
Longest common sub sequence & 0/1 Knapsack
Asif Shahriar
 
Knapsack Problem Analysis of Algorithm.ppt
Knapsack Problem Analysis of Algorithm.pptKnapsack Problem Analysis of Algorithm.ppt
Knapsack Problem Analysis of Algorithm.ppt
SharjeelFaisal4
 
Knapsack problem dynamicprogramming
Knapsack problem dynamicprogrammingKnapsack problem dynamicprogramming
Knapsack problem dynamicprogramming
rowntu
 
Presentation of knapsack
Presentation of knapsackPresentation of knapsack
Presentation of knapsack
Gaurav Dubey
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
RacksaviR
 
267 3 dot product-angles-projection-n
267 3 dot product-angles-projection-n267 3 dot product-angles-projection-n
267 3 dot product-angles-projection-n
math260
 
16 dot product angles-projection
16 dot product angles-projection16 dot product angles-projection
16 dot product angles-projection
math260
 
16 dot product angles-projection
16 dot product angles-projection16 dot product angles-projection
16 dot product angles-projection
math260
 
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programmingComparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Editor IJMTER
 
Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack Dynamic
Paras Patel
 
Re call basic operations in mathematics
Re call basic operations in mathematics Re call basic operations in mathematics
Re call basic operations in mathematics
Nadeem Uddin
 
Ad

Recently uploaded (20)

IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Ad

01 Knapsack using Dynamic Programming

  • 1. 0-1 KNAPSACK USING DYNAMIC PROGRAMMING MADE BY:- FENIL SHAH 15CE121 CHARUSAT UNIVERSITY
  • 2. What is Dynamic Programming ? • Dynamic programming is a method for solving optimization problems. The idea: Compute the solutions to the sub- problems once and store the solutions in a table, so that they can be reused (repeatedly) later. • It is a Bottom-Up approach. • Coined by Richard Bellman who described dynamic programming as the way of solving problems where you need to find the best decisions one after another
  • 3. Properties • Two main properties of a problem suggest that the given problem can be solved using Dynamic Programming. • These properties are overlapping sub- problems and optimal substructure.
  • 4. Steps of Dynamic Programming Approach • Dynamic Programming algorithm is designed using the following four steps − 1. Characterize the structure of an optimal solution. 2. Recursively define the value of an optimal solution. 3. Compute the value of an optimal solution, typically in a bottom-up fashion. 4. Construct an optimal solution from the computed information.
  • 5. Applications of Dynamic Programming Approach • Matrix Chain Multiplication • Longest Common Subsequence • Travelling Salesman Problem • Knapsack Problem
  • 6. The 0-1 Knapsack Problem (Rucksack Problem) • Given: A set S of n items, with each item i having – wi - a positive weight – bi - a positive benefit (value) • Goal: Choose items with maximum total benefit but with weight at most W (weight of Knapsack). – In this case, we let Tdenote the set of items we take – Objective: maximize – Constraint: Ti ib   Ti i Ww
  • 7. Why not Greedy? • Greedy approach provides the optimal solution to the fractional knapsack. • 0-1 Knapsack cannot be solved by Greedy approach. • It does not ensure an optimal solution to the 0-1 Knapsack
  • 8. EXAMPLE: • Capacity 200 • Items : X1 : v1/w1 = 12, weight : 50 X2 : v2/w2 = 10, weight : 55 X3 : v3/w3 = 8, weight : 10 X4 : v4/w4 = 6, weight : 100  Value of Greedy : 50 * 12 + 55 *10 + 8* 10= 1230  Optimal : 8 * 100 + 55*10 + 8*10= 1430  SO, Greedy Approach does not ensure an optimal solution to the 0-1 Knapsack To solve 0-1 Knapsack, Dynamic Programming approach is required.
  • 9. Recursive formula for sub-problems: 3 cases: 1. There are no items in the knapsack, or the weight of the knapsack is 0 - the benefit is 0 2. The weight of itemi exceeds the weight w of the knapsack - itemi cannot be included in the knapsack and the maximum benefit is V[i-1, w] 3. Otherwise, the benefit is the maximum achieved by either not including itemi ( i.e., V[i-1, w]), or by including itemi (i.e., V[i-1, w-wi]+bi) otherwise}],1[],,1[max{ wwif],1[ 0or0for0 ],[ i          ii bwwiVwiV wiV wi wiV
  • 10. 0-1 Knapsack Algorithm for w = 0 to W // Initialize 1st row to 0’s V[0,w] = 0 for i = 1 to n // Initialize 1st column to 0’s V[i,0] = 0 for i = 1 to n for w = 0 to W if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w
  • 11. Let’s run our algorithm on the following data: n = 4 (# of elements) W = 5 (max weight) Elements (weight, benefit): (2,3), (3,4), (4,5), (5,6) Example
  • 13. for w = 0 to W V[0,w] = 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW Example (2)
  • 14. for i = 1 to n V[i,0] = 0 0 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW Example (3)
  • 15. if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 0 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 0 i=1 bi=3 wi=2 w=1 w-wi =-1 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW 0 0 0 Example (4)
  • 16. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 300 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=1 bi=3 wi=2 w=2 w-wi =0 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w Example (5)
  • 17. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 300 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=1 bi=3 wi=2 w=3 w-wi =1 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 3 Example (6)
  • 18. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 300 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=1 bi=3 wi=2 w=4 w-wi =2 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 3 3 Example (7)
  • 19. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 300 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=1 bi=3 wi=2 w=5 w-wi =3 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 3 3 3 Example (8)
  • 20. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=2 bi=4 wi=3 w=1 w-wi =-2 3 3 3 3 0 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w Example (9)
  • 21. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=2 bi=4 wi=3 w=2 w-wi =-1 3 3 3 3 3 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 0 Example (10)
  • 22. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=2 bi=4 wi=3 w=3 w-wi =0 3 3 3 3 0 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 43 Example (11)
  • 23. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=2 bi=4 wi=3 w=4 w-wi =1 3 3 3 3 0 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 43 4 Example (12)
  • 24. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=2 bi=4 wi=3 w=5 w-wi =2 3 3 3 3 0 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 73 4 4 Example (13)
  • 25. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=3 bi=5 wi=4 w= 1..3 3 3 3 3 0 3 4 4 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 7 3 40 Example (14)
  • 26. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=3 bi=5 wi=4 w= 4 w- wi=0 3 3 3 3 0 3 4 4 7 0 3 4 5 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w Example (15)
  • 27. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=3 bi=5 wi=4 w= 5 w- wi=1 3 3 3 3 0 3 4 4 7 0 3 4 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 5 7 Example (16)
  • 28. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=4 bi=6 wi=5 w= 1..4 3 3 3 3 0 3 4 4 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 7 3 40 70 3 4 5 5 Example (17)
  • 29. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=4 bi=6 wi=5 w= 5 w- wi=0 3 3 3 3 0 3 4 4 7 0 3 4 if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w] else V[i,w] = V[i-1,w] // wi > w 5 7 7 0 3 4 5 Example (18)
  • 30. We’re DONE!! The max possible value that can be carried in this knapsack is $7
  • 31. • This algorithm only finds the max possible value that can be carried in the knapsack – i.e., the value in V[n,W] • To know the items that make this maximum value, we need to trace back through the table.
  • 32. How to find actual Knapsack Items • All of the information we need is in the table. • V[n,W] is the maximal value of items that can be placed in the Knapsack. • Let i=n and k=W if V[i,k]  V[i1,k] then mark the ith item as in the knapsack i = i1, k = k-wi else i = i1 // Assume the ith item is not in the knapsack
  • 33. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=4 k= 5 bi=6 wi=5 V[i,k] = 7 V[i1,k] =7 3 3 3 3 0 3 4 4 7 0 3 4 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the ith item as in the knapsack i = i1, k = k-wi else i = i1 5 7 0 3 4 5 7 Finding the Items
  • 34. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=4 k= 5 bi=6 wi=5 V[i,k] = 7 V[i1,k] =7 3 3 3 3 0 3 4 4 7 0 3 4 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the ith item as in the knapsack i = i1, k = k-wi else i = i1 5 7 0 3 4 5 7 Finding the Items (2)
  • 35. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=3 k= 5 bi=5 wi=4 V[i,k] = 7 V[i1,k] =7 3 3 3 3 0 3 4 4 7 0 3 4 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the ith item as in the knapsack i = i1, k = k-wi else i = i1 5 7 0 3 4 5 7 Finding the Items (3)
  • 36. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=2 k= 5 bi=4 wi=3 V[i,k] = 7 V[i1,k] =3 k  wi=2 3 3 3 3 0 3 4 4 7 0 3 4 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the ith item as in the knapsack i = i1, k = k-wi else i = i1 5 7 0 3 4 5 7 7 Finding the Items (4)
  • 37. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=1 k= 2 bi=3 wi=2 V[i,k] = 3 V[i1,k] =0 k  wi=0 3 3 3 3 0 3 4 4 7 0 3 4 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the ith item as in the knapsack i = i1, k = k-wi else i = i1 5 7 0 3 4 5 7 3 Finding the Items (5)
  • 38. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW 3 3 3 3 0 3 4 4 7 0 3 4 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the nth item as in the knapsack i = i1, k = k-wi else i = i1 5 7 0 3 4 5 7 i=0 k= 0 The optimal knapsack should contain {1, 2} Finding the Items (6)
  • 39. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW 3 3 3 3 0 3 4 4 7 0 3 4 i=n, k=W while i,k > 0 if V[i,k]  V[i1,k] then mark the nth item as in the knapsack i = i1, k = k-wi else i = i1 5 7 0 3 4 5 7 The optimal knapsack should contain {1, 2} 7 3 Finding the Items (7)
  • 40. for w = 0 to W V[0,w] = 0 for i = 1 to n V[i,0] = 0 for i = 1 to n for w = 0 to W < the rest of the code > What is the running time of this algorithm? O(W) O(W) Repeat n times O(n*W) Running time
  • 41. Applications of Knapsack Problem • Resourse allocation with financial constraints • Construction and Scoring of Heterogenous test • Selection of capital investments