Question

In: Computer Science

Recall the Matrix Chain Multiplication Algorithm for determining the optimal parenthesization for a product of matrices....

Recall the Matrix Chain Multiplication Algorithm for determining the optimal parenthesization for a product of matrices. Provide a recursive implementation of the function

void print_parenth(Matrix K[], int i, int j);

that takes as input the matrix K of k values that are needed to construct the optimal parenthesization for Ai · · · Aj . Assume access to a print function that takes as input a string and prints its value. You may also assume a “+” operation for string concatenation, and that integers are automatically converted to strings. Finally, assume that a single matrix does not require a set of outer parentheses, but two or more matrices do. For example, to print ((A1A2)A3), one could write

print "((A" + 1 + "A" + 2 + ")A" + 3 + ")"

Solutions

Expert Solution

Here am providing the full code of matrix chain multiplication. Hope this helps you. Please give an upvote. Thank you.

#include <bits/stdc++.h>
using namespace std;

// Matrix Ai has dimension p[i-1] x p[i]
// for i = 1..n
int MatrixChainOrder(int p[], int i, int j)
{
if (i == j)
return 0;
int k;
int min = INT_MAX;
int count;

// place parenthesis at different places
// between first and last matrix, recursively
// calculate count of multiplications for
// each parenthesis placement and return the
// minimum count
for (k = i; k < j; k++)
{
count = MatrixChainOrder(p, i, k)
+ MatrixChainOrder(p, k + 1, j)
+ p[i - 1] * p[k] * p[j];

if (count < min)
min = count;
}

// Return minimum count
return min;
}

// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 3 };
int n = sizeof(arr) / sizeof(arr[0]);

cout << "Minimum number of multiplications is "
<< MatrixChainOrder(arr, 1, n - 1);
}


Related Solutions

Using dynamic programming, find an optimal parenthesization of a matrix-chain product of 4 matrices whose dimensions...
Using dynamic programming, find an optimal parenthesization of a matrix-chain product of 4 matrices whose dimensions are p = { 1, 10, 5, 20, 2}. Show your work.
Find an optimal parenthesization of a matrix-chain product whose sequence of dimensions is < 4,8,3,12,4,40,6 >.
Find an optimal parenthesization of a matrix-chain product whose sequence of dimensions is < 4,8,3,12,4,40,6 >.
Recall the Matrix-Multiplication Algorithm for determining all-pairs distances in a graph. Provide a linear-time recursive implementation...
Recall the Matrix-Multiplication Algorithm for determining all-pairs distances in a graph. Provide a linear-time recursive implementation of the function void print_path(Matrix D[], int n, int i, int j); that takes as input the array of matrices D[1], D[2], . . . , D[n − 1] that have been computed by the algorithm, and prints the optimal path from vertex i to vertex j. Hint: for convenience you may use the notation Dr ij to denote the value D[r][i, j], i.e....
Find an optimal parenthesization of matrices whose sequence of dimensions is: <5, 10, 12, 5, 50>....
Find an optimal parenthesization of matrices whose sequence of dimensions is: <5, 10, 12, 5, 50>. Please write out both the m[·, ·] and s[·, ·] tables.
3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is...
3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is found such that it requires 6 multiplications and 31 additions of n/2 x n/2 submatrices. Write the recurrence for the running time T(n) of this algorithm and find the order of T(n).
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Consider a variant of the matrix-chain multiplication problem in which the goal is to parenthesize the sequence of matrices so as to maximize, rather than minimize, the number of scalar multiplications.
Consider a variant of the matrix-chain multiplication problem in which the goal is to parenthesize the sequence of matrices so as to maximize, rather than minimize, the number of scalar multiplications. Does this problem exhibit optimal substructure?
build a program which performs matrix multiplication on square matrices. use UNIX "time" to capture the...
build a program which performs matrix multiplication on square matrices. use UNIX "time" to capture the time it takes to run the program with different data sizes. Languages: Python Task: Create matrix multiplication Input: Size of square matrix.   Size should be    250, 500, 1000, 1500, 2000 Internals: Explicitly or implicitly allocate sufficient memory to hold three NxN floating point Matrices, using a random number generator -- populate two of the Matrices, Multiply the two matrices, putting the result into the...
Divide and Conquer (Strassen’s Matrix Multiplication) Given two square matrices A and B of size n...
Divide and Conquer (Strassen’s Matrix Multiplication) Given two square matrices A and B of size n x n each, find their multiplication matrix. Naive Method Following is a simple way to multiply two matrices.                void multiply(int A[][N], int B[][N], int C[][N]) {     for (int i = 0;   i < N; i++) {         for (int j = 0; j < N; j++) {             C[i][j] = 0;             for (int k = 0; k < N; k++) {                 C[i][j] += A[i][k]*B[k][j];             }...
Strassen’s algorithm for matrix multiplication relies on using 7 multiplications (instead of 8 as in the...
Strassen’s algorithm for matrix multiplication relies on using 7 multiplications (instead of 8 as in the straightforward algorithm) to multiply a pair of 2 by 2 matrices. Explain why it is not possible to further reduce this number, 7, to anything less than 4, in multiplying a pair of 2 by 2 matrices.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT