0% found this document useful (0 votes)
7 views7 pages

Assignment 5

The document outlines an assignment to determine the most efficient way to multiply five matrices using matrix chain multiplication. It includes an algorithm for calculating the minimum scalar multiplications and optimal parenthesization, along with a source code implementation in C. Additionally, it provides time and space complexity analyses, concluding that both are O(n³) and O(n²), respectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Assignment 5

The document outlines an assignment to determine the most efficient way to multiply five matrices using matrix chain multiplication. It includes an algorithm for calculating the minimum scalar multiplications and optimal parenthesization, along with a source code implementation in C. Additionally, it provides time and space complexity analyses, concluding that both are O(n³) and O(n²), respectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ASSIGNMENT-5

Given five matrices with the following dimensions:


• Matrix A: 4 × 10
• Matrix B: 10 × 3
• Matrix C: 3 × 12
• Matrix D: 12 × 20
• Matrix E: 20 × 7

Write a program to determine the most efficient way (in terms of


minimum scalar multiplications) to multiply these matrices using
matrix chain multiplication. Your code should output:

1. The optimal number of scalar multiplications required.


2. The optimal parenthesization for matrix multiplication.

Note: Matrix multiplication is associative, and the goal is to minimize


the number of scalar multiplications by choosing the best order
of multiplication.
Algorithm:
Matrix Chain Order
1. Let p[] be the array of matrix dimensions.
If you have 4 matrices A (10x20), B (20x30), C (30x40), D (40x30),
then:
p = {10, 20, 30, 40, 30}
2. Create two tables:
• m[i][j]: Minimum cost (number of multiplications) to compute
matrices i to j.
• bracket[i][j]: Index k where the optimal split occurs between i and
j.
3. Set the diagonal of m[i][i] = 0 because one matrix needs no
multiplication.
4. Loop over all chain lengths L = 2 to n:
• For each starting index i, compute the ending index j
= i + L -1.
• Try all possible split points k between i and j.
• Compute cost:
cost = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]
• Keep the minimum cost in m[i][j] and store the k in bracket[i][j].
5. Result:
• Minimum cost is in m[1][n-1]
• Use bracket[][] to find the best order to multiply
Print Optimal Parenthesis
1. If there’s only one matrix (i == j), print its name (like A, B, C...).
2. Otherwise:
o Print ( (start parentheses)
o Recursively solve the left part using bracket[i][j]
o Recursively solve the right part using bracket[i][j] + 1
o Print ) (end parentheses)
Source code:
#include <stdio.h>
#include <limits.h>
#define N 6
void printOptimalParens(int i, int j, int bracket[N][N], char *name) {
if (i == j) {
printf("%c", (*name)++);
return;
}
printf("(");
printOptimalParens(i, bracket[i][j], bracket, name);
printOptimalParens(bracket[i][j] + 1, j, bracket, name);
printf(")");
}

void matrixChainOrder(int p[], int n) {


int m[N][N];
int bracket[N][N];

for (int i = 1; i < n; i++)


m[i][i] = 0;

for (int L = 2; L < n; L++) {


for (int i = 1; i < n - L + 1; i++) {
int j = i + L - 1;
m[i][j] = INT_MAX;
for (int k = i; k < j; k++) {
int q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
if (q < m[i][j]) {
m[i][j] = q;
bracket[i][j] = k;
}
}
}
}
printf("Minimum number of multiplications: %d\n", m[1][n-1]);
printf("\nCost Table (m):\n");
for (int i = 1; i < n; i++) {
for (int j = 1; j < n; j++) {
if (i > j)
printf(" - ");
else
printf("%6d ", m[i][j]);
}
printf("\n");
}
printf("\nK Table (bracket):\n");
for (int i = 1; i < n; i++) {
for (int j = 1; j < n; j++) {
if (i >= j)
printf(" - ");
else
printf("%6d ", bracket[i][j]);
}
printf("\n");
}
printf("\nOptimal Parenthesization: ");
char name = 'A';
printOptimalParens(1, n-1, bracket, &name);
printf("\n");
}

int main() {
int p[] = {4, 10, 3, 12, 20, 7};
int n = sizeof(p)/sizeof(p[0]);
matrixChainOrder(p, n);
return 0;
}
Input/output:
Analysis:
1. Time Complexity Analysis:
• You are checking every way to multiply matrices from i to j.
• For each pair (i, j), you try every split point k.
• There are:
o O(n²) such pairs (all i and j)
o Up to O(n) split points (k) for each pair
• Total work = O(n²) × O(n) = O(n³)
• Time Complexity = O(n³)

2. Space Complexity Analysis:


• You use two 2D arrays:
o m[i][j] to store the minimum number of multiplications
o bracket[i][j] to store the best split position
• Each array is size n x n
• Space Complexity = O(n²)

You might also like