Download Week 5: Quicksort, Lower bound, Greedy and more Schemes and Mind Maps Data Structures and Algorithms in PDF only on Docsity!
Week 5: Quicksort, Lower bound, Greedy
Agenda:
โข Quicksort: Average case
โข Lower bound for sorting
โข Greedy method
Week 5: Quicksort
Recall Quicksort:
- The ideas:
- Pick one key
- Compare to others: partition into โsmallerโ and โgreaterโ sublists
- Recursively sort two sublists
- Pseudocode:
procedure Quicksort(A, p, r)
if p < r then q โ Partition(A, p, r) Quicksort(A, p, q โ 1) Quicksort(A, q + 1, r)
procedure Partition(A, p, r) ** A[r] is the key picked to do the partition
x โ A[r] i โ p โ 1 for j โ p to r โ 1 do if A[j] โค x then i โ i + 1 exchange A[i] โ A[j] exchange A[i + 1] โ A[r] return i + 1
Week 5: Quicksort
Quicksort correctness:
- It follows from the correctness of Partition.
- Partition correctness:
- Loop invariant: At the start of for loop:
- A[p..i] โค A[r] โ A[s] โค A[r], p โค s โค i
- A[(i + 1)..(j โ 1)] > A[r]
- x = A[r]
- Proof of LI: (pages 147 โ 148)
- Initialization
- Maintenance
- Termination
- LI correctness implies Partition correctness
- Why we study QuickSort and its analysis:
- very efficient, in use
- divide-and-conquer, randomization
- huge literature
- a model for analysis of algorithms
Week 5: Quicksort analysis
Quicksort recursion tree:
- Observations:
- (Again) key comparison is the dominant operation
- Counting KC โ only need to know (at each call) the rank of the split key
- An example: 06
04 09
02 05 07 12
01 03 08 11 13
10 14
15
- More observations:
- In the resulting recursion tree, at each node (all keys in left subtree) โค (key in this node) < (all keys in right subtree)
- 1-1 correspondence: quicksort recursion tree โโ binary search tree
Week 5: Quicksort analysis
Quicksort BC running time:
- Notice that when both subarrays are non-empty, we will be saving 1 KC ...
- Best case: each partition is a bipartition !!! Saving as many KC as possible every level ... The recursion tree is as short as possible ...
- Recurrence:
T (n) = 2 ร T (
n โ 1 2
) + (n โ 1),
- Solving the recurrence โ apply Master Theorem? not exactly T (n) โ ฮ(n log n)
- Question:
- What is the best case array? for n = 7?
- Conclusion:
- In order to save time, A[n] better BI-partitions the array ... โ usually it might not bipartition ... we will push it by a technique called randomization (future lectures)
Week 5: Quicksort
Quicksort BC running time (contโd):
- In the recursion tree, what is the number of KC at each level? Answer: - n โ 1 at the top level - at most 2 nodes at the 2nd level, at least (n 1 โ 1) + (n โ 1 โ n 1 โ 1) = n โ 3 KC - at most 4 nodes at the 3rd level, at least (n 1 โ 3) + (n โ 1 โ n 1 โ 3) = n โ 7 KC -... - at kth level, at most 2kโ^1 nodes, at least n โ 2 k^ + 1 KC
- How many levels are there? Answer: - At least lg n levels โ binary tree
- So, at least we need
โlg nโ 1
i=1 (n^ โ^2
i (^) + 1) KC, and
lg โ nโ 1
i=
(n โ 2 i^ + 1) = (n + 1)(lg n โ 1) โ (n โ 2) โ ฮ(n log n)
- Try n = 2k^ โ 1 to get the closed form for the following recur- rence
T (n) =
0 , if n = 1 (n โ 1) + T (bnโ 2 1 c) + T (dnโ 2 1 e), if n โฅ 2
Week 5: Quicksort
Solving T (n):
T (n) = (^) n^1 (T (0) + T (n โ 1))
- (^1) n (T (1) + T (n โ 2))
+...
= (^) n^2
โnโ 1
i=0 T^ (i) + (n^ โ^ 1)
โnโ 1
i=0 T^ (i) +^ n(n^ โ^ 1)
- (n โ 1) ร T (n โ 1) = 2
โnโ 2
i=0 T^ (i) + (n^ โ^ 1)(n^ โ^ 2)
n ร T (n) โ (n โ 1) ร T (n โ 1) = 2T (n โ 1) + 2(n โ 1)
Rearrange it:
nT (n) = (n + 1)T (n โ 1) + 2(n โ 1)
Week 5: Quicksort
Solving T (n) (contโd):
- Or we can say: T (n) n+1 =^
T (nโ1) n +^
2(nโ1) n(n+1)
= T^ (n nโ 1)+ (^) n+1^2 โ 2(^1 n โ (^) n+1^1 )
= T^ (n nโ 1)+ (^) n+1^4 โ (^2) n
which gives you (iterated substitution)
T (n) n + 1
โ^ n
i=
i + 1
n + 1
Recall that
โn
i=
1 i =^ Hn^ = ln^ n^ +^ ฮณ^ โ the Harmonic number where ฮณ โ 0. 577 ยท ยท ยท
T (n) n + 1
โ^ n
i=
i + 1
n + 1
we have T (n) = 2(n + 1)Hn+1 โ (4n + 2)
โ 2(n + 1)(ln(n + 1) + ฮณ) โ (4n + 2)
โ ฮ(n log n)
- Conclusion: Quicksort AC running time in ฮ(n log n).
Week 5: Lower Bounds for Comparison-Based Sorting
Two useful trees in algorithm analysis:
- Recursion tree
- node โโ recursive call
- describes algorithm execution for one particular input by showing all calls made
- one algorithm execution โโ all nodes (a tree)
- useful in analysis: sum the numbers of operations over all nodes
Week 5: Lower Bounds for Comparison-Based Sorting
Recursion tree example:
Merge(A; lo, mid, hi) **pre-condition: lo โค mid โค hi **pre-condition: A[lo, mid] and A[mid + 1, hi] sorted **post-condition: A[lo, hi] sorted
MergeSort(A; lo, hi)
if lo < hi then mid โ b(lo + hi)/ 2 c MergeSort(A; lo, mid) MergeSort(A; mid + 1, hi) Merge(A; lo, mid, hi)
[1]
[2] [3]
[4]
[5] [6]
- โข For different input instance, the number of operations at each node could be different.
Week 5: Lower Bounds for Comparison-Based Sorting
Selectionsort decision tree:
- Assume input keys in array A[1..3] = {a, b, c}
- Tree node: if A[k] > A[j] โ 2-way key comparison
- Node label A[j]
SelectionSort(A; n)
if n โฅ 1 then for j โ n downto 2 do psn โ j for k โ j โ 1 downto 1 do if A[k] > A[psn] then psn โ k exchange A[j] โ A[psn] return A[2] > A[3]?
No. Yes.
abc bac bca acb cab cba
- In every case โ whatever input instance is, 3 KC !!!
Week 5: Lower Bounds for Comparison-Based Sorting
Sorting lower bound:
- Comparison-based sort: keys can be (2-way) compared only!
- This lower bound argument considers only the comparison- based sorting algorithms. For example, - Insertionsort, Mergesort, Heapsort, Quicksort,
- Binary tree facts:
- Suppose there are t leaves and k levels. Then,
- t โค 2 kโ^1
- So, lg t โค (k โ 1)
- Equivalently, k โฅ 1 + lg t โ binary tree with t leaves has at least (1 + lg t) levels
- Comparison-based sorting algorithm facts:
- Look at its Decision Tree. We have,
- Itโs a binary tree.
- It should contain every possible permutation of the posi- tions { 1 , 2 ,... , n}.
- So, it contains at least n! leaves ...
- Equivalently, it has at least 1 + lg(n!) levels.
- A longest root-to-leaf path of length at least lg(n!).
- The worst case number of KC is at least lg(n!).
- lg(n!) โ ฮ(n log n)
- Therefore, Mergesort and Heapsort are asymptotically optimal (comparison-based) sorting algorithms.
Week 5: Greedy method
Example 1: Fractional Knapsack
- Suppose we have a set S of n items, each with a profit/value bi and weight wi.
- We also have a knapsack of capacity W ,
- Assume that each item can be picked at any fraction, that is we can pick 0 โค xi โค wi amount of item i.
- Our goal is to fill the knapsack (without exceeding its capac- ity) with a combination of the items with maximum profit.
- Formally, find 0 โค xi โค wi for 1 โค i โค n such that
โn
i=1 xi^ โค^ W and
โn
i=
xi wi ร^ bi^ is maximized.
- Greedy idea: start picking the items with more โvalueโ:
value โก
bi wi So let vi = (^) wbii. The algorithm will be as follows:
Week 5: Greedy method
Procedure Frac-Knapsack (S, W )
for i โ 1 to n do xi โ 0 vi โ (^) wbii CurrentW โ 0 While CurrentW < W do let ai be the next item in S with highest value xi โ min{wi, W โ CurrentW } add xi amount of i to knapsack CurrentW โ CurrentW + xi
- How to find next highest value in each step?
- One way is to sort S at the begining based on viโs in non- increasing order.
- Another way is to keep a PQ (max-heap) based on values.
- Since we check at most n items, the total time is O(n log n).
Correctness of the algorithm:
- We prove, for all i โฅ 0, that if we have picked x 1 ,... , xi from items 1,... , i in the first i iterations (respectively), then this partial solution can be extended to an optimal solution.
- In other words, there is some optimal solution call it OPT which has xj amount from item j for 1 โค j โค i.