Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. Loop Invariants:
  2. Precondition(s): m and s have been defined
  3. Postcondition(s): m[i,j] gives the cost of the optimal parenthisization of p[i...j] and
  4. s[i...j] contains the value k at which we split p[i...j] in an optimal parenthisization
  5.  
  6. Recursive(p):
  7. 1 n = p.length-1
  8. 2 let m[1...n,1...n] and s[1...n-1,2...n] be new tables
  9. 3 Recursive-Matrix-Chain(p,1,n)
  10. 4 return m and sPrecondition(s): none
  11.  
  12. Recursive-Matrix-Chain(p,i,j)
  13. 1 if i == j
  14. 2 return 0
  15. 3 m[i,j] = infinity
  16. 4 for k = i to j-1
  17. 5 q = Recursive-Matrix-Chain(p,i,k)
  18. + Recursive-Matrix-Chain(p,k+1,j)
  19. + p[i-1]p[k]p[j]
  20. 6 if q < m[i,j]
  21. 7 m[i,j] = q
  22. 8 s[i,j] = k
  23. 9 return m[i,j]
  24.  
  25. Loop Invariant:
  26. At the start of each iteration of the for loop on lines 4-8, m[i,j] contains the cost of the optimal parenthesization of p[i...k]
  27. Initialization: Before the first iteration of the loop, when k=i, m[i,j] trivially contains the optimal parenthisization of p[i...i], since it has none, and m[i,j] = infinity
  28.  
  29. Maintenance:
  30. During each iteration of the loop, q = the sum of the cost of the optimal parenthisization of p[i...k] and p[k+1...j] + p[i-1]*p[k]*p[j], which is the cost of the parenthization separated at k. If this value is less than the previous value of m[i,j], m[i,j] is set to q, making m[i,j] the cost of the optimal parenthesization of p[i...k+1]. when k is incremented, the loop invariant is satisfied.
  31.  
  32. Termination:
  33. After the final iteration of the loop, m[i,j] contains the cost of the optimal parenthesization of p[i...j]
  34. Matrix-Chain-Order(p)
  35. 1 n = p.length-1
  36. 2 let m[1...n,1...n] and s[1...n-1,2...n] be new tables
  37. 3 for i = 1 to n
  38. 4 m[i,i] = 0
  39. 5 for l = 2 to n
  40. 6 for i = l to n-l+1
  41. 7 j = i+l-1
  42. 8 m[i,j] = infinity
  43. 9 for k = i to j-1
  44. 10 q = m[i,j] + m[k+1,j] + p_i-1 * p_k * p_j
  45. 11 if q < m[i,j]
  46. 12 m[i,j] = q
  47. 13 s[i,j] = k
  48. 14 return m and s
  49.  
  50. Loop Invariant: At the start of each iteration of the for loop on lines 5-13,
  51. m[1..l-1][1..l-1] contain the correct minimum costs of the parenthesization
  52.  
  53.  
  54. Initialization:
  55. Before the start of the first loop, l==2.
  56. Plugging into the loop invariant, we get m[1..1][1..1] contain the correct minimum
  57. That range was initialized to 0 in the for loop on 3-4. Since m[1][1] does not contain a matrix, 0 is correct
  58. Maintenance:
  59. Within each iteration of the loop on lines 6-13, the loop on lines 9-13
  60. set the correct cost of m[i][j] for m[l..n-l+1][n+l-1]. L is then increased by 1, knowing that m[1..l-1][1..l-1] is maintained.
  61.  
  62. Termination:
  63. At the end of the for loop, l = n. Plugging into the loop invariant, we get m[1..n-1][1..n-1] contains the correct minium costs. The minimum cost for the whole input and the output of the algorithm is located in m[1][n-1]. Therefore this algorithm is correct.Precondition(s): none
  64. Postcondition(s): m gives the cost of the optimal parenthisization of p and s contains the value of k at which we split p in an optimal parenthisization
  65.  
  66.  
  67. Memoized-Matrix-Chain(p)
  68. 1 n = p.length - 1
  69. 2 let m[1...n, 1...n] be a new table
  70. 3 for i = 1 to n
  71. 4 for j = i to n
  72. 5 n[i,j] = infinity
  73. 6 return Lookup-Chain(m,p,l,n)
  74.  
  75.  
  76. Precondition(s): m and s have been defined
  77. Postcondition(s): m[i,j] gives the cost of the optimal parenthisization of p[i...j] and
  78. s[i...j] contains the value k at which we split p[i...j] in an optimal parenthisization
  79.  
  80. Lookup-Chain(m,p,i,j)
  81. 1 if m[i,j] < infinity
  82. 2 return m[i,j]
  83. 3 if i == j
  84. 4 m[i,j] = 0
  85. 5 else for k = i to j-1
  86. 6 q = Lookup-Chain(m,p,i,k)
  87. + Lookup-Chain(m,p,k+1,j) + p_i-1 * p_k * p_j
  88. 7 if q < m[i,j]
  89. 8 m[i,j] = q
  90. 9 return m[i,j]
  91.  
  92. Loop Invariant:
  93. At the start of each iteration of the for loop on lines 5-8, m[i,j] contains the cost of the optimal parenthesization of p[i...k]
  94. Initialization: Before the first iteration of the loop, when k=i, m[i,j] trivially contains the optimal parenthisization of p[i...i], since it has none, and m[i,j] = infinity
  95.  
  96. Maintenance:
  97. During each iteration of the loop, q = the sum of the cost of the optimal parenthisization of p[i...k] and p[k+1...j] + p[i-1]*p[k]*p[j], which is the cost of the parenthization separated at k. If this value is less than the previous value of m[i,j], m[i,j] is set to q, making m[i,j] the cost of the optimal parenthesization of p[i...k+1]. when k is incremented, the loop invariant is satisfied.
  98.  
  99. Termination:
  100. After the final iteration of the loop, m[i,j] contains the cost of the optimal parenthesization of p[i...j]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement