1、Dynamic Programming,Let fij denote the fastest possible time to get a chassis fromthe starting through station Si,j. Define lij to be the line number, whose station j-1 is used in afastest through station Si,j. Thus the fastest time is:f*=min (f1n+x1, f2n+x2)It is clear that:f11=e1+a1,1 ; f21=e2+a2,
2、1 Thus,Matrix-chain multiplication: Input : a sequence of n matrices A1A2An to be multipliedE.g. A1A2A3A4 = (A1(A2(A3A4)= (A1(A2A3)A4)= (A1A2)(A3A4)= (A1A2)A3)A4),associativity,A: pq matrix B: qr matrixMatrix-Multiply(A, B)If columnsA rowsBthen error “incompatible dim”else for i=1 to rowsAdo for j=1
3、 to columnsB do Ci, j = 0for k=1 to columnsAdo Ci, j = Ci, j + Ai, kBk, j return C AB : 總共需 pqr 個乘法運算if p=q=r=n O(n3),Strassens Matrix Multiplication 1969 ( 28.2)(1) c11 = a11b11 + a12b21 , c12 = a11b12 + a12b22 c21 = a21b11 + a22b21 , c22 = a21b12 + a22b22總共需 8 個乘法 (2) d1=(a11+a22)(b11+b22) , d2=(a
4、12-a22)(b21+b22)d3=(a11-a21)(b11+b12) , d4=(a11+a12)b22d5=(a21+a22)b11 , d6=a11(b12-b22)d7=a22(-b11+b21)c11= d1+d2-d4+d7c12= d4+d6 c21= d5+d7c22= d1-d3-d5+d6,只需 7 個乘法和 18 個加法,假設 n = 2k , k 為任意正整數令 T(n) : 兩個 nn 矩陣所需之乘法 採用(2)之方法T(n) = 7 T(n/2) , T(2)=7=,目前最佳方法只需 O(n2.376),Eg. A1: 10100 , A2: 1005 , A3
5、: 550 (A1A2)A3) 101005+10550 = 5000+2500(A1(A2A3) 100550+1010050 = 25000+50000(A1A2Ak) (Ak+1An)為方便起見令 Aij 表 (AiAi+1Aj) 連乘所得之矩陣令 mi, j 表算得 Aij 所需最少之乘法運算mi, i=0, i=1,n (AiAi+1Ak) (Ak+1Aj)Pi-1Pk PkPjmi, j= mi, k+ mk+1, j+Pi-1PkPj至於可能的 k 可從 i, i+1, , j-1 中選出 (共有 j-i 個可能)(AiAi+1Ak) 為一 Pi-1Pk 之矩陣(Ak+1Aj)
6、為一 PkPj 之矩陣,假設 Ai 為 Pi-1 Pi 矩陣Ai+1 為 Pi Pi+1 矩陣,故 AikAk+1j 需 Pi-1PkPj 個乘法,Defm1n, 1n: 用以儲存 mi, js1n, 1n: 用以儲存使 mi, j 達到最佳之 k(AiAi+1Ak) (Ak+1Aj),Matrix-Chain-Order(P)for i= 1 to n do mi, i=0for l= 2 to n do for i= 1 to n-l+1do j=i+l-1mi, j= for k= i to j-1 do q = mi, k+ mk+1, j+Pi-1PkPj if qmi, jthen
7、 mi, j = q ; si, j = k return m and sO(n3),Computing the optimal cost Eg. A1 3035 A2 3515 A3 155A4 510 A5 1020 A6 2025Input ,Recursive-Matrix-Chain (P, i, j)if i=j then return 0mi, j for k= i to j-1do q = Recursive-Matrix-Chain(P, i, k)+ Recursive-Matrix-Chain(P, k+1, j)+ Pi-1PkPj if q mi, j then mi
8、, j = qreturn mi, j分析:令 T(n) 為計算 m1, n 所需之時間(步數)T(1) 1T(n) 1+,T(2) 2T(1) + 2 = 4 = 22由歸納法假設 T(i) 2i-1 , for i=1, n-1現欲證 T(n) 2n-1,結論:上述演算法之效率不好,Memoization:目的在改良一較無效率的遞迴(recursive)演算法, 然而 recursive 演算法通常較為“自然”其方法是將計算過的子問題答案存起來 Memoized version of Recursive-Matrix-Chain:Memoized-Matrix-Chain(P) n = l
9、ength(P) 1;for i=1 to n do for j=i to n do mi, j= ;return Lookup-Chain(P, 1, n);,Lookup-Chain(P, i, j) if mi, j then return mi, j;if i=j then mi, j = 0else for k=i to j-1 do q= Lookup-Chain(P, i, k)+ Lookup-Chain(P, k+1, j)+ Pi-1PkPj if q mi, j then mi, j = qreturn mi, j time: O(n3) space: (n2),Long
10、est common subsequence (LCS): Problem: Given 2 sequences x1m and y1n, find a longest subsequence common to them.Eg. x: A B C B D A By: B D C A B A Answer: BCBA, BCAB Brute-force methodFor every subsequence of x, check if its a subsequence of yworst case : O(n2m) , (assume m n),Better way:Define ci,
11、j = length of LCS of x1i and y1jThen cm, n = length of LCS of x and yThm:,Thm 15.1 (Optimal substructure of an LCS)X = , Y = Let Z= be any Lcs of X and Y1. If xm= yn , then zk= xm= yn andZk-1 is an LCS of Xm-1 and Yn-12. If xm yn , then zk xm impliesZ is an LCS of Xm-1 and Y3. If xm yn , then zk yn
12、impliesZ is an LCS of X and Yn-1,pf:(1) If zk xm , then append xm = yn to z to obtain an LCSof X and Y of length k+1 -Thus zk= xm= yn and Zk-1 is an LCS of Xm-1 and Yn-1(2) If zk xm , then Z is a common subsequence of Xm-1 and Y There cant be an LCS of Xm-1 and Y of length greater than k(3) similar
13、to (2),Optimal substructure:The optimal solution to the problem contains optimal solution to subproblems.,Overlapping subproblems:Only (m,n) distinct subproblems.Implies there are many overlappings. Memoize ( to deal with overlapping subproblems)After computing solution to subproblem, store in table
14、. Subsequent calls do table lookupTime: O(mn) bi,j points to the table entry corresponding to the optimal subproblem solution chosen when computing ci,j,LCS-Length(X,Y)m lengthX;n lengthY;for i= 1 to m do ci,0 0;for j= 1 to n do c0,j 0;for i= 1 to m dofor j= 1 to n do If xi = yj then ci,j = ci-1,j-1
15、+1; bi,j = “”; else if ci-1,j ci,j-1then ci,j = ci-1,j; bi,j = “”; else ci,j = ci,j-1; bi,j = “”; return b, c,Print-LCS(b, X, i, j) If i=0 or j=0 then return;If bi,j = “ ”;then Print-LCS(b, X, i-1, j-1);print xi;else if bi,j = “”then Print-LCS(b, X, i-1, j);else Print-LCS(b, X, i, j-1);,Eg. X= , Y=,
16、Optimal polygon triangulationInput:P = : convex polygonw : weight function for triangle例如: w(vivjvk)=|vivj|+|vjvk|+|vkvi|目的: 找出一三角化方式使得其加權和最小,逆時針順序,Parse tree: (最佳三角化與矩陣連乘之關係) (A1(A2A3)(A4(A5A6)Ai 對應於邊 Ai+1j 對應於定義 w(vivjvk)=PiPjPk故 矩陣連乘為最佳三角化之特例q = mi, k+ mk+1, j+w(vi-1vkvj),Optimal binary search tr
17、ees: k=(k1,k2,kn) of n distinct keys in sorted order (k1k2kn) Each key ki has a probability pi that a search will be for ki Some searches may fail, so we also have n+1 dummy keys: d0,d1,dn, where d0k1, kndn and kidiki+1 for i=1,n-1 For each dummy key di, we have a probability qi that a search will c
18、orrespond to di .,Eg.depthT: a nodes depth in the search tree T, the expected cost of a search in T,Expected search cost: 2.8,Goal: Given a set of probabilities, want to construct a binary search tree whose expected search cost is smallest .Step 1: The structure of an optimal BST.Consider any subtre
19、e of a BST, which must contain contiguous keys ki,kj for some 1ijn and must also have as its leaves the dummy keys di-1,dj .Optimal-BST has the property of optimal substructure.,Step 2: A recursive solution Find an optimal BST for the keys ki,kj, where ji-1, i1, jn. (When j=i-1, there is no actual k
20、ey, but di-1) Define ei,j as the expected cost of searching an optimal BST containing ki,kj. Want to find e1,n .For dummy key di-1, ei,i-1=qi-1 . For ji, need to select a root kr among ki,kj .For a subtree with keys ki,kj, denote,ei,j=pr+(ei,r-1+w(i,r-1)+(er+1,j+w(r+1,j)=ei,r-1+er+1,j+w(i,j) Thus ei
21、,j=Step 3: Computing the expected search cost of an optimal BSTUse a table w1n+1,0n for w(i,j)s .wi,i-1=qi-1wi,j=wi,j-1+pj+qj,Optimal-BST(p,q,n)1 for i=1 to n+12 do ei,i-1=qi-13 wi,i-1=qi-14 for =1 to n5 do for i=1 to n-+16 do j=i+-17 ei,j=8 wi,j=wi,j-1+pj+qj9 for r=i to j 10 do t=ei,r-1+er+1,j+wi,j 11 if tei,j 12 then ei,j=t 13 rooti,j=r 14 return e and root (n3),