SlideShare a Scribd company logo
4
Most read
7
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

PPT
Knapsack problem
PPT
0/1 knapsack
PPT
Dynamic pgmming
DOCX
0-1 KNAPSACK PROBLEM
 
PPTX
Knapsack Problem (DP & GREEDY)
PPTX
Knapsack problem dynamicprogramming
PPTX
knapsack problem
PPTX
01 Knapsack using Dynamic Programming
Knapsack problem
0/1 knapsack
Dynamic pgmming
0-1 KNAPSACK PROBLEM
 
Knapsack Problem (DP & GREEDY)
Knapsack problem dynamicprogramming
knapsack problem
01 Knapsack using Dynamic Programming

What's hot (20)

PPT
Knapsack problem and Memory Function
PPT
Knapsack Algorithm www.geekssay.com
PPT
minimum spanning trees Algorithm
PPTX
Knapsack Problem
PPTX
All pair shortest path
PPTX
Kruskal Algorithm
PPT
Unit 4 external sorting
PPTX
Combinatorial Optimization
PPTX
Tsp branch and-bound
PPTX
Greedy algorithms
PPT
Bellman Ford's Algorithm
PPT
Knapsack problem using dynamic programming
PPTX
Fractional Knapsack Problem
PPTX
Divide and conquer - Quick sort
PPTX
Divide and Conquer
PPTX
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
PDF
Heap Tree.pdf
PPTX
PostgreSQL and Linux Containers
PPTX
Lecture 6 disjoint set
Knapsack problem and Memory Function
Knapsack Algorithm www.geekssay.com
minimum spanning trees Algorithm
Knapsack Problem
All pair shortest path
Kruskal Algorithm
Unit 4 external sorting
Combinatorial Optimization
Tsp branch and-bound
Greedy algorithms
Bellman Ford's Algorithm
Knapsack problem using dynamic programming
Fractional Knapsack Problem
Divide and conquer - Quick sort
Divide and Conquer
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Heap Tree.pdf
PostgreSQL and Linux Containers
Lecture 6 disjoint set
Ad

Similar to Dynamic Programming-Knapsack Problem (20)

PPTX
Presentation of knapsack
PPT
DynProg_Knapsack.ppt
PPTX
Knapsack Dynamic
PDF
1.Prove 3 n^3 + 5n , n = 1For n = 1, 1^3 + 51 = 1 + 5 = 6, a.pdf
PDF
Fuzzy clustering and merging
 
PDF
P1 . norm vector space
PDF
maths assignment .pdf for practicing note
PDF
sublabel accurate convex relaxation of vectorial multilabel energies
PPTX
Daa:Dynamic Programing
PDF
module3_Greedymethod_2022.pdf
PPT
5.3 dynamic programming 03
PDF
13 - 06 Feb - Dynamic Programming
PDF
dot product of vectors
PPTX
Greedy algo revision 2
PDF
Lesson03 Dot Product And Matrix Multiplication Slides Notes
PDF
Calculus Early Transcendentals 10th Edition Anton Solutions Manual
DOC
Data structure notes
PDF
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
PPT
Prolog resume
PPT
Prolog resume
Presentation of knapsack
DynProg_Knapsack.ppt
Knapsack Dynamic
1.Prove 3 n^3 + 5n , n = 1For n = 1, 1^3 + 51 = 1 + 5 = 6, a.pdf
Fuzzy clustering and merging
 
P1 . norm vector space
maths assignment .pdf for practicing note
sublabel accurate convex relaxation of vectorial multilabel energies
Daa:Dynamic Programing
module3_Greedymethod_2022.pdf
5.3 dynamic programming 03
13 - 06 Feb - Dynamic Programming
dot product of vectors
Greedy algo revision 2
Lesson03 Dot Product And Matrix Multiplication Slides Notes
Calculus Early Transcendentals 10th Edition Anton Solutions Manual
Data structure notes
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Prolog resume
Prolog resume
Ad

Recently uploaded (20)

PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Well-logging-methods_new................
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Sustainable Sites - Green Building Construction
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
additive manufacturing of ss316l using mig welding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
composite construction of structures.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Lecture Notes Electrical Wiring System Components
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Operating System & Kernel Study Guide-1 - converted.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
OOP with Java - Java Introduction (Basics)
CH1 Production IntroductoryConcepts.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Well-logging-methods_new................
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
CYBER-CRIMES AND SECURITY A guide to understanding
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Sustainable Sites - Green Building Construction
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
additive manufacturing of ss316l using mig welding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
composite construction of structures.pdf
Internet of Things (IOT) - A guide to understanding
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Lecture Notes Electrical Wiring System Components
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx

Dynamic Programming-Knapsack Problem