Question

Find an optimal parenthesization for matrix-chain multiplications using any PYTHON/java/c++/c for the number {26, 9, 41,...

Find an optimal parenthesization for matrix-chain multiplications using any PYTHON/java/c++/c for the number {26, 9, 41, 18, 13, 22, 28, 32, 25, 26, 30, 37, 19, 47, 11, 24, 20} using a top-down memorized solution. The output must be three lines:

1) the first line contains the optimal number of multiplication

2) the second line contains the optimal parenthesization and

3) the third line is the time required to compute the optimal parenthesization

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include<iostream>
#include <chrono>
#include <ctime>
#include<limits.h>
using namespace std;

void Parenthesis(int , int , int ,int *, char &) ;
void matrixChainMultiplication(int *, int );

int main()
{
int arr[] = {26, 9, 41, 18, 13, 22, 28, 32, 25, 26, 30, 37, 19, 47, 11, 24, 20};
int n = sizeof(arr)/sizeof(arr[0]);
matrixChainMultiplication(arr, n);
return 0;
}

void matrixChainMultiplication(int arr[], int n)
{
int DP[n][n];
int bracket[n][n];
for (int i=1; i<n; i++)
DP[i][i] = 0;
  

for (int L=2; L<n; L++)
{
for (int i=1; i<n-L+1; i++)
{
int j = i+L-1;
DP[i][j] = INT_MAX;
for (int k=i; k<=j-1; k++)
{
int q = DP[i][k] + DP[k+1][j] + arr[i-1]*arr[k]*arr[j];
if (q < DP[i][j])
{
DP[i][j] = q;
bracket[i][j] = k;
}
}
}
}
  
cout<<"Optimal number of multiplication : "<<DP[1][n-1]<<"\n";
char name = 'A';
  
std::chrono::time_point<std::chrono::system_clock> start, end;
   double time;
   std::chrono::duration<double> elapsed_seconds;
  
cout << "Optimal Parenthesization is : ";
start = std::chrono::system_clock::now();
Parenthesis(1, n-1, n, (int *)bracket, name);
end = std::chrono::system_clock::now();
elapsed_seconds = end - start;
time= elapsed_seconds.count();
  
cout<<"\nTime required to compute the optimal parenthesization "<<time<<"\n\n";
  
  
}
  

void Parenthesis(int i, int j, int n,int *bracket, char &name)
{
  
if (i == j)
{
cout << name++;
return;
}
  
cout << "(";

Parenthesis(i, *((bracket+i*n)+j), n,bracket, name);
  
Parenthesis(*((bracket+i*n)+j) + 1, j,n, bracket, name);
cout << ")";
}
//sample output

Add a comment
Know the answer?
Add Answer to:
Find an optimal parenthesization for matrix-chain multiplications using any PYTHON/java/c++/c for the number {26, 9, 41,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Find an optimal parenthesization for matrix-chain multiplications using any language PYTHON/Java/C++ ,C for the number {26, 9, 41, 18, 13, 22, 28, 32, 25, 26, 30, 37, 19, 47, 11, 24, 20} using a strai...

    Find an optimal parenthesization for matrix-chain multiplications using any language PYTHON/Java/C++ ,C for the number {26, 9, 41, 18, 13, 22, 28, 32, 25, 26, 30, 37, 19, 47, 11, 24, 20} using a straight forward-recursive solution. The output must be three lines: 1) the first line contains the optimal number of multiplication 2) the second line contains the optimal parenthesization and 3) the third line is the time required to compute the optimal parenthesization

  • Magic Square question for Python

    You have this solution in Java, I am interested in this same solution for Python.One interesting application of two-dimensional arrays is magic squares. A magic square is a square matrix in which the sum of every row, every column, and bothdiagonals is the same. Magic squares have been studied for many years, and there are some particularly famous magic squares. In this exercise you will write code todetermine whether a square is magic.You should find that the first, second, and...

  • I know how to do number one just not number 2. Please answer number 2 only...

    I know how to do number one just not number 2. Please answer number 2 only Write a java program that will print out following pattern. Use nested for loop. 2. Add a method to the above program that generates takes two numbers as parameters and prints out all the numbers from number 1 to number 2 separated by using a while loop. (Do not print the last ,'). For example: Calling method1(9,51) should print: 9, 10, 11, 12, 13,...

  • Student Name Student Number 18. Describe the center and spread of the data using either the...

    Student Name Student Number 18. Describe the center and spread of the data using either the mean and standard deviation or the five-number summary. Justify your choice by constructing a box-and-whisker plot for the data. Be sure to include the scale. A. 47, 16, 70, 80, 28, 33, 91, 55, 60, 45, 86, 54, 30, 98, 34, 87, 44, 35,64,58, 27,67, 72,68, 31, 95, 37, 41, 97,56, 49, 71, 84, 66, 45, 93 B. 40, 39, 37, 26, 25, 40,...

  • C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with...

    C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with each element representing its column and row value, starting from 1. So the element at the first column and the first row will be 11. If either length is out of the range, simply return a null. For exmaple, if colLength = 5 and rowLength = 4, you will see: 11 12 13 14 15 21 22 23 24 25 31 32 33 34...

  • Write a Java program, In this project, you are going to build a max-heap using array...

    Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...

  • 1 #include <string> 2 #include <iostream> 3 using std::string; 4 using std::cout; 5 using std::endl; 7...

    1 #include <string> 2 #include <iostream> 3 using std::string; 4 using std::cout; 5 using std::endl; 7 class Pet 8 { 9   public: 10       string name; 11       virtual void print( ) const; 12}; 13 14 class Dog:public Pet 15 { 16   public: 17       string breed; 18       virtual void print( ) const; 19}; 20 21 int main( ) 22 { 23   Dog vdog; 24   Pet vpet; 25   vdog.name = "Tiny"; 26   vdog.breed = "Great Dane"; 27   vpet =...

  • Using python Part 3c: Custom Number Range Make a copy of Part B and update it...

    Using python Part 3c: Custom Number Range Make a copy of Part B and update it so that the user can choose to examine a specific range of numbers for prime numbers. Here's a sample running of your program: Start number: 5 End number: -5 Start and end must be positive Start number: 5 End number: 3 End number must be greater than start number Start number: 5 End number: 23 5 7 11 13 17 19 23 Part 3d:...

  • in java by using the loop Write a program that finds all students who score the...

    in java by using the loop Write a program that finds all students who score the highest and lowest average marks of the first two homework in CS (I). Your program should read the data from a file called "hw2.dat" and displays the output to another file called “hw2.out” and on the screen. The first line of the input file contains the number of students, N. The next N lines contain information about the students. Each data line contains student...

  • C# Visual Studio using System; using System.Collections.Generic; using System.Text; 1. Modify the program to output that...

    C# Visual Studio using System; using System.Collections.Generic; using System.Text; 1. Modify the program to output that line N times. That is, output a square of N lines each with N characters. Example output for parameters N=5 and S="*" should appear as follows: 多多本事事 串串串串串 事事事串串 事事事事事 2. Modify the DrawChars method so that the number of the line appears at that position within the line. That is, line i must have N-1 copies of S with the additional character in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT