Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Luebke 1 2/19/2016 Priority Queues Quicksort.

Similar presentations


Presentation on theme: "David Luebke 1 2/19/2016 Priority Queues Quicksort."— Presentation transcript:

1 David Luebke 1 2/19/2016 Priority Queues Quicksort

2 David Luebke 2 2/19/2016 Priority Queues l Heapsort is a nice algorithm, but in practice Quicksort (coming up) usually wins l But the heap data structure is incredibly useful for implementing priority queues n A data structure for maintaining a set S of elements, each with an associated value or key Supports the operations Insert(), Maximum(), and ExtractMax() n What might a priority queue be useful for?

3 David Luebke 3 2/19/2016 Priority Queue Operations l Insert(S, x) inserts the element x into set S l Maximum(S) returns the element of S with the maximum key l ExtractMax(S) removes and returns the element of S with the maximum key l How could we implement these operations using a heap?

4 David Luebke 4 2/19/2016 Implementing Priority Queues HeapInsert(A, key) // what’s running time? { heap_size[A] ++; i = heap_size[A]; while (i > 1 AND A[Parent(i)] < key) { A[i] = A[Parent(i)]; i = Parent(i); } A[i] = key; }

5 David Luebke 5 2/19/2016 Implementing Priority Queues HeapMaximum(A) { // This one is really tricky: return A[i]; }

6 David Luebke 6 2/19/2016 Implementing Priority Queues HeapExtractMax(A) { if (heap_size[A] < 1) { error; } max = A[1]; A[1] = A[heap_size[A]] heap_size[A] --; Heapify(A, 1); return max; }

7 David Luebke 7 2/19/2016 Quicksort l Another divide-and-conquer algorithm n The array A[p..r] is partitioned into two non- empty subarrays A[p..q] and A[q+1..r] u Invariant: All elements in A[p..q] are less than all elements in A[q+1..r] n The subarrays are recursively sorted by calls to quicksort n Unlike merge sort, no combining step: two subarrays form an already-sorted array

8 David Luebke 8 2/19/2016 Quicksort Code Quicksort(A, p, r) { if (p < r) { q = Partition(A, p, r); Quicksort(A, p, q); Quicksort(A, q+1, r); }

9 David Luebke 9 2/19/2016 Partition Clearly, all the action takes place in the partition() function n Rearranges the subarray in place n End result: u Two subarrays u All values in first subarray  all values in second n Returns the index of the “pivot” element separating the two subarrays l How do you suppose we implement this function?

10 David Luebke 10 2/19/2016 Partition In Words l Partition(A, p, r): n Select an element to act as the “pivot” (which?) n Grow two regions, A[p..i] and A[j..r] u All elements in A[p..i] <= pivot u All elements in A[j..r] >= pivot n Increment i until A[i] >= pivot n Decrement j until A[j] <= pivot n Swap A[i] and A[j] n Repeat until i >= j n Return j Note: slightly different from book’s partition()

11 David Luebke 11 2/19/2016 Partition Code Partition(A, p, r) x = A[p]; i = p - 1; j = r + 1; while (TRUE) repeat j--; until A[j] <= x; repeat i++; until A[i] >= x; if (i < j) Swap(A, i, j); else return j; Illustrate on A = {5, 3, 2, 6, 4, 1, 3, 7}; What is the running time of partition() ?

12 David Luebke 12 2/19/2016 Partition Code Partition(A, p, r) x = A[p]; i = p - 1; j = r + 1; while (TRUE) repeat j--; until A[j] <= x; repeat i++; until A[i] >= x; if (i < j) Swap(A, i, j); else return j; partition() runs in O(n) time

13 David Luebke 13 2/19/2016 Analyzing Quicksort l What will be the worst case for the algorithm? n Partition is always unbalanced l What will be the best case for the algorithm? n Partition is perfectly balanced l Which is more likely? n The latter, by far, except... l Will any particular input elicit the worst case? n Yes: Already-sorted input

14 David Luebke 14 2/19/2016 Analyzing Quicksort l In the worst case: T(1) =  (1) T(n) = T(n - 1) +  (n) l Works out to T(n) =  (n 2 )

15 David Luebke 15 2/19/2016 Analyzing Quicksort l In the best case: T(n) = 2T(n/2) +  (n) l What does this work out to? T(n) =  (n lg n)

16 David Luebke 16 2/19/2016 Improving Quicksort l The real liability of quicksort is that it runs in O(n 2 ) on already-sorted input l Book discusses two solutions: n Randomize the input array, OR n Pick a random pivot element l How will these solve the problem? n By insuring that no particular input can be chosen to make quicksort run in O(n 2 ) time

17 David Luebke 17 2/19/2016 Analyzing Quicksort: Average Case l Assuming random input, average-case running time is much closer to O(n lg n) than O(n 2 ) l First, a more intuitive explanation/example: n Suppose that partition() always produces a 9-to-1 split. This looks quite unbalanced! n The recurrence is thus: T(n) = T(9n/10) + T(n/10) + n n How deep will the recursion go? (draw it) Use n instead of O(n) for convenience (how?)

18 David Luebke 18 2/19/2016 Analyzing Quicksort: Average Case l Intuitively, a real-life run of quicksort will produce a mix of “bad” and “good” splits n Randomly distributed among the recursion tree n Pretend for intuition that they alternate between best-case (n/2 : n/2) and worst-case (n-1 : 1) n What happens if we bad-split root node, then good-split the resulting size (n-1) node?

19 David Luebke 19 2/19/2016 Analyzing Quicksort: Average Case l Intuitively, a real-life run of quicksort will produce a mix of “bad” and “good” splits n Randomly distributed among the recursion tree n Pretend for intuition that they alternate between best-case (n/2 : n/2) and worst-case (n-1 : 1) n What happens if we bad-split root node, then good-split the resulting size (n-1) node? u We fail English

20 David Luebke 20 2/19/2016 Analyzing Quicksort: Average Case l Intuitively, a real-life run of quicksort will produce a mix of “bad” and “good” splits n Randomly distributed among the recursion tree n Pretend for intuition that they alternate between best-case (n/2 : n/2) and worst-case (n-1 : 1) n What happens if we bad-split root node, then good-split the resulting size (n-1) node? u We end up with three subarrays, size 1, (n-1)/2, (n-1)/2 u Combined cost of splits = n + n -1 = 2n -1 = O(n) u No worse than if we had good-split the root node!

21 David Luebke 21 2/19/2016 Analyzing Quicksort: Average Case l Intuitively, the O(n) cost of a bad split (or 2 or 3 bad splits) can be absorbed into the O(n) cost of each good split l Thus running time of alternating bad and good splits is still O(n lg n), with slightly higher constants l How can we be more rigorous?

22 David Luebke 22 2/19/2016 Analyzing Quicksort: Average Case l For simplicity, assume: n All inputs distinct (no repeats) Slightly different partition() procedure u partition around a random element, which is not included in subarrays u all splits (0:n-1, 1:n-2, 2:n-3, …, n-1:0) equally likely l What is the probability of a particular split happening? l Answer: 1/n

23 David Luebke 23 2/19/2016 Analyzing Quicksort: Average Case l So partition generates splits (0:n-1, 1:n-2, 2:n-3, …, n-2:1, n-1:0) each with probability 1/n l If T(n) is the expected running time, l What is each term under the summation for? l What is the  (n) term for?

24 David Luebke 24 2/19/2016 Analyzing Quicksort: Average Case l So… n Note: this is just like the book’s recurrence (p166), except that the summation starts with k=0 n We’ll take care of that in a second Write it on the board

25 David Luebke 25 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer n Assume that the inductive hypothesis holds n Substitute it in for some value < n n Prove that it follows for n

26 David Luebke 26 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u What’s the answer? n Assume that the inductive hypothesis holds n Substitute it in for some value < n n Prove that it follows for n

27 David Luebke 27 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) = O(n lg n) n Assume that the inductive hypothesis holds n Substitute it in for some value < n n Prove that it follows for n

28 David Luebke 28 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) = O(n lg n) n Assume that the inductive hypothesis holds u What’s the inductive hypothesis? n Substitute it in for some value < n n Prove that it follows for n

29 David Luebke 29 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) = O(n lg n) n Assume that the inductive hypothesis holds u T(n)  an lg n + b for some constants a and b n Substitute it in for some value < n n Prove that it follows for n

30 David Luebke 30 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) = O(n lg n) n Assume that the inductive hypothesis holds u T(n)  an lg n + b for some constants a and b n Substitute it in for some value < n u What value? n Prove that it follows for n

31 David Luebke 31 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) = O(n lg n) n Assume that the inductive hypothesis holds u T(n)  an lg n + b for some constants a and b n Substitute it in for some value < n u The value k in the recurrence n Prove that it follows for n

32 David Luebke 32 2/19/2016 Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) = O(n lg n) n Assume that the inductive hypothesis holds u T(n)  an lg n + b for some constants a and b n Substitute it in for some value < n u The value k in the recurrence n Prove that it follows for n u Grind through it…

33 David Luebke 33 2/19/2016 Note: leaving the same recurrence as the book What are we doing here? Analyzing Quicksort: Average Case The recurrence to be solved What are we doing here? Plug in inductive hypothesis Expand out the k=0 case 2b/n is just a constant, so fold it into  (n)

34 David Luebke 34 2/19/2016 What are we doing here? Evaluate the summation: b+b+…+b = b (n-1) The recurrence to be solved Since n-1<n, 2b(n-1)/n < 2b Analyzing Quicksort: Average Case What are we doing here? Distribute the summation This summation gets its own set of slides later

35 David Luebke 35 2/19/2016 How did we do this? Pick a large enough that an/4 dominates  (n)+b What are we doing here? Remember, our goal is to get T(n)  an lg n + b What the hell? We’ll prove this later What are we doing here? Distribute the (2a/n) term The recurrence to be solved Analyzing Quicksort: Average Case

36 David Luebke 36 2/19/2016 Analyzing Quicksort: Average Case l So T(n)  an lg n + b for certain a and b n Thus the induction holds n Thus T(n) = O(n lg n) n Thus quicksort runs in O(n lg n) time on average (phew!) l Oh yeah, the summation…

37 David Luebke 37 2/19/2016 What are we doing here? The lg k in the second term is bounded by lg n Tightly Bounding The Key Summation What are we doing here? Move the lg n outside the summation What are we doing here? Split the summation for a tighter bound

38 David Luebke 38 2/19/2016 The summation bound so far Tightly Bounding The Key Summation What are we doing here? The lg k in the first term is bounded by lg n/2 What are we doing here? lg n/2 = lg n - 1 What are we doing here? Move (lg n - 1) outside the summation

39 David Luebke 39 2/19/2016 The summation bound so far Tightly Bounding The Key Summation What are we doing here? Distribute the (lg n - 1) What are we doing here? The summations overlap in range; combine them What are we doing here? The Guassian series

40 David Luebke 40 2/19/2016 The summation bound so far Tightly Bounding The Key Summation What are we doing here? Rearrange first term, place upper bound on second What are we doing? X Guassian series What are we doing? Multiply it all out

41 David Luebke 41 2/19/2016 Tightly Bounding The Key Summation


Download ppt "David Luebke 1 2/19/2016 Priority Queues Quicksort."

Similar presentations


Ads by Google