Question
Need java code for the following problem. First, create a MATRIX-CHAIN-MULTIPLICATION class and include the bottom-up approach class methods MATRIX-CHAIN-ORDER and PRINT-OPTIMAL-PARENS. The pseudocode for
Need java code for the following problem.
First, create a MATRIX-CHAIN-MULTIPLICATION class and include the bottom-up approach class methods MATRIX-CHAIN-ORDER and PRINT-OPTIMAL-PARENS. The pseudocode for these two algorithms are given below. Also, you will need a recursive method MATRIX-CHAIN-MULTIPLY, as outlined in Exercise 15.2-2.
Next, add to the class the top-down approaches RECURSIVE-CHAIN-MULTIPY and MEMOIZED-MATRIXCHAIN together with its helper method LOOKUP-CHAIN.
Write a driver program that creates six matrices, A1 through A6 of the dimensions given in the example shown in Figure 15.5. Fill the matrices with random integers on the interval (0, 100).
Use a bottom-up approach to compute the product matrix A which is the result of multiplying all of the six input matrices together. Instrument your code so that you can measure the amount of time taken to compute the product matrix A.
Then use a top-down approach to compute the product matrix A using a recursive approach. Again, instrument your code to record the amount of time taken on the top-down recursive approach.
Finally, use the top-down memoized approach to compute the product matrix A and record the time required. Note the absence of the auxiliary array s in the top-down approach.
When you have written and run all of the code, and observed the running times, write up your observations about the amount of running time and space required. Contemplate the advantages to learning about studying an algorithmic approach and learning how to make programming as efficient as possible.
Exercise 15.2-2: Matrix Chain Multiplication
Give a recursive algorithm MATRIX_CHAIN_MULTIPLY (A,s,i,j) that actually performs the optimal matrix-chain multiplication, given the sequence of matrices A1, A2, , An>, the s table computed by MATRIX_CHAIN_ORDER, and the indices i and j. (The initial call would be MATRIX_CHAIN_MULTIPLY
(A,s,1,n)).
Solution:
MATRIX-CHAIN-ORDER(p) PRINT-OPTIMAL-PARENS(s, ij) n-R.length-1 let m[1..n, 1.n] and s2 new tables for i=1ton fi int "A" i 3 else print "" PRINT-OPTIMAL-PARENS(s, i, sli,J) PRINT-OPTIMAL-PARENS(s, sli, print "? print "(" 4 4 for l-2 to n // , is the | j) 6 fori1 ton-l+1 6 for k = 1 to J-1 12i q 14 return m and s First, create a MATRIX-CHAIN-MULTIPLICATION class and include the bottom-up approach class MATRIX-CHAIN-ORDER(p) PRINT-OPTIMAL-PARENS(s, ij) n-R.length-1 let m[1..n, 1.n] and s2 new tables for i=1ton fi int "A" i 3 else print "" PRINT-OPTIMAL-PARENS(s, i, sli,J) PRINT-OPTIMAL-PARENS(s, sli, print "? print "(" 4 4 for l-2 to n // , is the | j) 6 fori1 ton-l+1 6 for k = 1 to J-1 12i q 14 return m and s First, create a MATRIX-CHAIN-MULTIPLICATION class and include the bottom-up approach classStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started