1、Dynamic Programming - II,Algorithm : Design & Analysis 18,In the last class,Recursion and Subproblem Graph Basic Idea of Dynamic Programming Least Cost of Matrix Multiplication Extracting Optimal Multiplication Order,Dynamic Programming - II,Optimal Binary Search Tree Separating Sequence of Word Dyn
2、amic Programming Algorithms,Binary Search Tree,30,20,40,40,20,60,50,80,Poor balancing (n),Each node has a key, belonging to a linear ordered set An inorder traversal produces a sorted list of the keys,In a properly drawn tree, pushing forward to get the ordered list.,Good balancing (logn),Keys with
3、Different Frequencies,wing (0.050),ring (0.075),the (0.150),cabbage (0.025),has (0.025),said (0.075),king (0.050),pig (0.025),of (0.125),thing (0.075),time (0.050),come (0.050),and (0.150),walrus (0.025),talk (0.050),A binary search tree perfectly balanced,Since the keys with largest frequencies hav
4、e largest depth, this tree is not optimal.,Average: 3.25,Improved for a Better Average,wing (0.050),ring (0.075),the (0.150),cabbage (0.025),has (0.025),said (0.075),king (0.050),pig (0.025),of (0.125),thing (0.075),time (0.050),come (0.050),and (0.150),walrus (0.025),talk (0.050),= 2.915,Plan of Op
5、timal Binary Tree,K1,Kk -1,Kk +1,Kn,For each selected root Kk , the left and right subtrees are optimized.,The problem is decomposes by the choices of the root. Minimizing over all choices,The subproblems can be identified similarly as for matrix multOrder,Subproblems as left and right subtrees,Prob
6、lem Rephrased,Subproblem identification The keys are in sorted order. Each subproblem can be identified as a pair of index (low, high) Expected solution of the subproblem For each key Ki, a weight pi is associated. The subproblem (low, high) is to find the binary search tree with minimum weighted re
7、trieval cost.,Minimum Weighted Retrieval Cost,A(low, high, r) is the minimum weighted retrieval cost for subproblem (low, high) when Kr is chosen as the root of its binary search tree. A(low, high) is the minimum weighted retrieval cost for subproblem (low, high) over all choices of the root key. p(
8、low, high), equal to plow+plow+1+phigh, is the weight of the subproblem (low, high).,Integrating Solutions of Subproblem,Weighted retrieval cost of a subtree Let T is a particular tree containing Klow, , Khigh, the weighted retrieval cost of T is W, with T being a whole tree. Then, as a subtree with
9、 the root at level 1, the weighted retrieval cost of T will be: W+p(low, high) So, the recursive relations: A(low, high, r ) = pr+p(low, r-1)+A(low, r-1)+p(r+1, high)+A(r+1, high) = p(low, high)+A(low, r-1)+A(r+1, high) A(low, high) = minA(low, high, r) | lowrhigh,Avoiding Repeated Work by Storing,A
10、rray cost: costlowhigh gives the minimum weighted search cost of subproblem (low,high). Array root: rootlowhigh gives the best choice of root for subproblem (low,high) The costlowhigh depends upon subproblems with higher first index(row number) and lower second index(column number),Computation of th
11、e Array cost,high,low,0,0,0,0,0,0,1,2,n,0,0,pn,p3,p2,p1,1,2,n+1,costlowhigh,bestChoice(prob, cost, root, low, high)if (highlow)bestCost=0;bestRoot=-1;elsebestCost=;for (r=low; rhigh; r+)rCost=p(low,high)+costlowr-1+costr+1high;if (rCostbestCost)bestCost=rCost;bestRoot=r;costlowhigh=bestCost;rootlowh
12、igh=bestRoot;return,Optimal BST: DP Algorithm,optimalBST(prob,n,cost,root)for (low=n+1; low1; low-)for (high=low-1; highn; high+)bestChoice(prob,cost,root,low,high)return cost,in (n3),Separating Sequence of Words,Word-length w1, w2, , wn and line-width: W Basic constraint: if wi, wi+1, , wj are in o
13、ne line, then wi+wi+1+ +wjW Penalty for one line: some function of X. X is: 0 for the last line in a paragraph, and W (wi+wi+1+ +wj) for other lines The problem how to separate a sequence of words(forming a paragraph) into lines, making the penalty of the paragraph, which is the sum of the penalties
14、 of individual lines, minimized.,Solution by Greedy Strategy,i word Those who cannot remember the past are condemned to repeat it.,w 6 4 7 9 4 5 4 10 3 7 4,W is 17, and penalty is X3,Solution by greedy strategy,words (1,2,3) (4,5) (6,7) (8,9) (10,11)X 0 4 8 4 0 penalty 0 64 512 64 0 Total penalty is
15、 640,An improved solution,words (1,2) (3,4) (5,6,7) (8,9) (10,11)X 7 1 4 4 0 penalty 343 1 64 64 0 Total penalty is 472,Problem Decomposition,Representation of subproblem: a pair of indexes (i,j), breaking words i through j into lines with mininim penalty. Two kinds of subproblem (k, n): the penalty
16、 of the last line is 0 all other subproblems For some k, the combination of the optimal solution for (1,k) and (k+1,n) gives a optimal solution for (1,n). Subproblem graph About n2 vertices Each vertex (i,j) has a edge to about j i other vertices, so, the number of edges is in (n3),Simpler Identific
17、ation of subproblem,If a subproblem concludes the paragraph, then (k,n) can be simplified as (k). There are about k subproblems like this. Can we eliminate the use of (i,j) with jn? Put the first k words in the first line(with the basic constraint satisfied), the subproblem to be solved is (k+1,n) O
18、ptimizing the solution over all ks. (k is at most W/2),In DP version, “Storing” inserted,Breaking Sequence into lines,lineBreak(w,W,i,n,L)if (wi+ wi+1+ wn W)elsefor (k=1; wi+wi+k-1W; k+)X=W-(wi+wi+k-1);kPenalty=lineCost(X)+lineBreak(w,W, i+k, n, L+1)return penalty,In DP version this is replaced by “
19、Recursion or Retrieve”,Analysis of lineBreak,Since each subproblem is identified by only one integer k, for (k,n), the number of vertex in the subproblem is at most n. So, in DP version, the recursion is executed at most n times. The loop is executed at most W/2 times. So, the running time is in (Wn
20、). In fact, W, the line width, is usually a constant. So, (n). The extra space for the dictionary is in (n).,Dynamic Programming: Summary,Problem decomposition Subproblem representation Dictionary Deciding the computing order of subproblems Retrieving information from the dictionary,Home Assignments,pp.477- 10.10 10.11 10.12 10.14 10.15,