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

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
IJSRED
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Mrunal Patil
 

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 (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 30
sajinsc
 
lecture 29
lecture 29lecture 29
lecture 29
sajinsc
 
lecture 28
lecture 28lecture 28
lecture 28
sajinsc
 
lecture 27
lecture 27lecture 27
lecture 27
sajinsc
 
lecture 26
lecture 26lecture 26
lecture 26
sajinsc
 
lecture 24
lecture 24lecture 24
lecture 24
sajinsc
 
lecture 23
lecture 23lecture 23
lecture 23
sajinsc
 
lecture 22
lecture 22lecture 22
lecture 22
sajinsc
 
lecture 21
lecture 21lecture 21
lecture 21
sajinsc
 
lecture 20
lecture 20lecture 20
lecture 20
sajinsc
 
lecture 19
lecture 19lecture 19
lecture 19
sajinsc
 
lecture 18
lecture 18lecture 18
lecture 18
sajinsc
 
lecture 17
lecture 17lecture 17
lecture 17
sajinsc
 
lecture 16
lecture 16lecture 16
lecture 16
sajinsc
 
lecture 15
lecture 15lecture 15
lecture 15
sajinsc
 
lecture 14
lecture 14lecture 14
lecture 14
sajinsc
 
lecture 13
lecture 13lecture 13
lecture 13
sajinsc
 
lecture 12
lecture 12lecture 12
lecture 12
sajinsc
 
lecture 11
lecture 11lecture 11
lecture 11
sajinsc
 
lecture 10
lecture 10lecture 10
lecture 10
sajinsc
 

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

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Recently uploaded (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

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.