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

More Related Content

PPTX
Operation research bba15
PPTX
Matrix chain multiplication
PPTX
Matrix chain multiplication by MHM
PPTX
Longest Common Subsequence & Matrix Chain Multiplication
DOCX
Matrix multiplicationdesign
PPTX
Matrix mult class-17
PPTX
Dynamic Programming - Matrix Chain Multiplication
PPTX
Dynamic programming1
Operation research bba15
Matrix chain multiplication
Matrix chain multiplication by MHM
Longest Common Subsequence & Matrix Chain Multiplication
Matrix multiplicationdesign
Matrix mult class-17
Dynamic Programming - Matrix Chain Multiplication
Dynamic programming1

What's hot (20)

PPT
dynamic programming Rod cutting class
PPT
Dynamic programming
PPT
Matrix Multiplication(An example of concurrent programming)
PPT
Dynamic programming
PPTX
Daa:Dynamic Programing
PPTX
Linear programming graphical method
PPT
5.3 dynamic programming 03
PPTX
Dynamic Programming
PPTX
unit-4-dynamic programming
PPT
Greedy method
PPT
Dynamic programming in Algorithm Analysis
PDF
Section 2 :Linear Programming: Graphical method
PDF
PDF
Dynamic programming
PDF
Matrix Chain Scheduling Algorithm
PDF
Skiena algorithm 2007 lecture16 introduction to dynamic programming
PPT
Dynamicpgmming
PPTX
Polynomials and Curve Fitting in MATLAB
DOCX
Matrices and determinants assignment
PDF
Cat Quant Cheat Sheet
dynamic programming Rod cutting class
Dynamic programming
Matrix Multiplication(An example of concurrent programming)
Dynamic programming
Daa:Dynamic Programing
Linear programming graphical method
5.3 dynamic programming 03
Dynamic Programming
unit-4-dynamic programming
Greedy method
Dynamic programming in Algorithm Analysis
Section 2 :Linear Programming: Graphical method
Dynamic programming
Matrix Chain Scheduling Algorithm
Skiena algorithm 2007 lecture16 introduction to dynamic programming
Dynamicpgmming
Polynomials and Curve Fitting in MATLAB
Matrices and determinants assignment
Cat Quant Cheat Sheet
Ad

Similar to Dynamic programming (dp) in Algorithm (20)

PPTX
01 Knapsack using Dynamic Programming
PPT
AOA ppt.ppt
PPT
Learn about dynamic programming and how to design algorith
PPT
Knapsack problem
PPTX
Knapsack problem dynamicprogramming
PPT
DynProg_Knapsack.ppt
PPT
Knapsack problem and Memory Function
PPTX
Dynamic Programming-Knapsack Problem
PDF
ADA Unit — 3 Dynamic Programming and Its Applications.pdf
PPTX
Design and Analysis of Algorithm-Lecture.pptx
PPTX
8_dynamic_algorithm powerpoint ptesentation.pptx
PPT
0/1 knapsack
PPT
PPT
0-1 knapsack.ppt
PPTX
Knapsack Dynamic
PDF
PPT
Design and analysis of Algorithms - Lecture 15.ppt
PPTX
Chapter 5.pptx
PPT
lecture 25
PPTX
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
01 Knapsack using Dynamic Programming
AOA ppt.ppt
Learn about dynamic programming and how to design algorith
Knapsack problem
Knapsack problem dynamicprogramming
DynProg_Knapsack.ppt
Knapsack problem and Memory Function
Dynamic Programming-Knapsack Problem
ADA Unit — 3 Dynamic Programming and Its Applications.pdf
Design and Analysis of Algorithm-Lecture.pptx
8_dynamic_algorithm powerpoint ptesentation.pptx
0/1 knapsack
0-1 knapsack.ppt
Knapsack Dynamic
Design and analysis of Algorithms - Lecture 15.ppt
Chapter 5.pptx
lecture 25
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Ad

Recently uploaded (20)

PDF
Insiders guide to clinical Medicine.pdf
PPTX
Institutional Correction lecture only . . .
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Basic Mud Logging Guide for educational purpose
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
01-Introduction-to-Information-Management.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Classroom Observation Tools for Teachers
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Computing-Curriculum for Schools in Ghana
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Insiders guide to clinical Medicine.pdf
Institutional Correction lecture only . . .
102 student loan defaulters named and shamed – Is someone you know on the list?
Basic Mud Logging Guide for educational purpose
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Cell Structure & Organelles in detailed.
O7-L3 Supply Chain Operations - ICLT Program
01-Introduction-to-Information-Management.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Complications of Minimal Access Surgery at WLH
2.FourierTransform-ShortQuestionswithAnswers.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
O5-L3 Freight Transport Ops (International) V1.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Classroom Observation Tools for Teachers
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Computing-Curriculum for Schools in Ghana
Chapter 2 Heredity, Prenatal Development, and Birth.pdf

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}