cs4102 algorithms
play

CS4102 Algorithms Summer 2020 Warm up Show log ! = ( log ) Hint: - PowerPoint PPT Presentation

CS4102 Algorithms Summer 2020 Warm up Show log ! = ( log ) Hint: show ! 2 Hint 2: show ! 2 1 log ! = log ! = 1 2 2 1 = <


  1. CS4102 Algorithms Summer 2020 Warm up Show log π‘œ! = Θ(π‘œ log π‘œ) Hint: show π‘œ! ≀ π‘œ π‘œ π‘œ π‘œ 2 Hint 2: show π‘œ! β‰₯ 2 1

  2. log π‘œ! = 𝑃 π‘œ log π‘œ π‘œ! = π‘œ β‹… π‘œ βˆ’ 1 β‹… π‘œ βˆ’ 2 β‹… … β‹… 2 β‹… 1 = < < < < π‘œ π‘œ = π‘œ β‹… π‘œ β‹… π‘œ β‹… … β‹… π‘œ β‹… π‘œ π‘œ! ≀ π‘œ π‘œ β‡’ log π‘œ! ≀ log π‘œ π‘œ β‡’ log π‘œ! ≀ π‘œ log π‘œ β‡’ log π‘œ! = 𝑃(π‘œ log π‘œ) 2

  3. log π‘œ! = Ξ© π‘œ log π‘œ π‘œ! = π‘œ β‹… π‘œ βˆ’ 1 β‹… π‘œ βˆ’ 2 β‹… … β‹… π‘œ 2 β‹… π‘œ 2 βˆ’ 1 β‹… … β‹… 2 β‹… 1 > > > = > > = π‘œ π‘œ 2 = π‘œ 2 β‹… π‘œ 2 β‹… π‘œ 2 β‹… … β‹… π‘œ 2 β‹… 1 β‹… … β‹… 1 β‹… 1 2 π‘œ π‘œ! β‰₯ π‘œ 2 2 π‘œ π‘œ 2 β‡’ log π‘œ! β‰₯ log 2 β‡’ log π‘œ! β‰₯ π‘œ 2 log π‘œ 2 β‡’ log π‘œ! = Ξ©(π‘œ log π‘œ) 3

  4. Median of Medians, Run Time Θ(π‘œ) 1. Break list into chunks of 5 Θ(π‘œ) 2. Find the median of each chunk 3. Return median of medians (using Quickselect) 𝑇 π‘œ 5 𝑁 π‘œ = 𝑇 π‘œ 5 + Θ(π‘œ) 4

  5. Quickselect 𝑁 π‘œ = 𝑇 π‘œ 𝑇 π‘œ ≀ 𝑇 7π‘œ 5 + Θ(π‘œ) 10 + 𝑁 π‘œ + Θ(π‘œ) = 𝑇 7π‘œ 10 + 𝑇 π‘œ 5 + Θ(π‘œ) = 𝑇 7π‘œ 10 + 𝑇 2π‘œ + Θ(π‘œ) 10 ≀ 𝑇 9π‘œ + Θ(π‘œ) Because 𝑇 π‘œ = Ξ©(π‘œ) 10 Master theorem Case 3! 𝑇 π‘œ = O(π‘œ) 𝑇 π‘œ = Θ(π‘œ) 5

  6. Phew! Back to Quicksort Using Quickselect, with a median-of-medians partition: 2 5 1 3 6 4 7 8 10 9 11 12 2 1 3 5 6 4 7 8 9 10 11 12 Then we divide in half each time π‘ˆ π‘œ = 2π‘ˆ π‘œ 2 + Θ(π‘œ) π‘ˆ π‘œ = Θ(π‘œ log π‘œ) 6

  7. Random Pivot β€’ Using Quickselect to pick median guarantees Θ(π‘œ log π‘œ) run time – Approach has very large constants – If you really want Θ(π‘œ log π‘œ) , better off using MergeSort β€’ Better approach: Random pivot – Very small constant (very fast algorithm) – Expected to run in Θ(π‘œ log π‘œ) time β€’ Why? Unbalanced partitions are very unlikely – Other options: Median of 5 7

  8. Quicksort Run Time If the pivot is always π‘œ th order statistic: 10 10 + π‘ˆ 9π‘œ π‘œ π‘ˆ π‘œ = π‘ˆ + π‘œ 10 8

  9. 10 + π‘ˆ 9π‘œ π‘œ π‘ˆ π‘œ = π‘ˆ 10 + π‘œ π‘œ π‘œ π‘œ π‘œ/10 9π‘œ/10 π‘œ π‘œ 10 9π‘œ 10 + 9π‘œ/100 81π‘œ/100 9π‘œ/ 100 π‘œ/100 π‘œ 100 9π‘œ 100 9π‘œ 100 81π‘œ 100 π‘œ + + + log 10 π‘œ … … … … 9 1 1 + 1 1 1 + 1 1 + 1

  10. Quicksort Run Time If the pivot is always π‘œ th order statistic: 10 10 + π‘ˆ 9π‘œ π‘œ π‘ˆ π‘œ = π‘ˆ + π‘œ 10 π‘ˆ π‘œ = Θ(π‘œ log π‘œ) 10

  11. Quicksort Run Time If the pivot is always 𝑒 th order statistic: 1 5 2 3 6 4 7 8 10 9 11 12 1 2 3 5 6 4 7 8 10 9 11 12 Then we shorten by 𝑒 each time π‘ˆ π‘œ = π‘ˆ π‘œ βˆ’ 𝑒 + π‘œ π‘ˆ π‘œ = 𝑃(π‘œ 2 ) What’s the probability of this occurring? 11

  12. Probability of π‘œ 2 run time We must consistently select pivot from within the first 𝑒 terms 𝑒 Probability first pivot is among 𝑒 smallest: π‘œ 𝑒 Probability second pivot is among 𝑒 smallest: π‘œβˆ’π‘’ Probability all pivots are among 𝑒 smallest: 𝑒 𝑒 π‘œ βˆ’ 2𝑒 β‹… … β‹… 𝑒 𝑒 1 π‘œ β‹… π‘œ βˆ’ 𝑒 β‹… 2𝑒 β‹… 1 = π‘œ 𝑒 ! 12

  13. Formal Argument for π‘œ log π‘œ Average β€’ Remember, run time counts comparisons! β€’ Quicksort only compares against a pivot – Element 𝑗 only compared to element π‘˜ if one of them was the pivot 13

  14. Formal Argument for π‘œ log π‘œ Average β€’ What is the probability of comparing two given elements? 1 2 3 4 5 6 7 8 9 10 11 12 β€’ (Probability of comparing 3 and 4) = 1 – Why? Otherwise I wouldn’t know which came first – ANY sorting algorithm must compare adjacent elements 14

  15. Formal Argument for π‘œ log π‘œ Average β€’ What is the probability of comparing two given elements? 1 2 3 4 5 6 7 8 9 10 11 12 β€’ (Probability of comparing 1 and 12) = 2 12 – Why? β€’ I only compare 1 with 12 if either was chosen as the first pivot β€’ Otherwise they would be divided into opposite sublists 15

  16. Formal Argument for π‘œ log π‘œ Average β€’ Probability of comparing 𝑗 with π‘˜ ( π‘˜ > 𝑗 ): – dependent on the number of elements between 𝑗 and π‘˜ 1 – π‘˜βˆ’π‘—+1 β€’ Expected number of comparisons: 1 – 𝑗<π‘˜ π‘˜βˆ’π‘—+1 16

  17. Expected number of Comparisons 1 Consider when 𝑗 = 1 π‘˜ βˆ’ 𝑗 + 1 𝑗<π‘˜ 1 2 3 4 5 6 7 8 9 10 11 12 Compared if 1 or 2 are chosen as pivot (these will always be compared) 2 Sum so far: 2 17

  18. Expected number of Comparisons 1 Consider when 𝑗 = 1 π‘˜ βˆ’ 𝑗 + 1 𝑗<π‘˜ 1 2 3 4 5 6 7 8 9 10 11 12 Compared if 1 or 3 are chosen as pivot (but never if 2 is ever chosen) 2 2 2 + Sum so far: 3 18

  19. Expected number of Comparisons 1 Consider when 𝑗 = 1 π‘˜ βˆ’ 𝑗 + 1 𝑗<π‘˜ 1 2 3 4 5 6 7 8 9 10 11 12 Compared if 1 or 4 are chosen as pivot (but never if 2 or 3 are chosen) 2 2 2 2 + 3 + Sum so far: 4 19

  20. Expected number of Comparisons 1 Consider when 𝑗 = 1 π‘˜ βˆ’ 𝑗 + 1 𝑗<π‘˜ 1 2 3 4 5 6 7 8 9 10 11 12 Compared if 1 or 12 are chosen as pivot (but never if 2 -> 11 are chosen) 2 2 2 2 2 2 + 3 + 4 + 5 + β‹― + Overall sum: π‘œ 20

  21. Expected number of Comparisons 1 π‘˜ βˆ’ 𝑗 + 1 𝑗<π‘˜ 1 1 1 1 When 𝑗 = 1 : 2 2 + 3 + 4 + β‹― + π‘œ π‘œ terms overall 1 ≀ 2π‘œ 1 2 + 1 3 + β‹― + 1 Θ(log π‘œ) π‘œ π‘˜ βˆ’ 𝑗 + 1 𝑗<π‘˜ Quicksort overall: expected Θ π‘œ log π‘œ 21

  22. Sorting, so far β€’ Sorting algorithms we have discussed: 𝑃(π‘œ log π‘œ) – Mergesort 𝑃(π‘œ log π‘œ) – Quicksort β€’ Other sorting algorithms (will discuss): 𝑃(π‘œ 2 ) – Bubblesort 𝑃(π‘œ 2 ) – Insertionsort 𝑃(π‘œ log π‘œ) – Heapsort Can we do better than 𝑃(π‘œ log π‘œ) ? 22

  23. Worst Case Lower Bounds β€’ Prove that there is no algorithm which can sort faster than 𝑃(π‘œ log π‘œ) – Every algorithm, in the worst case, must have a certain lower bound β€’ Non-existence proof! – Very hard to do 23

  24. Strategy: Decision Tree β€’ Sorting algorithms use comparisons to figure out the order of input elements β€’ Draw tree to illustrate all possible execution paths One comparison Result of Possible comparison execution path < >or<? > >or<? >or<? < < > > >or<? >or<? >or<? >or<? < < > < > < > > >or<? >or<? >or<? >or<? >or<? >or<? >or<? >or<? … … … … Permutation … … [1,2,3,4,5] [2,1,3,4,5] [5,2,4,1,3] [5,4,3,2,1] of sorted list 24

  25. Strategy: Decision Tree β€’ Worst case run time is the longest execution path β€’ i.e., β€œheight” of the decision tree One comparison Result of Possible comparison execution path < >or<? > >or<? >or<? < < > > >or<? >or<? >or<? >or<? < < > < > < > > log π‘œ! >or<? >or<? >or<? >or<? >or<? >or<? >or<? >or<? Θ(π‘œ log π‘œ) … … … … Permutation … … [1,2,3,4,5] [2,1,3,4,5] [5,2,4,1,3] [5,4,3,2,1] of sorted list π‘œ! Possible permutations 25

  26. Strategy: Decision Tree β€’ Conclusion: Worst Case Optimal run time of sorting is Θ(π‘œ log π‘œ) – There is no (comparison-based) sorting algorithm with run time 𝑝(π‘œ log π‘œ) One comparison Result of Possible comparison execution path < >or<? > >or<? >or<? < < > > >or<? >or<? >or<? >or<? < < > < > < > > log π‘œ! >or<? >or<? >or<? >or<? >or<? >or<? >or<? >or<? Θ(π‘œ log π‘œ) … … … … Permutation … … [1,2,3,4,5] [2,1,3,4,5] [5,2,4,1,3] [5,4,3,2,1] of sorted list π‘œ! Possible permutations 26

  27. Sorting, so far β€’ Sorting algorithms we have discussed: 𝑃(π‘œ log π‘œ) – Mergesort Optimal! 𝑃(π‘œ log π‘œ) – Quicksort Optimal! β€’ Other sorting algorithms (will discuss): 𝑃(π‘œ 2 ) – Bubblesort 𝑃(π‘œ 2 ) – Insertionsort 𝑃(π‘œ log π‘œ) – Heapsort Optimal! 27

  28. Speed Isn’t Everything β€’ Important properties of sorting algorithms: β€’ Run Time – Asymptotic Complexity – Constants β€’ In Place (or In-Situ) – Done with only constant additional space β€’ Adaptive – Faster if list is nearly sorted β€’ Stable – Equal elements remain in original order β€’ Parallelizable – Runs faster with multiple computers 28

  29. Mergesort β€’ Run Time? Divide: – Break π‘œ -element list into two lists of π‘œ 2 elements Θ(π‘œ log π‘œ) β€’ Conquer: – If π‘œ > 1 : Sort each sublist recursively Optimal! – If π‘œ = 1 : List is already sorted (base case) β€’ Combine: – Merge together sorted sublists into one sorted list In Place? Adaptive? Stable? No No Yes! (usually)

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend