SlideShare a Scribd company logo
1 of 39
[object Object],[object Object],[object Object]
Review: Dynamic programming ,[object Object],[object Object],[object Object],[object Object],[object Object]
Properties of a problem that can be solved with dynamic programming ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Review:  Longest Common Subsequence (LCS) ,[object Object],[object Object],[object Object],[object Object]
Review:  Longest Common Subsequence (LCS) continued ,[object Object],[object Object],[object Object],[object Object],c[m,n] is the final solution
Review:  Longest Common Subsequence (LCS) ,[object Object],[object Object]
0-1 Knapsack problem ,[object Object],[object Object],[object Object]
0-1 Knapsack problem: a picture w i b i 10 9 8 5 5 4 4 3 3 2 Weight Benefit value This is a knapsack Max weight: W = 20 Items W = 20
0-1 Knapsack problem ,[object Object],[object Object],[object Object]
0-1 Knapsack problem: brute-force approach ,[object Object],[object Object],[object Object],[object Object]
0-1 Knapsack problem: brute-force approach ,[object Object],[object Object],[object Object],Let’s try this: If items are labeled  1..n , then a subproblem  would be to find an optimal solution for  S k  = {items labeled 1, 2, .. k}
Defining a Subproblem  ,[object Object],[object Object],[object Object],[object Object]
Defining a Subproblem Max weight: W = 20 For S 4 : Total weight: 14; total benefit: 20 w i b i 10 8 5 5 4 4 3 3 2 Weight Benefit 9 Item # 4 3 2 1 5 S 4 S 5 For S 5 : Total weight: 20 total benefit: 26 Solution for S 4  is not part of the solution for S 5 !!! ? w 1  =2 b 1  =3 w 2  =4 b 2  =5 w 3  =5 b 3  =8 w 4  =3 b 4  =4 w 1  =2 b 1  =3 w 2  =4 b 2  =5 w 3  =5 b 3  =8 w 4  =9 b 4  =10
Defining a Subproblem (continued) ,[object Object],[object Object],[object Object],[object Object]
Recursive Formula for subproblems ,[object Object],[object Object],[object Object],[object Object]
Recursive Formula ,[object Object],[object Object],[object Object]
0-1 Knapsack Algorithm ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Running time ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],What is the running time of this algorithm? O(W) O(W) Repeat  n  times O(n*W) Remember that the brute-force algorithm  takes O(2 n )
Example 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 (2) for w = 0 to W B[0,w] = 0 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 4
Example (3) for i = 0 to n B[i,0] = 0 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 4
Example (4) if w i  <= w  // item i can be part of the solution if b i  + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i  + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else  B[i,w] = B[i-1,w]   // w i  > w  0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=1 b i =3 w i =2 w= 1 w-w i  =-1 Items: 1: (2,3) 2: (3,4) 3: (4,5)  4: (5,6) 4 0
Example (5) if  w i  <= w   // item i can be part of the solution if  b i  + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i  + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w]  // w i  > w  0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=1 b i =3 w i =2 w= 2 w-w i  =0 Items: 1: (2,3) 2: (3,4) 3: (4,5)  4: (5,6) 4 0 3
Example (6) if  w i  <= w   // item i can be part of the solution if  b i  + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i  + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w]  // w i  > w  0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=1 b i =3 w i =2 w= 3 w-w i =1 Items: 1: (2,3) 2: (3,4) 3: (4,5)  4: (5,6) 4 0 3 3
Example (7) if  w i  <= w   // item i can be part of the solution if  b i  + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i  + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w]  // w i  > w  0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=1 b i =3 w i =2 w= 4 w-w i =2 Items: 1: (2,3) 2: (3,4) 3: (4,5)  4: (5,6) 4 0 3 3 3
Example (8) if  w i  <= w   // item i can be part of the solution if  b i  + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i  + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w]  // w i  > w  0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=1 b i =3 w i =2 w= 5 w-w i =2 Items: 1: (2,3) 2: (3,4) 3: (4,5)  4: (5,6) 4 0 3 3 3 3
Example (9) if w i  <= w  // item i can be part of the solution if b i  + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i  + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else   B[i,w] = B[i-1,w]   // w i  > w  0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=2 b i =4 w i =3 w= 1 w-w i =-2 Items: 1: (2,3) 2: (3,4) 3: (4,5)  4: (5,6) 4 0 3 3 3 3 0
Example (10) if w i  <= w  // item i can be part of the solution if b i  + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i  + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else   B[i,w] = B[i-1,w]   // w i  > w  0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=2 b i =4 w i =3 w= 2 w-w i =-1 Items: 1: (2,3) 2: (3,4) 3: (4,5)  4: (5,6) 4 0 3 3 3 3 0 3
Example (11) if  w i  <= w   // item i can be part of the solution if  b i  + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i  + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w]  // w i  > w  0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=2 b i =4 w i =3 w= 3 w-w i =0 Items: 1: (2,3) 2: (3,4) 3: (4,5)  4: (5,6) 4 0 3 3 3 3 0 3 4
Example (12) if  w i  <= w   // item i can be part of the solution if  b i  + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i  + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w]  // w i  > w  0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=2 b i =4 w i =3 w= 4 w-w i =1 Items: 1: (2,3) 2: (3,4) 3: (4,5)  4: (5,6) 4 0 3 3 3 3 0 3 4 4
Example (13) if  w i  <= w   // item i can be part of the solution if  b i  + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i  + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w]  // w i  > w  0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=2 b i =4 w i =3 w= 5 w-w i =2 Items: 1: (2,3) 2: (3,4) 3: (4,5)  4: (5,6) 4 0 3 3 3 3 0 3 4 4 7
Example (14) if w i  <= w  // item i can be part of the solution if b i  + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i  + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else   B[i,w] = B[i-1,w]   // w i  > w  0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=3 b i =5 w i =4 w= 1..3 Items: 1: (2,3) 2: (3,4) 3: (4,5)  4: (5,6) 4 0 3 3 3 3 0 0 3 4 4 7 0 3 4
Example (15) if  w i  <= w   // item i can be part of the solution if  b i  + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i  + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w]  // w i  > w  0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=3 b i =5 w i =4 w= 4 w- w i =0 Items: 1: (2,3) 2: (3,4) 3: (4,5)  4: (5,6) 4 0 0 0 3 4 4 7 0 3 4 5 3 3 3 3
Example (15) if  w i  <= w   // item i can be part of the solution if b i  + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i  + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w]  // w i  > w  0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=3 b i =5 w i =4 w= 5 w- w i =1 Items: 1: (2,3) 2: (3,4) 3: (4,5)  4: (5,6) 4 0 0 0 3 4 4 7 0 3 4 5 7 3 3 3 3
Example (16) if w i  <= w  // item i can be part of the solution if b i  + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i  + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else  B[i,w] = B[i-1,w]   // w i  > w  0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=3 b i =5 w i =4 w= 1..4 Items: 1: (2,3) 2: (3,4) 3: (4,5)  4: (5,6) 4 0 0 0 3 4 4 7 0 3 4 5 7 0 3 4 5 3 3 3 3
Example (17) if  w i  <= w   // item i can be part of the solution if b i  + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i  + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w]  // w i  > w  0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=3 b i =5 w i =4 w= 5 Items: 1: (2,3) 2: (3,4) 3: (4,5)  4: (5,6) 4 0 0 0 3 4 4 7 0 3 4 5 7 0 3 4 5 7 3 3 3 3
Comments ,[object Object],[object Object],[object Object]
Conclusion ,[object Object],[object Object],[object Object],[object Object],[object Object]
The End

More Related Content

What's hot

Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack DynamicParas Patel
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsackAmin Omi
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEMi i
 
Presentation of knapsack
Presentation of knapsackPresentation of knapsack
Presentation of knapsackGaurav Dubey
 
Knapsack dp
Knapsack dpKnapsack dp
Knapsack dpVivek Rathi
 
Simultaneous Triple Series Equations Involving Konhauser Biorthogonal Polynom...
Simultaneous Triple Series Equations Involving Konhauser Biorthogonal Polynom...Simultaneous Triple Series Equations Involving Konhauser Biorthogonal Polynom...
Simultaneous Triple Series Equations Involving Konhauser Biorthogonal Polynom...IOSR Journals
 
0-1 knapsack problem
0-1 knapsack problem0-1 knapsack problem
0-1 knapsack problemA. S. M. Shafi
 
A Study on Intuitionistic Multi-Anti Fuzzy Subgroups
A Study on Intuitionistic Multi-Anti Fuzzy Subgroups A Study on Intuitionistic Multi-Anti Fuzzy Subgroups
A Study on Intuitionistic Multi-Anti Fuzzy Subgroups mathsjournal
 
A fixed point theorem for weakly c contraction mappings of integral type.
A fixed point theorem for  weakly c   contraction mappings of integral type.A fixed point theorem for  weakly c   contraction mappings of integral type.
A fixed point theorem for weakly c contraction mappings of integral type.Alexander Decker
 
Neutrosophic Soft Topological Spaces on New Operations
Neutrosophic Soft Topological Spaces on New OperationsNeutrosophic Soft Topological Spaces on New Operations
Neutrosophic Soft Topological Spaces on New OperationsIJSRED
 
Integration 2 elur niahc
Integration 2 elur niahcIntegration 2 elur niahc
Integration 2 elur niahcShaun Wilson
 
FACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDED
FACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDEDFACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDED
FACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDEDZac Darcy
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEMMrunal Patil
 
Chapter002math
Chapter002mathChapter002math
Chapter002mathYuna Argadewi
 
On Fuzzy Soft Multi Set and Its Application in Information Systems
On Fuzzy Soft Multi Set and Its Application in Information Systems On Fuzzy Soft Multi Set and Its Application in Information Systems
On Fuzzy Soft Multi Set and Its Application in Information Systems ijcax
 

What's hot (20)

Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack Dynamic
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
 
Presentation of knapsack
Presentation of knapsackPresentation of knapsack
Presentation of knapsack
 
Knapsack dp
Knapsack dpKnapsack dp
Knapsack dp
 
Simultaneous Triple Series Equations Involving Konhauser Biorthogonal Polynom...
Simultaneous Triple Series Equations Involving Konhauser Biorthogonal Polynom...Simultaneous Triple Series Equations Involving Konhauser Biorthogonal Polynom...
Simultaneous Triple Series Equations Involving Konhauser Biorthogonal Polynom...
 
0-1 knapsack problem
0-1 knapsack problem0-1 knapsack problem
0-1 knapsack problem
 
A Study on Intuitionistic Multi-Anti Fuzzy Subgroups
A Study on Intuitionistic Multi-Anti Fuzzy Subgroups A Study on Intuitionistic Multi-Anti Fuzzy Subgroups
A Study on Intuitionistic Multi-Anti Fuzzy Subgroups
 
A fixed point theorem for weakly c contraction mappings of integral type.
A fixed point theorem for  weakly c   contraction mappings of integral type.A fixed point theorem for  weakly c   contraction mappings of integral type.
A fixed point theorem for weakly c contraction mappings of integral type.
 
Neutrosophic Soft Topological Spaces on New Operations
Neutrosophic Soft Topological Spaces on New OperationsNeutrosophic Soft Topological Spaces on New Operations
Neutrosophic Soft Topological Spaces on New Operations
 
Indefinite Integral 18
Indefinite Integral 18Indefinite Integral 18
Indefinite Integral 18
 
Integration 2 elur niahc
Integration 2 elur niahcIntegration 2 elur niahc
Integration 2 elur niahc
 
redes neuronais
redes neuronaisredes neuronais
redes neuronais
 
Spherical interval-valued fuzzy bi-ideals of gamma near-rings
Spherical interval-valued fuzzy bi-ideals of gamma near-ringsSpherical interval-valued fuzzy bi-ideals of gamma near-rings
Spherical interval-valued fuzzy bi-ideals of gamma near-rings
 
Alg1 lesson 7-2
Alg1 lesson 7-2Alg1 lesson 7-2
Alg1 lesson 7-2
 
FACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDED
FACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDEDFACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDED
FACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDED
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
 
50320140501001
5032014050100150320140501001
50320140501001
 
Chapter002math
Chapter002mathChapter002math
Chapter002math
 
On Fuzzy Soft Multi Set and Its Application in Information Systems
On Fuzzy Soft Multi Set and Its Application in Information Systems On Fuzzy Soft Multi Set and Its Application in Information Systems
On Fuzzy Soft Multi Set and Its Application in Information Systems
 

Similar to lecture 25

DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.pptRuchika Sinha
 
Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1Amit 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 KnapsackAsif Shahriar
 
Dynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse studentsDynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse studentsDeepakGowda357858
 
Greedy algo revision 2
Greedy algo revision 2Greedy algo revision 2
Greedy algo revision 2maamir farooq
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problemRacksaviR
 

Similar to lecture 25 (7)

DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.ppt
 
0-1 knapsack.ppt
0-1 knapsack.ppt0-1 knapsack.ppt
0-1 knapsack.ppt
 
Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1
 
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
 
Dynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse studentsDynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse students
 
Greedy algo revision 2
Greedy algo revision 2Greedy algo revision 2
Greedy algo revision 2
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 

More from sajinsc

lecture 30
lecture 30lecture 30
lecture 30sajinsc
 
lecture 29
lecture 29lecture 29
lecture 29sajinsc
 
lecture 28
lecture 28lecture 28
lecture 28sajinsc
 
lecture 27
lecture 27lecture 27
lecture 27sajinsc
 
lecture 26
lecture 26lecture 26
lecture 26sajinsc
 
lecture 24
lecture 24lecture 24
lecture 24sajinsc
 
lecture 23
lecture 23lecture 23
lecture 23sajinsc
 
lecture 22
lecture 22lecture 22
lecture 22sajinsc
 
lecture 21
lecture 21lecture 21
lecture 21sajinsc
 
lecture 20
lecture 20lecture 20
lecture 20sajinsc
 
lecture 19
lecture 19lecture 19
lecture 19sajinsc
 
lecture 18
lecture 18lecture 18
lecture 18sajinsc
 
lecture 17
lecture 17lecture 17
lecture 17sajinsc
 
lecture 16
lecture 16lecture 16
lecture 16sajinsc
 
lecture 15
lecture 15lecture 15
lecture 15sajinsc
 
lecture 14
lecture 14lecture 14
lecture 14sajinsc
 
lecture 13
lecture 13lecture 13
lecture 13sajinsc
 
lecture 12
lecture 12lecture 12
lecture 12sajinsc
 
lecture 11
lecture 11lecture 11
lecture 11sajinsc
 
lecture 10
lecture 10lecture 10
lecture 10sajinsc
 

More from sajinsc (20)

lecture 30
lecture 30lecture 30
lecture 30
 
lecture 29
lecture 29lecture 29
lecture 29
 
lecture 28
lecture 28lecture 28
lecture 28
 
lecture 27
lecture 27lecture 27
lecture 27
 
lecture 26
lecture 26lecture 26
lecture 26
 
lecture 24
lecture 24lecture 24
lecture 24
 
lecture 23
lecture 23lecture 23
lecture 23
 
lecture 22
lecture 22lecture 22
lecture 22
 
lecture 21
lecture 21lecture 21
lecture 21
 
lecture 20
lecture 20lecture 20
lecture 20
 
lecture 19
lecture 19lecture 19
lecture 19
 
lecture 18
lecture 18lecture 18
lecture 18
 
lecture 17
lecture 17lecture 17
lecture 17
 
lecture 16
lecture 16lecture 16
lecture 16
 
lecture 15
lecture 15lecture 15
lecture 15
 
lecture 14
lecture 14lecture 14
lecture 14
 
lecture 13
lecture 13lecture 13
lecture 13
 
lecture 12
lecture 12lecture 12
lecture 12
 
lecture 11
lecture 11lecture 11
lecture 11
 
lecture 10
lecture 10lecture 10
lecture 10
 

Recently uploaded

Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 

Recently uploaded (20)

Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 

lecture 25

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8. 0-1 Knapsack problem: a picture w i b i 10 9 8 5 5 4 4 3 3 2 Weight Benefit value This is a knapsack Max weight: W = 20 Items W = 20
  • 9.
  • 10.
  • 11.
  • 12.
  • 13. Defining a Subproblem Max weight: W = 20 For S 4 : Total weight: 14; total benefit: 20 w i b i 10 8 5 5 4 4 3 3 2 Weight Benefit 9 Item # 4 3 2 1 5 S 4 S 5 For S 5 : Total weight: 20 total benefit: 26 Solution for S 4 is not part of the solution for S 5 !!! ? w 1 =2 b 1 =3 w 2 =4 b 2 =5 w 3 =5 b 3 =8 w 4 =3 b 4 =4 w 1 =2 b 1 =3 w 2 =4 b 2 =5 w 3 =5 b 3 =8 w 4 =9 b 4 =10
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19. Example 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)
  • 20. Example (2) for w = 0 to W B[0,w] = 0 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 4
  • 21. Example (3) for i = 0 to n B[i,0] = 0 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 4
  • 22. Example (4) if w i <= w // item i can be part of the solution if b i + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // w i > w 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=1 b i =3 w i =2 w= 1 w-w i =-1 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 4 0
  • 23. Example (5) if w i <= w // item i can be part of the solution if b i + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // w i > w 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=1 b i =3 w i =2 w= 2 w-w i =0 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 4 0 3
  • 24. Example (6) if w i <= w // item i can be part of the solution if b i + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // w i > w 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=1 b i =3 w i =2 w= 3 w-w i =1 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 4 0 3 3
  • 25. Example (7) if w i <= w // item i can be part of the solution if b i + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // w i > w 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=1 b i =3 w i =2 w= 4 w-w i =2 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 4 0 3 3 3
  • 26. Example (8) if w i <= w // item i can be part of the solution if b i + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // w i > w 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=1 b i =3 w i =2 w= 5 w-w i =2 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 4 0 3 3 3 3
  • 27. Example (9) if w i <= w // item i can be part of the solution if b i + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // w i > w 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=2 b i =4 w i =3 w= 1 w-w i =-2 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 4 0 3 3 3 3 0
  • 28. Example (10) if w i <= w // item i can be part of the solution if b i + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // w i > w 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=2 b i =4 w i =3 w= 2 w-w i =-1 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 4 0 3 3 3 3 0 3
  • 29. Example (11) if w i <= w // item i can be part of the solution if b i + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // w i > w 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=2 b i =4 w i =3 w= 3 w-w i =0 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 4 0 3 3 3 3 0 3 4
  • 30. Example (12) if w i <= w // item i can be part of the solution if b i + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // w i > w 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=2 b i =4 w i =3 w= 4 w-w i =1 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 4 0 3 3 3 3 0 3 4 4
  • 31. Example (13) if w i <= w // item i can be part of the solution if b i + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // w i > w 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=2 b i =4 w i =3 w= 5 w-w i =2 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 4 0 3 3 3 3 0 3 4 4 7
  • 32. Example (14) if w i <= w // item i can be part of the solution if b i + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // w i > w 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=3 b i =5 w i =4 w= 1..3 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 4 0 3 3 3 3 0 0 3 4 4 7 0 3 4
  • 33. Example (15) if w i <= w // item i can be part of the solution if b i + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // w i > w 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=3 b i =5 w i =4 w= 4 w- w i =0 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 4 0 0 0 3 4 4 7 0 3 4 5 3 3 3 3
  • 34. Example (15) if w i <= w // item i can be part of the solution if b i + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // w i > w 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=3 b i =5 w i =4 w= 5 w- w i =1 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 4 0 0 0 3 4 4 7 0 3 4 5 7 3 3 3 3
  • 35. Example (16) if w i <= w // item i can be part of the solution if b i + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // w i > w 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=3 b i =5 w i =4 w= 1..4 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 4 0 0 0 3 4 4 7 0 3 4 5 7 0 3 4 5 3 3 3 3
  • 36. Example (17) if w i <= w // item i can be part of the solution if b i + B[i-1,w-w i ] > B[i-1,w] B[i,w] = b i + B[i-1,w- w i ] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // w i > w 0 0 0 0 0 0 W 0 1 2 3 4 5 i 0 1 2 3 0 0 0 0 i=3 b i =5 w i =4 w= 5 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 4 0 0 0 3 4 4 7 0 3 4 5 7 0 3 4 5 7 3 3 3 3
  • 37.
  • 38.