cs4102 algorithms
play

CS4102 Algorithms Fall 2018 Warm up Compare + with + () When - PowerPoint PPT Presentation

CS4102 Algorithms Fall 2018 Warm up Compare + with + () When = () When = () 1 = O() () () () () + +


  1. CS4102 Algorithms Fall 2018 Warm up Compare 𝑔 π‘œ + 𝑛 with 𝑔 π‘œ + 𝑔(𝑛) When 𝑔 π‘œ = 𝑃(π‘œ) When 𝑔 π‘œ = Ξ©(π‘œ) 1

  2. 𝑔 π‘œ = O(π‘œ) 𝑔(𝑛) 𝑔(𝑛) 𝑔(π‘œ) 𝑔(π‘œ) π‘œ 𝑛 π‘œ + 𝑛 𝑔 π‘œ + 𝑛 ≀ 𝑔 π‘œ + 𝑔(𝑛) 2

  3. 𝑔 π‘œ = Ξ©(π‘œ) 𝑔(𝑛) 𝑔(𝑛) 𝑔(π‘œ) 𝑔(π‘œ) π‘œ 𝑛 π‘œ + 𝑛 𝑔 π‘œ + 𝑛 β‰₯ 𝑔 π‘œ + 𝑔(𝑛) 3

  4. 𝑔 π‘œ = Θ(π‘œ) 𝑔(𝑛) 𝑔(π‘œ) 𝑔(𝑛) 𝑔(π‘œ) π‘œ 𝑛 π‘œ + 𝑛 𝑔 π‘œ + 𝑛 = 𝑔 π‘œ + 𝑔(𝑛) 4

  5. Today’s Keywords β€’ Divide and Conquer β€’ Sorting β€’ Quicksort β€’ Median β€’ Order statistic β€’ Quickselect β€’ Median of Medians 5

  6. CLRS Readings β€’ Chapter 7 6

  7. More on HW2 Homeworks β€’ You must read from garden.txt file automatically (it’s a fixed filename) β€’ That file has a list of pairs of floats (not ints) β€’ Hw2 due 11pm Friday! β€’ You must only output one floating point number (minimum distance) – Programming (use Python or Java!) β€’ Uploaded files: β€’ One python file, or – Divide and conquer β€’ One or more java files (uploaded individually) – Closest pair of points β€’ Don’t use packages! β€’ Don’t use subdirectories! β€’ Hw3 released soon β€’ DO NOT upload a zip file! β€’ Try it yourself: – Divide and conquer β€’ Put the files you are going to upload in a directory (with a garden.txt file) – Written (use LaTeX!) β€’ python closestpair_mst3k.py β€’ javac *.java java ClosestPair β€’ Use the one for your language and you should get a result 7

  8. Quicksort β€’ Idea: pick a pivot element, recursively sort two sublists around that element β€’ Divide: select an element π‘ž , Partition( π‘ž ) β€’ Conquer: recursively sort left and right sublists β€’ Combine: Nothing! 8

  9. Partition (Divide step) β€’ Given: a list, a pivot π‘ž Start: unordered list 8 5 7 3 12 10 1 2 4 9 6 11 Goal: All elements < π‘ž on left, all > π‘ž on right 5 7 3 1 2 4 6 8 12 10 9 11 9

  10. Partition Summary 1. Put π‘ž at beginning of list 2. Put a pointer (Begin) just after π‘ž , and a pointer (End) at the end of the list 3. While Begin < End: 1. If Begin value < π‘ž , move Begin right 2. Else swap Begin value with End value, move End Left 4. If pointers meet at element < π‘ž : Swap π‘ž with pointer position 5. Else If pointers meet at element > π‘ž : Swap π‘ž with value to the left 10

  11. Quicksort Run Time β€’ If the pivot is always the median: 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 π‘œ) 11

  12. Quicksort Run Time β€’ If the partition is always unbalanced: 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 1 each time π‘ˆ π‘œ = π‘ˆ π‘œ βˆ’ 1 + π‘œ π‘ˆ π‘œ = 𝑃(π‘œ 2 ) 12

  13. Good Pivot β€’ What makes a good Pivot? – Roughly even split between left and right – Ideally: median β€’ Can we find median in linear time? – Yes! – Quickselect 13

  14. Quickselect β€’ Finds 𝑗 th order statistic – 𝑗 th smallest element in the list – 1 st order statistic: minimum – π‘œ th order statistic: maximum π‘œ – th order statistic: median 2 14

  15. Quickselect β€’ Finds 𝑗 th order statistic β€’ Idea: pick a pivot element, partition, then recurse on sublist containing index 𝑗 β€’ Divide: select an element π‘ž , Partition( π‘ž ) β€’ Conquer: if 𝑗 = index of π‘ž , done! – if 𝑗 < index of π‘ž recurse left. Else recurse right β€’ Combine: Nothing! 15

  16. Partition (Divide step) β€’ Given: a list, a pivot value π‘ž Start: unordered list 8 5 7 3 12 10 1 2 4 9 6 11 Goal: All elements < π‘ž on left, all > π‘ž on right 5 7 3 1 2 4 6 8 12 10 9 11 16

  17. Conquer 2 5 7 3 6 4 1 8 10 9 11 12 All elements > π‘ž All elements < π‘ž Exactly where it belongs! Recurse on sublist that contains index 𝑗 (add index of the pivot to 𝑗 if recursing right) 17

  18. Quickselect Run Time β€’ If the pivot is always the median: 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 + π‘œ 𝑇 π‘œ = 𝑃(π‘œ) 18

  19. Quickselect Run Time β€’ If the partition is always unbalanced: 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 1 each time 𝑇 π‘œ = 𝑇 π‘œ βˆ’ 1 + π‘œ 𝑇 π‘œ = 𝑃(π‘œ 2 ) 19

  20. Good Pivot β€’ What makes a good Pivot? – Roughly even split between left and right – Ideally: median β€’ Here’s what’s next: – An algorithm for finding a β€œrough” split – This algorithm uses Quickselect as a subroutine DΓ©jΓ  vu? 20

  21. Good Pivot β€’ What makes a good Pivot? – Both sides of Pivot >30% >30% Or Select Pivot from this range >30% 21

  22. Median of Medians β€’ Fast way to select a β€œgood” pivot β€’ Guarantees pivot is greater than 30% of elements and less than 30% of the elements β€’ Idea: break list into chunks, find the median of each chunk, use the median of those medians 22

  23. Median of Medians 1. Break list into chunks of size 5 2. Find the median of each chunk 3. Return median of medians (using Quickselect) 23

  24. Why is this good? Each chunk sorted, chunks ordered by their medians MedianofMedians < < < < is Greater than all of these < < < < < 5 < < < < < < < < < < < < < < π‘œ 5 24

  25. Why is this good? MedianofMedians < < < < is larger than all of these < < < < < < < < < < < < < < < < < < < Larger than 3 π‘œ things in each 5 (but one) list to 1 π‘œ 3π‘œ < 3 2 β‹… 5 βˆ’ 2 β‰ˆ 10 βˆ’ 6 elements the left 1 π‘œ 3π‘œ > Similarly: 3 2 β‹… 5 βˆ’ 2 β‰ˆ 10 βˆ’ 6 elements 25

  26. Quickselect β€’ Divide: select an element π‘ž using Median of Medians, Partition( π‘ž ) 𝑁 π‘œ + Θ(π‘œ) β€’ Conquer: if 𝑗 = index of π‘ž , done, if 𝑗 < index of π‘ž recurse left. Else recurse right 7 ≀ 𝑇 π‘œ 10 β€’ Combine: Nothing! 7 𝑇 π‘œ ≀ 𝑇 π‘œ + 𝑁 π‘œ + Θ(π‘œ) 10 26

  27. 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 + Θ(π‘œ) 27

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

  29. 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 π‘œ) 29

  30. Is it worth it? β€’ 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 30

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

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

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

  34. 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? 34

  35. 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 = π‘œ 𝑒 ! 35

  36. 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 36

  37. 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 37

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