Guest User

Untitled

a guest
Mar 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. /*
  2. * Compute the prioirty of matrix multiplications
  3. * (in a multiplication chain) that minimizes
  4. * the number of operations needed in total.
  5. * Using dynamic programming (memoize)
  6. */
  7.  
  8. #include <iostream>
  9. #include <vector>
  10. #include <list>
  11.  
  12. using namespace std;
  13. using vint = vector<int>;
  14. using vstr = vector<string>;
  15. using vvchar = vector<vector<char>>;
  16.  
  17. /*
  18. * Binary tree node. Used to represent multiplication priorities
  19. */
  20. struct BNode{
  21. int v;
  22. BNode* l;
  23. BNode* r;
  24. BNode(int val): v(val), l(nullptr), r(nullptr){}
  25. bool isLeaf(){
  26. return l == nullptr && r == nullptr;
  27. }
  28. };
  29.  
  30. /*
  31. * Wrapper struct, contains total number of multiplications and the multiplication priorities
  32. */
  33. struct Result{
  34. long multCount;
  35. BNode* paren;
  36.  
  37. Result(){}
  38. Result(long mc, BNode* p){
  39. multCount = mc;
  40. paren = p;
  41. }
  42. };
  43. using vvres = vector<vector<Result*>>;
  44.  
  45. /*
  46. * Recursive helper function to 'parenthesize(string &s, BNode* n)'
  47. */
  48. string parenthesizeUtil(string &s, BNode* n){
  49. // base case + recurr
  50. string left;
  51. string right;
  52. if(n->l == nullptr){
  53. left = s[n->v];
  54. }else{
  55. left = parenthesizeUtil(s, n->l);
  56. }
  57. if(n->r == nullptr){
  58. right = s[n->v+1];
  59. }else{
  60. right = parenthesizeUtil(s, n->r);
  61. }
  62. if(left.size()>1)
  63. left = '(' + left + ')';
  64. if(right.size()>1)
  65. right = '(' + right + ')';
  66. return left+right;
  67. }
  68.  
  69. /*
  70. * Utility function to build parenthesized/prioritized multiplication string
  71. */
  72. string parenthesize(string &s, BNode* n){
  73. if(n==nullptr) return s;
  74. return parenthesizeUtil(s, n);
  75. }
  76.  
  77. /*
  78. * Recursive memoizing helper function to 'findMinimumParentheses(vint &v)'
  79. * Maintains a table, updates table and fetches when query is present
  80. */
  81. Result findMinimumParenthesesUtil(vint &v, int l, int r, vvres &table){
  82. // memoize
  83. if(table[l][r]!= nullptr) return *(table[l][r]);
  84.  
  85. // base case
  86. if(r-l<2){
  87. auto* res = new Result(0, nullptr);
  88. table[l][r] = res;
  89. return *res;
  90. }
  91. if(r-l==2){
  92. auto* res = new Result(v[l]*v[l+1]*v[l+2], new BNode(l));
  93. table[l][r] = res;
  94. return *res;
  95. }
  96.  
  97. // recurr
  98. auto* resBest = new Result(LONG_MAX, nullptr);
  99. for (int i=l+1; i<=r-1; ++i) {
  100. Result resL = findMinimumParenthesesUtil(v, l, i, table);
  101. Result resR = findMinimumParenthesesUtil(v, i, r, table);
  102. if(resL.multCount + resR.multCount + v[l]*v[i]*v[r] < resBest->multCount){
  103. resBest->multCount = resL.multCount + resR.multCount + v[l]*v[i]*v[r];
  104. resBest->paren = new BNode(i-1);
  105. resBest->paren->l = resL.paren; resBest->paren->r = resR.paren;
  106. }
  107. }
  108.  
  109. // save
  110. table[l][r] = resBest;
  111. return *resBest;
  112. }
  113.  
  114. /*
  115. * Given a vector<int> of matrix dimensions,
  116. * computes the optimal way to performing chain multiplication
  117. * Returns the 'result' containing total number of multiplications
  118. * and the 'way' to multiply represented as a binary tree
  119. */
  120. Result findMinimumParentheses(vint &v){
  121. vvres table(v.size(), vector<Result*>(v.size(), nullptr));
  122. return findMinimumParenthesesUtil(v, 0, v.size()-1, table);
  123. }
  124.  
  125. /*
  126. * Driver function
  127. */
  128. int main(){
  129. // string s = "ABC";
  130. // vint v({10,30,5,60});
  131.  
  132. string s = "AB"; // two matrices
  133. vint v({10, 20, 30}); // A is (10,20), B is (20,30)
  134.  
  135. Result res = findMinimumParentheses(v);
  136. cout << res.multCount << endl;
  137.  
  138. string p = parenthesize(s,res.paren);
  139. cout << p;
  140.  
  141. return 0;
  142. }
Add Comment
Please, Sign In to add comment