Docsity

Dynamic Programming II: Optimal Trees, Triangulations, Matrix Multiplication - Prof. Chand, Study notes of Algorithms and Programming

Toggle sidebar
bg1
5: Dynamic programming II - The Recursion Strikes Back
Sariel Har-Peled
February 1, 2006
473G - Algorithms
Version: 0.1
1 Optimal Search Trees
Given a binary search tree T, the time to search for an element x, that is stored in T, is O(1 +depth(T,x)),
where depth(T,x) denotes the depth of xin T(i.e., this is the length of the path connecting xwith the root
of T).
Problem 1.1 Given a set of n(sorted) keys A[1..n], build the best binary search tree for the elements of A.
4
12
21
32
45
45
32
12
4 21
Two possible search trees for the set
A=[4,12,21,32,45].
Note, that we store the values in the internal node of the
binary trees. The figure on the right shows two possible search
trees for the same set of numbers. Clearly, if we are accessing
the number 12 all the time, the tree on the left would be better
to use than the tree on the right.
Usually, we just build a balanced binary tree, and this is
good enough. But less assume that we have additional informa-
tion about what is the frequency in which we access the element
A[i]. Namely, we know that A[i] is going be accessed f[i] times,
for i=1,...,n.
In this case, we know that the Total search time for a tree Tis S(T)=
n
X
i=1
(depth(T,i)+1)f[i], where
depth(T,i) is the depth of the node in Tstoring the value A[i]. Assume that A[r] is the value stored in the
root of the tree T. Clearly, all the values smaller than A[r] are in the subtree leftT, and all values larger than
rare in rightT. Thus, total search time is:
S(T)=
r−1
X
i=1
(depth(leftT,i)+1)f[i]+
price of access to root
z }| {
n
X
i=1
f[i]+
n
X
i=r+1depth(rightT,i)+1f[i].
Observe, that if Tis the optimal search tree for the access frequencies f[1 . . . n], then The subtree leftT
must be optimal for the elements accessing it (i.e., A[1..r−1] where ris the root).
Thus, the price of Tis
S(T)=S(leftT)+S(rightT)+
n
X
i=1
f[i],
where S(Q) is the price of searching in Qfor the frequency of elements stored in Q.
1
pf3
pf4

Partial preview of the text

Download Dynamic Programming II: Optimal Trees, Triangulations, Matrix Multiplication - Prof. Chand and more Study notes Algorithms and Programming in PDF only on Docsity!

5: Dynamic programming II - The Recursion Strikes Back

Sariel Har-Peled

February 1, 2006

473G - Algorithms

Version: 0.

1 Optimal Search Trees

Given a binary search tree T , the time to search for an element x, that is stored in T , is O(1 + depth(T, x)), where depth(T, x) denotes the depth of x in T (i.e., this is the length of the path connecting x with the root of T ).

Problem 1.1 Given a set of n (sorted) keys A[1..n], build the best binary search tree for the elements of A.

4

12

21

32

45

45

32

12

4 21 Two possible search trees for the set A = [4, 12 , 21 , 32 , 45].

Note, that we store the values in the internal node of the binary trees. The figure on the right shows two possible search trees for the same set of numbers. Clearly, if we are accessing the number 12 all the time, the tree on the left would be better to use than the tree on the right. Usually, we just build a balanced binary tree, and this is good enough. But less assume that we have additional informa- tion about what is the frequency in which we access the element A[i]. Namely, we know that A[i] is going be accessed f [i] times, for i = 1 ,... , n.

In this case, we know that the Total search time for a tree T is S (T ) =

∑^ n

i= 1

(depth(T, i) + 1) f [i], where

depth(T, i) is the depth of the node in T storing the value A[i]. Assume that A[r] is the value stored in the root of the tree T. Clearly, all the values smaller than A[r] are in the subtree leftT , and all values larger than r are in rightT. Thus, total search time is:

S (T ) =

∑^ r−^1

i= 1

(depth(leftT , i) + 1) f [i] +

price of access to root ︷ ︸︸ ︷ ∑^ n

i= 1

f [i] +

∑^ n

i=r+ 1

depth(rightT , i) + 1

f [i].

Observe, that if T is the optimal search tree for the access frequencies f [1... n], then The subtree leftT must be optimal for the elements accessing it (i.e., A[1..r − 1] where r is the root). Thus, the price of T is

S (T ) = S (leftT ) + S (rightT ) +

∑^ n

i= 1

f [i],

where S (Q) is the price of searching in Q for the frequency of elements stored in Q.

Figure 1: A polygon and two possible triangulations of the polygon.

CompBestTree(A[1..n], f [1..n]) for r = 1 ...n do Tle f t ← CompBestTree(A[1..r − 1], f [1..r − 1]) Tright ← CompBestTree(A[r + 1 ..n], f [r + 1 ..n]) Tr ← T ree

Tle f t , A[r] , Tright

Pr ← S (Tr)

return cheapest tree out of T 1 ,... , Tn.

This recursive formula naturally gives rise to a recursive algorithm, which is depicted on the right. The naive implementation requires O(n^2 ) time (ignoring the recursive call). But in fact, by a more careful implementation, to- gether with the tree T , we can also return the price of searching on this tree with the given frequencies. Thus, this modified algorithm. Thus, the running time for this function takes O(n) time (ignoring recursive calls). The running time of the resulting algorithm is

α(n) = O(n) +

∑^ n−^1

i= 0

(α(i) + α(n − i − 1)) ,

and the solution of this recurrence is O(n 3 n). We can, of course, improve the running time using memoization. There are only O(n^2 ) different recursive calls, and as such, the running time of CompBestTreeMemoize is O(n^2 ) · O(n) = O(n^3 ).

Theorem 1.2 Can can compute the optimal binary search tree in O

n^3

time using O

n^2

space. A further improvement raises from the fact that the root location is “monotone”. Formally, if R[i, j] denotes the location of the element stored in the root for the elements A[i... j] then it holds that R[i, j − 1] ≤ R[i, j] ≤ R[i, j + 1]. This limits the search space, and we can be more efficient in the search. This leads to O

n^2

algorithm. Details are in Jeff Erickson class notes.

2 Optimal Triangulations

Given a convex polygon P in the plane, we would like to find the triangulation of P of minimum length. See Figure 1.

Definition 2.1 A set S ⊆ IRd^ is convex if for any to x, y ∈ S , the segment xy is contained in S. A convex polygon is a closed cycle of segments, with no vertex pointing inward. Formally, it is a simple closed polygonal curve which encloses a convex set. A diagonal is a line segment connecting two vertices of a polygon which are not adjacent. A triangula- tion is a partition of a convex polygon into (interior) disjoint triangles using diagonals.

Observation 2.2 Any triangulation of a convex polygon with n vertices is made out of exactly n− 2 triangles.

Again, let us define a recurrence for this problem, where M[i, j] is the amount of work involved in computing the product of the matrices Mi · · · M (^) j. We have

M[i, j] =

0 j = i D[i − 1] · D[i] · D[i + 1] j = i + 1 mini≤k< j(M[i, k] + M[k + 1 , j] + D[i − 1] · D[k] · D[ j]) j > i + 1.

Again, using memoization (or dynamic programming), one can compute M[1, n], in O(n^3 ) time, using O(n^2 ) space.

4 Longest Ascending Subsequence

Given an array of numbers A[1..n] we are interested in the problem finding the longest ascending subse- quence. For example, if A = [6, 3 , 2 , 5 , 1 , 12] the longest ascending subsequence is 2, 5 , 12. This this end, let M[i] denote longest increasing subsequence having A[i] as the last element in the subsequence. The

corresponding recurrence is M[n] =

1 n = 1 1 + max 1 ≤k<n,A[k]<A[n] M[k] otherwise. The length of the longest increasing subsequence is maxni= 1 M[i]. Again, using dynamic programming we get a running time O(n^2 ) algorithm for this problem. It is also not hard to modify the algorithm so that it outputs this sequence (you should figure out the details of this modification). A better O(n log n) solution is possible using some data-structure magic.

5 Pattern Matching

Tidbit Magna Carta or Magna Charta - the great charter that King John of Eng- land was forced by the English barons to grant at Runnymede, June 15, 1215, traditionally interpreted as guaranteeing certain civil and political liberties.

Assume you have a string S = ”Magna Carta” and a pattern P = ”?ag ∗ at ∗ a ∗ ” where “?” can match a single character, and “*” can match any substring. You would like to decide if the pattern matches the string. We are interested in solving this problem using dynamic programming. This is not too hard since this is similar to the edit-distance that was already covered.

IsMatch(S [1..n], P[1..m]) if m = 0 and n = 0 then return True. if m = 0 then return False. if n = 0 then if P[1..m] is all stars then return True else return False if (P[m] = ’?’) then return IsMatch(S [1..n − 1], P[1..m − 1]) if (P[m] , ’*’) then if P[m] , S [n] then return False else return IsMatch(S [1..n − 1], P[1..m − 1]) for i = 0 to n do if IsMatch(S [1..i], P[1..m − 1]) then return True return False

The resulting code is depicted on the left, and as you can see this is pretty tedious. Now, use memoization together with this recursive code, and you get an algorithm with running time O

mn^2

and space O(nm), where the input string of length n, and m is the length of the pattern. Being slightly more clever, one can get a faster algorithm with running time O(nm). BTW, one can do even better. A O(m+n) time is possible but it requires Knuth-Morris-Pratt al- gorithm, which is a fast string matching algo- rithm.