Salta al contenuto
Note per Studenti Memoization of Triangular Recurrences Report

Memoization of Triangular Recurrences

In questa pagina 4

A worked exercise on memoization (Dynamic Programming - Paradigm and Memoizationdynamic programming improves on divide and conquer by storing the solutions of repeated subinstances in a table; Fibonacci shows the gap (F_n - 1 additions recursively, n - 1 with a table), memoization is recursion plus a global table, and optimization problems follow a five-step scheme built on optimal substructure and cut-and-paste.Dynamic Programming - Paradigm and Memoization →) with a recurrence defined on the triangle 1≤i≤j≤n1 \le i \le j \le n, like the one of the longest palindromic subsequence (String Alignment and Longest Palindromic Subsequencetwo string problems solved by dynamic programming like the LCS; optimal DNA alignment with score matrix S and gap penalty g uses C(i,j) = max{C(i-1,j-1) + s, C(i-1,j) + g, C(i,j-1) + g} in Theta(mn) time and Theta(min{m,n}) space for the cost alone, while the longest palindromic subsequence works on substrings with p(i,j) = 2 + p(i+1,j-1) if x_i = x_j, else max of the two shorter substrings, scanned by diagonals with n(n-1)/2 comparisons.String Alignment and Longest Palindromic Subsequence →) but with a product instead of a maximum. The goal is twofold: count the operations of memoized code exactly by analyzing the call tree, and see how a property of the recurrence can shrink the subproblem space by an order of magnitude.

The recurrence

Let n>0n > 0. For 1≤i≤j≤n1 \le i \le j \le n:

M(i,j)={1i=j2j=i+1M(i+1,j−1)⋅M(i+1,j)⋅M(i,j−1)j>i+1M(i, j) = \begin{cases} 1 & i = j \\ 2 & j = i + 1 \\ M(i+1, j-1) \cdot M(i+1, j) \cdot M(i, j-1) & j > i + 1 \end{cases}

We want M(1,n)M(1, n) and the exact number T(n)T(n) of multiplications between integers. Cell (i,j)(i, j) depends on three cells of smaller length (the substring has length ℓ=j−i+1\ell = j - i + 1; the dependencies have length ℓ−2\ell - 2, ℓ−1\ell - 1, ℓ−1\ell - 1).

Standard memoized code

The value 00 is an admissible default, because M(i,j)>0M(i, j) > 0 for all i≤ji \le j (a product of positive numbers starting from 11 and 22).

INIT_M(n)
  if n = 1 then return 1               { M(1,1) = 1 }
  if n = 2 then return 2               { M(1,2) = 2 }
  for i <- 1 to n-1 do
    M[i, i] <- 1
    M[i, i+1] <- 2
  M[n, n] <- 1
  for i <- 1 to n-2 do
    for j <- i+2 to n do M[i, j] <- 0   { to be computed }
  return REC_M(1, n)

REC_M(i, j)
  if M[i, j] = 0 then
    M[i, j] <- REC_M(i+1, j-1) * REC_M(i+1, j) * REC_M(i, j-1)
  return M[i, j]

Cost. INIT_M performs no multiplication. In the call tree of REC_M(1, n):

  • the pairs with j≥i+2j \ge i + 2 are internal nodes, each exactly once: all are reached from the root (one gets to (i,j)(i, j) by removing characters from the ends), and later calls find the cell already filled. Each internal node performs two multiplications (three factors);
  • every other call is a leaf with no work (base cases and table reads).

T(n)=∑i=1n−2∑j=i+2n2=2∑i=1n−2(n−i−1)=2∑k=1n−2k=(n−1)(n−2).T(n) = \sum_{i=1}^{n-2} \sum_{j=i+2}^{n} 2 = 2\sum_{i=1}^{n-2} (n - i - 1) = 2 \sum_{k=1}^{n-2} k = (n-1)(n-2).

For fixed ii the number of values of jj is n−(i+2)+1=n−i−1n - (i+2) + 1 = n - i - 1; then k=n−i−1k = n - i - 1 runs from n−2n - 2 to 11. For n=6n = 6: 2020 multiplications, 1010 internal nodes (1,3),(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,5),(3,6),(4,6)(1,3), (1,4), (1,5), (1,6), (2,4), (2,5), (2,6), (3,5), (3,6), (4,6). The algorithm is Θ(n2)\Theta(n^2).

A better algorithm: constant diagonals

Table for n=6n = 6 (row ii, column jj):

j=1j=1 j=2j=2 j=3j=3 j=4j=4 j=5j=5 j=6j=6
i=1i=1 1 2 4 32 4096 536870912
i=2i=2 1 2 4 32 4096
i=3i=3 1 2 4 32
i=4i=4 1 2 4
i=5i=5 1 2
i=6i=6 1

Every diagonal (ℓ=j−i+1\ell = j - i + 1 constant) holds the same value.

Proposition. For every 1≤ℓ≤n1 \le \ell \le n, all entries M(i,j)M(i, j) with j−i+1=ℓj - i + 1 = \ell equal one number V(ℓ)V(\ell), where V(ℓ)={1ℓ=12ℓ=2V(ℓ−1)2⋅V(ℓ−2)ℓ≥3V(\ell) = \begin{cases} 1 & \ell = 1 \\ 2 & \ell = 2 \\ V(\ell - 1)^2 \cdot V(\ell - 2) & \ell \ge 3 \end{cases}

Proof by induction on ℓ\ell. Base: ℓ=1\ell = 1: M(i,i)=1=V(1)M(i, i) = 1 = V(1); ℓ=2\ell = 2: M(i,i+1)=2=V(2)M(i, i+1) = 2 = V(2). Step: let ℓ≥3\ell \ge 3 and assume the claim for smaller diagonals. An entry M(i,j)M(i, j) on diagonal ℓ\ell is the product of M(i+1,j−1)M(i+1, j-1) (diagonal ℓ−2\ell - 2, value V(ℓ−2)V(\ell - 2)) and of M(i+1,j)M(i+1, j) and M(i,j−1)M(i, j-1) (both on diagonal ℓ−1\ell - 1, value V(ℓ−1)V(\ell - 1)). So it equals V(ℓ−1)⋅V(ℓ−1)⋅V(ℓ−2)V(\ell - 1) \cdot V(\ell - 1) \cdot V(\ell - 2), independent of ii. □\square

The answer is M(1,n)=V(n)M(1, n) = V(n): a vector V[1..n]V[1..n] replaces the two-dimensional table.

INIT_V(n)
  if n = 1 then return 1
  if n = 2 then return 2
  for l <- 3 to n do V[l] <- 0
  V[1] <- 1; V[2] <- 2
  return REC_V(n)

REC_V(l)
  if V[l] = 0 then V[l] <- (REC_V(l-1))^2 * REC_V(l-2)
  return V[l]

For n≥3n \ge 3 the tree of REC_V(n) has exactly one internal node for each ℓ\ell with 3≤ℓ≤n3 \le \ell \le n, each with two multiplications (the square and the product by V(ℓ−2)V(\ell - 2)):

T(n)=2 (n−2).T(n) = 2\,(n - 2).

This is linear instead of quadratic: for n=6n = 6, 88 multiplications instead of 2020. Values: V(3)=22⋅1=4V(3) = 2^2 \cdot 1 = 4, V(4)=42⋅2=32V(4) = 4^2 \cdot 2 = 32, V(5)=322⋅4=4096V(5) = 32^2 \cdot 4 = 4096, V(6)=40962⋅32=229=536 870 912V(6) = 4096^2 \cdot 32 = 2^{29} = 536\,870\,912.

Remark on number size. All values are powers of 22: V(ℓ)=2e(ℓ)V(\ell) = 2^{e(\ell)} with e(1)=0e(1) = 0, e(2)=1e(2) = 1, e(ℓ)=2e(ℓ−1)+e(ℓ−2)e(\ell) = 2e(\ell-1) + e(\ell-2), i.e. e=0,1,2,5,12,29,70,…e = 0, 1, 2, 5, 12, 29, 70, \dots (Pell numbers). They grow like (1+2)ℓ(1 + \sqrt 2)^\ell, so unit cost per multiplication is legitimate only in the arithmetic model.

In short. Before setting up a two-index table, look at the values for small nn: regularities such as constant diagonals reduce the subproblem space from Θ(n2)\Theta(n^2) to Θ(n)\Theta(n) and the work from quadratic to linear.

Typical exam questions

  1. What is M(1,5)M(1, 5)? How many multiplications does the standard code perform for n=7n = 7, and the diagonal version? Answer: M(1,5)=V(5)=4096M(1, 5) = V(5) = 4096; (n−1)(n−2)=30(n-1)(n-2) = 30 against 2(n−2)=102(n-2) = 10.
  2. Why count internal nodes of the call tree rather than use a recurrence on operations? Answer: the recurrence counts every expansion; in the memoized tree each non-base subinstance is expanded once and other calls are free table reads.
  3. Prove that the diagonals are constant. Answer: induction on ℓ\ell; for ℓ≥3\ell \ge 3 the cell is the product of one cell on diagonal ℓ−2\ell - 2 and two on diagonal ℓ−1\ell - 1.

Versione ripasso

  • Recurrence. M(i,i)=1M(i,i) = 1, M(i,i+1)=2M(i,i+1) = 2, M(i,j)=M(i+1,j−1)⋅M(i+1,j)⋅M(i,j−1)M(i,j) = M(i+1,j-1) \cdot M(i+1,j) \cdot M(i,j-1) for j>i+1j > i+1, on 1≤i≤j≤n1 \le i \le j \le n. Want M(1,n)M(1,n) and the exact number of multiplications.
  • Standard code. INIT_M sets base cases and the rest to 00 (valid: all values are >0> 0), then calls REC_M(1,n); REC_M(i,j) computes the product of three calls if M[i,j]=0M[i,j] = 0.
  • Cost. One internal node per pair with j≥i+2j \ge i+2, all reached from (1,n)(1,n), two multiplications each:

T(n)=∑i=1n−2∑j=i+2n2=2∑k=1n−2k=(n−1)(n−2).T(n) = \sum_{i=1}^{n-2}\sum_{j=i+2}^{n} 2 = 2\sum_{k=1}^{n-2} k = (n-1)(n-2).

For n=6n = 6: 2020 multiplications, 1010 internal nodes.

  • Constant diagonals. All M(i,j)M(i,j) with j−i+1=ℓj - i + 1 = \ell equal V(ℓ)V(\ell): V(1)=1V(1) = 1, V(2)=2V(2) = 2, V(ℓ)=V(ℓ−1)2⋅V(ℓ−2)V(\ell) = V(\ell-1)^2 \cdot V(\ell-2) (induction on ℓ\ell: one factor on diagonal ℓ−2\ell - 2, two on ℓ−1\ell - 1). Answer M(1,n)=V(n)M(1,n) = V(n).
REC_V(l): if V[l] = 0 then V[l] <- (REC_V(l-1))^2 * REC_V(l-2); return V[l]

One internal node for each ℓ=3..n\ell = 3..n, two multiplications: T(n)=2(n−2)T(n) = 2(n - 2), linear. For n=6n = 6: 88 against 2020.

  • Values. V(3..6)=4,32,4096,229=536 870 912V(3..6) = 4, 32, 4096, 2^{29} = 536\,870\,912; exponents are Pell numbers 0,1,2,5,12,29,…0, 1, 2, 5, 12, 29, \dots, so unit cost per multiplication holds only in the arithmetic model.
  • Lesson. Inspect small cases before building a table: regularities cut the subproblem space from Θ(n2)\Theta(n^2) to Θ(n)\Theta(n).
  • Typical questions: M(1,5)=4096M(1,5) = 4096; for n=7n = 7: 3030 against 1010 multiplications.
  • Typical mistakes: counting all n(n+1)/2n(n+1)/2 cells as internal nodes (base cases do not multiply); one multiplication per internal node instead of two; a sentinel the recurrence can produce; using an operation-count recurrence instead of the call tree.

Teoria collegata