Prezentace se nahrává, počkejte prosím

Prezentace se nahrává, počkejte prosím

Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

Podobné prezentace


Prezentace na téma: "Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …"— Transkript prezentace:

1 Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Dynamic programming Optimal binary search tree Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

2 Optimal binary search tree
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Optimal binary search tree Balanced but not optimal 0.04 Hugo 0.01 0.09 Dana Lea 0.08 0.05 0.05 0.03 Ben Fred Jack Nick Ann Cole Edna Gene Irma Ken Mark Orrie 0.03 0.12 0.04 0.22 0.06 0.15 0.02 0.01 Query probability Key Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

3 Optimal binary search tree
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Optimal binary search tree Node costs in a BST Node cost = probability  depth 0.04 1 0.04  1 = 0.04 Hugo 0.01 2 0.01  2 = 0.02 Dana 0.05 3 0.05  3 = 0.15 Fred 4 0.22  4 = 0.88 Gene depth 0.22 Node cost = average no. of tests in one operation Find. Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

4 Cost of balanced search tree
X36DSA 2005 Θ(n·log2(n)), … Cost of balanced search tree key probab. pk depth dk pk  dk Ben Cole Dana Edna Fred Gene Hugo Irma Lea Jack Ken Mark Nick Orrie Ann 0.08 0.12 0.01 0.04 0.05 0.22 0.06 0.09 0.15 0.02 0.03 3 4 2 1 0.08 0.12 0.01 0.04 0.05 0.22 0.06 0.09 0.15 0.02 0.03  3 = 0.24  4 = 0.48  2 = 0.02  4 = 0.16  3 = 0.15  4 = 0.88  1 = 0.04  4 = 0.24  2 = 0.18  4 = 0.60  4 = 0.08  3 = 0.09  4 = 0.12  4 = 0.04 Total cost: 3.47 Total cost = avg. no. of tests in all operatios Find. Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

5 Optimal binary search tree
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Optimal binary search tree Optimal BST with specific query probabilities 0.22 Gene 0.12 0.15 Cole Ken 0.08 0.05 0.06 0.09 Ben Fred Irma Lea 0.04 0.03 Ann Edna Hugo Jack Nick 0.03 0.04 0.05 Dana Mark Orrie 0.01 0.02 0.01 Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

6 Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
Cost of optimal BST key probab. pk depth dk pk  dk Ben Cole Dana Edna Fred Gene Hugo Irma Lea Jack Ken Mark Nick Orrie Ann 0.03 3 2 5 4 1 0.08 0.12 0.01 0.04 0.05 0.22 0.06 0.09 0.15 0.02 0.03  3 = 0.24  2 = 0.24  5 = 0.05  4 = 0.16  3 = 0.15  1 = 0.22  3 = 0.18  3 = 0.27  4 = 0.20  2 = 0.30  5 = 0.10  4 = 0.12 0.08 0.12 0.01 0.04 0.05 0.22 0.04 0.06 0.05 0.15 0.09 0.02 0.03 0.01 Total cost 2.56 Speedup : = 1 : 0.74 Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

7   Computing the cost of optimal BST Recursive idea pi pi
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Computing the cost of optimal BST pk Xyz C1k cost of the left subtree of node k C2k cost of the right subtree of node k C2k C1k L k-1 k k+1 R Recursive idea pk k-1 i=L pi R i=k+1 pi cost = C1k + + C2k + + pk Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

8 Computing the cost of optimal BST
X36DSA 2005 The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(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. There are N optimal subtrees of size Subtree size = no. of nodes = L  R+1 N 1 2 N 2 3 subtree N In total, there are N * (N+1) /2 different optimal subtrees. Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

9  Minimizing the cost of optimal BST Idea of recursive solution:
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Minimizing the cost of optimal BST Idea of recursive solution: 1. Assumption: All smaller optimal subtrees are known. 2. Try all possibilities: k = L, L+1, L+2, ..., R k=L k=L+1 k=L+2 … k=R 3. Register the index k, which minimizes the cost expressed as R i=k+1 pi C1k + + k-1 i=L + C2k + pk 4. The key with index k is the root of the optimal subtree. Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

10     Minimizing the cost of optimal BST
X36DSA 2005 The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Minimizing the cost of optimal BST C(L,R) Cost of optimal subtree containig keys with indices: L, L+1, L+2, ..., R-1, R i=L k-1 pi i=k+1 R pi C(L,R) = min { C(L, k-1) + + C(k+1,R) + + pk } = L  k  R R = min { C(L, k-1) + C(k+1,R) + pi } = L  k  R i=L i=L R pi (*) = min { C(L, k-1) + C(k+1,R) } + L  k  R The value minimizing (*) is the index of the root of the optimal subtree Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

11 Data structures for computing optimal BST
X36DSA 2005 The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(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) R R N L 1 1 L 2 2 3 3 L ≤ R L ≤ R N+1 N+1 diagonal ... L= R diagonal ... L= R Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

12 Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Computing optimal BST The cost of a particular optimal subtree p1 L=3, R=9 p2 p3 a b c d e p4 x i=L R pi + L  k  R C(L,R) = min { C(L, k-1) + C(k+1,R) } p5 y p6 z p7 t C(L,R) = min{ 0+x, p3+y, a+z, b+t, c+w, d+p9, e+0 } p8 w p9 pN Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

13 Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(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), Ω(n2), Θ(n·log2(n)), …

14 Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Computing optimal BST Computing arrays of costs and roots def optimalTree( Prob, N ): Costs = [[0]*N for i in range(N)] Roots = [[0]*N for i in range(N)] # size = 1, main diagonal for i in range(N): Costs[i][i] = Prob[i]; Roots[i][i] = i # size > 1, diagonals above main diagonal for size in range(1, N): L = 1; R = size while R < N: Costs[L][R] = min(Costs[L][k-1] + Costs[k+1][R], k = L..R) roots[L][R] = ‘k minimizing previous line’ Costs[L][R] += sum(Costs[L:R+1]) L += 1; R += 1 return Costs, Roots Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

15 Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(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] # nodes in Nodes have to be sorted in increasing # order by their key values Tree.insert( Nodes[rootindex].key ) buildTree( Tree, L, rootindex-1, Roots, Nodes ) buildTree( Tree, rootindex+1, R, Roots, Nodes ) } Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

16 Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Computing optimal BST Roots of optimal subtrees Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

17 Computing optimal BST Tree and array correspondence
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Computing optimal BST Tree and array correspondence Mark 0.02 Orrie Fred 0.05 Ben 0.08 Dana Hugo 0.22 Lea 0.15 Nick 0.09 0.01 Irma 0.04 Jack Edna Gene Ann 0.03 Cole Ken 0.06 0.12 Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

18 Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Computing optimal BST Costs of optimal subtrees 1-A 2-B 3-C 4-D 5-E 6-F 7-G 8-H 9-I 10-J 11-K 12-L 13-M 14-N 15-O 1-A 2-B 3-C 4-D 5-E 6-F 7-G 8-H 9-I 10-J 11-K 12-L 13-M 14-N 15-O Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

19 Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Dynamic programming Longest common subsequence (LCS) Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

20 Longest common subsequence
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(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| = 3 C: C D A Longest common subsequence (LCS) A: C B E A D D E A B: D E C D B D A |C| = 4 C: E D D A Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

21 Longest common subsequence
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Longest common subsequence 1 2 8 3 4 5 6 7 An: (a1, a2, ..., an) A8: C B E A D D E A Bm: (b1, b2, ..., bm) Ck: (c1, c2, ..., ck) B7: D E C D B D A C4: E D D A Ck = LCS(An, Bm) Recursive rules: ( an = bm ) ==> (ck = an = bm) & (Ck-1 = LCS (An-1, Bm-1) ) 1 2 8 3 4 5 6 7 1 2 8 3 4 5 6 7 A8: C B E A D D E A A7: C B E A D D E A B7: D E C D B D A B6: D E C D B D A C4: E D D A C3: E D D A Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

22 Longest common subsequence
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Longest common subsequence ( an != bm ) & (ck != an ) ==> (Ck = LCS (An-1, Bm) ) 1 2 8 3 4 5 6 7 1 2 8 3 4 5 6 7 A7: C B E A D D E A6: C B E A D D E B6: D E C D B D B6: D E C D B D C3: E D D C3: E D D ( an != bm ) & (ck != bm ) ==> (Ck = LCS (An, Bm-1) ) 1 2 8 3 4 5 6 7 1 2 8 3 4 5 6 7 A5: C B E A D A5: C B E A D B5: D E C D B B4: D E C D B C2: E D C2: E D Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

23 Longest common subsequence
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Longest common subsequence Recursive function c(m, n) computes LCS length n = 0 or m = 0 C(n,m) = C(n-1, m-1) +1 n > 0, m > 0, an = bm max{ C(n-1, m), C(n, m-1) } n > 0, m > 0, an ≠ bm Dynamic programming strategy m C[n][m] for a in range( 1, n+1 ): for b in range( 1, m+1 ): C[a][b] = .... } n Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

24 Longest common subsequence
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Longest common subsequence Construction of 2D LCS array def findLCS(): for a in range( 1, n+1 ): for b in range( 1, m+1 ): if A[a] == B[b]: C[a][b] = C[a-1][b-1]+1 arrows[a][b] = DIAG else: if C[a-1][b] > C[a][b-1]: C[a][b] = C[a-1][b]; arrows[a][b] = UP C[a][b] = C[a][b-1]; arrows[a][b] = LEFT Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

25 Longest common subsequence
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Longest common subsequence 1 2 3 4 5 6 7 C B: D E C D B D A LCS array for “CBEADDEA” and “DECDBDA” 1 C 1 1 1 1 1 2 B 1 1 2 2 2 3 E 1 1 1 2 2 2 4 A 1 1 1 2 2 3 A: 5 D 1 1 1 2 2 3 3 6 D 1 1 1 2 2 3 3 7 E 1 2 2 2 2 3 3 8 A 1 2 2 2 2 3 4 Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …

26 Longest common subsequence
The complexity of different algorithms varies: O(n), Ω(n2), Θ(n·log2(n)), … Longest common subsequence LSC printout -- recursively :) def outLCS(a, b) { if a == 0 or b == 0 return if arrows[a][b] == DIAG: outLCS(a-1, b-1); // recursion ... print(A[a]) //... reverses the sequence! else: if arrows[a][b] == UP: outLCS(a-1,b); outLCS(a,b-1); } Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …


Stáhnout ppt "Různé algoritmy mají různou složitost: O(n), Ω(n2), Θ(n·log2(n)), …"

Podobné prezentace


Reklamy Google