SlideShare a Scribd company logo
1 of 11
Md. Saidul Islam
In mathematics or computer science, Dynamic
Programming is a method for solving complex
problems by breaking them down into simpler subproblems. So, Matrix chain multiplication is an ideal
example that demonstrates utility of dynamic
programming.
Engineering applications often have to multiply a large
chain of matrices with large dimensions, for example:
100 matrices of dimension 100×100. We can multiply
this chain of matrices in many different ways, but we
will go for that way which takes lower computations.
For example, we are going to multiply 4 matrices:
M1 = 2 x 3
M2 = 3 x 4
M3 = 4 x 5
M4 = 5 x 7
And we have conditions for multiplying matrices:
• We can multiply only two matrices at a time.
• When we go to multiply 2 matrices, the number of
columns of 1st matrix should be same as the number
of rows of 2nd matrix.
We can multiply the chain of matrices by following
those conditions in these ways:
M1
M2
M3
M4

=2x3
=3x4
=4x5
=5x7

( M1 M2 )( M3 M4 )
(( M1 M2 ) M3 ) M4
M1 ( M 2 ( M 3 M4 )
( M1 ( M 2 M 3 ) M 4
M1 (( M2 M3 ) M4 )

= 220
= 134
= 266
= 160
= 207

Numbers of the rightmost side is number of total
scalar multiplication. So we have realized that we
can reduce the number of total multiplication and
this reduced time is a fact for a large chain of
matrices.
Renaming matrices as Mi and dimensions as Pi - 1 x Pi ,
we have got:
M1 = P0 x P 1
M2 = P1 x P 2
M3 = P 2 x P 3
M4 = P3 x P 4
|
|
|
Mi = Pi – 1 x Pi
We will use a formula:

Where C i, j means Mi to Mj .
i.e.: C 1, 4 means M1 to M4 .
And we will use a variable 'k' as follows:
M1 |k=1 M2 M3 M4
M1 M2 |k=2 M3 M4
M1 M2 M3 |k=3 M4
The thing we’re going to do is to apply above formula for
every 'k' in the range 'i' to 'j' and pick the lowest value every
step.

C 1 , 4 = min ( C1 , 1 + C2 , 4 + P0 * P1 * P4 , C1 , 2 + C3 , 4 + P0 *
P2 * P4 , C1 , 3 + C4 , 4 + P0 * P3 * P4 ) = min ( 207, 220, 134 )
= 134
C 2, 4 = min ( C2 , 2 + C3 , 4 + P1 * P2 * P4 , C2 , 3 + C4 , 4 + P1 *
P3 * P4 ) = min ( 224, 165 ) = 165
C 1, 3 = min ( C1 , 1 + C2 , 3 + P0 * P1 * P3 , C1 , 2 + C3 , 3 + P0 *
P2 * P3 ) = min ( 90, 64 ) = 64
C 1, 2 = P0 * P1 * P2 = 24
C 2, 3 = P1 * P2 * P3 = 60
C 3, 4 = P2 * P3 * P4 = 140
1.

int Chain( int i, int j )

2.

{

3.

int min = 10000, value, k;

4.

if( i == j ){

5.

return 0;

6.

}

7.

else{

8.

for( k = i; k < j; k++ ){

9.

value = (Chain(i, k) + Chain(k + 1, j) + (dimensions[i-1] *
dimensions[k] * dimensions[j]));

10.

if( min > value ){

11.

min = value;

12.

mat[i][j] = k;

13.

}

14.

}

15.

}

16.

return min;

17. }
1.

int main(void)

2.

{

3.

int result, i;

4.

printf("Enter number of matrices: ");

5.

scanf("%d", &n);

6.

printf("Enter dimensions : ");

7.

for( i = 0; i <= n; i++ ){

8.

scanf("%d", &dimensions[i]);

9.

}

10.

result = Chain(1, n);

11.

printf("nTotal number of multiplications: %d andn", result);

12.

printf("Multiplication order is: ");

13.

PrintOrder( 1, n );

14.

printf("n");

15.

}
Dynamic Programming - Matrix Chain Multiplication
Dynamic Programming - Matrix Chain Multiplication

More Related Content

What's hot

Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemMadhu Bala
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmHoneyChintal
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy methodhodcsencet
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen ProblemSukrit Gupta
 
Elements of Dynamic Programming
Elements of Dynamic ProgrammingElements of Dynamic Programming
Elements of Dynamic ProgrammingVishwajeet Shabadi
 
knapsack problem
knapsack problemknapsack problem
knapsack problemAdnan Malak
 
Operator Precedence Grammar
Operator Precedence GrammarOperator Precedence Grammar
Operator Precedence GrammarHarisonFekadu
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic ProgrammingSahil Kumar
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsGanesh Solanke
 
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 EfficiencySaranya Natarajan
 

What's hot (20)

Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Graph coloring using backtracking
Graph coloring using backtrackingGraph coloring using backtracking
Graph coloring using backtracking
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
 
Unit 1 chapter 1 Design and Analysis of Algorithms
Unit 1   chapter 1 Design and Analysis of AlgorithmsUnit 1   chapter 1 Design and Analysis of Algorithms
Unit 1 chapter 1 Design and Analysis of Algorithms
 
Huffman Encoding Pr
Huffman Encoding PrHuffman Encoding Pr
Huffman Encoding Pr
 
The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
Elements of Dynamic Programming
Elements of Dynamic ProgrammingElements of Dynamic Programming
Elements of Dynamic Programming
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
 
knapsack problem
knapsack problemknapsack problem
knapsack problem
 
Operator Precedence Grammar
Operator Precedence GrammarOperator Precedence Grammar
Operator Precedence Grammar
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Code generation
Code generationCode generation
Code generation
 
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
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
 
Answer quiz minimax
Answer quiz minimaxAnswer quiz minimax
Answer quiz minimax
 
Greedy method
Greedy method Greedy method
Greedy method
 

Similar to Dynamic Programming - Matrix Chain Multiplication

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
 
A study on number theory and its applications
A study on number theory and its applicationsA study on number theory and its applications
A study on number theory and its applicationsItishree Dash
 
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 algorithmRajKumar323561
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfjannatulferdousmaish
 
Longest Common Subsequence & Matrix Chain Multiplication
Longest Common Subsequence & Matrix Chain MultiplicationLongest Common Subsequence & Matrix Chain Multiplication
Longest Common Subsequence & Matrix Chain MultiplicationJaneAlamAdnan
 
Calculating Mine Probability in Minesweeper
Calculating Mine Probability in MinesweeperCalculating Mine Probability in Minesweeper
Calculating Mine Probability in MinesweeperLukeVideckis
 
14-applications-of-number-theory.ppt
14-applications-of-number-theory.ppt14-applications-of-number-theory.ppt
14-applications-of-number-theory.pptIdcIdk1
 
Dynamic Programming Matrix Chain Multiplication
Dynamic Programming Matrix Chain MultiplicationDynamic Programming Matrix Chain Multiplication
Dynamic Programming Matrix Chain MultiplicationKrishnakoumarC
 
DAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxDAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxvaishnavi339314
 
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.pdfyashodamb
 
NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...
NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...
NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...csandit
 
Dynamic programming1
Dynamic programming1Dynamic programming1
Dynamic programming1debolina13
 
Testing of Matrices Multiplication Methods on Different Processors
Testing of Matrices Multiplication Methods on Different ProcessorsTesting of Matrices Multiplication Methods on Different Processors
Testing of Matrices Multiplication Methods on Different ProcessorsEditor IJMTER
 
Appendix a 2
Appendix a 2Appendix a 2
Appendix a 2Loc Tran
 

Similar to Dynamic Programming - Matrix Chain Multiplication (20)

Lec39
Lec39Lec39
Lec39
 
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)
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
A study on number theory and its applications
A study on number theory and its applicationsA study on number theory and its applications
A study on number theory and its applications
 
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
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdf
 
Longest Common Subsequence & Matrix Chain Multiplication
Longest Common Subsequence & Matrix Chain MultiplicationLongest Common Subsequence & Matrix Chain Multiplication
Longest Common Subsequence & Matrix Chain Multiplication
 
Lecture11
Lecture11Lecture11
Lecture11
 
Calculating Mine Probability in Minesweeper
Calculating Mine Probability in MinesweeperCalculating Mine Probability in Minesweeper
Calculating Mine Probability in Minesweeper
 
E33018021
E33018021E33018021
E33018021
 
14-applications-of-number-theory.ppt
14-applications-of-number-theory.ppt14-applications-of-number-theory.ppt
14-applications-of-number-theory.ppt
 
Dynamic Programming Matrix Chain Multiplication
Dynamic Programming Matrix Chain MultiplicationDynamic Programming Matrix Chain Multiplication
Dynamic Programming Matrix Chain Multiplication
 
DAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxDAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptx
 
MFCS-17.ppt
MFCS-17.pptMFCS-17.ppt
MFCS-17.ppt
 
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
 
NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...
NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...
NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...
 
Dynamic programming1
Dynamic programming1Dynamic programming1
Dynamic programming1
 
Palm ch1
Palm ch1Palm ch1
Palm ch1
 
Testing of Matrices Multiplication Methods on Different Processors
Testing of Matrices Multiplication Methods on Different ProcessorsTesting of Matrices Multiplication Methods on Different Processors
Testing of Matrices Multiplication Methods on Different Processors
 
Appendix a 2
Appendix a 2Appendix a 2
Appendix a 2
 

Recently uploaded

Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Recently uploaded (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

Dynamic Programming - Matrix Chain Multiplication

  • 2. In mathematics or computer science, Dynamic Programming is a method for solving complex problems by breaking them down into simpler subproblems. So, Matrix chain multiplication is an ideal example that demonstrates utility of dynamic programming. Engineering applications often have to multiply a large chain of matrices with large dimensions, for example: 100 matrices of dimension 100×100. We can multiply this chain of matrices in many different ways, but we will go for that way which takes lower computations.
  • 3. For example, we are going to multiply 4 matrices: M1 = 2 x 3 M2 = 3 x 4 M3 = 4 x 5 M4 = 5 x 7 And we have conditions for multiplying matrices: • We can multiply only two matrices at a time. • When we go to multiply 2 matrices, the number of columns of 1st matrix should be same as the number of rows of 2nd matrix.
  • 4. We can multiply the chain of matrices by following those conditions in these ways: M1 M2 M3 M4 =2x3 =3x4 =4x5 =5x7 ( M1 M2 )( M3 M4 ) (( M1 M2 ) M3 ) M4 M1 ( M 2 ( M 3 M4 ) ( M1 ( M 2 M 3 ) M 4 M1 (( M2 M3 ) M4 ) = 220 = 134 = 266 = 160 = 207 Numbers of the rightmost side is number of total scalar multiplication. So we have realized that we can reduce the number of total multiplication and this reduced time is a fact for a large chain of matrices.
  • 5. Renaming matrices as Mi and dimensions as Pi - 1 x Pi , we have got: M1 = P0 x P 1 M2 = P1 x P 2 M3 = P 2 x P 3 M4 = P3 x P 4 | | | Mi = Pi – 1 x Pi
  • 6. We will use a formula: Where C i, j means Mi to Mj . i.e.: C 1, 4 means M1 to M4 . And we will use a variable 'k' as follows: M1 |k=1 M2 M3 M4 M1 M2 |k=2 M3 M4 M1 M2 M3 |k=3 M4
  • 7. The thing we’re going to do is to apply above formula for every 'k' in the range 'i' to 'j' and pick the lowest value every step. C 1 , 4 = min ( C1 , 1 + C2 , 4 + P0 * P1 * P4 , C1 , 2 + C3 , 4 + P0 * P2 * P4 , C1 , 3 + C4 , 4 + P0 * P3 * P4 ) = min ( 207, 220, 134 ) = 134 C 2, 4 = min ( C2 , 2 + C3 , 4 + P1 * P2 * P4 , C2 , 3 + C4 , 4 + P1 * P3 * P4 ) = min ( 224, 165 ) = 165 C 1, 3 = min ( C1 , 1 + C2 , 3 + P0 * P1 * P3 , C1 , 2 + C3 , 3 + P0 * P2 * P3 ) = min ( 90, 64 ) = 64 C 1, 2 = P0 * P1 * P2 = 24 C 2, 3 = P1 * P2 * P3 = 60 C 3, 4 = P2 * P3 * P4 = 140
  • 8. 1. int Chain( int i, int j ) 2. { 3. int min = 10000, value, k; 4. if( i == j ){ 5. return 0; 6. } 7. else{ 8. for( k = i; k < j; k++ ){ 9. value = (Chain(i, k) + Chain(k + 1, j) + (dimensions[i-1] * dimensions[k] * dimensions[j])); 10. if( min > value ){ 11. min = value; 12. mat[i][j] = k; 13. } 14. } 15. } 16. return min; 17. }
  • 9. 1. int main(void) 2. { 3. int result, i; 4. printf("Enter number of matrices: "); 5. scanf("%d", &n); 6. printf("Enter dimensions : "); 7. for( i = 0; i <= n; i++ ){ 8. scanf("%d", &dimensions[i]); 9. } 10. result = Chain(1, n); 11. printf("nTotal number of multiplications: %d andn", result); 12. printf("Multiplication order is: "); 13. PrintOrder( 1, n ); 14. printf("n"); 15. }