C++ Program to Perform Optimal Parenthesize using Dynamic Programming

This is a C++ Program to perform optimal paranthesization using DP.

Here is source code of the C++ Program to Perform Optimal Paranthesization Using Dynamic Programming. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<limits.h>
  3. #include<iostream>
  4.  
  5. using namespace std;
  6.  
  7. // Matrix Ai has dimension p[i-1] x p[i] for i = 1..n
  8.  
  9. int MatrixChainOrder(int p[], int n)
  10. {
  11.     /* For simplicity of the program, one extra row and one extra column are
  12.      allocated in m[][].  0th row and 0th column of m[][] are not used */
  13.     int m[n][n];
  14.     int s[n][n];
  15.     int i, j, k, L, q;
  16.  
  17.     /* m[i,j] = Minimum number of scalar multiplications needed to compute
  18.      the matrix A[i]A[i+1]...A[j] = A[i..j] where dimention of A[i] is
  19.      p[i-1] x p[i] */
  20.  
  21.     // cost is zero when multiplying one matrix.
  22.     for (i = 1; i < n; i++)
  23.         m[i][i] = 0;
  24.  
  25.     // L is chain length.
  26.     for (L = 2; L < n; L++)
  27.     {
  28.         for (i = 1; i <= n - L + 1; i++)
  29.         {
  30.             j = i + L - 1;
  31.             m[i][j] = INT_MAX;
  32.             for (k = i; k <= j - 1; k++)
  33.             {
  34.                 // q = cost/scalar multiplications
  35.                 q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
  36.                 if (q < m[i][j])
  37.                 {
  38.                     m[i][j] = q;
  39.                     s[i][j] = k;
  40.                 }
  41.             }
  42.         }
  43.     }
  44.  
  45.     return m[1][n - 1];
  46. }
  47. int main()
  48. {
  49.     cout
  50.             << "Enter the array p[], which represents the chain of matrices such that the ith matrix Ai is of dimension p[i-1] x p[i]";
  51.     cout << "Enter the total length:";
  52.     int n;
  53.     cin >> n;
  54.     int array[n];
  55.     cout << "Enter the dimensions: ";
  56.     for (int var = 0; var < n; ++var)
  57.     {
  58.         cin >> array[var];
  59.     }
  60.     cout << "Minimum number of multiplications is: " << MatrixChainOrder(array,
  61.             n);
  62.     return 0;
  63. }

Output:

$ g++ OptimalParanthesizationDP.cpp
$ a.out
 
Enter the array p[], which represents the chain of matrices such that the ith matrix Ai is of dimension p[i-1] x p[i]Enter the total length:4
Enter the dimensions: 2 4 3 5
Minimum number of multiplications is: 54

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement

Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.