



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Ninety percent of science fiction is crud. But then, ninety percent of everything is crud, and itās the ten percent that isnāt crud that is important. ā [Theodore] Sturgeonās Law (1953)
Dynamic programming is a powerful technique for efficiently solving recursive problems, but itās hardly the end of the story. In many cases, once we have a basic dynamic programming algorithm in place, we can make further improvements to bring down the running time or the space usage. We saw one example in the Fibonacci number algorithm. Buried inside the naĆÆve iterative Fibonacci algorithm is a recursive problemācomputing a power of a matrixāthat can be solved more efficiently by dynamic programming techniquesāin this case, repeated squaring.
Just as we did for the Fibonacci recurrence, we can reduce the space complexity of our edit distance algorithm from O ( mn ) to O ( m + n ) by only storing the current and previous rows of the memoization table. This āsliding windowā technique provides an easy space improvement for most (but not all) dynamic programming algorithm. Unfortunately, this technique seems to be useful only if we are interested in the cost of the optimal edit sequence, not if we want the optimal edit sequence itself. By throwing away most of the table, we apparently lose the ability to walk backward through the table to recover the optimal sequence. However, if we throw away most of the rows in the table, it seems we no longer have enough information to reconstruct the actual editing sequence. Now what? Fortunately for memory-misers, in 1975 Dan Hirshberg discovered a simple divide-and-conquer strategy that allows us to compute the optimal edit sequence in O ( mn ) time, using just O ( m + n ) space. The trick is to record not just the edit distance for each pair of prefixes, but also a single position in the middle of the editing sequence for that prefix. Specifically, the optimal editing sequence that transforms A [ 1 .. m ] into B [ 1 .. n ] can be split into two smaller editing sequences, one transforming A [ 1 .. m/ 2 ] into B [ 1 .. h ] for some integer h , the other transforming A [ m/ 2 + 1 .. m ] into B [ h + 1 .. n ]. To compute this breakpoint h , we define a second function Half ( i , j ) as follows:
Half ( i , j ) =
ā if i < m/ 2 j if i = m/ 2 Half ( i ā 1, j ) if i > m/ 2 and Edit ( i , j ) = Edit ( i ā 1, j ) + 1 Half ( i , j ā 1 ) if i > m/ 2 and Edit ( i , j ) = Edit ( i , j ā 1 ) + 1 Half ( i ā 1, j ā 1 ) otherwise
A simple inductive argument implies that Half ( m , n ) is the correct value of h. We can easily modify our earlier algorithm so that it computes Half ( m , n ) at the same time as the edit distance Edit ( m , n ), all in O ( mn ) time, using only O ( m ) space. Now, to compute the optimal editing sequence that transforms A into B , we recursively compute the optimal subsequences. The recursion bottoms out when one string has only constant length, in which case we can determine the optimal editing sequence by our old dynamic programming algorithm.
Overall the running time of our recursive algorithm satisfies the following recurrence:
T ( m , n ) =
O ( n ) if m ⤠1 O ( m ) if n ⤠1 O ( mn ) + T ( m/ 2, h ) + T ( m/ 2, n ā h ) otherwise
Itās easy to prove inductively that T ( m , n ) = O ( mn ), no matter what the value of h is. Specifically, the entire algorithmās running time is at most twice the time for the initial dynamic programming phase.
T ( m , n ) ⤠αmn + T ( m/ 2, h ) + T ( m/ 2, n ā h ) ⤠αmn + 2 αmh/ 2 + 2 αm ( n ā h ) / 2 [inductive hypothesis] = 2 αmn
A similar inductive argument implies that the algorithm uses only O ( n + m ) space. Hirschbergās divide-and-conquer trick can be applied to almost any dynamic programming problem to obtain an algorithm to construct an optimal structure (in this case, the cheapest edit sequence) within the same space and time bounds as computing the cost of that optimal structure (in this case, edit distance). For this reason, we will almost always ask you for algorithms to compute the cost of some optimal structure, not the optimal structure itself.
In many applications of dynamic programming, we are faced with instances where almost every recursive subproblem will be resolved exactly the same way. We call such instances sparse. For example, we might want to compute the edit distance between two strings that have few characters in common, which means there are few āfreeā substitutions anywhere in the table. Most of the table has exactly the same structure. If we can reconstruct the entire table from just a few key entries, then why compute the entire table? To better illustrate how to exploit sparseness, letās consider a simplification of the edit distance problem, in which substitutions are not allowed (or equivalently, where a substitution counts as two operations instead of one). Now our goal is to maximize the number of āfreeā substitutions, or equivalently, to find the longest common subsequence of the two input strings. Let LCS ( i , j ) denote the length of the longest common subsequence of two fixed strings A [ 1 .. m ] and B [1 .. n ]. This function can be defined recursively as follows:
LCS ( i , j ) =
0 if i = 0 or j = 0 LCS ( i ā 1, j ā 1 ) if A [ i ] = B [ j ] max
LCS ( i , j ā 1 ), LCS ( i ā 1, j ) otherwise
This recursive definition directly translates into an O ( mn )-time dynamic programming algorithm. Call an index pair ( i , j ) a match point if A [ i ] = B [ j ]. In some sense, match points are the only āinterestingā locations in the memoization table; given a list of the match points, we could easily reconstruct the entire table.
To efficiently evaluate our modified recurrence, we once again turn to dynamic programming. We consider the match points in lexicographic orderāthe order they would be encountered in a standard row-major traversal of the m Ć n tableāso that when we need to evaluate LCS ( i , j ), all match points ( i ā², j ā²) with i ā²^ < i and j ā²^ < j have already been evaluated.
SPARSELCS( A [1 .. m ], B [1 .. n ]): Match [1 .. K ] ā FINDMATCHES( A , B ) Match [ K + 1 ] ā ( m + 1, n + 1 ) ā©ā© Add end sentinel āŖāŖ Sort M lexicographically for k ā 1 to K ( i , j ) ā Match [ k ] LCS [ k ] ā 1 ā©ā© From start sentinel āŖāŖ for _ ā 1 to _k_ ā 1 ( _i_ ā², _j_ ā²) ā _Match_ [ _ ] if i ā²^ < i and j ā²^ < j LCS [ k ] ā min{ LCS [ k ], 1 + LCS [ ` ]} return LCS [ K + 1 ] ā 1
The overall running time of this algorithm is O ( m log m + n log n + K^2 ). So as long as K = o (
p mn ), this algorithm is actually faster than naĆÆve dynamic programming.
Recall the optimal binary search tree problem from the previous lecture. Given an array F [ 1 .. n ] of access frequencies for n items, the problem it to compute the binary search tree that minimizes the cost of all accesses. A straightforward dynamic programming algorithm solves this problem in O ( n^3 ) time. As for longest common subsequence problem, the algorithm can be improved by exploiting some structure in the memoization table. In this case, however, the relevant structure isnāt in the table of costs, but rather in the table used to reconstruct the actual optimal tree. Let OptRoot [ i , j ] denote the index of the root of the optimal search tree for the frequencies F [ i .. j ]; this is always an integer between i and j. Donald Knuth proved the following nice monotonicity property for optimal subtrees: If we move either end of the subarray, the optimal root moves in the same direction or not at all. More formally:
OptRoot [ i , j ā 1 ] ⤠OptRoot [ i , j ] ⤠OptRoot [ i + 1, j ] for all i and j.
This (nontrivial!) observation leads to the following more efficient algorithm:
FASTEROPTIMALSEARCHTREE( f [1 .. n ]): INITF( f [1 .. n ]) for i ā 1 downto n OptCost [ i , i ā 1 ] ā 0 OptRoot [ i , i ā 1 ] ā i for d ā 0 to n for i ā 1 to n COMPUTECOSTANDROOT( i , i + d ) return OptCost [1, n ]
COMPUTECOSTANDROOT( i , j ): OptCost [ i , j ] ā ā for r ā OptRoot [ i , j ā 1 ] to OptRoot [ i + 1, j ] tmp ā OptCost [ i , r ā 1 ] + OptCost [ r + 1, j ] if OptCost [ i , j ] > tmp OptCost [ i , j ] ā tmp OptRoot [ i , j ] ā r OptCost [ i , j ] ā OptCost [ i , j ] + F [ i , j ]
Itās not hard to see that the loop index r increases monotonically from 1 to n during each iteration of the outermost for loop of FASTEROPTIMALSEARCHTREE. Consequently, the total cost of all calls to COMPUTECOSTANDROOT is only O ( n^2 ). If we formulate the problem slightly differently, this algorithm can be improved even further. Suppose we require the optimum external binary tree, where the keys A [ 1 .. n ] are all stored at the leaves, and
intermediate pivot values are stored at the internal nodes. An algorithm due to Te Ching Hu and Alan Tucker^1 computes the optimal binary search tree in this setting in only O ( n log n ) time!
(^1) T. C. Hu and A. C. Tucker, Optimal computer search trees and variable length alphabetic codes, SIAM J. Applied Math. 21:514ā532, 1971. For a slightly simpler algorithm with the same running time, see A. M. Garsia and M. L. Wachs, A new algorithms for minimal binary search trees, SIAM J. Comput. 6:622ā642, 1977. The original correctness proofs for both algorithms are rather intricate; for simpler proofs, see Marek Karpinski, Lawrence L. Larmore, and Wojciech Rytter, Correctness of constructing optimal alphabetic trees revisited, Theoretical Computer Science, 180:309-324, 1997.