adplus-dvertising
frame-decoration

Question

Consider the following dynamic programming implementation of the matrix chain problem.
Which of the following lines should be inserted to complete the below code?
#include<stdio.h>
#include<limits.h>
int mat_chain_multiplication(int *mat, int n)
{
      int arr[n][n];
      int i,k,row,col,len;
      for(i=1;i<n;i++)
          arr[i][i] = 0;
      for(len = 2; len < n; len++)
      {
           for(row = 1; row <= n - len + 1; row++)
           {
               col = row + len - 1;
               arr[row][col] = INT_MAX;
               for(k = row; k <= col - 1; k++)
               {
                    int tmp = ________________________;
                    if(tmp < arr[row][col])
                    arr[row][col] = tmp;
               }
           }
      }
      return arr[1][n - 1];
}
int main()
{
     int mat[6] = {20,5,30,10,40};
     int ans = mat_chain_multiplication(mat,5);
     printf("%d",ans);
     return 0;
}

a.

arr[row][k] – arr[k + 1][col] + mat[row – 1] * mat[k] * mat[col];

b.

arr[row][k] + arr[k + 1][col] – mat[row – 1] * mat[k] * mat[col];

c.

arr[row][k] + arr[k + 1][col] + mat[row – 1] * mat[k] * mat[col];

d.

arr[row][k] – arr[k + 1][col] – mat[row – 1] * mat[k] * mat[col];

Answer: (c).arr[row][k] + arr[k + 1][col] + mat[row – 1] * mat[k] * mat[col];

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. Consider the following dynamic programming implementation of the matrix chain problem. Which of the following lines should be inserted to complete the below code?

Similar Questions

Discover Related MCQs

Q. Which of the following methods can be used to solve the longest common subsequence problem?

Q. Consider the strings “PQRSTPQRS” and “PRATPBRQRPS”. What is the length of the longest common subsequence?

Q. Which of the following problems can be solved using the longest subsequence problem?

Q. Longest common subsequence is an example of ____________

Q. What is the time complexity of the brute force algorithm used to find the longest common subsequence?

Q. Which of the following is the longest common subsequence between the strings “hbcfgmnapq” and “cbhgrsfnmq” ?

Q. Which of the following methods can be used to solve the longest palindromic subsequence problem?

Q. Which of the following strings is a palindromic subsequence of the string “ababcdabba”?

Q. For which of the following, the length of the string is equal to the length of the longest palindromic subsequence?

Q. What is the length of the longest palindromic subsequence for the string “ababcdabba”?

Q. What is the time complexity of the brute force algorithm used to find the length of the longest palindromic subsequence?

Q. For every non-empty string, the length of the longest palindromic subsequence is at least one.

Q. Longest palindromic subsequence is an example of ______________

Q. Which of the following methods can be used to solve the edit distance problem?

Q. The edit distance satisfies the axioms of a metric when the costs are non-negative.

Q. Which of the following is an application of the edit distance problem?

Q. In which of the following cases will the edit distance between two strings be zero?

Q. Suppose each edit (insert, delete, replace) has a cost of one. Then, the maximum edit distance cost between the two strings is equal to the length of the larger string.

Q. Consider the strings “monday” and “tuesday”. What is the edit distance between the two strings?

Q. Consider the two strings “”(empty string) and “abcd”. What is the edit distance between the two strings?