Advertisement
skb50bd

Matrix Chain Multiplication [Parenthesization Evaluation]

Oct 25th, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. // Matrix Chain Multiplication, Parenthesization Evaluation
  2. // Given the dimensions of "N" number of matrices and the Parenthesization (Multiplication Procedure), have to find the number of multiplications operations required for these "N" matrices using the given procedure.
  3. /* Sample Input: 10 20 30 40 50
  4.                 ( 1 ( 2 3 ) 4 ) [Spaces are mandatory]
  5. */
  6.  
  7.  
  8. #include <iostream>
  9. #include <string>
  10. #include <sstream>
  11. #define SIZE 100
  12.  
  13. using namespace std;
  14.  
  15. struct matrix {
  16.     int row;
  17.     int col;
  18. };
  19.  
  20.  
  21. class stack {
  22. private:
  23.     struct matrix M;
  24.     class stack *next;
  25. public:
  26.     stack(): next(NULL) {}
  27.     ~stack() {}
  28.  
  29.     class stack *push (struct matrix m) {
  30.         class stack *temp = new stack;
  31.         temp -> M = m;
  32.         temp -> next = this;
  33.         return temp;
  34.     }
  35.  
  36.     class stack *pop() {
  37.         if (this != NULL) {
  38.             class stack *temp = this -> next;
  39.             delete this;
  40.             return temp;
  41.         }
  42.         else
  43.             return this;
  44.     }
  45.  
  46.     struct matrix top() {
  47.         if (this != NULL)
  48.             return this -> M;
  49.         else {
  50.             struct matrix X;
  51.             X.row = 0;
  52.             X.col = 0;
  53.             return X;
  54.         }
  55.     }
  56. };
  57.  
  58. int main() {
  59.     string line;
  60.     cout << "Enter the P Values: ";
  61.     getline(cin, line);
  62.  
  63.     istringstream Pv(line);
  64.     int N = -1;
  65.     int P[SIZE];
  66.     while (Pv >> line)
  67.         stringstream(line) >> P[++N];
  68.  
  69.    
  70.     struct matrix M[SIZE];
  71.     for (int i = 0; i < N; i++) {
  72.         M[i].row = P[i];
  73.         M[i].col = P[i + 1];
  74.     }
  75.  
  76.     cout << "Enter the Expression: ";
  77.     getline(cin, line);
  78.     istringstream Exp(line);
  79.  
  80.     struct matrix a, b, c;
  81.     class stack *S = NULL;
  82.  
  83.     int x, sum = 0;
  84.     while (Exp >> line) {
  85.         if (line == ")") {
  86.             a = S -> top();
  87.             S = S -> pop();
  88.             b = S -> top();
  89.             S = S -> pop();
  90.             sum += b.row * a.row * a.col;
  91.             c.row = b.row;
  92.             c.col = a.col;
  93.             S = S -> push(c);
  94.         }
  95.         else if (line != "(") {
  96.             stringstream(line) >> x;
  97.             S = S -> push (M[x - 1]);
  98.         }
  99.     }
  100.  
  101.     cout << "Number of Multiplication Required: " << sum << endl;
  102.  
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement