Advertisement
skb50bd

Matrix Chain Multiplication [Optimal Solution]

Oct 20th, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. // CSE245 LAB-06
  2. // Maxtrix Chain Multiplication - Optimal Solution
  3. // Given a sequence of dimensions of "N" number of matrices, find the least number of multiplication required to obtain the product of these matrices and also find the optimal parenthesization.
  4.  
  5. #include <iostream>
  6. #include <sstream>
  7. #include <string>
  8. #include <iomanip>
  9. #define TAB 7
  10. #define SIZE 100
  11.  
  12. using namespace std;
  13.  
  14. int M[SIZE][SIZE];
  15. int S[SIZE][SIZE];
  16.  
  17. void matrixChainOrder (int P[], int N) {
  18.     for (int i = 1; i <= N; i++)
  19.         M[i][i] = 0;
  20.  
  21.     for (int l = 2; l <= N; l++)
  22.         for (int i = 1; i <= N - l + 1; i++) {
  23.             int j = i + l - 1;
  24.             M[i][j] = INT_MAX;
  25.             for (int k = i; k <= j - 1; k++) {
  26.                 int q = M[i][k] + M[k + 1][j] + (P[i - 1] * P[k] * P[j]);
  27.                 if (q < M[i][j]) {
  28.                     M[i][j] = q;
  29.                     S[i][j] = k;
  30.                 }
  31.             }
  32.         }
  33. }
  34.  
  35.  
  36. void printOptimalParens(int i, int j) {
  37.     if (i == j)
  38.         cout << " A" << i << " ";
  39.     else {
  40.         cout << "(";
  41.         printOptimalParens (i, S[i][j]);
  42.         printOptimalParens (S[i][j] + 1, j);
  43.         cout << ")";
  44.     }
  45. }
  46.  
  47.  
  48. void printTable (int N) {
  49.     cout << "M Table: " << endl;
  50.     for (int i = 0; i < N; i++)
  51.         cout << setw(TAB) << right << i + 1;
  52.     cout << endl;
  53.     for (int i = 0; i < N * TAB + 3; i++)
  54.         cout << "-";
  55.     cout << endl;
  56.     for (int i = 1; i <= N; i++) {
  57.         for (int j = 1; j <= N; j++)
  58.             cout << setw(TAB)  << right << M[i][j];
  59.         cout << "  | " << setw(TAB)  << left << i;
  60.         cout << endl;
  61.     }
  62.     cout << endl << endl;
  63.  
  64.     cout << "S Table: " << endl;
  65.     for (int i = 2; i <= N; i++)
  66.         cout << setw(TAB) << right << i;
  67.     cout << endl;
  68.     for (int i = 0; i < N * TAB - 4; i++)
  69.         cout << "-";
  70.     cout << endl;
  71.     for (int i = 1; i < N; i++) {
  72.         for (int j = 2; j <= N; j++)
  73.             cout << setw(TAB) << right << S[i][j];
  74.         cout << "  | " << setw(TAB) << left << i;
  75.         cout << endl;
  76.     }
  77.     cout << endl << endl;
  78. }
  79.  
  80.  
  81.  
  82. int main() {
  83.     string line;
  84.     cout << "Enter the P Values: ";
  85.     getline(cin, line);
  86.  
  87.     istringstream iss(line);
  88.     int N = -1;
  89.     int P[SIZE];
  90.     while (iss >> line)
  91.         stringstream(line) >> P[++N];
  92.  
  93.     matrixChainOrder(P, N);
  94.     cout << "Optiimal Parenthesis: ";
  95.     printOptimalParens(1, N);
  96.     cout << endl << endl;
  97.  
  98.     printTable(N);
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement