SlideShare a Scribd company logo
Dynamic Programming
Lecture:17 DEBOLINA PAN
Date: 01.10.13 I.D-110811026
1. Dynamic Programming
2. Comparison with divide n concuer
3. Where used?
4. Matrix Multiplication
5. Algo to multiply 2 matrices
6. #scaler multiplication used in matrix multiplication
7. Costs &parenthisization
8. Matrix multiplication Problem
9. Counting the #parenthisization
10. Brute force Method
11. Computing optimal cost
12. Matrix chain Order
13. Overlapping Subproblem
14. Memorization
 dynamic programming is a method for
solving complex problems by breaking them
down into simpler subproblems.and the
subproblems are dependent.
 programming is an expression of algorithm.i
dynamic programing the languag,used does
not refer to any computer language,rather it
refers to formal language.
 Dynamic programming, like the divide-and-conquer method,
solves problems by combining the solutions to subproblems.
 divide-and-conquer algorithms partition the problem into
disjoint subproblems, solve the subproblems recursively, and
then combine their solutions to solve the original problem. In
contrast, dynamic programming applies when the subproblems
overlap—that is, when subproblems share subsubproblems.
 a divide-and-conquer algorithm does more work than necessary,
repeatedly solving the common subsubproblems. A dynamic-
programming algorithm solves each subsubproblem just once
and then saves its answer in a table, thereby avoiding the work
of recomputing the answer every time it solves each
subsubproblem
 We typically apply dynamic programming to
optimization problems.
 Such problems can have many possible
solutions. Each solution has a value, and we
wish to find a solution with the optimal
(minimum or maximum) value. We call such a
solution an optimal solution to the problem,
as opposed to the optimal solution, since
there may be several solutions that achieve
the optimal value.
 Say,there are 2 matrices A & B.
 We can multiply 2 matrices only if they are
compatible.
 Condition to be multiplied:#collumns of A
=#rows of B.
 If the resultant matrix is C,then
Am*n * B n*p= Cm*p
MATRIX-MULTIPLY(A,B)
1 .if columns[A] ≠rows[B]
2 . Then error “incompatible dimensions”
3 . let C be a new A:rows B:columns matrix
4 .for i <-1 to A.rows
5 .for j<-1 to B.columns
6 .cij =0
7 .for k =1 to A.columns
8 .cij =cij + aik .bkj
9 .return C
 In the above example of matrix multiplication
if we consider,
 m=2,n=3,p=4,the total number of scaler
multiplication used is 24,i.e m*n*p.
 We shall express costs in terms of the
number of scaler multiplications.
 Example:
A1->10*100
A2->100*5
A3->5*50
If(A1*A2)*A3=>#scaler multiplication:
(10*100*5) + (10*5*50)=7500
IfA1*(A2*A3)=>#scaler multiplication:
(10*100*50)+(100*5*50)=75000
=>Ordering of parenthasization is important.
 matrix-chain multiplication problem can be
stated as follows: given a chain
<A1,A2,…,An> of n matrices, where for
i=1,2, …,n, matrix Ai has dimension pi-1*pi ,
fully parenthesize the product A1A2…An in a
way that minimizes the number of scalar
multiplications.
 Our goal is only to determine an order for
multiplying matrices that has the lowest cost.
 P(n) is the number of alternative
parenthesizations of a sequence of n
matrices.
 When n=1,P(n)=1,as there is only 1 matrix,so
only one way to put parenthesis.
 n=2,P(n)=1,only one way to put parenthesis.
 n=3,P(n)=2, (A1*A2)*A3 & A1*(A2*A3)
 N=4,P(n)=6
 (((A1*A2)*A3)*A4) (A1*((A2*A3)*A4)
 ((A1*A2)*(A3*A4)) (A1*(A2*(A3*A4)))
 (A1*(A2*A3))*A4 ((A1*A2)*(A3*A4))
 Thus we obtain the sequence:
◦ P(n)= 1 if n=1
 ∑k=1to n-1 P(k)P(n-k) if n≥2 a fully
parenthasize matrix product is a
prodct of 2 fully parenthasize matrix
sbproducts.split between two
sbprducts may occr between the k th
& k+1 th matrix for any k=1…n-1
Rate of growth: Ω((4^n)/(n^(3/2)))
 general problem-solving technique that
consists of systematically enumerating all
possible candidates for the solution and
checking whether each candidate satisfies the
problem's statement.
 Lower bound of this problem using brute
force method is Ω((4^n)/(n^(3/2)))
 That means brute force method as the solution of
the problem is not so good.
 At this point, we could easily write a recursive
algorithm based on recurrence to compute the
minimum cost m[1,n]for multiplying A1,A2,…, An.
Aswe sawfor the rod-cutting problem, and this
recursive algorithm takes exponential time, which is
no better than the brute-force method of checking
each way of parenthesizing the product.
 we have relatively few distinct subproblems: one
subproblem for each choice of i and j satisfying 1 ≤i≤
j≤n, or (nc2 +n)=Θ(n^2) in all. A recursive algorithm
may encounter each subproblem many times in
different branches of its recursion tree. This property
of overlapping subproblems is the second hallmark of
when dynamic programming applies.
 Instead of computing the solution to recurrence
recursively, we compute the optimal cost by using a
tabular, bottom-up approach.
 We shall implement the tabular, bottom-up method
in the procedure MATRIXCHAIN-ORDER, which
appears below. This procedure assumes that matrix
Ai has dimensions pi-1 *pi for i=1,2,…,n. Its input is
a sequence p=<p0; p1; : : : ;pni> where length[p] =
n+1. The procedure uses an auxiliary table
m[1…n,1…n] for storing the m[I,j] costs and another
auxiliary table s[1…n,1…n]that records which index
of k achieved the optimal cost in computing m[i; j
].We shall use the table s to construct an optimal
solution.
 In order to implement the bottom-up approach, we
must determine which entries of the table we refer to
when computing m[i,j].the cost m[i ,j] of computing a
matrix-chain product of j-i+1matrices depends only
on the costs of computing matrix-chain products of
fewer than j-i+1 matrices. That is, for k =I,i +1,…,j-
1, the matrix Ai…k is a product of k-i+1< j-i+1
matrices and the matrix Ak+1…j is a product of j-k<
j-i +1 matrices. Thus, the algorithm should fill in the
table min a manner that corresponds to solving the
parenthesization problem on matrix chains of
increasing length. For the subproblem of optimally
parenthesizing the chain Ai,Ai+1,… Aj,we consider
the subproblem size to be the length j-i+1 of the
chain.
MATRIX-CHAIN-ORDER.p/
1. n D p:length 1
2.Let m[1,… n.1,… n] and s[1,… n-1. 2…n] be new tables
3. for i =1 to n
4.Do m[i,j] <-0
5. for l =2 to n // l is the chain length
6. dofor i =1 to n –l+1
7. Do j<-i+l-1
8. m[i, j]<- infinity
9.for k <-i to j-1
10. Do q <-m[i, k]+m[k+1,j]+pi-1.pk.pj
11. if q <m[i,j]
12. m[i, j]<-q
13. s[i,j]<-k
14. return m and s
 running time of this algorithm is in fact also
Ω(n^3) The algorithm requiresΘ(n^2) space
to store the m and s tables. Thus, MATRIX-
CHAINORDER is much more efficient than the
exponential-time method of enumerating all
possible parenthesizations and checking each
one
 In dynamic problem
subproblems are
dependent.
 Dynamic-programming
algorithms typically take
advantage of overlapping
subproblems by solving
each subproblem once and
then storing the solution in
a table where it can be
looked up when needed,
using constant time per
lookup.
 Cost is reduced.
(A1 *A2) (A3*(A4*A5))
(A1*A2) (A3*A4)*A5
^
|
Overlapping
subproblem
 A memoized recursive algorithm maintains an
entry in a table for the solution to each
subproblem. Each table entry initially
contains a special value to indicate that the
entry has yet to be filled in. When the
subproblem is first encountered as the
recursive algorithm unfolds, its solution is
computed and then stored in the table. Each
subsequent time that we encounter this
subproblem, we simply look up the value
stored in the table and return it

More Related Content

What's hot

Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
ElifTech
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
Rajendran
 
Dynamic Programming - Matrix Chain Multiplication
Dynamic Programming - Matrix Chain MultiplicationDynamic Programming - Matrix Chain Multiplication
Dynamic Programming - Matrix Chain Multiplication
Pecha Inc.
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
Sahil Kumar
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
Amrinder Arora
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programingrupali_2bonde
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programmingOye Tu
 
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)
Mumtaz Ali
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Amit Kumar Rathi
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Jay Nagar
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Yıldırım Tam
 
Matrix multiplicationdesign
Matrix multiplicationdesignMatrix multiplicationdesign
Matrix multiplicationdesign
Respa Peter
 
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 programmingzukun
 
Dynamic programming (dp) in Algorithm
Dynamic programming (dp) in AlgorithmDynamic programming (dp) in Algorithm
Dynamic programming (dp) in Algorithm
RaihanIslamSonet
 
Dynamic programming Basics
Dynamic programming BasicsDynamic programming Basics
Dynamic programming Basics
Kvishnu Dahatonde
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part II
Amrinder Arora
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
Amrinder Arora
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Melaku Bayih Demessie
 
DS ppt
DS pptDS ppt
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 

What's hot (20)

Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
 
Dynamic Programming - Matrix Chain Multiplication
Dynamic Programming - Matrix Chain MultiplicationDynamic Programming - Matrix Chain Multiplication
Dynamic Programming - Matrix Chain Multiplication
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
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)
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Matrix multiplicationdesign
Matrix multiplicationdesignMatrix multiplicationdesign
Matrix multiplicationdesign
 
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
 
Dynamic programming (dp) in Algorithm
Dynamic programming (dp) in AlgorithmDynamic programming (dp) in Algorithm
Dynamic programming (dp) in Algorithm
 
Dynamic programming Basics
Dynamic programming BasicsDynamic programming Basics
Dynamic programming Basics
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part II
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
DS ppt
DS pptDS ppt
DS ppt
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 

Viewers also liked

Dynamic Program Problems
Dynamic Program ProblemsDynamic Program Problems
Dynamic Program ProblemsRanjit Sasmal
 
Android
AndroidAndroid
Android
debolina13
 
Dynamic programmng2
Dynamic programmng2Dynamic programmng2
Dynamic programmng2
debolina13
 
Hashing
HashingHashing
Hashing
debolina13
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
jyothimonc
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
Hinal Lunagariya
 
Minimum Spanning Tree
Minimum Spanning TreeMinimum Spanning Tree
Minimum Spanning Treezhaokatherine
 
strassen matrix multiplication algorithm
strassen matrix multiplication algorithmstrassen matrix multiplication algorithm
strassen matrix multiplication algorithm
evil eye
 
Sec15 dynamic programming
Sec15 dynamic programmingSec15 dynamic programming
Sec15 dynamic programming
Keisuke OTAKI
 

Viewers also liked (10)

Dynamic Program Problems
Dynamic Program ProblemsDynamic Program Problems
Dynamic Program Problems
 
Android
AndroidAndroid
Android
 
Dynamic programmng2
Dynamic programmng2Dynamic programmng2
Dynamic programmng2
 
Hashing
HashingHashing
Hashing
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Minimum Spanning Tree
Minimum Spanning TreeMinimum Spanning Tree
Minimum Spanning Tree
 
strassen matrix multiplication algorithm
strassen matrix multiplication algorithmstrassen matrix multiplication algorithm
strassen matrix multiplication algorithm
 
Sec15 dynamic programming
Sec15 dynamic programmingSec15 dynamic programming
Sec15 dynamic programming
 

Similar to Dynamic programming1

Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdf
jannatulferdousmaish
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
B.Kirron Reddi
 
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfUnit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
yashodamb
 
DAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxDAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptx
vaishnavi339314
 
Dynamic1
Dynamic1Dynamic1
Dynamic1MyAlome
 
Exhaustive Combinatorial Enumeration
Exhaustive Combinatorial EnumerationExhaustive Combinatorial Enumeration
Exhaustive Combinatorial Enumeration
Mathieu Dutour Sikiric
 
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)
Pramit Kumar
 
9 - DynamicProgramming-plus latihan.ppt
9 - DynamicProgramming-plus latihan.ppt9 - DynamicProgramming-plus latihan.ppt
9 - DynamicProgramming-plus latihan.ppt
KerbauBakar
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
Nv Thejaswini
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
Tekle12
 
Daa chapter11
Daa chapter11Daa chapter11
Daa chapter11
B.Kirron Reddi
 
adminSticky Note- the process of inversion is shown in .docx
adminSticky Note- the process of inversion is shown in  .docxadminSticky Note- the process of inversion is shown in  .docx
adminSticky Note- the process of inversion is shown in .docx
bobbywlane695641
 
adminSticky Note- the process of inversion is shown in .docx
adminSticky Note- the process of inversion is shown in  .docxadminSticky Note- the process of inversion is shown in  .docx
adminSticky Note- the process of inversion is shown in .docx
galerussel59292
 
Matlab intro notes
Matlab intro notesMatlab intro notes
Matlab intro notes
pawanss
 
Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Investigación de operaciones 026 programación lineal Solución Simplex con R S...Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Jorge Pablo Rivas
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
B.Kirron Reddi
 
tw1979 Exercise 1 Report
tw1979 Exercise 1 Reporttw1979 Exercise 1 Report
tw1979 Exercise 1 ReportThomas Wigg
 

Similar to Dynamic programming1 (20)

Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdf
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfUnit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
DAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxDAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptx
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
 
Analysis of Algorithm
Analysis of AlgorithmAnalysis of Algorithm
Analysis of Algorithm
 
Exhaustive Combinatorial Enumeration
Exhaustive Combinatorial EnumerationExhaustive Combinatorial Enumeration
Exhaustive Combinatorial Enumeration
 
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)
 
9 - DynamicProgramming-plus latihan.ppt
9 - DynamicProgramming-plus latihan.ppt9 - DynamicProgramming-plus latihan.ppt
9 - DynamicProgramming-plus latihan.ppt
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
Daa chapter11
Daa chapter11Daa chapter11
Daa chapter11
 
adminSticky Note- the process of inversion is shown in .docx
adminSticky Note- the process of inversion is shown in  .docxadminSticky Note- the process of inversion is shown in  .docx
adminSticky Note- the process of inversion is shown in .docx
 
adminSticky Note- the process of inversion is shown in .docx
adminSticky Note- the process of inversion is shown in  .docxadminSticky Note- the process of inversion is shown in  .docx
adminSticky Note- the process of inversion is shown in .docx
 
Matlab intro notes
Matlab intro notesMatlab intro notes
Matlab intro notes
 
Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Investigación de operaciones 026 programación lineal Solución Simplex con R S...Investigación de operaciones 026 programación lineal Solución Simplex con R S...
Investigación de operaciones 026 programación lineal Solución Simplex con R S...
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
 
tw1979 Exercise 1 Report
tw1979 Exercise 1 Reporttw1979 Exercise 1 Report
tw1979 Exercise 1 Report
 

Recently uploaded

DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABSDESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
itech2017
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdfThe Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
Nettur Technical Training Foundation
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Water billing management system project report.pdf
Water billing management system project report.pdfWater billing management system project report.pdf
Water billing management system project report.pdf
Kamal Acharya
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 

Recently uploaded (20)

DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABSDESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdfThe Role of Electrical and Electronics Engineers in IOT Technology.pdf
The Role of Electrical and Electronics Engineers in IOT Technology.pdf
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Water billing management system project report.pdf
Water billing management system project report.pdfWater billing management system project report.pdf
Water billing management system project report.pdf
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 

Dynamic programming1

  • 1. Dynamic Programming Lecture:17 DEBOLINA PAN Date: 01.10.13 I.D-110811026
  • 2. 1. Dynamic Programming 2. Comparison with divide n concuer 3. Where used? 4. Matrix Multiplication 5. Algo to multiply 2 matrices 6. #scaler multiplication used in matrix multiplication 7. Costs &parenthisization 8. Matrix multiplication Problem 9. Counting the #parenthisization 10. Brute force Method 11. Computing optimal cost 12. Matrix chain Order 13. Overlapping Subproblem 14. Memorization
  • 3.  dynamic programming is a method for solving complex problems by breaking them down into simpler subproblems.and the subproblems are dependent.  programming is an expression of algorithm.i dynamic programing the languag,used does not refer to any computer language,rather it refers to formal language.
  • 4.  Dynamic programming, like the divide-and-conquer method, solves problems by combining the solutions to subproblems.  divide-and-conquer algorithms partition the problem into disjoint subproblems, solve the subproblems recursively, and then combine their solutions to solve the original problem. In contrast, dynamic programming applies when the subproblems overlap—that is, when subproblems share subsubproblems.  a divide-and-conquer algorithm does more work than necessary, repeatedly solving the common subsubproblems. A dynamic- programming algorithm solves each subsubproblem just once and then saves its answer in a table, thereby avoiding the work of recomputing the answer every time it solves each subsubproblem
  • 5.  We typically apply dynamic programming to optimization problems.  Such problems can have many possible solutions. Each solution has a value, and we wish to find a solution with the optimal (minimum or maximum) value. We call such a solution an optimal solution to the problem, as opposed to the optimal solution, since there may be several solutions that achieve the optimal value.
  • 6.  Say,there are 2 matrices A & B.  We can multiply 2 matrices only if they are compatible.  Condition to be multiplied:#collumns of A =#rows of B.  If the resultant matrix is C,then Am*n * B n*p= Cm*p
  • 7. MATRIX-MULTIPLY(A,B) 1 .if columns[A] ≠rows[B] 2 . Then error “incompatible dimensions” 3 . let C be a new A:rows B:columns matrix 4 .for i <-1 to A.rows 5 .for j<-1 to B.columns 6 .cij =0 7 .for k =1 to A.columns 8 .cij =cij + aik .bkj 9 .return C
  • 8.  In the above example of matrix multiplication if we consider,  m=2,n=3,p=4,the total number of scaler multiplication used is 24,i.e m*n*p.  We shall express costs in terms of the number of scaler multiplications.
  • 9.  Example: A1->10*100 A2->100*5 A3->5*50 If(A1*A2)*A3=>#scaler multiplication: (10*100*5) + (10*5*50)=7500 IfA1*(A2*A3)=>#scaler multiplication: (10*100*50)+(100*5*50)=75000 =>Ordering of parenthasization is important.
  • 10.  matrix-chain multiplication problem can be stated as follows: given a chain <A1,A2,…,An> of n matrices, where for i=1,2, …,n, matrix Ai has dimension pi-1*pi , fully parenthesize the product A1A2…An in a way that minimizes the number of scalar multiplications.  Our goal is only to determine an order for multiplying matrices that has the lowest cost.
  • 11.  P(n) is the number of alternative parenthesizations of a sequence of n matrices.  When n=1,P(n)=1,as there is only 1 matrix,so only one way to put parenthesis.  n=2,P(n)=1,only one way to put parenthesis.  n=3,P(n)=2, (A1*A2)*A3 & A1*(A2*A3)  N=4,P(n)=6  (((A1*A2)*A3)*A4) (A1*((A2*A3)*A4)  ((A1*A2)*(A3*A4)) (A1*(A2*(A3*A4)))  (A1*(A2*A3))*A4 ((A1*A2)*(A3*A4))
  • 12.  Thus we obtain the sequence: ◦ P(n)= 1 if n=1  ∑k=1to n-1 P(k)P(n-k) if n≥2 a fully parenthasize matrix product is a prodct of 2 fully parenthasize matrix sbproducts.split between two sbprducts may occr between the k th & k+1 th matrix for any k=1…n-1 Rate of growth: Ω((4^n)/(n^(3/2)))
  • 13.  general problem-solving technique that consists of systematically enumerating all possible candidates for the solution and checking whether each candidate satisfies the problem's statement.  Lower bound of this problem using brute force method is Ω((4^n)/(n^(3/2)))  That means brute force method as the solution of the problem is not so good.
  • 14.  At this point, we could easily write a recursive algorithm based on recurrence to compute the minimum cost m[1,n]for multiplying A1,A2,…, An. Aswe sawfor the rod-cutting problem, and this recursive algorithm takes exponential time, which is no better than the brute-force method of checking each way of parenthesizing the product.  we have relatively few distinct subproblems: one subproblem for each choice of i and j satisfying 1 ≤i≤ j≤n, or (nc2 +n)=Θ(n^2) in all. A recursive algorithm may encounter each subproblem many times in different branches of its recursion tree. This property of overlapping subproblems is the second hallmark of when dynamic programming applies.
  • 15.  Instead of computing the solution to recurrence recursively, we compute the optimal cost by using a tabular, bottom-up approach.  We shall implement the tabular, bottom-up method in the procedure MATRIXCHAIN-ORDER, which appears below. This procedure assumes that matrix Ai has dimensions pi-1 *pi for i=1,2,…,n. Its input is a sequence p=<p0; p1; : : : ;pni> where length[p] = n+1. The procedure uses an auxiliary table m[1…n,1…n] for storing the m[I,j] costs and another auxiliary table s[1…n,1…n]that records which index of k achieved the optimal cost in computing m[i; j ].We shall use the table s to construct an optimal solution.
  • 16.  In order to implement the bottom-up approach, we must determine which entries of the table we refer to when computing m[i,j].the cost m[i ,j] of computing a matrix-chain product of j-i+1matrices depends only on the costs of computing matrix-chain products of fewer than j-i+1 matrices. That is, for k =I,i +1,…,j- 1, the matrix Ai…k is a product of k-i+1< j-i+1 matrices and the matrix Ak+1…j is a product of j-k< j-i +1 matrices. Thus, the algorithm should fill in the table min a manner that corresponds to solving the parenthesization problem on matrix chains of increasing length. For the subproblem of optimally parenthesizing the chain Ai,Ai+1,… Aj,we consider the subproblem size to be the length j-i+1 of the chain.
  • 17. MATRIX-CHAIN-ORDER.p/ 1. n D p:length 1 2.Let m[1,… n.1,… n] and s[1,… n-1. 2…n] be new tables 3. for i =1 to n 4.Do m[i,j] <-0 5. for l =2 to n // l is the chain length 6. dofor i =1 to n –l+1 7. Do j<-i+l-1 8. m[i, j]<- infinity 9.for k <-i to j-1 10. Do q <-m[i, k]+m[k+1,j]+pi-1.pk.pj 11. if q <m[i,j] 12. m[i, j]<-q 13. s[i,j]<-k 14. return m and s
  • 18.  running time of this algorithm is in fact also Ω(n^3) The algorithm requiresΘ(n^2) space to store the m and s tables. Thus, MATRIX- CHAINORDER is much more efficient than the exponential-time method of enumerating all possible parenthesizations and checking each one
  • 19.  In dynamic problem subproblems are dependent.  Dynamic-programming algorithms typically take advantage of overlapping subproblems by solving each subproblem once and then storing the solution in a table where it can be looked up when needed, using constant time per lookup.  Cost is reduced. (A1 *A2) (A3*(A4*A5)) (A1*A2) (A3*A4)*A5 ^ | Overlapping subproblem
  • 20.  A memoized recursive algorithm maintains an entry in a table for the solution to each subproblem. Each table entry initially contains a special value to indicate that the entry has yet to be filled in. When the subproblem is first encountered as the recursive algorithm unfolds, its solution is computed and then stored in the table. Each subsequent time that we encounter this subproblem, we simply look up the value stored in the table and return it