scratch

§ LCS DP: The Speedup Is from Filtration

created 2021-10-31 · last edited 2022-05-30
  • I feel like I finally see where the power of dynamic programming lies.
  • Consider the longest common subsequence problem over arrays AAA, BBB of size nnn, mmm.
  • Naively, we have 2n×2m2^n \times 2^m2n×2m pairs of subsequences and we need to process each of them.
  • How does the LCS DP solution manage to solve this in O(nm)O(nm)O(nm)?
  • Key idea 1: create "filtration" of the problem Fi,j⊆2n×2mF_{i, j} \subseteq 2^n\times2^mFi,j​⊆2n×2m. At step (i,j)(i, j)(i,j), consider the "filter" Fi,jF_{i, j}Fi,j​containing all pairs of subsequences (s∈2n,t∈2m)(s \in 2^n, t \in 2^m)(s∈2n,t∈2m) where maxix(s) \leq i and maxix(t) \leq j.
  • These filters of the filtration nest into one another, so Fi,j⊆Fi′,j′F_{i, j} \subseteq F_{i', j'}Fi,j​⊆Fi′,j′​ iff i≤i′i \leq i'i≤i′ and j≤j′j \leq j'j≤j′.
  • Key idea 2: The value of max LCS(filter) is (a) monotonic, and (b) can be computed efficiently from the values of lower filtration. So we have a monotone map from the space of filters to the solution space, and this monotone map is efficiently computable, given the values of filters below this in the filtration.
  • This gives us a recurrence, where we start from the bottom filter and proceed to build upward.
  • See that this really has nothing to do with recursion. It has to do with problem decomposition . We decompose the space 2n×2m2^n \times 2^m2n×2mcleverly via filtration Fi,jF_{i, j}Fi,j​ such that max LCS(F[i, j]) was efficiently computable.
  • To find a DP, think of the entire state space, then think of filtrations, such that the solution function becomes a monotone map, and the solution function is effeciently computable given the values of filters below it.
❦
Newer ৪ Blog ৪ Older