Docsity

Advanced Dynamics Programming Tricks - Algorithms | CS 573, Study notes of Algorithms and Programming

Toggle sidebar
bg1
Algorithms Lecture 6: Advanced Dynamic Programming Tricks
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)
6 Advanced Dynamic Programming Tricks
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.
6.1 Saving Space: Divide and Conquer
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
jif 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.
1
pf3
pf4
pf5

Partial preview of the text

Download Advanced Dynamics Programming Tricks - Algorithms | CS 573 and more Study notes Algorithms and Programming in PDF only on Docsity!

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)

6 Advanced Dynamic Programming Tricks

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.

6.1 Saving Space: Divide and Conquer

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.

6.2 Saving Time: Sparseness

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.

6.3 Saving Time: Monotonicity

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.