SlideShare ist ein Scribd-Unternehmen logo
1 von 18
Topic
Dynamic Programming (DP)
Dynamic Programming is a general algorithm design technique for solving
problems defined by recurrences with overlapping subproblems
•Invented by American mathematician Richard Bellman in the 1950s to solve
optimization problems and later assimilated by CS
•“Programming” here means “planning”
•Main idea:
- set up a recurrence relating a solution to a larger instance to solutions of some
smaller instances
- solve smaller instances once
- record solutions in a table
- extract solution to the initial instance from that table
Dynamic Programming (DP)
• Two key ingredients for an optimization problem
to be suitable for a dynamic-programming
solution:
Each substructure is
optimal.
(Principle of optimality)
1. optimal substructures 2. overlapping subproblems
Subproblems are dependent.
(otherwise, a divide-and-
conquer approach is the
choice.)
– The recurrence relation (for defining the value of an
optimal solution);
– The tabular computation (for computing the value of an
optimal solution);
– The traceback (for delivering an optimal solution).
The development of a dynamic-programming algorithm has three
basic components:
Some exemple for DP
• Coin Change
• 0/1 Knapsack
• LCS , Etc.
Problem
Set Of Coins = { 8 , 5 , 1}
Make Change Of Taka, n =10
Exemple for Coin Change
T[i]=min[T[i],1+T[i-coin[j]]]
Solution
j c o i n [ j ]
0 8
1 5
2 1
1 2 3 4 5 6 7 8 9 10
1 2 3 4 1 2 3 1 2 2
1 2 3 4 5 6 7 8 9 10
2 2 2 2 1 2 2 0 2 1
10 - 5 = 5 - 5 = 0 5+5=10
Problem :
n = 4
S = 5
Elements (size,value)= {(2,3),(3,4),(4,5),(5,6
Need = 7
Exemple for 0/1 Knapsack
is 0 1 2 3 4 5
0
1
2
3
4
Here ,
max item = 4
max size = 5
Solution
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
For s = 0 to s For i = 0
to n
V[0,s] = 0 V[i,0] =
0
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0
2 0
3 0
4 0
Item (Si,Vi
)
1 (2,3)
2 (3,4)
3 (4,5)
4 (5,6)
If Si<=S
if Vi+V[i-1,S-Si]>V[i-1,S]
V[I,s]=Vi+V[i-1,S-Si]
else
V[I,s]=V[i-1,S]
Else V[I,S]=V[i-1,S]
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
is 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Item (Si,Vi
)
1 (2,3)
2 (3,4)
3 (4,5)
4 (5,6)
i=n,k=S
while i,k > 0
if V[I,k]=V[1-i,k]
I = i-1, k = k-Si
else
i = i-1
is 0 1 2 3 4 5
0 0 0 0 0 00
1 0 0 3 3 33
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
The optimal knapsack should
contain {1,2} = 7
Item (Si,Vi
)
1 (2,3)
2 (3,4)
3 (4,5)
4 (5,6)
Problem
X = {A, B, C, B, D, A, B}
Y = {B, D, C, A, B, A}
Exemple for LCS
SOLUTION
0 if i = 0 or j = 0
c[i-1, j-1] + 1 if xi = yj
max(c[i, j-1], c[i-1, j]) if xi  yj
0 1 2 3 4 5 6
yj B D C A B A
0 0 0 0 0 0 0
0

0

0

0 1 1 1
0 1 1 1

1 2 2
0

1

1 2 2

2

2
0 1

1

2

2 3 3
0

1 2

2

2

3

3
0

1

2

2 3

3 4
0 1

2

2

3 4

4
If xi = yj
b[i, j] = “ ”
Else if
1 xi
2 A
c[i - 1, j] ≥ c[i, j-1] 2 B
3 C
4 B
5 D
6 A
7 B
b[i, j] = “  ”
else
b[i, j] = “  ”
c[i, j] =
• Start at b[m, n] and follow the arrows
• When we encounter a “ “ in b[i, j]  xi = yj is an element
of the LCS
1 xi
2 A
3 B
4 C
5 B
6 D
7 A
8 B
0 1 2 3 4 5 6
yj B D C A B A
0 0 0 0 0 0 0
0
0
0
0
0
0
0

0
1

1
1

1

1
1

0
1

1

1
2

2

2

0
1
2

2

2

2

2
1

1
2

2

2
3

3
1
2

2
3

3

3
4
1
2

2
3

3
4

4
Longest common subsequence is {A,B,C,B}
Dynamic programming (dp) in Algorithm

Weitere ähnliche Inhalte

Was ist angesagt?

Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
rupali_2bonde
 
Skiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingSkiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programming
zukun
 

Was ist angesagt? (20)

dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Linear programming graphical method
Linear programming graphical methodLinear programming graphical method
Linear programming graphical method
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
 
Greedy method
Greedy method Greedy method
Greedy method
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
 
Section 2 :Linear Programming: Graphical method
Section 2 :Linear Programming: Graphical methodSection 2 :Linear Programming: Graphical method
Section 2 :Linear Programming: Graphical method
 
DP
DPDP
DP
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Matrix Chain Scheduling Algorithm
Matrix Chain Scheduling AlgorithmMatrix Chain Scheduling Algorithm
Matrix Chain Scheduling Algorithm
 
Skiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programmingSkiena algorithm 2007 lecture16 introduction to dynamic programming
Skiena algorithm 2007 lecture16 introduction to dynamic programming
 
Dynamicpgmming
DynamicpgmmingDynamicpgmming
Dynamicpgmming
 
Polynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLABPolynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLAB
 
Matrices and determinants assignment
Matrices and determinants assignmentMatrices and determinants assignment
Matrices and determinants assignment
 
Cat Quant Cheat Sheet
Cat Quant Cheat SheetCat Quant Cheat Sheet
Cat Quant Cheat Sheet
 

Ähnlich wie Dynamic programming (dp) in Algorithm

Lesson 4.3 regular
Lesson 4.3 regularLesson 4.3 regular
Lesson 4.3 regular
morrobea
 
GCSEYr9-SolvingQuadratics.pptx
GCSEYr9-SolvingQuadratics.pptxGCSEYr9-SolvingQuadratics.pptx
GCSEYr9-SolvingQuadratics.pptx
Angelle Pantig
 
Lesson 5.3 honors
Lesson 5.3 honorsLesson 5.3 honors
Lesson 5.3 honors
morrobea
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
MyAlome
 
2.7 distributive property day 1
2.7 distributive property day 12.7 distributive property day 1
2.7 distributive property day 1
bweldon
 
Question 11. Determine which of the following points lies on .docx
Question 11.  Determine which of the following points lies on .docxQuestion 11.  Determine which of the following points lies on .docx
Question 11. Determine which of the following points lies on .docx
makdul
 

Ähnlich wie Dynamic programming (dp) in Algorithm (20)

Ncert solutions for class 7 maths chapter 1 integers exercise 1
Ncert solutions for class 7 maths chapter 1 integers exercise 1Ncert solutions for class 7 maths chapter 1 integers exercise 1
Ncert solutions for class 7 maths chapter 1 integers exercise 1
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
555_Spring12_topic06.ppt
555_Spring12_topic06.ppt555_Spring12_topic06.ppt
555_Spring12_topic06.ppt
 
Lesson 4.3 regular
Lesson 4.3 regularLesson 4.3 regular
Lesson 4.3 regular
 
Bentuk akar dan pangkat.pptx
Bentuk akar dan pangkat.pptxBentuk akar dan pangkat.pptx
Bentuk akar dan pangkat.pptx
 
GCSEYr9-SolvingQuadratics.pptx
GCSEYr9-SolvingQuadratics.pptxGCSEYr9-SolvingQuadratics.pptx
GCSEYr9-SolvingQuadratics.pptx
 
Duality in Linear Programming
Duality in Linear ProgrammingDuality in Linear Programming
Duality in Linear Programming
 
Dynamic Programming.pptx
Dynamic Programming.pptxDynamic Programming.pptx
Dynamic Programming.pptx
 
Ncert solutions for class 7 maths chapter 1 integers exercise 1.4
Ncert solutions for class 7 maths chapter 1 integers exercise 1.4Ncert solutions for class 7 maths chapter 1 integers exercise 1.4
Ncert solutions for class 7 maths chapter 1 integers exercise 1.4
 
Lesson 5.3 honors
Lesson 5.3 honorsLesson 5.3 honors
Lesson 5.3 honors
 
Fungsi rekursiv
Fungsi rekursivFungsi rekursiv
Fungsi rekursiv
 
Alegebra Powers Substitution
Alegebra Powers SubstitutionAlegebra Powers Substitution
Alegebra Powers Substitution
 
5617723.pptx
5617723.pptx5617723.pptx
5617723.pptx
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
 
Algebra Revision.ppt
Algebra Revision.pptAlgebra Revision.ppt
Algebra Revision.ppt
 
2.7 distributive property day 1
2.7 distributive property day 12.7 distributive property day 1
2.7 distributive property day 1
 
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
 
ملزمة الرياضيات للصف السادس الاحيائي الفصل الاول
ملزمة الرياضيات للصف السادس الاحيائي الفصل الاولملزمة الرياضيات للصف السادس الاحيائي الفصل الاول
ملزمة الرياضيات للصف السادس الاحيائي الفصل الاول
 
Dynamic Programming Matrix Chain Multiplication
Dynamic Programming Matrix Chain MultiplicationDynamic Programming Matrix Chain Multiplication
Dynamic Programming Matrix Chain Multiplication
 
Question 11. Determine which of the following points lies on .docx
Question 11.  Determine which of the following points lies on .docxQuestion 11.  Determine which of the following points lies on .docx
Question 11. Determine which of the following points lies on .docx
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
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)
 
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...
 
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
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
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
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).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
 

Dynamic programming (dp) in Algorithm

  • 2. Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems •Invented by American mathematician Richard Bellman in the 1950s to solve optimization problems and later assimilated by CS •“Programming” here means “planning” •Main idea: - set up a recurrence relating a solution to a larger instance to solutions of some smaller instances - solve smaller instances once - record solutions in a table - extract solution to the initial instance from that table Dynamic Programming (DP)
  • 3. • Two key ingredients for an optimization problem to be suitable for a dynamic-programming solution: Each substructure is optimal. (Principle of optimality) 1. optimal substructures 2. overlapping subproblems Subproblems are dependent. (otherwise, a divide-and- conquer approach is the choice.)
  • 4. – The recurrence relation (for defining the value of an optimal solution); – The tabular computation (for computing the value of an optimal solution); – The traceback (for delivering an optimal solution). The development of a dynamic-programming algorithm has three basic components:
  • 5. Some exemple for DP • Coin Change • 0/1 Knapsack • LCS , Etc.
  • 6. Problem Set Of Coins = { 8 , 5 , 1} Make Change Of Taka, n =10 Exemple for Coin Change
  • 7. T[i]=min[T[i],1+T[i-coin[j]]] Solution j c o i n [ j ] 0 8 1 5 2 1 1 2 3 4 5 6 7 8 9 10 1 2 3 4 1 2 3 1 2 2 1 2 3 4 5 6 7 8 9 10 2 2 2 2 1 2 2 0 2 1 10 - 5 = 5 - 5 = 0 5+5=10
  • 8. Problem : n = 4 S = 5 Elements (size,value)= {(2,3),(3,4),(4,5),(5,6 Need = 7 Exemple for 0/1 Knapsack
  • 9. is 0 1 2 3 4 5 0 1 2 3 4 Here , max item = 4 max size = 5 Solution
  • 10. is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 2 0 3 0 4 0 For s = 0 to s For i = 0 to n V[0,s] = 0 V[i,0] = 0
  • 11. is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 2 0 3 0 4 0 Item (Si,Vi ) 1 (2,3) 2 (3,4) 3 (4,5) 4 (5,6) If Si<=S if Vi+V[i-1,S-Si]>V[i-1,S] V[I,s]=Vi+V[i-1,S-Si] else V[I,s]=V[i-1,S] Else V[I,S]=V[i-1,S]
  • 12. is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7
  • 13. is 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 3 3 3 3 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 Item (Si,Vi ) 1 (2,3) 2 (3,4) 3 (4,5) 4 (5,6) i=n,k=S while i,k > 0 if V[I,k]=V[1-i,k] I = i-1, k = k-Si else i = i-1
  • 14. is 0 1 2 3 4 5 0 0 0 0 0 00 1 0 0 3 3 33 2 0 0 3 4 4 7 3 0 0 3 4 5 7 4 0 0 3 4 5 7 The optimal knapsack should contain {1,2} = 7 Item (Si,Vi ) 1 (2,3) 2 (3,4) 3 (4,5) 4 (5,6)
  • 15. Problem X = {A, B, C, B, D, A, B} Y = {B, D, C, A, B, A} Exemple for LCS
  • 16. SOLUTION 0 if i = 0 or j = 0 c[i-1, j-1] + 1 if xi = yj max(c[i, j-1], c[i-1, j]) if xi  yj 0 1 2 3 4 5 6 yj B D C A B A 0 0 0 0 0 0 0 0  0  0  0 1 1 1 0 1 1 1  1 2 2 0  1  1 2 2  2  2 0 1  1  2  2 3 3 0  1 2  2  2  3  3 0  1  2  2 3  3 4 0 1  2  2  3 4  4 If xi = yj b[i, j] = “ ” Else if 1 xi 2 A c[i - 1, j] ≥ c[i, j-1] 2 B 3 C 4 B 5 D 6 A 7 B b[i, j] = “  ” else b[i, j] = “  ” c[i, j] =
  • 17. • Start at b[m, n] and follow the arrows • When we encounter a “ “ in b[i, j]  xi = yj is an element of the LCS 1 xi 2 A 3 B 4 C 5 B 6 D 7 A 8 B 0 1 2 3 4 5 6 yj B D C A B A 0 0 0 0 0 0 0 0 0 0 0 0 0 0  0 1  1 1  1  1 1  0 1  1  1 2  2  2  0 1 2  2  2  2  2 1  1 2  2  2 3  3 1 2  2 3  3  3 4 1 2  2 3  3 4  4 Longest common subsequence is {A,B,C,B}