READ CAREFULLY AND CODE IN C++
Dynamic Programming: Matrix Chain Multiplication Description In this assignment you are asked to implement a dynamic programming algorithm: matrix chain multiplication (chapter 15.2), where the goal is to find the most computationally efficient matrix order when multiplying an arbitrary number of matrices in a row. You can assume that the entire input will be given as integers that can be stored using the standard C++ int type and that matrix sizes will be at least 1. Input The input has the following format. The first number, n ≥ 1, in the test case will tell you how many matrices are in the sequence. The first number will be then followed by n + 1 numbers indicating the size of the dimensions of the matrices. Recall that the given information is enough to fully specify the dimensions of the matrices to be multiplied. Output First, you need to output the minimum number of scalar multiplications needed to multiply the given matrices. Then, print the matrix multiplication sequence, via parentheses, that minimizes the total number of number multiplications and the . for multiplication symbol. Each matrix should be named A#, where # is the matrix number starting at 0 (zero) and ending at n − 1. See the examples below. Examples of input and output 2 2 3 5 30 (A0.A1) 3 10 100 5 50 7500 ((A0.A1).A2) 3 10 30 5 60 4500 ((A0.A1).A2) 6 30 35 15 5 10 20 25 15125 ((A0.(A1.A2)).((A3.A4).A5))
READ CAREFULLY AND CODE IN C++
READ CAREFULLY AND CODE IN C++
If you have any doubts, please give me comment...
#include <iostream>
#include <climits>
using namespace std;
void printParenthesis(int i, int j, int n, int *brac, int &name)
{
if (i == j)
{
cout << "A" << name++;
return;
}
cout << "(";
printParenthesis(i, *((brac + i * n) + j), n, brac, name);
cout<<".";
printParenthesis(*((brac + i * n) + j) + 1, j, n, brac, name);
cout << ")";
}
void matrixChainOrder(int arr[], int n)
{
int arr1[n][n];
int brac[n][n];
for (int i = 1; i < n; i++)
arr1[i][i] = 0;
for (int L = 2; L < n; L++)
{
for (int i = 1; i < n - L + 1; i++)
{
int j = i + L - 1;
arr1[i][j] = INT_MAX;
for (int k = i; k <= j - 1; k++)
{
int q = arr1[i][k] + arr1[k + 1][j] + arr[i - 1] * arr[k] * arr[j];
if (q < arr1[i][j])
{
arr1[i][j] = q;
brac[i][j] = k;
}
}
}
}
int name = 0;
cout << arr1[1][n - 1] << endl;
printParenthesis(1, n - 1, n, (int *)brac, name);
cout << endl;
}
int main()
{
int n, i;
cin >> n;
int *arr = new int[n + 1];
for (i = 0; i <= n; i++)
{
cin >> arr[i];
}
matrixChainOrder(arr, n + 1);
return 0;
}
READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you...
DO PART ii THE PSEUDO CODE PART 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. Can you apply dynamic programming for this versin? Argument your answer (10 points). Write the pseudocode for this new algorithm (10 points)
Use the dynamic programming technique to find an optimal parenthesization of a matrix-chain product whose sequence of dimensions is <5, 8, 4, 10, 7, 50, 6>. Matrix Dimension A1 5*8 A2 8*4 A3 4*10 A4 10*7 A5 7*50 A6 50*6 You may do this either by implementing the MATRIX-CHAIN-ORDER algorithm in the text or by simulating the algorithm by hand. In either case, show the dynamic programming tables at the end of the computation. Using Floyd’s algorithm (See Dynamic Programming...
Use the dynamic programming technique to find an optimal parenthesization of a matrix-chain product whose sequence of dimensions is <5, 8, 40, 10, 20, 6>. Matrix Dimension A1 5 * 8 A2 8*40 A3 40*10 A4 10*20 A5 20*6
10×8,8×6,6×15,15×12 4. [15] Dynamic Programming. We are given a set of matrices Ap.A1, A2. .. .An-1. which we must multiply in this order. We let (d, dira) be the dimension of matrix A. The minimal number Nij of operations required to multiply matrices (A, Ati .. A) is defined by: a. Explain this formula. Apply this formula to compute the optimal parenthetization of the product of matrices Ao-A1,Az,A3, where the dimensions of these matrices are, respectively: 6x15, and 15x12. b....
Consider the following matrices for the matrix-chain multiplication problem: A1: 30 × 5 A2: 5 × 40 A3: 40 × 10 A4: 10 × 25 A5: 25 × 20 Compute the values of M[i, j], 1 ≤ i ≤ j ≤ 5 and s[i, j], 1 ≤ i < j ≤ 5. Show the optimal factorization found.
10×8,8×6,6×15,15×12 4. [15] Dynamic Programming. We are given a set of matrices Ap.A1, A2. .. .An-1. which we must multiply in this order. We let (d, dira) be the dimension of matrix A. The minimal number Nij of operations required to multiply matrices (A, Ati .. A) is defined by: a. Explain this formula. Apply this formula to compute the optimal parenthetization of the product of matrices Ao-A1,Az,A3, where the dimensions of these matrices are, respectively: 6x15, and 15x12. b....
Please help me out with this assignment. Please read the requirements carefully. And in the main function please cout the matrix done by different operations! Thanks a lot! For this homework exercise you will be exploring the implementation of matrix multiplication using C++ There are third party libraries that provide matrix multiplication, but for this homework you will be implementing your own class object and overloading some C+ operators to achieve matrix multiplication. 1. 10pts] Create a custom class called...
** Use Java programming language Program the following algorithms in JAVA: A. Classical matrix multiplication B. Divide-and-conquer matrix multiplication In order to obtain more accurate results, the algorithms should be tested with the same matrices of different sizes many times. The total time spent is then divided by the number of times the algorithm is performed to obtain the time taken to solve the given instance. For example, you randomly generate 1000 sets of input data for size_of_n=16. For each...
Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...
5. Dynamic Programming (a) Given a set of four matrices for the following dimensions: We need to compute Al* A2 A3 A4 Al=2X3; A2=3X5; A3=5X2: A4=2X4 Find the order in which the matrix pairs should be multiplied to produce the optimum number of operations. Show all your steps (10) (b) For the problems given below, determine whether it is more efficient to use a divide and conquer strategy or a dynamic programming strategy. Give the reasons for your choice (5*3=15)...