Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.

Slides:



Advertisements
Similar presentations
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Advertisements

Introduction to Algorithms Quicksort
Analysis of Algorithms
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Spring 2015 Lecture 5: QuickSort & Selection
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Ch. 7 - QuickSort Quick but not Guaranteed. Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
Analysis of Algorithms CS 477/677
1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 15.
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Analysis of Algorithms CS 477/677
Introduction to Algorithms Jiafen Liu Sept
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
CSS106 Introduction to Elementary Algorithms M.Sc Askar Satabaldiyev Lecture 05: MergeSort & QuickSort.
Deterministic and Randomized Quicksort Andreas Klappenecker.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
CSC317 1 Quicksort on average run time We’ll prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing.
Nothing is particularly hard if you divide it into small jobs. Henry Ford Nothing is particularly hard if you divide it into small jobs. Henry Ford.
Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.
Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Computer Sciences Department1. Sorting algorithm 4 Computer Sciences Department3.
Sorting. 2 The Sorting Problem Input: A sequence of n numbers a 1, a 2,..., a n Output: A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.
Analysis of Algorithms CS 477/677
Introduction to Algorithms Prof. Charles E. Leiserson
CSC 413/513: Intro to Algorithms
Ch 7: Quicksort Ming-Te Chi
Lecture 3 / 4 Algorithm Analysis
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS 583 Analysis of Algorithms
Divide and Conquer (Merge Sort)
CS 3343: Analysis of Algorithms
CS 332: Algorithms Quicksort David Luebke /9/2019.
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Quicksort and Randomized Algs
Quicksort Quick sort Correctness of partition - loop invariant
Analysis of Algorithms CS 477/677
Presentation transcript:

Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6

CS 477/677 - Lecture 62 Merge Sort Approach To sort an array A[p.. r]: Divide –Divide the n-element sequence to be sorted into two subsequences of n/2 elements each Conquer –Sort the subsequences recursively using merge sort –When the size of the sequences is 1 there is nothing more to do Combine –Merge the two sorted subsequences

CS 477/677 - Lecture 63 Analyzing Divide and Conquer Algorithms The recurrence is based on the three steps of the paradigm: –T(n) – running time on a problem of size n –Divide the problem into a subproblems, each of size n/b: takes D(n) –Conquer (solve) the subproblems: takes aT(n/b) –Combine the solutions: takes C(n)  (1) if n ≤ c T(n) = aT(n/b) + D(n) + C(n) otherwise

CS 477/677 - Lecture 64 MERGE – SORT Running Time Divide: –compute q as the average of p and r: D(n) =  (1) Conquer: –recursively solve 2 subproblems, each of size n/2  2T (n/2) Combine: –MERGE on an n -element subarray takes  (n) time  C(n) =  (n)  (1) if n =1 T(n) = 2T(n/2) +  (n) if n > 1

CS 477/677 - Lecture 65 Quicksort Sort an array A[p…r] Divide –Partition the array A into 2 subarrays A[p..q] and A[q+1..r], such that each element of A[p..q] is smaller than or equal to each element in A[q+1..r] –The index (pivot) q is computed Conquer –Recursively sort A[p..q] and A[q+1..r] using Quicksort Combine –Trivial: the arrays are sorted in place  no work needed to combine them: the entire array is now sorted A[p…q]A[q+1…r] ≤

CS 477/677 - Lecture 66 QUICKSORT Alg.: QUICKSORT (A, p, r) if p < r then q  PARTITION (A, p, r) QUICKSORT (A, p, q) QUICKSORT (A, q+1, r)

CS 477/677 - Lecture 67 Partitioning the Array Idea –Select a pivot element x around which to partition –Grows two regions A[p…i]  x x  A[j…r] –For now, choose the value of the first element as the pivot x A[p…i]  xx  A[j…r] ij

CS 477/677 - Lecture 68 Example ij ij ij ij ij A[p…r] ij A[p…q]A[q+1…r]

CS 477/677 - Lecture 69 Partitioning the Array Alg. PARTITION (A, p, r) 1. x  A[p] 2. i  p – 1 3. j  r while TRUE 5. do repeat j  j – 1 6. until A[j] ≤ x 7. repeat i  i until A[i] ≥ x 9. if i < j 10. then exchange A[i]  A[j] 11. else return j Running time:  (n) n = r – p i j A: arar apap ij=q A: A[p…q]A[q+1…r] ≤ p r

CS 477/677 - Lecture 610 Performance of Quicksort Worst-case partitioning –One region has 1 element and one has n – 1 elements –Maximally unbalanced Recurrence T(n) = T(n – 1) + T(1) +  (n) = n n - 1 n - 2 n n n n n - 1 n  (n 2 )

CS 477/677 - Lecture 611 Performance of Quicksort Best-case partitioning –Partitioning produces two regions of size n/2 Recurrence T(n) = 2T(n/2) +  (n) T(n) =  (nlgn) (Master theorem)

CS 477/677 - Lecture 612 Performance of Quicksort Balanced partitioning –Average case is closer to best case than to worst case –(if partitioning always produces a constant split) E.g.: 9-to-1 proportional split T(n) = T(9n/10) + T(n/10) + n

CS 477/677 - Lecture 613 Performance of Quicksort Average case –All permutations of the input numbers are equally likely –On a random input array, we will have a mix of well balanced and unbalanced splits –Good and bad splits are randomly distributed throughout the tree Alternation of a bad and a good split Nearly well balanced split n n (n – 1)/2 n (n – 1)/2 + 1 Running time of Quicksort when levels alternate between good and bad splits is O(nlgn) combined cost: 2n-1 =  (n) combined cost: n =  (n)

CS 477/677 - Lecture 614 Randomizing Quicksort Randomly permute the elements of the input array before sorting Modify the PARTITION procedure –First we exchange element A[p] with an element chosen at random from A[p…r] –Now the pivot element x = A[p] is equally likely to be any one of the original r – p + 1 elements of the subarray

CS 477/677 - Lecture 615 Randomized Algorithms The behavior is determined in part by values produced by a random-number generator –RANDOM (a, b) returns an integer r, where a ≤ r ≤ b and each of the b-a+1 possible values of r is equally likely Algorithm generates randomness in input No input can consistently elicit worst case behavior –Worst case occurs only if we get “unlucky” numbers from the random number generator

CS 477/677 - Lecture 616 Randomized PARTITION Alg.: RANDOMIZED-PARTITION (A, p, r) i ← RANDOM (p, r) exchange A[p] ↔ A[i] return PARTITION (A, p, r)

CS 477/677 - Lecture 617 Randomized Quicksort Alg. : RANDOMIZED-QUICKSORT (A, p, r) if p < r then q ← RANDOMIZED-PARTITION (A, p, r) RANDOMIZED-QUICKSORT (A, p, q) RANDOMIZED-QUICKSORT (A, q + 1, r)

CS 477/677 - Lecture 618 Worst-Case Analysis of Quicksort T(n) = worst-case running time T(n) = max (T(q) + T(n-q)) +  (n) 1 ≤ q ≤ n-1 Use substitution method to show that the running time of Quicksort is O(n 2 ) Guess T(n) = O(n 2 ) –Induction goal: T(n) ≤ cn 2 –Induction hypothesis: T(k) ≤ ck 2 for any k ≤ n

CS 477/677 - Lecture 619 Worst-Case Analysis of Quicksort Proof of induction goal: T(n) ≤ max (cq 2 + c(n-q) 2 ) +  (n) = 1 ≤ q ≤ n-1 = c  max (q 2 + (n-q) 2 ) +  (n) 1 ≤ q ≤ n-1 The expression q 2 + (n-q) 2 achieves a maximum over the range 1 ≤ q ≤ n-1 at the endpoints of this interval max (q 2 + (n - q) 2 ) = (n - 1) 2 = n 2 – 2(n – 1) 1 ≤ q ≤ n-1 T(n) ≤ cn 2 – 2c(n – 1) +  (n) ≤ cn 2

CS 477/677 - Lecture 620 Another Way to PARTITION Given an array A, partition the array into the following subarrays: –A pivot element x = A[q] –Subarray A[p..q-1] such that each element of A[p..q-1] is smaller than or equal to x (the pivot) –Subarray A[q+1..r], such that each element of A[p..q+1] is strictly greater than x (the pivot) Note: the pivot element is not included in any of the two subarrays A[p…i] ≤ xA[i+1…j-1] > x pii+1 rj-1 unknown pivot j

CS 477/677 - Lecture 621 Example

CS 477/677 - Lecture 622 Another Way to PARTITION Alg.: PARTITION(A, p, r) 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 Chooses the last element of the array as a pivot Grows a subarray [p..i] of elements ≤ x Grows a subarray [i+1..j-1] of elements >x Running Time:  (n), where n=r-p+1 A[p…i] ≤ xA[i+1…j-1] > x pii+1 rj-1 unknown pivot j

CS 477/677 - Lecture 623 Loop Invariant 1.All entries in A[p.. i] are smaller than the pivot 2.All entries in A[i j - 1] are strictly larger than the pivot 3.A[r] = pivot 4.A[ j.. r -1] elements not yet examined A[p…i] ≤ xA[i+1…j-1] > x pii+1 rj-1 x unknown pivot

CS 477/677 - Lecture 624 Loop Invariant Initialization: Before the loop starts: –A[r] is the pivot –subarrays A[p.. i] and A[i j - 1] are empty –All elements in the array are not examined p,j i r x unknown pivot

CS 477/677 - Lecture 625 Loop Invariant Maintenance: While the loop is running –if A[ j ] ≤ pivot, then i is incremented, A[ j ] and A[i +1] are swapped and then j is incremented –If A[ j ] > pivot, then increment only j A[p…i] ≤ xA[i+1…j-1] > x pii+1 rj-1 x unknown pivot

CS 477/677 - Lecture 626 Maintenance of Loop Invariant x p x >x>x pi ij jr r ≤ x> x ≤ x> x x ≤x≤x x p p i ij jr r ≤ x> x ≤ x> x If A[j] > pivot: only increment j If A[j] ≤ pivot: i is incremented, A[j] and A[i] are swapped and then j is incremented

CS 477/677 - Lecture 627 Loop Invariant Termination: When the loop terminates: –j = r  all elements in A are partitioned into one of the three cases: A[p.. i ] ≤ pivot, A[i r - 1] > pivot, and A[r] = pivot A[p…i] ≤ xA[i+1…j-1] > x pii+1 j=r j-1 x pivot

CS 477/677 - Lecture 628 Randomized Quicksort Alg. : RANDOMIZED-QUICKSORT (A, p, r) if p < r then q ← RANDOMIZED-PARTITION (A, p, r) RANDOMIZED-QUICKSORT (A, p, q - 1) RANDOMIZED-QUICKSORT (A, q + 1, r) The pivot is no longer included in any of the subarrays!!

CS 477/677 - Lecture 629 Analysis of Randomized Quicksort Alg. : RANDOMIZED-QUICKSORT (A, p, r) if p < r then q ← RANDOMIZED-PARTITION (A, p, r) RANDOMIZED-QUICKSORT (A, p, q - 1) RANDOMIZED-QUICKSORT (A, q + 1, r) The running time of Quicksort is dominated by PARTITION !! PARTITION is called at most n times (at each call a pivot is selected and never again included in future calls)

CS 477/677 - Lecture 630 PARTITION Alg.: PARTITION(A, p, r) 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 O(1) - constant Number of comparisons between the pivot and the other elements Need to compute the total number of comparisons performed in all calls to PARTITION

CS 477/677 - Lecture 631 Random Variables and Expectation Def.: (Discrete) random variable X: a function from a sample space S to the real numbers. –It associates a real number with each possible outcome of an experiment E.g.: X = face of one fair dice –Possible values: –Probability to take any of the values: {1, 2, 3, 4, 5, 6} 1/6

CS 477/677 - Lecture 632 Random Variables and Expectation Expected value (expectation, mean) of a discrete random variable X is: E[X] = Σ x x Pr{X = x} –“Average” over all possible values of random variable X E.g.: X = face of one fair dice E[X] = 1  1/6 + 2  1/6 + 3  1/6 + 4  1/6 + 5  1/6 + 6  1/6 = 3.5

CS 477/677 - Lecture 633 Example E.g.: flipping two coins: –Earn $3 for each head, lose $2 for each tail –X: random variable representing your earnings –Three possible values for variable X: 2 heads  x = $3 + $3 = $6, Pr{2 H’s} = ¼ 2 tails  x = -$2 - $2 = -$4, Pr{2 T’s} = ¼ 1 head, 1 tail  x = $3 - $2 = $1, Pr{1 H, 1 T} = ½ –The expected value of X is: E[X] = 6  Pr{2 H’s} + 1  Pr{1 H, 1 T} – 4 Pr{2 T’s} = 6  ¼ + 1  ½ - 4  ¼ = 1

CS 477/677 - Lecture 634 More Examples E.g: X = lottery earnings –1/ probability to win a prize –Possible values: –Probability to win 0: E[X] = 0 and /

CS 477/677 - Lecture 635 Readings Chapter 7