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

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....
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.
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).
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 >.
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.
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.
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.
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?
2. Using matrices, create an algorithm that takes a matrix of dimension N x N and...
2. Using matrices, create an algorithm that takes a matrix of dimension N x N and feed it in a spiral shape with the sequential number from 1 to N^2. Then do an algorithm in PSEint
Using the dynamic programming-based algorithm for chain matrix multiplcation, show the best way to multiply a...
Using the dynamic programming-based algorithm for chain matrix multiplcation, show the best way to multiply a chain of matrices with dimensions that are: 10x5, 5x2, 2x20, 20x12, 12x4 and 4x60. Finally, use the resulting tables to provide the complete optimal parenthesization of the matrix chain product A0 · A1 · A2 · A3 · A4 · A5.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT