8.5k views
16 votes
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).)

1 Answer

6 votes

Answer:

Follows are the code to this question:

MATRIX_CHAIN_MULTIPLY(A,s,i,j)//defining a method MATRIX_CHAIN_MULTIPLY that accepts 4 parameters

if(i == j)//use if block to check i and j value is equal

return A[i]//return array values

if(j == i+1)//use another if block to check j value is equal to incrementing value of i

return A[i]*A[j];//return multiplication of array

else//defining else block

x1 = MATRIX_CHAIN_MULTIPLY(A,s,i,S[i,j])//x1 variable to call above method

x2 = MATRIX_CHAIN_MULTIPLY(A,s,S[i,j]+1,j)//x2 variable to call above method

return B1*B2//return multiplication of method holding value

Step-by-step explanation:

Please find the attached file of the complete question:

In the code, a method "MATRIX_CHAIN_MULTIPLY" is declared, which accepts parameters and uses a conditional statement to check value, which can be defined as follows:

  • In the first, if block, it checks the variable "i and j" value that is equal if it is true it will return an array.
  • In the second if it checks variable j value is equal to incrementing the value of i, if this is true it will return multiplication of array.
  • In the else block two-variable "x1, x2" is used that calls the above method and returns its multiplicating value.
Give a recursive algorithm MATRIX-CHAIN-MULTIPLY (A, s, i, j) that actually performs-example-1
User Stepan Grigoryan
by
3.1k points