CS4102 Algorithms Summer 2020 Warm up Show log π! = Ξ(π log π) Hint: show π! β€ π π π π 2 Hint 2: show π! β₯ 2 1
log π! = π π log π π! = π β π β 1 β π β 2 β β¦ β 2 β 1 = < < < < π π = π β π β π β β¦ β π β π π! β€ π π β log π! β€ log π π β log π! β€ π log π β log π! = π(π log π) 2
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
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
Quickselect π π = π π π π β€ π 7π 5 + Ξ(π) 10 + π π + Ξ(π) = π 7π 10 + π π 5 + Ξ(π) = π 7π 10 + π 2π + Ξ(π) 10 β€ π 9π + Ξ(π) Because π π = Ξ©(π) 10 Master theorem Case 3! π π = O(π) π π = Ξ(π) 5
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
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
Quicksort Run Time If the pivot is always π th order statistic: 10 10 + π 9π π π π = π + π 10 8
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
Quicksort Run Time If the pivot is always π th order statistic: 10 10 + π 9π π π π = π + π 10 π π = Ξ(π log π) 10
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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)
Recommend
More recommend