SlideShare a Scribd company logo
2
Most read
4
Most read
8
Most read
Dynamic Programming-Knapsack Problem
Dynamic Programming
It is a multistage optimizing decision
making problem closely related to “divide
& conquer” technique.
It solves the sub-problem only once &
stores the result in a table instead of
solving it recursively.
0/1 Knapsack Problem
 Given a set of n items and a knapsack having
capacity w, each item has weight wi and
value vi.
 The problem is to pack the knapsack in such
a way so that maximum total value is
achieved and the total weight should not be
more than the fixed weight.
 The problem is called 0/1 knapsack because
the items are either accepted or rejected.
Algorithm
 Loop, for w←0 to W(total capacity)
Set Kp[0,w] ←0.
 Loop, for i ←1 to n(set of items)
Set Kp[i,0] ←0.
 Check whether K is a part of solution or not-
if (wi<=w)(can be part of the solution)
if (vi+kp[i-1,w-wi])>Kp(i-1,w)
Set kp[i,w] ←vi
+kp[i-1,w-wi]
Else
Set Kp[i,w] ←Kp[i-1,w]
 Exit
Consider a knapsack with weight capacity
W=3 and total items 3 such that
S=3
wi={1,2,3}
vi={2,3,4}
On applying knapsack algorithm,
Item wi
vi
1 1 2
2 2 3
3 3 4
i/w 0 1 2 3
0
1
2
3
for w ←0 to W, for i←1 to n
Set Kp[0,w] ←0, Set Kp[i,0]←0
i/w 0 1 2 3
0 0 0 0 0
1 0
2 0
3 0
i=1,vi=2,wi=1,w=1,w-wi=0
if (wi<=w), set kp[i,w] ←vi
+kp[i-1,w-wi]
Kp[1,1]←2+Kp[0,0]=2
i/w 0 1 2 3
0 0 0 0 0
1 0
2 0
3 0
i/w 0 1 2 3
0 0 0 0 0
1 0 2
2 0
3 0
i=1,vi=2,wi=1,w=2,w-wi=1
if (wi<=w), set kp[i,w] ←vi
+kp[i-1,w-wi]
Kp[1,1]←2+Kp[1,1]=2
i/w 0 1 2 3
0 0 0 0
↓
0
1 0 2 2
2 0
3 0
i=1,vi=2,wi=1,w=3,w-wi=2
(wi<=w), Kp[1,3]←2+Kp[0,2]=2
i=2,vi=3,wi=2,w=1,w-wi=-1
if (wi>w), Set Kp[i,w] ←Kp[i-1,w]
kp[2,1]←Kp[1,1]=2
i/w 0 1 2 3
0 0 0 0 0
1 0 2 2 2
2 0 2
3 0
i=2,vi=3,wi=2,w=2,w-wi=0
(wi<=w), Kp[2,2]←3+Kp[1,0]=3
i=2,vi=3,wi=2,w=3,w-wi=1
(wi<=w), Kp[2,3]←3+Kp[1,1]=3+2=5
i/w 0 1 2 3
0 0 0 0 0
1 0 2 2 2
2 0 2 3 5
3 0
i=3,vi=4,wi=3,w=1,w-wi=-2
if (wi>w), Set Kp[i,w] ←Kp[i-1,w]=2
i=3,vi=4,wi=3,w=2,w-wi=-1
if (wi>w), Set Kp[i,w] ←Kp[i-1,w]=3
i/w 0 1 2 3
0 0 0 0 0
1 0 2 2 2
2 0 2 3 5
3 0 2 3
i=3,vi=4,wi=3,w=3,w-wi=0 (wi<=w),
if (vi+kp[i-1,w-wi])>Kp(i-1,w)=4<5
Kp[3,3]←5
i/w 0 1 2 3
0 0 0 0 0
1 0 2 2 2
2 0 2 3 5
3 0 2 3 5
Now we have computed the maximum
possible values that can be taken in a
knapsack i.e Kp[n,W]
In order to select items that can take part
in making this maximum value let
i=n,k=W.
Algorithm
 Set i←n
set k←W
 Loop to check K is a part of knapsack
while (i>0 and k>0)
if Kp[i,k]≠Kp[i-1,k] then
mark the ith item in knapsack
set i ←i-1
set k ←k-wi
Else
set i ←i-1
 Exit
i=3,k=3,vi=4,wi=3,Kp[i,k]=5,Kp[i-1,k]=5
i←i-1
i/w 0 1 2 3
0 0 0 0 0
1 0 2 2 2
2 0 2 3 5↕
3 0 2 3 5↕
i=2,k=3,vi=3,wi=2,Kp[i,k]=5,Kp[i-1,k]=2
Set k←k-wi=1
Set i←i-1
i/w 0 1 2 3
0 0 0 0 0
1 0 2 2 2↕
2 0 2 3 5↕
3 0 2 3 5
i=1,k=1,vi=2,wi=1,Kp[i,k]=2,Kp[i-1,k]=0
Set k←k-wi=0
Set i←i-1
i=0
K=0
i/w 0 1 2 3
0 0 0↕ 0 0
1 0 ↖2↕ 2 2↕
2 0 2 3 ↖5↕
3 0 2 3 5
The optimal knapsack contains
S= {1,2}
wi= {1,2}={3} <= W
vi= {2,3}= {5}» Optimum Value
THANKS!!!!

More Related Content

What's hot (20)

PPTX
Fractional Knapsack Problem
harsh kothari
 
PPTX
Knapsack problem algorithm, greedy algorithm
HoneyChintal
 
PDF
Optimal binary search tree dynamic programming
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PPT
Bellman Ford's Algorithm
Tanmay Baranwal
 
PPTX
Knapsack
Karthik Chetla
 
PPTX
Knapsack Problem
Jenny Galino
 
PPTX
N queen problem
Ridhima Chowdhury
 
PPTX
knapsack problem
Adnan Malak
 
PPT
Knapsack problem and Memory Function
Barani Tharan
 
PPT
dynamic programming Rod cutting class
giridaroori
 
PPTX
Backtracking
subhradeep mitra
 
PPTX
Daa:Dynamic Programing
rupali_2bonde
 
PPTX
0 1 knapsack using branch and bound
Abhishek Singh
 
PPTX
Dynamic programming
Melaku Bayih Demessie
 
DOC
Unit 3 daa
Nv Thejaswini
 
PPTX
Knapsack problem using greedy approach
padmeshagrekar
 
PDF
Dynamic Programming knapsack 0 1
Amit Kumar Rathi
 
PPTX
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
PPT
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Mohanlal Sukhadia University (MLSU)
 
PPTX
Dijkstra's algorithm presentation
Subid Biswas
 
Fractional Knapsack Problem
harsh kothari
 
Knapsack problem algorithm, greedy algorithm
HoneyChintal
 
Bellman Ford's Algorithm
Tanmay Baranwal
 
Knapsack
Karthik Chetla
 
Knapsack Problem
Jenny Galino
 
N queen problem
Ridhima Chowdhury
 
knapsack problem
Adnan Malak
 
Knapsack problem and Memory Function
Barani Tharan
 
dynamic programming Rod cutting class
giridaroori
 
Backtracking
subhradeep mitra
 
Daa:Dynamic Programing
rupali_2bonde
 
0 1 knapsack using branch and bound
Abhishek Singh
 
Dynamic programming
Melaku Bayih Demessie
 
Unit 3 daa
Nv Thejaswini
 
Knapsack problem using greedy approach
padmeshagrekar
 
Dynamic Programming knapsack 0 1
Amit Kumar Rathi
 
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Mohanlal Sukhadia University (MLSU)
 
Dijkstra's algorithm presentation
Subid Biswas
 

Similar to Dynamic Programming-Knapsack Problem (20)

PPTX
Knapsack Dynamic
Paras Patel
 
PPT
Design and analysis of Algorithms - Lecture 15.ppt
QurbanAli72
 
DOCX
Exp.docx
vennapusaIndrasenare
 
PPTX
Dynamic programming (dp) in Algorithm
RaihanIslamSonet
 
PPTX
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Abhishek Singh
 
PPT
DynProg_Knapsack.ppt
Ruchika Sinha
 
PPTX
Knapsack problem
RacksaviR
 
PPT
Lec37
Nikhil Chilwant
 
PPT
Knapsack Problem Analysis of Algorithm.ppt
SharjeelFaisal4
 
PPTX
Knapsack problem
garishma bhatia
 
PDF
306_0_1_Knapsack .pdf
anashgour47
 
PDF
0-1 knapsack problem
A. S. M. Shafi
 
PPTX
Module 3_Greedy Technique_2021 Scheme.pptx
RITIKKUMAR168218
 
PPT
AOA ppt.ppt
SaimaShaheen14
 
PPT
0-1 knapsack.ppt
AyushJaiswal513854
 
PDF
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Editor IJMTER
 
PPTX
Dynamic Programming
Kasun Ranga Wijeweera
 
PPTX
Presentation of knapsack
Gaurav Dubey
 
PDF
ADA Unit — 3 Dynamic Programming and Its Applications.pdf
RGPV De Bunkers
 
PDF
Knapsack dynamic programming formula bottom up
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Knapsack Dynamic
Paras Patel
 
Design and analysis of Algorithms - Lecture 15.ppt
QurbanAli72
 
Dynamic programming (dp) in Algorithm
RaihanIslamSonet
 
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Abhishek Singh
 
DynProg_Knapsack.ppt
Ruchika Sinha
 
Knapsack problem
RacksaviR
 
Knapsack Problem Analysis of Algorithm.ppt
SharjeelFaisal4
 
Knapsack problem
garishma bhatia
 
306_0_1_Knapsack .pdf
anashgour47
 
0-1 knapsack problem
A. S. M. Shafi
 
Module 3_Greedy Technique_2021 Scheme.pptx
RITIKKUMAR168218
 
AOA ppt.ppt
SaimaShaheen14
 
0-1 knapsack.ppt
AyushJaiswal513854
 
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Editor IJMTER
 
Dynamic Programming
Kasun Ranga Wijeweera
 
Presentation of knapsack
Gaurav Dubey
 
ADA Unit — 3 Dynamic Programming and Its Applications.pdf
RGPV De Bunkers
 
Ad

Recently uploaded (20)

PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Ad

Dynamic Programming-Knapsack Problem