1 / 47

1 / 3

X36DSA 2005. The complexity of different algorithms varies: O(n) , Ω (n 2 ) , Θ ( n · log 2 (n)) , …. 1 / 3. DSA. Různé algoritmy mají různou složitost. Různé algoritmy mají různou složitost : O(n) , Ω (n 2 ) , Θ ( n · log 2 (n)) , …. X36DSA 2005.

willis
Download Presentation

1 / 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 1 / 3 DSA Různé algoritmy mají různou složitost Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  2. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 2 / 3 DSA The complexity of different algorithms varies Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  3. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 3 / 3 Stable sort sorted sorted sorted Cook Amundsen Andrew Andrew Andrew Barbara Amundsen Amundsen Charles Amundsen Andrew Brown Barbara Andrew Brown Cook Brown Barbara Brown Barbara Sort by Amundsen Charles Brown Barbara family name only Andrew Cook Charles Amundsen Charles Cook Barbara Cook Cook Charles Brown Charles Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  4. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 4 / 3 Unstable sort sorted sorted Barbara Amundsen Cook Andrew Andrew Amundsen Andrew Amundsen QuickSort Charles Amundsen Andrew Brown Barbara Brown Barbara Cook Charles Brown Barbara Brown Sort by Andrew Brown Amundsen Barbara family name only Charles Cook Charles Amundsen Andrew Cook Charles Cook Barbara Cook Charles Brown Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  5. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 5 / 3 Sorting Heap Sort Řazení haldou Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  6. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 6 / 3 a    a b a c c b Heap A Heap B D M J T E Z O K R U Heap rule Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  7. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 7 / 3 a c b Heap Terminology ...... predcessor, parent of a b c ...... předchůdce, rodič , ...... successor, child of b c a ...... následník, potomek ...... (heap) top ...... vrchol (haldy) A D B Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  8. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 8 / 3 10 11 12 4 8 3 2 5 1 9 7 6 2k Heap in an array Heap stored in an array A B D r T M J E k t s Z O K R U 2k+1 succesors succesors 1 2 3 4 5 6 7 8 9 10 11 12 k 2k 2k+1 s t A U B D M J T E Z O K R r Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  9. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 9 / 3 1 1 2 2 3 → last first Repair a heap Top removed (1) insert top remove top A ! A U ! D B D B M M J T E J T E O Z O K R U Z K R U > B, U > D, B < D Swap B↔U Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  10. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 10 / 3 3 Repair a heap Top removed (2) insert top - continues B B A A J D ! U ! D M U T E M J T E ! ! Z O K R Z O K R U > M, U > J, J < M U > K, U > R, K < R Swap J↔U Swap K↔U Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  11. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 11 / 3 3 Repair a heap Top removed (3) B insert top - done A J D M T E K Z O U R Heap, OK Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  12. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 12 / 3 1 1 2 3 → last first Repair a heap Top removed II (1) insert top remove top B A R ! B A ! J D J D M T E K M T K E Z O U R Z O U R > J, R > D, D < J Swap D↔R Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  13. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 13 / 3 3 3 Repair a heap Top removed II (2) Top removed II (3) insert top - continues insert top - done B A D B A D J R J E ! M T E K M K T R Z O U Z O U R < T, R > E Swap E↔R Heap, OK Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  14. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 14 / 3 A B D K M J T E J E D B A Z O K R U Heap Sort I R J U Z B T E M O K A D Unsorted II III for (i = 0; i < n; i++) a[i] = “Remove top”; Create a heap R M Z O T U IV Z U T R O M K J E D B A Sorted Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  15. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 15 / 3 Heap recursive property A D B M J T E Z O K R U is a heap  is a heap and is a heap Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  16. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 16 / 3 Create a heap Z A B heap heap not a heap Z > A or Z > B Swap: Z↔min(A,B) Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  17. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 17 / 3 C F Create a heap A B Z heap heap heap not a heap not a heap Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  18. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 18 / 3 1 1 3 4 5 6 3 2 4 5 6 2 Create a heap Make a heap here… R … make a heap here… J U … make a heap here… … make a heap here… Z B T E … make a heap here… M O K A D … make a heap here. Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  19. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 19 / 3 10 11 12 4 8 3 2 5 1 9 7 6 2k Heap in an array Heap stored in an array A B D r T M J E k t s Z O K R U 2k+1 succesors succesors 1 2 3 4 5 6 7 8 9 10 11 12 k 2k 2k+1 s t A U B D M J T E Z O K R r Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  20. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 20 / 3 1 2 3 4 5 11 10 12 5 2 5 3 8 4 3 4 1 7 4 6 5 9 2 1 2 6 1 6 3 6 7 8 9 10 11 12 Create a heap in an array Unsorted Not a heap R R J J U U Z Z B B T T E E M M O O K K A A D D Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  21. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 21 / 3 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 2 3 1 5 4 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 9 9 9 9 9 10 10 10 10 10 11 11 11 11 11 12 12 12 12 12 Create a heap in an array Creating a heap R R R R R J J J J J U U U U D Z Z M M Z B B A A A T D D D T E E E E E M M M Z Z O O O O O K K K K K A A B B B D T T T U Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  22. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 22 / 3 1 1 2 2 3 3 4 4 5 5 11 12 10 7 5 8 3 2 9 1 4 6 6 6 6 7 7 8 8 9 9 10 10 11 11 12 12 Create a heap in an array Creating a heap Heap R A A A B B D D D M M M B J J T T T E E E Z Z Z O O O K K K J R R U U U Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  23. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 23 / 3 Heap sort A B D M J T E Z O K R U Heap A B D M J T E Z O K R U Step 1 1 2 3 4 5 6 7 8 9 10 11 12 U A B D M J T E Z O K R A B J D M K T E Z O U R heap Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  24. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 24 / 3 Heap Sort Step k A J K R M O T U Z E D B 1 2 3 4 5 6 7 8 9 10 11 12 A Z K R M O T U J E D B A B D K M R Z O T U E J heap k Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  25. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 25 / 3 Heap Sort // array: a[1]...a[n] !!!! void heapSort(Item a[], int size) { int i, j, // create a heap for (i = size/2; i > 0; i--) repairTop(array, i, size); // sort for (i = size; i > 1; i--) { swap(a, 1, i); repairTop(array, 1, i-1); } } Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  26. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 26 / 3 Heap Sort // array: a[1]...a[n] !!!!!! void repairTop(Item a[], int top, int bott) { int i = top; // a[2*i] and a[2*i+1] int j = i*2; // are successors of a[i] Item topVal = a[top]; // try to find a successor < topVal if ((j < bott) && (a[j] > a[j+1])) j++; // while (successors < topVal) // move successors up while ((j <= bott) && (topVal > a[j])) { a[i] = a[j]; i = j; j = j*2; // skip to next successor if ((j < bott) && (a[j] > a[j+1])) j++; } a[i] = topVal; // put the topVal } Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  27. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 27 / 3 (n·log2(n)) (n·log2(n)) Heap Sort repairTop operation worst case ... log2(n) (n=heap size) create a heap ... n/2 repairTop operations log2(n/2) + log2(n/2+1) + ... + log2(n)  (n/2)(log2(n)) = (n·log(n)) sort ... n-1 repairTop operations, worst case: log2(n) + log2(n-1) + ... + 1  n · log2(n) = (n·log2(n)) But even best case = total ... create heap + sort = (n·log2(n)) Asymptotic complexity of Heap sort is It is not a stable sort Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  28. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 28 / 3 Sorting Merge Sort Řazení sléváním Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  29. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 29 / 3 Merge Sort Merge two sorted arrays Compare elements B K M T U Z A D E J O R B K M T U Z A A D E J O R B K M T U Z B A A D E J O R Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  30. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 30 / 3 Merge Sort Merge two sorted arrays – cont. B K M T U Z B D A A D E J O R B K M T U Z B D E A A D E J O R B K M T U Z B D E J A A D E J O R Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  31. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 31 / 3 Merge Sort Merge two sorted arrays B K M T U Z B D E J K A A D E J O R B K M T U Z B D E J K M A A D E J O R B K M T U Z B D E J K M O A A D E J O R Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  32. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 32 / 3 Merge Sort Merge two sorted arrays - cont Copy the rest B K M T U Z R B D E J K M O A A D E J O R Sorted B K M T U Z R B D E J K M O T U Z A A D E J O R Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  33. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 33 / 2 The problem The problem The problem The solution The solution The solution Divide & Conquer! Divide et Impera! their work The problem our work Divide! Solve the subproblem Solve the subproblem Merge! Merge! Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  34. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 34 / 3 Merge Sort Unsorted K Z U B M T E A J R D O Divide! K Z U B M T E A J R D O Process separately Process separately Sort! Sort! B K M T U Z A D E J O R Conquer! Merge! J R B D E K M O T U Z A Sorted Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  35. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 35 / 3 … … … … … … … … Merge Sort Unsorted K Z U B M T E A J R D O Divide! K Z U B M T E A J R D O Divide! Divide! Merge! Merge! K U Z B M T A E J D O R Merge! B K M T U Z A D E J O R Sorted Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  36. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 36 / 3 Merge Sort void mergeSort (int a[], int aux[], int low, int high) { int half = (low+high)/2; int i; if (low >= high) return; // too small! // sort mergeSort(a, aux, low, half); // left half mergeSort(a, aux, half+1, high); // right half merge(a, aux, low, high); // merge halves // put result back to a for (i = low; i <= high; i++) a[i] = aux[i]; // optimization idea: /* swapArray(a, aux) */ // better to swap // references to a & aux! } Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  37. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 37 / 3 Merge Sort void merge(int in[], int out[], int low, int high) { int half = (low+high)/2; int i1 = low; int i2 = half+1; int j = low; // compare and merge while ((i1 <= half) && (i2 <= high)) { if (in[i1] <= in[i2]) { out[j] = in[i1]; i1++; } else { out[j] = in[i2]; i2++; } j++; } // copy the rest while (i1 <= half) { out[j] = in[i1]; i1++; j++; } while (i2 <= high) { out[j] = in[i2]; i2++; j++; } } Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  38. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 38 / 3 Merge Sort Asymptotic complexity  log2(n) times Divide! ........  log2(n) times Merge! ........ Divide! ........ (1) operations Merge! ........ (n) operations (n)· (log2(n)) = (n·log2(n))operations Total ........ Asymptotic complexity of MergeSort is(n·log2(n)) Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  39. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 39 / 3 Merge Sort Stability Divide! ........ Does not move the elements Nepohybuje prvky Merge! ….... “ if (in[i1] <= in[i2]) { out[j] = in[i1]; …” process the left element first when mergingequal values Zařaď nejprve levý prvek když slučuješ stejné hodnoty MergeSort is a stable sort Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  40. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 40 / 3 Sorting Radix Sort Přihrádkové řazení Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  41. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 41 / 3 11 10 13 12 5 9 1 6 8 7 3 4 2 Radix sort Unsorted Sort by 3rd digit Cbb DaD _ _ a B _ _ b _ _ C _ _ D aDb DCa 4 DCa 1 2 11 1 Cbb 3 5 CCC 8 DaD CCC 12 Cba 2 aDb 4 aDC 9 6 aDD 12 3 8 aDD DDb 5 bbC 10 11 DbD 13 7 9 DDb bab 6 10 aDC aCb 13 7 bbC bab DbD Cba aCb Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  42. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 42 / 3 13 12 12 11 10 10 13 11 3 1 5 9 1 6 2 7 8 9 8 2 3 4 5 6 7 4 Radix sort Sorted by 3rd digit Sort by 2nd digit DCa Cba _ a _ B _ b _ _ C _ _ D_ Cbb aDb 1 bab Cba 3 DCa 7 aDb 10 DDb DaD 2 Cbb 4 aCb 8 DDb 11 bab 9 bbC 5 CCC aDC 12 aCb DbD 6 aDD 13 CCC aDC bbC DaD aDD DbD Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  43. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 43 / 3 13 13 12 12 10 10 11 11 7 7 8 5 9 1 6 2 3 4 4 5 6 1 2 3 9 8 Radix sort Sorted by 2nd digit Sort by 1st digit bab DaD a _ _ B b _ _ C _ _ D_ _ Cba Cbb 1 aCb bab 5 Cba 7 DaD 10 bbC aDb 2 bbC Cbb 8 DbD 11 6 DbD 9 aDC 3 CCC DCa 12 DCa 4 aDD DDb 13 aCb CCC aDb DDb aDC Sorted! aDD Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  44. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 44 / 3 13 12 10 11 6 9 7 5 2 8 4 3 1 Radix sort implementation Unsorted Sorted by 3rd digit Array data structure Cbb _ _ a B start & end next DaD 4 DCa 1 _ _ C aDb 12 Cba 1 3 3 2 4 12 a 5 CCC 8 DCa 6 2 1 13 b aDC 9 3 7 CCC 8 12 5 9 4 C bbC 10 aDD _ _ b 9 5 8 D 2 11 DDb 11 1 Cbb 3 6 aDC 7 10 _ _ D aDb 4 3 8 9 bbC DDb 5 7 11 DaD 9 0 2 bab bab 6 10 10 13 aDD 12 6 DbD aCb 13 7 11 0 DbD 13 11 Cba 12 0 Example Example 13 0 aCb Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  45. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 45 / 3 Summary d digits ............ d loops (n)operations loop ............ (d·n)operations total ............ d << n  ..…. (n)operations RadixSort never changes order of equal values Přihrádkové řazení nemění pořadí stejných hodnot Asymptotic complexity of RadixSort is (n) It is a stable sort Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  46. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 46 / 3 DSA Různé algoritmy mají různou složitost Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

  47. X36DSA 2005 The complexity of different algorithms varies: O(n),Ω(n2),Θ(n·log2(n)), … 47 / 3 DSA The complexity of different algorithms varies Různé algoritmy mají různou složitost: O(n),Ω(n2),Θ(n·log2(n)), …

More Related