The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Dynamic programming Optimal binary search tree Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Optimal binary search tree Balanced but not optimal. Dana.4 Hugo.9 Lea.8 Ben.5 Fred.5 Jack. Nick Ann Cole Edna Gene Irma Ken Mark Orrie...4..6.5.. Key Query probability Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Optimal binary search tree Node costs in a BST Node cost = probability depth.4 Hugo.4 =.4. Dana. =..5 Fred.5 =.5 Gene 4. depth. 4 =.88 Node cost = average no. of tests in one operation Find. Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
X6DSA 5 Θ(n log (n)), Cost of balanced search tree key probab. p k depth d k p k d k Ann Ben Cole Dana Edna Fred Gene Hugo Irma Jack Ken Lea..8...4.5..4.6.5.5.9 4 4 4 4 4 4. 4 =..8 =.4. 4 =.48. =..4 4 =.6.5 =.5. 4 =.88.4 =.4.6 4 =.4.5 =.5.5 4 =.6.9 =.8 Mark Nick Orrie... 4 4. 4 =.8. =.9. 4 =.4 Total cost:.47 Total cost = avg. no. of tests in all operatios Find. Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Optimal binary search tree Optimal BST with specific query probabilities. Cole. Gene.5 Ken.8 Ben.5 Fred.6 Irma.9 Lea Ann.4 Edna Hugo Jack. Nick..4.5 Dana Mark Orrie... Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
Θ(n log (n)), Cost of optimal BST key probab. p k depth d k p k d k. 4. 4 =..8.8 =.4.. =.4. 5. 5 =.5.4 4.4 4 =.6.5.5 =.5.. =..4 4.4 4 =.6.6 Ann Ben Cole Dana Edna Fred Gene Hugo Irma Jack Ken Lea Mark Nick Orrie.5.5.9... Speedup.47 :.56 = :.74.6 =.8.5 4 =..5 =..9 =.7. 5 =.. 4 =.. 5 =.5 Total cost.56 Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)), 4 5 4 5
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Computing the cost of optimal BST Xyz p k C k C k cost of the left subtree of node k cost of the right subtree of node k C k C k L k- k k+ R Recursive idea k- cost = C k + p i + C k + i=l R i=k+ p i p k + p k Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
X6DSA 5 The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Computing the cost of optimal BST Small optimal subtrees L R N Surely, there exists an optimal subtree containing keys with indices ranging from L to R. Subtree size = no. of nodes = L R+ There are N optimal subtrees of size N N subtree N In total, there are N * (N+) / different optimal subtrees. Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Minimizing the cost of optimal BST Idea of recursive solution:. Assumption: All smaller optimal subtrees are known.. Try all possibilities: k = L, L+, L+,..., R k=l k=l+ k=l+... k=r. Register the index k, which minimizes the cost expressed as k- C k + p i + C k + i=l R i=k+ p i + p k 4. The key with index k is the root of the optimal subtree. Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
X6DSA 5 The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Minimizing the cost of optimal BST C(L,R)... Cost of optimal subtree containig keys with indices: L, L+, L+,..., R-, R C(L,R) = k- p i i=l min { C(L, k-) + L k R + C(k+,R) + R p i i=k+ + p k } = = min { C(L, k-) + L k R C(k+,R) + R p i i=l } = (*) = min { C(L, k-) + L k R C(k+,R) } + R p i i=l The value minimizing (*) is the index of the root of the optimal subtree Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
X6DSA 5 The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Data structures for computing optimal BST Costs of optimal subtrees array C [L][R] (L R) Roots of optimal subtrees array roots [L][R] (L R) L R 4 N L R L R L R N+ N+ diagonal... L= R diagonal... L= R Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Computing optimal BST The cost of a particular optimal subtree p p L=, R=9 p a b c d e p 4 p 5 p 6 p7 x y z C(L,R) = min { C(L, k-) + C(k+,R) } L k R R + p i i=l t p 8 w p9 C(L,R) = min{ +x, p +y, a+z, b+t, c+w, d+p 9, e+ } p N Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Computing optimal BST Dynamic programming strategy First process the smallest subtrees, then the bigger ones, then still more bigger ones, etc... Stop Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Computing optimal BST Computing arrays of costs and roots def optimaltree( Prob, N ): Costs = [[]*N for i in range(n)] Roots = [[]*N for i in range(n)] # size =, main diagonal for i in range(n): Costs[i][i] = Prob[i]; Roots[i][i] = i # size >, diagonals above main diagonal for size in range(, N): L = ; R = size while R < N: Costs[L][R] = min(costs[l][k-] + Costs[k+][R], k = L..R) roots[l][r] = k minimizing previous line Costs[L][R] += sum(costs[l:r+]) L += ; R += return Costs, Roots Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Computing optimal BST Building optimal BST using the array of subtree roots # standard BST insert def buildtree( Tree, L, R, Roots, Nodes ): if R < L: return rootindex = Roots[L][R] # standard BST insert # nodes in Nodes have to be sorted in increasing # order of their key values Tree.insert( Nodes[rootindex].key ) buildtree( Tree, L, rootindex-, Roots, Nodes ) buildtree( Tree, rootindex+, R, Roots, Nodes ) } Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Computing optimal BST Roots of optimal subtrees 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 4 5 6 7 7 7 7 7 7 7 7 7 5 6 7 7 7 7 7 7 7 7 7 6 7 7 7 7 7 7 7 7 7 7 7 7 8 9 9 9 9 4 4 4 4 5 Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Computing optimal BST Tree and array correspondence.8 Ben Ann.. Cole.4 Edna Dana..5 Fred. Gene.6 Irma Hugo.4 Jack.5.5 Ken.9 Lea. Nick Mark. Orrie. 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 4 5 6 7 7 7 7 7 7 7 7 7 5 6 7 7 7 7 7 7 7 7 7 6 7 7 7 7 7 7 7 7 7 7 7 7 8 9 9 9 9 4 4 4 4 5 Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Computing optimal BST Costs of optimal subtrees -A -B -C 4-D 5-E 6-F 7-G 8-H 9-I -J -K -L -M 4-N 5-O -A..4.7.9.48.6.7.6.4.57..9.7.5.56 -B.8.8..9.54.6.4..45.9.7.5.9.44 -C..4..8.8.9.6..66.9..5. 4-D..6.6.48.56.7.87..59.67.8.86 5-E.4..44.5.68.8.8.55.6.77.8 6-F.5..4.56.7.6.4.5.6.67 7-G...46.6.6..7.48.5 8-H.4.4.4.54.7.78.89.9 9-I.6.6.4.6.66.77.8 -J.5.5.4.49.6.64 -K.5..9.5.54 -L.9...4 -M..7.9 4-N..5 5-O. Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Dynamic programming Longest common subsequence (LCS) Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Longest common subsequence Two sequences A: C B E A D D E A A = 8 B: D E C D B D A B = 7 Common subsequence A: C B E A D D E A B: D E C D B D A C: C D A C = Longest common subsequence (LCS) A: C B E A D D E A B: D E C D B D A C: E D D A C = 4 Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Longest common subsequence A n : (a, a,..., a n ) B m : (b, b,..., b m ) C k : (c, c,..., c k ) C k = LCS(A n, B m ) A 8 : B 7 : C 4 : 4 5 6 7 8 C B E A D D E A D E C D B D A E D D A Recursive rules: ( a n = b m ) ==> (c k = a n = b m ) & (C k- = LCS (A n-, B m- ) ) 4 5 6 7 8 4 5 6 7 8 A 8 : C B E A D D E A A 7 : C B E A D D E A B 7 : D E C D B D A B 6 : D E C D B D A C 4 : E D D A C : E D D A Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Longest common subsequence ( a n!= b m ) & (c k!= a n ) ==> (C k = LCS (A n-, B m ) ) 4 5 6 7 8 4 5 6 7 8 A 7 : C B E A D D E A 6 : C B E A D D E B 6 : D E C D B D B 6 : D E C D B D C : E D D C : E D D ( a n!= b m ) & (c k!= b m ) ==> (C k = LCS (A n, B m- ) ) 4 5 6 7 8 4 5 6 7 8 A 5 : C B E A D A 5 : C B E A D B 5 : D E C D B B 4 : D E C D B C : E D C : E D Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Longest common subsequence Recursive function c(m, n) computes LCS length C(n,m) = C(n-, m-) + max{ C(n-, m), C(n, m-) } n = or m = n >, m >, a n =b m n >, m >, a n b m Dynamic programming strategy C[n][m] n m for a in range(, n+ ): for b in range(, m+ ): C[a][b] =... } Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Longest common subsequence Construction of D LCS array def findlcs(): for a in range(, n+ ): for b in range(, m+ ): if A[a] == B[b]: C[a][b] = C[a-][b-]+ arrows[a][b] = DIAG else: if C[a-][b] > C[a][b-]: C[a][b] = C[a-][b]; arrows[a][b] = UP else: C[a][b] = C[a][b-]; arrows[a][b] = LEFT Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)), 4 5 6 7 C B E A D D E A 4 4 5 6 7 8 C D B D A D E C A: B: LCS array for CBEADDEA and DECDBDA Longest common subsequence
The complexity of different algorithms varies: O(n), Ω(n ), Θ(n log (n)), Longest common subsequence LSC printout -- recursively :) def outlcs(a, b) { if a == or b == return if arrows[a][b] == DIAG: outlcs(a-, b-); // recursion... print(a[a]) //... reverses the sequence! else: if arrows[a][b] == UP: outlcs(a-,b); else: outlcs(a,b-); } Různé algoritmy mají různou složitost: O(n), Ω(n ), Θ(n log (n)),