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

Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithmsrutisenpatra
 
Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13Kumar
 
Bucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision AlgorithmBucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision AlgorithmKrupali Mistry
 
Sequential Version / Version 1 Unsigned Multiplication Algorithm
Sequential Version / Version 1 Unsigned Multiplication AlgorithmSequential Version / Version 1 Unsigned Multiplication Algorithm
Sequential Version / Version 1 Unsigned Multiplication Algorithmbabuece
 
P np &amp; np completeness
P np &amp; np completenessP np &amp; np completeness
P np &amp; np completenessAnwal Mirza
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemMadhu Bala
 
backtracking algorithms of ada
backtracking algorithms of adabacktracking algorithms of ada
backtracking algorithms of adaSahil Kumar
 
Unit 3-pipelining &amp; vector processing
Unit 3-pipelining &amp; vector processingUnit 3-pipelining &amp; vector processing
Unit 3-pipelining &amp; vector processingvishal choudhary
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using BacktrackingAbhishek Singh
 
Huffman Coding Algorithm Presentation
Huffman Coding Algorithm PresentationHuffman Coding Algorithm Presentation
Huffman Coding Algorithm PresentationAkm Monir
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problemharsh kothari
 
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
 
Design & Check Cyclic Redundancy Code using VERILOG HDL
Design & Check Cyclic Redundancy Code using VERILOG HDLDesign & Check Cyclic Redundancy Code using VERILOG HDL
Design & Check Cyclic Redundancy Code using VERILOG HDLijsrd.com
 

What's hot (20)

Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Bucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision AlgorithmBucket sort- A Noncomparision Algorithm
Bucket sort- A Noncomparision Algorithm
 
Sequential Version / Version 1 Unsigned Multiplication Algorithm
Sequential Version / Version 1 Unsigned Multiplication AlgorithmSequential Version / Version 1 Unsigned Multiplication Algorithm
Sequential Version / Version 1 Unsigned Multiplication Algorithm
 
P np &amp; np completeness
P np &amp; np completenessP np &amp; np completeness
P np &amp; np completeness
 
Counting sort
Counting sortCounting sort
Counting sort
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
backtracking algorithms of ada
backtracking algorithms of adabacktracking algorithms of ada
backtracking algorithms of ada
 
Unit 3-pipelining &amp; vector processing
Unit 3-pipelining &amp; vector processingUnit 3-pipelining &amp; vector processing
Unit 3-pipelining &amp; vector processing
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 
Np cooks theorem
Np cooks theoremNp cooks theorem
Np cooks theorem
 
Huffman Coding Algorithm Presentation
Huffman Coding Algorithm PresentationHuffman Coding Algorithm Presentation
Huffman Coding Algorithm Presentation
 
Greedy method
Greedy methodGreedy method
Greedy method
 
Control Memory
Control MemoryControl Memory
Control Memory
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
 
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
 
Counting Sort
Counting SortCounting Sort
Counting Sort
 
Design & Check Cyclic Redundancy Code using VERILOG HDL
Design & Check Cyclic Redundancy Code using VERILOG HDLDesign & Check Cyclic Redundancy Code using VERILOG HDL
Design & Check Cyclic Redundancy Code using VERILOG HDL
 

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
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplicationKiran K
 
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
 
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
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
 
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
 
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

MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 

Recently uploaded (20)

MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 

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. }