Fastrail08

(V.IMPORTANT) 2 types of interval DPs(MCM vs BB)

Jul 27th, 2025
3
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. **Matrix Chain Multiplication (MCM) vs Burst Balloons (BB) β€” Full Annotated Guide**
  2.  
  3. ---
  4.  
  5. ## πŸ” Problem Setup:
  6.  
  7. ### Matrix Chain Multiplication (MCM)
  8.  
  9. Given an array `arr` of size `n`, where:
  10.  
  11. * Matrix A\_i has dimension `arr[i-1] x arr[i]`
  12. * Goal: Find the **minimum cost** to multiply matrices A1 \* A2 \* ... \* An
  13.  
  14. ### Burst Balloons (BB)
  15.  
  16. Given an array `nums` representing balloon values:
  17.  
  18. * Bursting balloon at index `k` gives coins: `nums[i-1] * nums[k] * nums[j+1]`
  19. * Goal: Find the **maximum coins** you can collect by bursting balloons in optimal order
  20.  
  21. ---
  22.  
  23. ## 1️⃣ Inclusive Interval in MCM
  24.  
  25. We define `solve(i, j)` to mean multiplying matrices A\_i to A\_j **inclusive**.
  26.  
  27. ### Example:
  28.  
  29. Let `arr = [10, 20, 30, 40]`:
  30.  
  31. * A1: 10x20
  32. * A2: 20x30
  33. * A3: 30x40
  34.  
  35. To compute A1*A2*A3, we call `solve(1, 3)`
  36.  
  37. ### Recurrence:
  38.  
  39. ```cpp
  40. solve(i, j) = min over k in [i, j-1] of:
  41. solve(i, k) + solve(k+1, j) + arr[i-1] * arr[k] * arr[j];
  42. ```
  43.  
  44. πŸ“Š Visual Representation:
  45.  
  46. ```
  47. arr: [10 20 30 40]
  48. |----|----|----|
  49. A1 A2 A3
  50.  
  51. i = 1, j = 3 (we're multiplying A1 to A3)
  52.  
  53. k = 1:
  54. β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  55. β”‚ A1 β”‚ x β”‚ (A2 x A3) β”‚
  56. β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  57.  
  58. k = 2:
  59. β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  60. β”‚ (A1 x A2) β”‚ x β”‚ A3 β”‚
  61. β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  62. ```
  63.  
  64. βœ… Why `k` is a Split Point in MCM
  65.  
  66. * `k` divides the chain into **two separate multiplication subproblems**
  67. * You **solve each side recursively**, then multiply the resulting matrices
  68. * `k` is **not** a matrix, it’s a **cut** between matrices
  69.  
  70. ---
  71.  
  72. ## 2️⃣ Exclusive Interval in Burst Balloons
  73.  
  74. We define `solve(i, j)` to mean bursting all balloons in the **exclusive range (i, j)**.
  75.  
  76. ### Why Exclusive?
  77.  
  78. * Balloons at index `i` and `j` are used as the boundaries to compute coins when `k` is burst.
  79. * So we must **leave them unburst** until we process a subinterval between them.
  80. * This allows us to always safely say: `coins = nums[i] * nums[k] * nums[j]` because `i` and `j` are still intact.
  81.  
  82. ### Recurrence:
  83.  
  84. ```cpp
  85. solve(i, j) = max over k in (i+1, j-1) of:
  86. nums[i] * nums[k] * nums[j] + solve(i, k) + solve(k, j);
  87. ```
  88.  
  89. πŸ“Š Visual Representation:
  90.  
  91. ```
  92. nums: [1 3 1 5 8 1]
  93. ^ ^
  94. i j
  95. solve(i, j) => burst between i and j (exclusive)
  96.  
  97. k = i+1 to j-1
  98. β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  99. β”‚ solve(i,k) β”‚ + β”‚ solve(k,j) β”‚ + nums[i]*nums[k]*nums[j]
  100. β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  101. ```
  102.  
  103. βœ… Why `k` is the Last to Burst in BB
  104.  
  105. * We assume `k` is the last balloon to be popped in range `(i, j)`
  106. * This means the surroundings (`nums[i]` and `nums[j]`) are still unburst when `k` bursts
  107. * That's why `k` is **not a divider** but a **last choice**
  108.  
  109. ---
  110.  
  111. ## πŸ”„ Inclusive vs Exclusive β€” Summary Table
  112.  
  113. | Feature | MCM | Burst Balloons |
  114. | ----------------------- | ------------------------------------ | ------------------------------------- |
  115. | Interval Type | Inclusive `[i, j]` | Exclusive `(i, j)` |
  116. | Meaning of `solve(i,j)` | Multiply matrices A\_i to A\_j | Burst all balloons between i and j |
  117. | Role of `k` | Divider (split point) | Last balloon to burst |
  118. | Purpose of Split | Divide chain into two subchains | Defer k until subproblems are solved |
  119. | Cost Formula | arr\[i-1] \* arr\[k] \* arr\[j] | nums\[i] \* nums\[k] \* nums\[j] |
  120. | Recursive Merge | Combine results of left Γ— right | Add result of left + right + burst(k) |
  121. | Boundary Behavior | No artificial padding usually needed | Padding with 1s at both ends often |
  122.  
  123. ---
  124.  
  125. ## 🧠 Final Takeaways
  126.  
  127. ### For MCM:
  128.  
  129. * In **MCM**, `solve(i, j)` = cost to multiply full chain A\_i to A\_j
  130. * Use **inclusive intervals**
  131. * `k` is a **divider** in `[i, j-1]`
  132. * Recursively solve left and right, then combine with cost of multiplication
  133.  
  134. ### For Burst Balloons:
  135.  
  136. * In **BB**, `solve(i, j)` = max coins by bursting balloons **between** i and j
  137. * Use **exclusive intervals**
  138. * `k` is the **last to burst** in `(i, j)`
  139. * Coins are calculated after solving left and right, then bursting `k`
  140.  
  141. ---
  142.  
  143. Let me know if you'd like this merged content exported as an updated PDF!
  144.  
Advertisement
Add Comment
Please, Sign In to add comment