SlideShare a Scribd company logo
Module 3
DYNAMIC PROGRAMMING
General method, Matrix-chain multiplication,
All pairs shortest path, Optimal binary search
trees, 0/1 Knapsack problem, Traveling
salesperson problem, Flow shop scheduling.
 Dynamic programming is an algorithm design method that
can be used when the solution to a problem can be
viewed as the result of a sequence of decisions.
 Dynamic programming is applicable when the sub-
problems are not independent, that is when sub-problems
share sub sub-problems.
 A dynamic programming algorithm solves every sub-sub-
problem just once and then saves its answer in a table,
there by avoiding the work of re-computing the answer
every time the sub-problem is encountered.
 Dynamic programming design involves 4 major steps.

 1) Characterize the structure of optimal solution.
 2) Recursively define the value of an optimal solution.
 3) Compute the value of an optimum solution in a bottom up fashion.
 4) Construct an optimum solution from computed information.
 The Dynamic programming technique was
developed by Bellman based upon his principle
known as principle of optimality.
This principle states that “An optimal policy has the
property that, what ever the initial decisions are,
the remaining decisions must constitute an optimal
policy with regard to the state resulting from the
first decision”.
 Matrix-chain multiplication,
 All pairs shortest path,
 Optimal binary search trees,
 0/1 Knapsack problem,
 Traveling salesperson problem,
 Flow shop scheduling
APPLICATIONS OF DYNAMIC
PROGRAMMING
 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
.
 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 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 sub subproblems.
 “Programming” refers to a tabular method
 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.
Module 2ppt.pptx divid and conquer method
 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
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
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
Algorithm for Matrix Chain Multiplication
Algorithm matrix_chain_multiplication(array P[1..n])
{
for i:= 1 to n do
{
c[i,i]:=0; // initialisation
}
for len:=1 to n-1 do
{
for i:=1 to n – len do
{
j:=i+len; i<=k<j;
c[i][j]:= Min{c[i,k] + c[k+1,j]+pi-1*pk*pj;
S[i][j]=k;
}
}
}
return[1][n]
}
Algorithm OBST(p, q, n)
{
for i = 0 to n-1 do
{
w[i,i] = q[i];
c[i,i] = 0;
r[i,i] = 0;
w[i,i+1] = q[i]+q[i+1]+p[i+1];
r[i,i+1] = i+1;
c[i,i+1] = q[i]+q[i+1]+p[i+1];
}
w[n,n] = q[n]; c[n,n] = 0; r[n,n] = 0;
for m = 2 to n do
for i = 0 to n - m do
{
j=i+m;
w[i,j] = w[i,j-1]+p[j]+q[j];
k=Find(c, r, i, j);
c[i,j] = c[i,k-1]+c[k,j]+w[i, j];
r[i,j] = k;
}
Write(c[0,n], w[0,n], r[0,n]);
}
Algorithm Find(c, r, i, j)
{
min = ∞
for m = r[i, j-1] to r[i+1, j] do
if ( c[i,m-1] + c[m,j] < min then
{
min = c[i, m-1] + c[m, j];
l=m;
}
return l;
}
Optimal Binary Search Trees (OBST)
 Binary Search Tree : The values present in the left subtree are less than root
value and the values present in the right subtree are greater than the root
value.
 For a given set of identifies, we can construct more than one binary search
tree.
 The binary search trees exhibit different performance characteristics.
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method

More Related Content

PPTX
Dynamic programming
PPTX
Dynamic Programming - Part 1
PPTX
Introduction to dynamic programming
PPT
Dynamic programming 2
PPTX
Dynamic programming class 16
PDF
Daa chapter 3
PPTX
ADA Unit 2.pptx
PPTX
Introduction to Dynamic Programming, Principle of Optimality
Dynamic programming
Dynamic Programming - Part 1
Introduction to dynamic programming
Dynamic programming 2
Dynamic programming class 16
Daa chapter 3
ADA Unit 2.pptx
Introduction to Dynamic Programming, Principle of Optimality

Similar to Module 2ppt.pptx divid and conquer method (20)

PPTX
Dynamic Programing.pptx good for understanding
PPT
Dynamic pgmming
PPTX
Dynamic programming prasintation eaisy
PPTX
Dynamic programmng2
PPTX
Algorithm Design Technique
PDF
Dynamic Programming
PPT
dynamic-programming unit 3 power point presentation
PDF
Dynamic programming
PPTX
dynamic programming complete by Mumtaz Ali (03154103173)
PPTX
week 9 lec 15.pptx
PDF
Dynamic Programming Algorithm CSI-504.pdf
PPTX
dinosourrrrrrrrrrrrrrrrrrrrrr formula .pptx
PDF
Unit 4 of design and analysis of algorithms
PPT
Lecture11
PPTX
Computer science in Dynamic programming .pptx
PDF
L21_L27_Unit_5_Dynamic_Programming Computer Science
PPT
5.3 dynamic programming 03
PPT
Dynamicpgmming
PPTX
W8L1 Introduction & Fibonacci Numbers part 1.pptx
PPTX
Annotaed slides for dynamic programming algorithm
Dynamic Programing.pptx good for understanding
Dynamic pgmming
Dynamic programming prasintation eaisy
Dynamic programmng2
Algorithm Design Technique
Dynamic Programming
dynamic-programming unit 3 power point presentation
Dynamic programming
dynamic programming complete by Mumtaz Ali (03154103173)
week 9 lec 15.pptx
Dynamic Programming Algorithm CSI-504.pdf
dinosourrrrrrrrrrrrrrrrrrrrrr formula .pptx
Unit 4 of design and analysis of algorithms
Lecture11
Computer science in Dynamic programming .pptx
L21_L27_Unit_5_Dynamic_Programming Computer Science
5.3 dynamic programming 03
Dynamicpgmming
W8L1 Introduction & Fibonacci Numbers part 1.pptx
Annotaed slides for dynamic programming algorithm
Ad

More from JyoReddy9 (6)

PPT
operating system over view.ppt operating sysyems
PPTX
FDS PPT_Unit-5.pptx fundamentals of data science
PDF
UNIT-V.pdf daa unit material 5 th unit ppt
PPTX
UNIT-II.pptx
PPTX
DISJOINT SETS.pptx
PPT
os storage mass.ppt
operating system over view.ppt operating sysyems
FDS PPT_Unit-5.pptx fundamentals of data science
UNIT-V.pdf daa unit material 5 th unit ppt
UNIT-II.pptx
DISJOINT SETS.pptx
os storage mass.ppt
Ad

Recently uploaded (20)

PDF
Insiders guide to clinical Medicine.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Pre independence Education in Inndia.pdf
PPTX
Lesson notes of climatology university.
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Insiders guide to clinical Medicine.pdf
01-Introduction-to-Information-Management.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Pre independence Education in Inndia.pdf
Lesson notes of climatology university.
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Microbial disease of the cardiovascular and lymphatic systems
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
VCE English Exam - Section C Student Revision Booklet
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
GDM (1) (1).pptx small presentation for students
Microbial diseases, their pathogenesis and prophylaxis
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Final Presentation General Medicine 03-08-2024.pptx

Module 2ppt.pptx divid and conquer method

  • 1. Module 3 DYNAMIC PROGRAMMING General method, Matrix-chain multiplication, All pairs shortest path, Optimal binary search trees, 0/1 Knapsack problem, Traveling salesperson problem, Flow shop scheduling.
  • 2.  Dynamic programming is an algorithm design method that can be used when the solution to a problem can be viewed as the result of a sequence of decisions.  Dynamic programming is applicable when the sub- problems are not independent, that is when sub-problems share sub sub-problems.  A dynamic programming algorithm solves every sub-sub- problem just once and then saves its answer in a table, there by avoiding the work of re-computing the answer every time the sub-problem is encountered.
  • 3.  Dynamic programming design involves 4 major steps.   1) Characterize the structure of optimal solution.  2) Recursively define the value of an optimal solution.  3) Compute the value of an optimum solution in a bottom up fashion.  4) Construct an optimum solution from computed information.
  • 4.  The Dynamic programming technique was developed by Bellman based upon his principle known as principle of optimality. This principle states that “An optimal policy has the property that, what ever the initial decisions are, the remaining decisions must constitute an optimal policy with regard to the state resulting from the first decision”.
  • 5.  Matrix-chain multiplication,  All pairs shortest path,  Optimal binary search trees,  0/1 Knapsack problem,  Traveling salesperson problem,  Flow shop scheduling APPLICATIONS OF DYNAMIC PROGRAMMING
  • 6.  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 .  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
  • 7.  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 sub subproblems.  “Programming” refers to a tabular method
  • 8.  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.
  • 10.  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.
  • 11.  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
  • 12.  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.
  • 13.  Example 1: 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
  • 16. Algorithm for Matrix Chain Multiplication Algorithm matrix_chain_multiplication(array P[1..n]) { for i:= 1 to n do { c[i,i]:=0; // initialisation } for len:=1 to n-1 do { for i:=1 to n – len do { j:=i+len; i<=k<j; c[i][j]:= Min{c[i,k] + c[k+1,j]+pi-1*pk*pj; S[i][j]=k; } } } return[1][n] }
  • 17. Algorithm OBST(p, q, n) { for i = 0 to n-1 do { w[i,i] = q[i]; c[i,i] = 0; r[i,i] = 0; w[i,i+1] = q[i]+q[i+1]+p[i+1]; r[i,i+1] = i+1; c[i,i+1] = q[i]+q[i+1]+p[i+1]; } w[n,n] = q[n]; c[n,n] = 0; r[n,n] = 0; for m = 2 to n do for i = 0 to n - m do { j=i+m; w[i,j] = w[i,j-1]+p[j]+q[j]; k=Find(c, r, i, j); c[i,j] = c[i,k-1]+c[k,j]+w[i, j]; r[i,j] = k; } Write(c[0,n], w[0,n], r[0,n]); }
  • 18. Algorithm Find(c, r, i, j) { min = ∞ for m = r[i, j-1] to r[i+1, j] do if ( c[i,m-1] + c[m,j] < min then { min = c[i, m-1] + c[m, j]; l=m; } return l; }
  • 19. Optimal Binary Search Trees (OBST)  Binary Search Tree : The values present in the left subtree are less than root value and the values present in the right subtree are greater than the root value.  For a given set of identifies, we can construct more than one binary search tree.  The binary search trees exhibit different performance characteristics.