SlideShare a Scribd company logo
UNIT 4
Dynamic Programming
Ms.Mary Jacob
Assistant Professor , Kristu Jayanti College (Autonomous),
Bangalore
Dynamic Programming
ØAn algorithm design method which can be used when the solution to a
problem may be viewed as the result of a sequence of decisions.
ØDynamic Programming algorithm stores the solutions for small
subproblems and looks them up to the stored results rather than
recomputing them again when it needs later to solve larger subproblems
ØTypically applied to optimiation problems
Dynamic Programming
4-Steps of Developing Dynamic Programming Algorithm
üCharacterize the structure of an optimal solution
üRecursively define the value of an optimal solution
üCompute the value of an optimal solution in a bottom-up fashion
üConstruct an optimal solution from computed nformation
Principle of Optimalilty
Ø Principle of Optimalilty states that a an optimal sequence of decisions has the
property that whatever the initial state of decision is, the remaining decisions must
constitute an optimal decision sequence with regard to that resulting from the first
decision.
Ø In other words this principles states that the optimal solution for a larger
subproblem contains an optimal solution for a smaller subproblem.
Dynamic Programming VS. Greedy Method Vs Divide-and-Conquer
Ø In Greedy Method only one decision sequence is ever genertated
Ø InDynamic Programming has many decision sequences that may be genertated.
Ø In Divide-and-Conquer we partition the problem into independent subproblems,
solve the subproblems recursively, and then combine their solutions to solve the
original problem
Ø Dynamic Programming is applicable when the subproblems are not
independent, that is, when subproblems share subsubproblems. Solves every
subsubproblem just once and then saves its answer in a table, thereby avoiding
the work of recomputing the answer every time the subsubproblem is
encountered
Dynamic Programming-Multistage Graph
Ø A multistage graph is a graph
G= (V, E) with V partitioned into K >= 2 disjoint subsets such that if an
edge (u, v) is in E, then u is in Vi , and v is in Vi+1 where 1<=i<=k, for
some subsets in the partition.
Ø The number of vertices in V1 and Vk is one. i.e) | V1 | = | VK | = 1. The
node in the first set V1 is source and the node in the last set Vk is
destination. Each set Vi is called a stage in the graph.
Ø The "multistage graph problem" is to find the minimum cost path from
the source to the destination. There are two approaches.
1. Multistage graph-Forward approach
2. Multistage graph –Backward approach.
Dynamic Programming-Multistage Graph
Multistage graph-Forward approach
A dynamic programming solution for a K-stage graph is obtained by first
noticing that every path from source to destination is a result of a sequence of
K-2 decisions. The ith decision involves in determining which vertex in stage
Vi+1 where 1<=i<=K-2 is to be selected to be on the path. Let P (i, j) be the
minimum cost path from vertex j in Vi to vertex T (destination vertex)
Cost of this path cost (i , j) = min{c(j, L)+cost(i+1,L)}
where L ϵ Vi+1 and (j, L) ϵ E.
Here j denotes the vertex in stage I and L denotes the vertex in stage i+1.
Dynamic Programming-Multistage Graph
Multistage graph-Backward approach
Multistage graph problem can also be solved using backward approach. Let cost
(i, j ) be the minimum cost path from vertex S(source) to vertex j in Vi. The cost
(i, j) using backward approach is
cost (i , j) = min{ cost (i-1, L) + c(L, j) }
Where L ϵ Vi -1 and (L, j) ϵ E.
Here j denotes the vertex in stage i and L denotes the vertex in stage i - 1.
All Pairs shortest Path – FLOYD Warshall Algorithm
Ø Used for solving the All Pairs Shortest Path problem.
Ø The problem is to find shortest distances between every pair of vertices in a given
edge.
Ø The solution matrix is initialized using the given graph . Then the solution matrix
is updated by considering all the vertices as an intermediate vertex.
Ø We have to pick one by one all the vertices and updates all shortest paths which
include the picked vertex as an intermediate vertex in the shortest path. When we
pick vertex number k as an intermediate vertex, we already have considered
vertices {0, 1, 2, .. k-1} as intermediate vertices. For every pair (i, j) of the source
and destination vertices, there are two possible cases1) k is not an intermediate
vertex in shortest path from i to j. We keep the value of dist[i][j] as it is2) k is an
intermediate vertex in shortest path from i to j. We update the value of dist[i][j] as
dist[i][k] + dist[k][j] if dist[i][j] > dist[i][k] + dist[k][j]
All Pairs shortest Path – FLOYD Warshall Algorithm
Formula:
Dk [i, j] =min {Dk-1[i, j], Dk-1[i, k] +Dk-1[k, j]}
Where Dk represents the matrix D after kth iteration. Initially D0=C (cost matrix). i
is the source vertex , j is the destination vertex and k represents the intermediate
vertex used.
THANK YOU

More Related Content

Similar to Unit 4- Dynamic Programming.pdf

Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
danielgetachew0922
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
 
Algorithm Design and Complexity - Course 6
Algorithm Design and Complexity - Course 6Algorithm Design and Complexity - Course 6
Algorithm Design and Complexity - Course 6
Traian Rebedea
 
Transportation and assignment_problem
Transportation and assignment_problemTransportation and assignment_problem
Transportation and assignment_problem
Ankit Katiyar
 

Similar to Unit 4- Dynamic Programming.pdf (20)

Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
 
Approx
ApproxApprox
Approx
 
Chap12 slides
Chap12 slidesChap12 slides
Chap12 slides
 
Analysis and design of algorithms part 4
Analysis and design of algorithms part 4Analysis and design of algorithms part 4
Analysis and design of algorithms part 4
 
Lecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.pptLecture_10_Parallel_Algorithms_Part_II.ppt
Lecture_10_Parallel_Algorithms_Part_II.ppt
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
Chap10 slides
Chap10 slidesChap10 slides
Chap10 slides
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
Algorithm Design and Complexity - Course 6
Algorithm Design and Complexity - Course 6Algorithm Design and Complexity - Course 6
Algorithm Design and Complexity - Course 6
 
1535 graph algorithms
1535 graph algorithms1535 graph algorithms
1535 graph algorithms
 
Cryptography Baby Step Giant Step
Cryptography Baby Step Giant StepCryptography Baby Step Giant Step
Cryptography Baby Step Giant Step
 
Transportation and assignment_problem
Transportation and assignment_problemTransportation and assignment_problem
Transportation and assignment_problem
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Episode 50 : Simulation Problem Solution Approaches Convergence Techniques S...
Episode 50 :  Simulation Problem Solution Approaches Convergence Techniques S...Episode 50 :  Simulation Problem Solution Approaches Convergence Techniques S...
Episode 50 : Simulation Problem Solution Approaches Convergence Techniques S...
 
Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Draft6
Draft6Draft6
Draft6
 
DIJKSTRA_123.pptx
DIJKSTRA_123.pptxDIJKSTRA_123.pptx
DIJKSTRA_123.pptx
 
Approximation
ApproximationApproximation
Approximation
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
 

More from MaryJacob24

More from MaryJacob24 (12)

Unit 1-Introduction to Data Structures-BCA.pdf
Unit 1-Introduction to Data Structures-BCA.pdfUnit 1-Introduction to Data Structures-BCA.pdf
Unit 1-Introduction to Data Structures-BCA.pdf
 
Unit 2-Data Modeling.pdf
Unit 2-Data Modeling.pdfUnit 2-Data Modeling.pdf
Unit 2-Data Modeling.pdf
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
 
tree traversals.pdf
tree traversals.pdftree traversals.pdf
tree traversals.pdf
 
Linked List-Types.pdf
Linked List-Types.pdfLinked List-Types.pdf
Linked List-Types.pdf
 
Unit 5- Cloud Applications.pdf
Unit 5- Cloud Applications.pdfUnit 5- Cloud Applications.pdf
Unit 5- Cloud Applications.pdf
 
Simplification of Circuits.pdf
Simplification of Circuits.pdfSimplification of Circuits.pdf
Simplification of Circuits.pdf
 
2 bit comparator, 4 1 Multiplexer, 1 4 Demultiplexer, Flip Flops and Register...
2 bit comparator, 4 1 Multiplexer, 1 4 Demultiplexer, Flip Flops and Register...2 bit comparator, 4 1 Multiplexer, 1 4 Demultiplexer, Flip Flops and Register...
2 bit comparator, 4 1 Multiplexer, 1 4 Demultiplexer, Flip Flops and Register...
 
Algorithm-Introduction ,Characterestics & Control Structures.pdf
Algorithm-Introduction ,Characterestics & Control Structures.pdfAlgorithm-Introduction ,Characterestics & Control Structures.pdf
Algorithm-Introduction ,Characterestics & Control Structures.pdf
 
Data Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdfData Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdf
 
quick sort.pdf
quick sort.pdfquick sort.pdf
quick sort.pdf
 
Graph Traversals
Graph TraversalsGraph Traversals
Graph Traversals
 

Recently uploaded

678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated crops
parmarsneha2
 

Recently uploaded (20)

Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated crops
 

Unit 4- Dynamic Programming.pdf

  • 1. UNIT 4 Dynamic Programming Ms.Mary Jacob Assistant Professor , Kristu Jayanti College (Autonomous), Bangalore
  • 2. Dynamic Programming ØAn algorithm design method which can be used when the solution to a problem may be viewed as the result of a sequence of decisions. ØDynamic Programming algorithm stores the solutions for small subproblems and looks them up to the stored results rather than recomputing them again when it needs later to solve larger subproblems ØTypically applied to optimiation problems
  • 3. Dynamic Programming 4-Steps of Developing Dynamic Programming Algorithm üCharacterize the structure of an optimal solution üRecursively define the value of an optimal solution üCompute the value of an optimal solution in a bottom-up fashion üConstruct an optimal solution from computed nformation
  • 4. Principle of Optimalilty Ø Principle of Optimalilty states that a an optimal sequence of decisions has the property that whatever the initial state of decision is, the remaining decisions must constitute an optimal decision sequence with regard to that resulting from the first decision. Ø In other words this principles states that the optimal solution for a larger subproblem contains an optimal solution for a smaller subproblem.
  • 5. Dynamic Programming VS. Greedy Method Vs Divide-and-Conquer Ø In Greedy Method only one decision sequence is ever genertated Ø InDynamic Programming has many decision sequences that may be genertated. Ø In Divide-and-Conquer we partition the problem into independent subproblems, solve the subproblems recursively, and then combine their solutions to solve the original problem Ø Dynamic Programming is applicable when the subproblems are not independent, that is, when subproblems share subsubproblems. Solves every subsubproblem just once and then saves its answer in a table, thereby avoiding the work of recomputing the answer every time the subsubproblem is encountered
  • 6. Dynamic Programming-Multistage Graph Ø A multistage graph is a graph G= (V, E) with V partitioned into K >= 2 disjoint subsets such that if an edge (u, v) is in E, then u is in Vi , and v is in Vi+1 where 1<=i<=k, for some subsets in the partition. Ø The number of vertices in V1 and Vk is one. i.e) | V1 | = | VK | = 1. The node in the first set V1 is source and the node in the last set Vk is destination. Each set Vi is called a stage in the graph. Ø The "multistage graph problem" is to find the minimum cost path from the source to the destination. There are two approaches. 1. Multistage graph-Forward approach 2. Multistage graph –Backward approach.
  • 7. Dynamic Programming-Multistage Graph Multistage graph-Forward approach A dynamic programming solution for a K-stage graph is obtained by first noticing that every path from source to destination is a result of a sequence of K-2 decisions. The ith decision involves in determining which vertex in stage Vi+1 where 1<=i<=K-2 is to be selected to be on the path. Let P (i, j) be the minimum cost path from vertex j in Vi to vertex T (destination vertex) Cost of this path cost (i , j) = min{c(j, L)+cost(i+1,L)} where L ϵ Vi+1 and (j, L) ϵ E. Here j denotes the vertex in stage I and L denotes the vertex in stage i+1.
  • 8. Dynamic Programming-Multistage Graph Multistage graph-Backward approach Multistage graph problem can also be solved using backward approach. Let cost (i, j ) be the minimum cost path from vertex S(source) to vertex j in Vi. The cost (i, j) using backward approach is cost (i , j) = min{ cost (i-1, L) + c(L, j) } Where L ϵ Vi -1 and (L, j) ϵ E. Here j denotes the vertex in stage i and L denotes the vertex in stage i - 1.
  • 9. All Pairs shortest Path – FLOYD Warshall Algorithm Ø Used for solving the All Pairs Shortest Path problem. Ø The problem is to find shortest distances between every pair of vertices in a given edge. Ø The solution matrix is initialized using the given graph . Then the solution matrix is updated by considering all the vertices as an intermediate vertex. Ø We have to pick one by one all the vertices and updates all shortest paths which include the picked vertex as an intermediate vertex in the shortest path. When we pick vertex number k as an intermediate vertex, we already have considered vertices {0, 1, 2, .. k-1} as intermediate vertices. For every pair (i, j) of the source and destination vertices, there are two possible cases1) k is not an intermediate vertex in shortest path from i to j. We keep the value of dist[i][j] as it is2) k is an intermediate vertex in shortest path from i to j. We update the value of dist[i][j] as dist[i][k] + dist[k][j] if dist[i][j] > dist[i][k] + dist[k][j]
  • 10. All Pairs shortest Path – FLOYD Warshall Algorithm Formula: Dk [i, j] =min {Dk-1[i, j], Dk-1[i, k] +Dk-1[k, j]} Where Dk represents the matrix D after kth iteration. Initially D0=C (cost matrix). i is the source vertex , j is the destination vertex and k represents the intermediate vertex used.