Question

In: Computer Science

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.

Solutions

Expert Solution

Here is code in c++ to find an optimal parenthesization for the given matrices:


// C++ program to print optimal parenthesization 
// in matrix chain multiplication. 
#include<bits/stdc++.h> 
using namespace std; 

// Function for printing the optimal 
// parenthesization of a matrix chain product 
void printParenthesis(int i, int j, int n, 
                                        int *bracket, char &name) 
{ 
        // If only one matrix left in current segment 
        if (i == j) 
        { 
                cout << name++; 
                return; 
        } 

        cout << "("; 

        // Recursively put brackets around subexpression 
        // from i to bracket[i][j]. 
        // Note that "*((bracket+i*n)+j)" is similar to 
        // bracket[i][j] 
        printParenthesis(i, *((bracket+i*n)+j), n, 
                                        bracket, name); 

        // Recursively put brackets around subexpression 
        // from bracket[i][j] + 1 to j. 
        printParenthesis(*((bracket+i*n)+j) + 1, j, 
                                        n, bracket, name); 
        cout << ")"; 
} 

// Matrix Ai has dimension p[i-1] x p[i] for i = 1..n 
// Please refer below article for details of this 
// function 
// https://goo.gl/k6EYKj 
void matrixChainOrder(int p[], int n) 
{ 
        /* For simplicity of the program, one extra 
        row and one extra column are allocated in 
                m[][]. 0th row and 0th column of m[][] 
                are not used */
        int m[n][n]; 

        // bracket[i][j] stores optimal break point in 
        // subexpression from i to j. 
        int bracket[n][n]; 

        /* m[i,j] = Minimum number of scalar multiplications 
        needed to compute the matrix A[i]A[i+1]...A[j] = 
        A[i..j] where dimension of A[i] is p[i-1] x p[i] */

        // cost is zero when multiplying one matrix. 
        for (int i=1; i<n; i++) 
                m[i][i] = 0; 

        // L is chain length. 
        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-1; k++) 
                        { 
                                // q = cost/scalar multiplications 
                                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; 

                                        // Each entry bracket[i,j]=k shows 
                                        // where to split the product arr 
                                        // i,i+1....j for the minimum cost. 
                                        bracket[i][j] = k; 
                                } 
                        } 
                } 
        } 

        // The first matrix is printed as 'A', next as 'B', 
        // and so on 
        char name = 'A'; 

        cout << "Optimal Parenthesization is : "; 
        printParenthesis(1, n-1, n, (int *)bracket, name); 
        cout << "\nOptimal Cost is : " << m[1][n-1]; 
} 


int main() 
{ 
        int arr[] = {1, 10, 5, 20, 2}; 
        int n = sizeof(arr)/sizeof(arr[0]); 
        matrixChainOrder(arr, n); 
        return 0; 
} 

Output:


Related Solutions

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 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...
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.
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.
Find the dimensions of the following linear spaces. (a) The space of all 3×4 matrices (b)...
Find the dimensions of the following linear spaces. (a) The space of all 3×4 matrices (b) The space of all upper triangular 5×5 matrices (c) The space of all diagonal 6×6 matrices
For the following matrices, first find a basis for the column space of the matrix. Then...
For the following matrices, first find a basis for the column space of the matrix. Then use the Gram-Schmidt process to find an orthogonal basis for the column space. Finally, scale the vectors of the orthogonal basis to find an orthonormal basis for the column space. (a) [1 1 1, 1 0 2, 3 1 0, 0 0 4 ] b) [?1 6 6, 3 ?8 3, 1 ?2 6, 1 ?4 ?3 ]
Using Python, create a function whose inputs are two matrices and whose output is the element...
Using Python, create a function whose inputs are two matrices and whose output is the element wise product of the two matrices.
Solve this linear programming (LP) problem using the transportation method. Find the optimal transportation plan and...
Solve this linear programming (LP) problem using the transportation method. Find the optimal transportation plan and the minimum cost. (Leave no cells blank - be certain to enter "0" wherever required. Omit the "$" sign in your response.) Minimize 8x11 + 2x12 + 5x13 + 2x21 + x22 + 3x23 + 7x31 + 2x32 + 6x33 Subject to x11 + x12 + x13 = 90 x21 + x22 + x23 = 105 x31 + x32 + x33 = 105 x11...
The following two questions can be solved by dynamic programming. For each question, please describe optimal...
The following two questions can be solved by dynamic programming. For each question, please describe optimal substructure and express recurrence relation. Give pseudo-code and analyze time and space complexity of your algorithm. 1. Longest palindrome subsequence A palindrome is a nonempty string over some alphabet that reads the same forward and backward. Examples of palindromes are all strings of length 1, “civic”, “racecar”, and “aibohphobia” (fear of palindromes). Design a dynamic programming algorithm to find the longest palindrome that is...
A chain of four matrices A1, A2, A3 and A4, with order 3 X 4, 4...
A chain of four matrices A1, A2, A3 and A4, with order 3 X 4, 4 X 2, 2 X 8 and 8 X 7 respectively. Deduce m[1, 4] to find best possible minimum number of multiplication
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT