CS4102 Algorithms Fall 2018 Warm up Compare π π + π with π π + π(π) When π π = π(π) When π π = Ξ©(π) 1
π π = O(π) π(π) π(π) π(π) π(π) π π π + π π π + π β€ π π + π(π) 2
π π = Ξ©(π) π(π) π(π) π(π) π(π) π π π + π π π + π β₯ π π + π(π) 3
π π = Ξ(π) π(π) π(π) π(π) π(π) π π π + π π π + π = π π + π(π) 4
Todayβs Keywords β’ Divide and Conquer β’ Sorting β’ Quicksort β’ Median β’ Order statistic β’ Quickselect β’ Median of Medians 5
CLRS Readings β’ Chapter 7 6
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Good Pivot β’ What makes a good Pivot? β Both sides of Pivot >30% >30% Or Select Pivot from this range >30% 21
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
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
Why is this good? Each chunk sorted, chunks ordered by their medians MedianofMedians < < < < is Greater than all of these < < < < < 5 < < < < < < < < < < < < < < π 5 24
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
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
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
Quickselect π π = π π π π β€ π 7π 5 + Ξ(π) 10 + π π + Ξ(π) = π 7π 10 + π π 5 + Ξ(π) = π 7π 10 + π 2π + Ξ(π) 10 β€ π 9π + Ξ(π) Because π π = Ξ©(π) 10 Master theorem Case 3! π π = O(π) 28
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
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
Quicksort Run Time β’ If the pivot is always π th order statistic: 10 10 + π 9π π π π = π + π 10 31
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
Quicksort Run Time β’ If the pivot is always π th order statistic: 10 10 + π 9π π π π = π + π 10 π π = Ξ(π log π) 33
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
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
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
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
Recommend
More recommend