SlideShare a Scribd company logo
1 of 27
Respa Peter
Roll No:12
 What is Dynamic Programming?
 Matrix-Chain Multiplication
 Dynamic Programming is a technique for algorithm design.
 It is a tabular method in which it uses divide-and-conquer
to solve problems.
 dynamic programming is applicable when the subproblems
are not independent.
 So to solve a given problem, we need to solve different
parts of the problem.
 A dynamic programming approach consists
of a sequence of 4 steps
1. Characterize the structure of an optimal solution
2. Recursively define the value of an optimal solution
3. Compute the value of an optimal solution in a
bottom-up fashion
4. Construct an optimal solution from computed
information
Input: a chain of matrices to be multiplied
Output: a parenthesizing of the chain
Objective: minimize number of steps
needed for the multiplication
 Suppose we have a sequence or chain A1, A2, …, An of n
matrices to be multiplied
That is, we want to compute the product A1A2…An
 There are many possible ways (parenthesizations) to
compute the product
 Example: consider the chain A1, A2, A3, A4 of 4
matrices
◦ Let us compute the product A1A2A3A4
• 5 different orderings = 5 different parenthesizations
1. (A1(A2(A3A4)))
2. (A1((A2A3)A4))
3. ((A1A2)(A3A4))
4. ((A1(A2A3))A4)
5. (((A1A2)A3)A4)
• Matrix multiplication is associative ,
e.g.,
so parenthenization does not
change result.
• To compute the number of scalar
multiplications necessary, we must know:
• Algorithm to multiply two matrices
• Matrix dimensions
Algorithm..
Matrix-Multiply(A,B)
1.If columns[A]!=rows[B]
2. then error “incomplete dimensions”
3. else for i 1 to rows[A]
4. do for j 1 to columns[B]
5. do C[i,j] 0
6. for k 1 to columns[A]
7. Do C[i,j]=C[i,j]+A[i,k]*B[k,j]
8. Return C
•The time to compute C is dominated by the
number of scalar multiplications.
•To illustrate the different costs incurred by
different paranthesization of a matrix product.
Example: Consider three matrices A10100, B1005, and C550 There are 2 ways to
parenthesize
((AB)C) = D105 · C550
AB  10·100·5=5,000 scalar multiplications
DC  10·5·50 =2,500 scalar multiplications
(A(BC)) = A10100 · E10050
BC  100·5·50=25,000 scalar multiplications Total: 75,000
AE  10·100·50 =50,000 scalar multiplications
Total:
7,500
• Matrix-chain multiplication problem
 Given a chain A1, A2, …, An of n
matrices, where for i=1, 2, …, n, matrix
Ai has dimension pi-1pi
 Parenthesize the product A1A2…An such
that the total number of scalar
multiplications is minimized
• Before solving by Dynamic programming
exhaustively check all paranthesizations.
• P(n) : paranthesization of a sequence of
n matrices
Counting the Number of Parenthesizations
•We obtain the recurrence
P n
if n
P k p n k if n
k
n( )
( ) ( )


 






1 1
2
1
1
1.The Structure of an Optimal Parenthesization
Step 1: Characterize the structure of an optimal solution
•Ai..j : matrix that results from evaluating the product Ai Ai+1 Ai+2 ... Aj
•An optimal parenthesization of the product A1A2 ... An
– Splits the product between Ak and Ak+1, for some 1≤k<n
(A1A2A3 ... Ak) · (Ak+1Ak+2 ... An)
– i.e., first compute A1..k and Ak+1..n and then multiply these two
• The cost of this optimal parenthesization
Cost of computing A1..k
+ Cost of computing Ak+1..n
+ Cost of multiplying A1..k · Ak+1..n
Optimal (sub)structure:
• Suppose that optimal parenthesization of Ai;j splits between Ak and Ak+1.
• Then, parenthesizations of Ai;k and Ak+1;j must be optimal, too
(otherwise, enhance overall solution — subproblems are independent!).
Construct optimal solution:
1. split into subproblems (using optimal split!),
2. parenthesize them optimally,
3. combine optimal subproblem solutions.
2. Recursively def. value of opt. solution
•Let m[i; j] denote minimum number of scalar multiplications needed to
compute Ai;j = Ai*Ai+1….Aj (full problem: m[1; n]).
Recursive definition of m[i; j]:
• if i = j, then
m[i; j] = m[i; i] = 0
(Ai;i = Ai, no mult. needed).
•We also keep track of optimal splits:
4.Computing the Optimal Cost
Computing the Optimal Cost(cont..)
Algorithm for Computing the
Optimal Costs
Example:
656
545
434
323
212
101
2520
2010
105
515
1535
3530
ppA
ppA
ppA
ppA
ppA
ppA






The m and s table computed by MATRIX-
CHAIN-ORDER for n=6
m[2,5]=
min{
m[2,2]+m[3,5]+p1p2p5=0+2500+351520
=13000,
m[2,3]+m[4,5]+p1p3p5=2625+1000+355
20=7125,
m[2,4]+m[5,5]+p1p4p5=4375+0+351020
=11374
}
=7125
4.Constructing an Optimal
Solution
Matrix chain multiplication
Matrix chain multiplication

More Related Content

What's hot

daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13
Kumar
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
Mohd Arif
 

What's hot (20)

Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13
 
Disjoint sets
Disjoint setsDisjoint sets
Disjoint sets
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycleBacktracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 
Introduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of OptimalityIntroduction to Dynamic Programming, Principle of Optimality
Introduction to Dynamic Programming, Principle of Optimality
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's Algorithm
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
 

Similar to Matrix chain multiplication

Matrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithmMatrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithm
RajKumar323561
 
Matrix mult class-17
Matrix mult class-17Matrix mult class-17
Matrix mult class-17
Kumar
 
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docxMATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
andreecapon
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
MyAlome
 
Intro matlab and convolution islam
Intro matlab and convolution islamIntro matlab and convolution islam
Intro matlab and convolution islam
Islam Alabbasy
 

Similar to Matrix chain multiplication (20)

Matrix multiplicationdesign
Matrix multiplicationdesignMatrix multiplicationdesign
Matrix multiplicationdesign
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdf
 
Matrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithmMatrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithm
 
Matrix mult class-17
Matrix mult class-17Matrix mult class-17
Matrix mult class-17
 
Dynamic programming1
Dynamic programming1Dynamic programming1
Dynamic programming1
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
Lecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptxLecture -16-merge sort (slides).pptx
Lecture -16-merge sort (slides).pptx
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
matrices basic operation.ppt
matrices basic operation.pptmatrices basic operation.ppt
matrices basic operation.ppt
 
DAA - chapter 1.pdf
DAA - chapter 1.pdfDAA - chapter 1.pdf
DAA - chapter 1.pdf
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docxMATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
 
Assignment On Matlab
Assignment On MatlabAssignment On Matlab
Assignment On Matlab
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
 
Mechanical Engineering Homework Help
Mechanical Engineering Homework HelpMechanical Engineering Homework Help
Mechanical Engineering Homework Help
 
Lecture 7.pptx
Lecture 7.pptxLecture 7.pptx
Lecture 7.pptx
 
Lesson 1 matrix
Lesson 1 matrixLesson 1 matrix
Lesson 1 matrix
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Intro matlab and convolution islam
Intro matlab and convolution islamIntro matlab and convolution islam
Intro matlab and convolution islam
 
matrix algebra
matrix algebramatrix algebra
matrix algebra
 

More from Respa Peter

More from Respa Peter (13)

Tpes of Softwares
Tpes of SoftwaresTpes of Softwares
Tpes of Softwares
 
Information technology for business
Information technology for business Information technology for business
Information technology for business
 
Types of sql injection attacks
Types of sql injection attacksTypes of sql injection attacks
Types of sql injection attacks
 
DataMining Techniq
DataMining TechniqDataMining Techniq
DataMining Techniq
 
Database
DatabaseDatabase
Database
 
software failures
 software failures software failures
software failures
 
Cloud computing
Cloud computingCloud computing
Cloud computing
 
Managing software development
Managing software developmentManaging software development
Managing software development
 
Data mining
Data miningData mining
Data mining
 
Knime
KnimeKnime
Knime
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 
Web services have made the development of mobile Web applications much easier...
Web services have made the development of mobile Web applications much easier...Web services have made the development of mobile Web applications much easier...
Web services have made the development of mobile Web applications much easier...
 
Open shortest path first (ospf)
Open shortest path first (ospf)Open shortest path first (ospf)
Open shortest path first (ospf)
 

Recently uploaded

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 

Recently uploaded (20)

PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 

Matrix chain multiplication

  • 2.  What is Dynamic Programming?  Matrix-Chain Multiplication
  • 3.  Dynamic Programming is a technique for algorithm design.  It is a tabular method in which it uses divide-and-conquer to solve problems.  dynamic programming is applicable when the subproblems are not independent.  So to solve a given problem, we need to solve different parts of the problem.
  • 4.  A dynamic programming approach consists of a sequence of 4 steps 1. Characterize the structure of an optimal solution 2. Recursively define the value of an optimal solution 3. Compute the value of an optimal solution in a bottom-up fashion 4. Construct an optimal solution from computed information
  • 5.
  • 6. Input: a chain of matrices to be multiplied Output: a parenthesizing of the chain Objective: minimize number of steps needed for the multiplication
  • 7.  Suppose we have a sequence or chain A1, A2, …, An of n matrices to be multiplied That is, we want to compute the product A1A2…An  There are many possible ways (parenthesizations) to compute the product
  • 8.  Example: consider the chain A1, A2, A3, A4 of 4 matrices ◦ Let us compute the product A1A2A3A4 • 5 different orderings = 5 different parenthesizations 1. (A1(A2(A3A4))) 2. (A1((A2A3)A4)) 3. ((A1A2)(A3A4)) 4. ((A1(A2A3))A4) 5. (((A1A2)A3)A4)
  • 9. • Matrix multiplication is associative , e.g., so parenthenization does not change result. • To compute the number of scalar multiplications necessary, we must know: • Algorithm to multiply two matrices • Matrix dimensions
  • 10. Algorithm.. Matrix-Multiply(A,B) 1.If columns[A]!=rows[B] 2. then error “incomplete dimensions” 3. else for i 1 to rows[A] 4. do for j 1 to columns[B] 5. do C[i,j] 0 6. for k 1 to columns[A] 7. Do C[i,j]=C[i,j]+A[i,k]*B[k,j] 8. Return C
  • 11. •The time to compute C is dominated by the number of scalar multiplications. •To illustrate the different costs incurred by different paranthesization of a matrix product. Example: Consider three matrices A10100, B1005, and C550 There are 2 ways to parenthesize ((AB)C) = D105 · C550 AB  10·100·5=5,000 scalar multiplications DC  10·5·50 =2,500 scalar multiplications (A(BC)) = A10100 · E10050 BC  100·5·50=25,000 scalar multiplications Total: 75,000 AE  10·100·50 =50,000 scalar multiplications Total: 7,500
  • 12. • Matrix-chain multiplication problem  Given a chain A1, A2, …, An of n matrices, where for i=1, 2, …, n, matrix Ai has dimension pi-1pi  Parenthesize the product A1A2…An such that the total number of scalar multiplications is minimized
  • 13. • Before solving by Dynamic programming exhaustively check all paranthesizations. • P(n) : paranthesization of a sequence of n matrices Counting the Number of Parenthesizations
  • 14. •We obtain the recurrence P n if n P k p n k if n k n( ) ( ) ( )           1 1 2 1 1
  • 15. 1.The Structure of an Optimal Parenthesization Step 1: Characterize the structure of an optimal solution •Ai..j : matrix that results from evaluating the product Ai Ai+1 Ai+2 ... Aj •An optimal parenthesization of the product A1A2 ... An – Splits the product between Ak and Ak+1, for some 1≤k<n (A1A2A3 ... Ak) · (Ak+1Ak+2 ... An) – i.e., first compute A1..k and Ak+1..n and then multiply these two • The cost of this optimal parenthesization Cost of computing A1..k + Cost of computing Ak+1..n + Cost of multiplying A1..k · Ak+1..n
  • 16. Optimal (sub)structure: • Suppose that optimal parenthesization of Ai;j splits between Ak and Ak+1. • Then, parenthesizations of Ai;k and Ak+1;j must be optimal, too (otherwise, enhance overall solution — subproblems are independent!). Construct optimal solution: 1. split into subproblems (using optimal split!), 2. parenthesize them optimally, 3. combine optimal subproblem solutions.
  • 17. 2. Recursively def. value of opt. solution •Let m[i; j] denote minimum number of scalar multiplications needed to compute Ai;j = Ai*Ai+1….Aj (full problem: m[1; n]). Recursive definition of m[i; j]: • if i = j, then m[i; j] = m[i; i] = 0 (Ai;i = Ai, no mult. needed).
  • 18. •We also keep track of optimal splits:
  • 20. Computing the Optimal Cost(cont..)
  • 21. Algorithm for Computing the Optimal Costs
  • 23. The m and s table computed by MATRIX- CHAIN-ORDER for n=6