SlideShare a Scribd company logo
Dynamic Programming and Applications
Yıldırım TAM
Dynamic Programming
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
Divide-and-conquer
• Divide-and-conquer method for algorithm design:
• Divide: If the input size is too large to deal with in a
straightforward manner, divide the problem into two or
more disjoint subproblems
• Conquer: conquer recursively to solve the subproblems
• Combine: Take the solutions to the subproblems and
“merge” these solutions into a solution for the original
problem
Divide-and-conquer - Example
Dynamic programming
• Dynamic programming is a way of improving on inefficient divide-
and-conquer algorithms.
• By “inefficient”, we mean that the same recursive call is made
over and over.
• If same subproblem is solved several times, we can use table to
store result of a subproblem the first time it is computed and thus
never have to recompute it again.
• Dynamic programming is applicable when the subproblems are
dependent, that is, when subproblems share subsubproblems.
• “Programming” refers to a tabular method
Difference between DP and Divide-
and-Conquer
• Using Divide-and-Conquer to solve these
problems is inefficient because the same
common subproblems have to be solved many
times.
• DP will solve each of them once and their
answers are stored in a table for future use.
Dynamic Programming vs. Recursion
and Divide & Conquer
Elements of Dynamic Programming
(DP)
DP is used to solve problems with the following characteristics:
• Simple subproblems
– We should be able to break the original problem to smaller
subproblems that have the same structure
• Optimal substructure of the problems
– The optimal solution to the problem contains within optimal
solutions to its subproblems.
• Overlapping sub-problems
– there exist some places where we solve the same subproblem more
than once.
Steps to Designing a
Dynamic Programming Algorithm
1. Characterize optimal substructure
2. Recursively define the value of an optimal
solution
3. Compute the value bottom up
4. (if needed) Construct an optimal solution
Principle of Optimality
• The dynamic Programming works on a principle
of optimality.
• Principle of optimality states that in an optimal
sequence of decisions or choices, each sub
sequences must also be optimal.
Example 1: Fibonacci numbers
11
• Recall definition of Fibonacci numbers:
F(n) = F(n-1) + F(n-2)
F(0) = 0
F(1) = 1
• Computing the nth Fibonacci number recursively (top-down):
F(n)
F(n-1) + F(n-2)
F(n-2) + F(n-3) F(n-3) + F(n-4)
...
Fibonacci Numbers
• Fn= Fn-1+ Fn-2 n ≥ 2
• F0 =0, F1 =1
• 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …
• Straightforward recursive procedure is slow!
• Let’s draw the recursion tree
Fibonacci Numbers
Fibonacci Numbers
• We can calculate Fn in linear time by remembering
solutions to the solved subproblems – dynamic
programming
• Compute solution in a bottom-up fashion
• In this case, only two values need to be
remembered at any time
Example Applications of Dynamic
Programming
Knapsack Problem
1
Value
18
22
28
1
Weight
5
6
6 2
7
Item
1
3
4
5
2W = 11
Shortest path problems
A traveler wishes to minimize the length of a journey
from town A to J.
Shortest path problems(Cont..)
The length of the route A-B-F-I-J: 2+4+3+4=13.
Can we find shorter route?
Matrix chain multiplication
Matrix Chain Multiplication
• Given : a chain of matrices {A1,A2,…,An}.
• Once all pairs of matrices are parenthesized, they can
be multiplied by using the standard algorithm as a sub-
routine.
• A product of matrices is fully parenthesized if it is either
a single matrix or the product of two fully parenthesized
matrix products, surrounded by parentheses. [Note: since
matrix multiplication is associative, all parenthesizations yield the
same product.]
Matrix Chain Multiplication cont.
• For example, if the chain of matrices is {A, B, C,
D}, the product A, B, C, D can be fully
parenthesized in 5 distinct ways:
(A ( B ( C D ))),
(A (( B C ) D )),
((A B ) ( C D )),
((A ( B C )) D),
((( A B ) C ) D ).
• The way the chain is parenthesized can have a
dramatic impact on the cost of evaluating the
product.
Matrix Chain Multiplication Optimal
Parenthesization
• Example: A[30][35], B[35][15], C[15][5]
minimum of A*B*C
A*(B*C) = 30*35*5 + 35*15*5 = 7,585
(A*B)*C = 30*35*15 + 30*15*5 = 18,000
• How to optimize:
– Brute force – look at every possible way to
parenthesize : Ω(4n/n3/2)
– Dynamic programming – time complexity of Ω(n3) and
space complexity of Θ(n2).
Matrix Chain Multiplication Structure of
Optimal Parenthesization
• For n matrices, let Ai..j be the result of AiAi+1….Aj
• An optimal parenthesization of AiAi+1…An splits
the product between Ak and Ak+1 where 1  k <
n.
• Example, k = 4 (A1A2A3A4)(A5A6)
Total cost of A1..6 = cost of A1..4 plus total
cost of multiplying these two matrices
together.
Matrix Chain Multiplication
Overlapping Sub-Problems
• Overlapping sub-problems helps in reducing the
running time considerably.
– Create a table M of minimum Costs
– Create a table S that records index k for each optimal sub-
problem
– Fill table M in a manner that corresponds to solving the
parenthesization problem on matrix chains of increasing
length.
– Compute cost for chains of length 1 (this is 0)
– Compute costs for chains of length 2
A1..2, A2..3, A3..4, …An-1…n
– Compute cost for chain of length n
A1..nEach level relies on smaller sub-strings
Q1.What is the shortest path?
Answer Q1
The length of the route A-B-F-I-J:
2+4+3+4=13.
Q2. Who invited dynamic programming?
• A. Richard Ernest Bellman
• B. Edsger Dijkstra
• C. Joseph Kruskal
• D. David A. Huffman
Answer Q2
• A. Richard Ernest Bellman
• B. Edsger Dijkstra
• C. Joseph Kruskal
• D. David A. Huffman
Q3. What is meaning of programming in DP?
A. Plannig
B. Computer Programming
C. Programming languages
D. Curriculum
Q3. What is meaning of programming in DP?
A. Plannig
B. Computer Programming
C. Programming languages
D. Curriculum

More Related Content

What's hot (20)

PPT
Divide and conquer
Dr Shashikant Athawale
 
PPT
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
PPT
Dinive conquer algorithm
Mohd Arif
 
PPTX
Matrix chain multiplication
Respa Peter
 
PPTX
Elements of dynamic programming
Tafhim Islam
 
PPTX
Knapsack Problem
Jenny Galino
 
PPTX
Graph coloring using backtracking
shashidharPapishetty
 
PPTX
Dijkstra's Algorithm
Rashik Ishrak Nahian
 
PPT
Randomized algorithms ver 1.0
Dr. C.V. Suresh Babu
 
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
PPTX
Greedy algorithms
sandeep54552
 
PPT
Branch and bound
Dr Shashikant Athawale
 
PDF
Algorithms Lecture 1: Introduction to Algorithms
Mohamed Loey
 
PPT
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
PPTX
Algorithm
IHTISHAM UL HAQ
 
PPTX
01 Knapsack using Dynamic Programming
Fenil Shah
 
PPTX
Tsp branch and-bound
Saravanan Natarajan
 
PPTX
Travelling salesman dynamic programming
maharajdey
 
PPTX
Greedy Algorithms
Amrinder Arora
 
Divide and conquer
Dr Shashikant Athawale
 
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
Dinive conquer algorithm
Mohd Arif
 
Matrix chain multiplication
Respa Peter
 
Elements of dynamic programming
Tafhim Islam
 
Knapsack Problem
Jenny Galino
 
Graph coloring using backtracking
shashidharPapishetty
 
Dijkstra's Algorithm
Rashik Ishrak Nahian
 
Randomized algorithms ver 1.0
Dr. C.V. Suresh Babu
 
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
Greedy algorithms
sandeep54552
 
Branch and bound
Dr Shashikant Athawale
 
Algorithms Lecture 1: Introduction to Algorithms
Mohamed Loey
 
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
Algorithm
IHTISHAM UL HAQ
 
01 Knapsack using Dynamic Programming
Fenil Shah
 
Tsp branch and-bound
Saravanan Natarajan
 
Travelling salesman dynamic programming
maharajdey
 
Greedy Algorithms
Amrinder Arora
 

Viewers also liked (20)

PDF
Randomized Algorithms in Linear Algebra & the Column Subset Selection Problem
Wei Xue
 
PPT
Chap08alg
Munkhchimeg
 
PDF
Solving The Shortest Path Tour Problem
Nozir Shokirov
 
PPT
21 backtracking
Aparup Behera
 
PPT
Karnaugh Map
Syed Absar
 
PDF
DP
Subba Oota
 
PPTX
Dynamic programming class 16
Kumar
 
PPT
5.5 back track
Krish_ver2
 
PPTX
Subset sum problem Dynamic and Brute Force Approch
Ijlal Ijlal
 
PPT
Dynamic programming in Algorithm Analysis
Rajendran
 
PPTX
Class warshal2
Debarati Das
 
PPT
Covering (Rules-based) Algorithm
ZHAO Sam
 
PPT
Backtracking
Vikas Sharma
 
PPTX
Dynamic Programming - Part 1
Amrinder Arora
 
PPT
Lect6 csp
trivedidr
 
PPTX
Backtracking
Sally Salem
 
PPT
Dynamic pgmming
Dr. C.V. Suresh Babu
 
PPTX
Queue- 8 Queen
Ha Ninh
 
PPT
01 knapsack using backtracking
mandlapure
 
PPTX
Back tracking and branch and bound class 20
Kumar
 
Randomized Algorithms in Linear Algebra & the Column Subset Selection Problem
Wei Xue
 
Chap08alg
Munkhchimeg
 
Solving The Shortest Path Tour Problem
Nozir Shokirov
 
21 backtracking
Aparup Behera
 
Karnaugh Map
Syed Absar
 
Dynamic programming class 16
Kumar
 
5.5 back track
Krish_ver2
 
Subset sum problem Dynamic and Brute Force Approch
Ijlal Ijlal
 
Dynamic programming in Algorithm Analysis
Rajendran
 
Class warshal2
Debarati Das
 
Covering (Rules-based) Algorithm
ZHAO Sam
 
Backtracking
Vikas Sharma
 
Dynamic Programming - Part 1
Amrinder Arora
 
Lect6 csp
trivedidr
 
Backtracking
Sally Salem
 
Dynamic pgmming
Dr. C.V. Suresh Babu
 
Queue- 8 Queen
Ha Ninh
 
01 knapsack using backtracking
mandlapure
 
Back tracking and branch and bound class 20
Kumar
 
Ad

Similar to Dynamic programming (20)

PPTX
Module 2ppt.pptx divid and conquer method
JyoReddy9
 
PPTX
Dynamic programming prasintation eaisy
ahmed51236
 
PPTX
Introduction to dynamic programming
Amisha Narsingani
 
PPTX
Dynamic Programing.pptx good for understanding
HUSNAINAHMAD39
 
PPTX
Dynamic programmng2
debolina13
 
PPTX
Introduction to Dynamic Programming, Principle of Optimality
Bhavin Darji
 
PPTX
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
PPT
5.3 dynamic programming 03
Krish_ver2
 
PPT
Lecture11
Nv Thejaswini
 
PPTX
Computer science in Dynamic programming .pptx
shorabi2127061
 
PPTX
unit-4-dynamic programming
hodcsencet
 
PPT
Dynamic programming 2
Roy Thomas
 
PPT
Dynamic programming
princekoushik2
 
PPTX
dinosourrrrrrrrrrrrrrrrrrrrrr formula .pptx
ShohidulIslamSovon
 
PDF
Dynamic programming
Jay Nagar
 
PDF
Daa chapter 3
B.Kirron Reddi
 
PPTX
Annotaed slides for dynamic programming algorithm
johnathangamal27
 
PPTX
week 9 lec 15.pptx
SulamanHassan
 
PPTX
ADA Unit 2.pptx
AmanKumar879992
 
PDF
L21_L27_Unit_5_Dynamic_Programming Computer Science
priyanshukumarbt23cs
 
Module 2ppt.pptx divid and conquer method
JyoReddy9
 
Dynamic programming prasintation eaisy
ahmed51236
 
Introduction to dynamic programming
Amisha Narsingani
 
Dynamic Programing.pptx good for understanding
HUSNAINAHMAD39
 
Dynamic programmng2
debolina13
 
Introduction to Dynamic Programming, Principle of Optimality
Bhavin Darji
 
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
5.3 dynamic programming 03
Krish_ver2
 
Lecture11
Nv Thejaswini
 
Computer science in Dynamic programming .pptx
shorabi2127061
 
unit-4-dynamic programming
hodcsencet
 
Dynamic programming 2
Roy Thomas
 
Dynamic programming
princekoushik2
 
dinosourrrrrrrrrrrrrrrrrrrrrr formula .pptx
ShohidulIslamSovon
 
Dynamic programming
Jay Nagar
 
Daa chapter 3
B.Kirron Reddi
 
Annotaed slides for dynamic programming algorithm
johnathangamal27
 
week 9 lec 15.pptx
SulamanHassan
 
ADA Unit 2.pptx
AmanKumar879992
 
L21_L27_Unit_5_Dynamic_Programming Computer Science
priyanshukumarbt23cs
 
Ad

Recently uploaded (20)

PPTX
apidays Singapore 2025 - Generative AI Landscape Building a Modern Data Strat...
apidays
 
PPTX
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
 
PDF
apidays Singapore 2025 - The API Playbook for AI by Shin Wee Chuang (PAND AI)
apidays
 
PDF
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
PPT
tuberculosiship-2106031cyyfuftufufufivifviviv
AkshaiRam
 
PDF
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
PDF
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
PDF
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
PPTX
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
PDF
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 
PDF
Using AI/ML for Space Biology Research
VICTOR MAESTRE RAMIREZ
 
PDF
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
PPTX
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
PDF
apidays Singapore 2025 - Trustworthy Generative AI: The Role of Observability...
apidays
 
PDF
Driving Employee Engagement in a Hybrid World.pdf
Mia scott
 
PPTX
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
PPT
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
PDF
Avatar for apidays apidays PRO June 07, 2025 0 5 apidays Helsinki & North 2...
apidays
 
PPTX
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
PPTX
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 
apidays Singapore 2025 - Generative AI Landscape Building a Modern Data Strat...
apidays
 
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
 
apidays Singapore 2025 - The API Playbook for AI by Shin Wee Chuang (PAND AI)
apidays
 
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
tuberculosiship-2106031cyyfuftufufufivifviviv
AkshaiRam
 
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 
Using AI/ML for Space Biology Research
VICTOR MAESTRE RAMIREZ
 
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
apidays Singapore 2025 - Trustworthy Generative AI: The Role of Observability...
apidays
 
Driving Employee Engagement in a Hybrid World.pdf
Mia scott
 
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
Avatar for apidays apidays PRO June 07, 2025 0 5 apidays Helsinki & North 2...
apidays
 
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 

Dynamic programming

  • 1. Dynamic Programming and Applications Yıldırım TAM
  • 2. Dynamic Programming 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
  • 3. Divide-and-conquer • Divide-and-conquer method for algorithm design: • Divide: If the input size is too large to deal with in a straightforward manner, divide the problem into two or more disjoint subproblems • Conquer: conquer recursively to solve the subproblems • Combine: Take the solutions to the subproblems and “merge” these solutions into a solution for the original problem
  • 5. Dynamic programming • Dynamic programming is a way of improving on inefficient divide- and-conquer algorithms. • By “inefficient”, we mean that the same recursive call is made over and over. • If same subproblem is solved several times, we can use table to store result of a subproblem the first time it is computed and thus never have to recompute it again. • Dynamic programming is applicable when the subproblems are dependent, that is, when subproblems share subsubproblems. • “Programming” refers to a tabular method
  • 6. Difference between DP and Divide- and-Conquer • Using Divide-and-Conquer to solve these problems is inefficient because the same common subproblems have to be solved many times. • DP will solve each of them once and their answers are stored in a table for future use.
  • 7. Dynamic Programming vs. Recursion and Divide & Conquer
  • 8. Elements of Dynamic Programming (DP) DP is used to solve problems with the following characteristics: • Simple subproblems – We should be able to break the original problem to smaller subproblems that have the same structure • Optimal substructure of the problems – The optimal solution to the problem contains within optimal solutions to its subproblems. • Overlapping sub-problems – there exist some places where we solve the same subproblem more than once.
  • 9. Steps to Designing a Dynamic Programming Algorithm 1. Characterize optimal substructure 2. Recursively define the value of an optimal solution 3. Compute the value bottom up 4. (if needed) Construct an optimal solution
  • 10. Principle of Optimality • The dynamic Programming works on a principle of optimality. • Principle of optimality states that in an optimal sequence of decisions or choices, each sub sequences must also be optimal.
  • 11. Example 1: Fibonacci numbers 11 • Recall definition of Fibonacci numbers: F(n) = F(n-1) + F(n-2) F(0) = 0 F(1) = 1 • Computing the nth Fibonacci number recursively (top-down): F(n) F(n-1) + F(n-2) F(n-2) + F(n-3) F(n-3) + F(n-4) ...
  • 12. Fibonacci Numbers • Fn= Fn-1+ Fn-2 n ≥ 2 • F0 =0, F1 =1 • 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … • Straightforward recursive procedure is slow! • Let’s draw the recursion tree
  • 14. Fibonacci Numbers • We can calculate Fn in linear time by remembering solutions to the solved subproblems – dynamic programming • Compute solution in a bottom-up fashion • In this case, only two values need to be remembered at any time
  • 15. Example Applications of Dynamic Programming
  • 17. Shortest path problems A traveler wishes to minimize the length of a journey from town A to J.
  • 18. Shortest path problems(Cont..) The length of the route A-B-F-I-J: 2+4+3+4=13. Can we find shorter route?
  • 20. Matrix Chain Multiplication • Given : a chain of matrices {A1,A2,…,An}. • Once all pairs of matrices are parenthesized, they can be multiplied by using the standard algorithm as a sub- routine. • A product of matrices is fully parenthesized if it is either a single matrix or the product of two fully parenthesized matrix products, surrounded by parentheses. [Note: since matrix multiplication is associative, all parenthesizations yield the same product.]
  • 21. Matrix Chain Multiplication cont. • For example, if the chain of matrices is {A, B, C, D}, the product A, B, C, D can be fully parenthesized in 5 distinct ways: (A ( B ( C D ))), (A (( B C ) D )), ((A B ) ( C D )), ((A ( B C )) D), ((( A B ) C ) D ). • The way the chain is parenthesized can have a dramatic impact on the cost of evaluating the product.
  • 22. Matrix Chain Multiplication Optimal Parenthesization • Example: A[30][35], B[35][15], C[15][5] minimum of A*B*C A*(B*C) = 30*35*5 + 35*15*5 = 7,585 (A*B)*C = 30*35*15 + 30*15*5 = 18,000 • How to optimize: – Brute force – look at every possible way to parenthesize : Ω(4n/n3/2) – Dynamic programming – time complexity of Ω(n3) and space complexity of Θ(n2).
  • 23. Matrix Chain Multiplication Structure of Optimal Parenthesization • For n matrices, let Ai..j be the result of AiAi+1….Aj • An optimal parenthesization of AiAi+1…An splits the product between Ak and Ak+1 where 1  k < n. • Example, k = 4 (A1A2A3A4)(A5A6) Total cost of A1..6 = cost of A1..4 plus total cost of multiplying these two matrices together.
  • 24. Matrix Chain Multiplication Overlapping Sub-Problems • Overlapping sub-problems helps in reducing the running time considerably. – Create a table M of minimum Costs – Create a table S that records index k for each optimal sub- problem – Fill table M in a manner that corresponds to solving the parenthesization problem on matrix chains of increasing length. – Compute cost for chains of length 1 (this is 0) – Compute costs for chains of length 2 A1..2, A2..3, A3..4, …An-1…n – Compute cost for chain of length n A1..nEach level relies on smaller sub-strings
  • 25. Q1.What is the shortest path?
  • 26. Answer Q1 The length of the route A-B-F-I-J: 2+4+3+4=13.
  • 27. Q2. Who invited dynamic programming? • A. Richard Ernest Bellman • B. Edsger Dijkstra • C. Joseph Kruskal • D. David A. Huffman
  • 28. Answer Q2 • A. Richard Ernest Bellman • B. Edsger Dijkstra • C. Joseph Kruskal • D. David A. Huffman
  • 29. Q3. What is meaning of programming in DP? A. Plannig B. Computer Programming C. Programming languages D. Curriculum
  • 30. Q3. What is meaning of programming in DP? A. Plannig B. Computer Programming C. Programming languages D. Curriculum