Analysis of Algorithms CS 477/677

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
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.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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 Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
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.
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.
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
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.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.
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.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
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.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Algorithms Sorting – Part 3.
Advanced Sorting.
CMPT 438 Algorithms.
Order Statistics.
Quick Sort Divide: Partition the array into two sub-arrays
Order Statistics Comp 122, Spring 2004.
Introduction to Algorithms Prof. Charles E. Leiserson
Algorithm Design & Analysis
Advance Analysis of Algorithms
CSC 413/513: Intro to Algorithms
Analysis of Algorithms CS 477/677
CO 303 Algorithm Analysis And Design Quicksort
Ch 7: Quicksort Ming-Te Chi
Divide and Conquer (Merge Sort)
Algorithms and Data Structures Lecture III
Lecture 3 / 4 Algorithm Analysis
Topic: Divide and Conquer
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS 583 Analysis of Algorithms
Design and 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.
Chapter 7 Quicksort.
Divide & Conquer Algorithms
Order Statistics Comp 122, Spring 2004.
Analysis of Algorithms
Algorithms Sorting.
The Selection Problem.
Quicksort and Randomized Algs
Design and Analysis of Algorithms
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

Selection Sort Idea: Invariant: Disadvantage: 1 3 2 9 6 4 8 Idea: Find the smallest element in the array Exchange it with the element in the first position Find the second smallest element and exchange it with the element in the second position Continue until the array is sorted Invariant: All elements to the left of the current index are in sorted order and never changed again Disadvantage: Running time depends only slightly on the amount of order in the file CS 477/677 - Lecture 6

Example 1 3 2 9 6 4 8 8 6 9 4 3 2 1 8 3 2 9 6 4 1 8 9 6 4 3 2 1 8 3 4 9 6 2 1 9 8 6 4 3 2 1 8 6 4 9 3 2 1 9 8 6 4 3 2 1 CS 477/677 - Lecture 6

Selection Sort Alg.: SELECTION-SORT(A) n ← length[A] for j ← 1 to n - 1 do smallest ← j for i ← j + 1 to n do if A[i] < A[smallest] then smallest ← i exchange A[j] ↔ A[smallest] 1 3 2 9 6 4 8 CS 477/677 - Lecture 6

Analysis of Selection Sort Alg.: SELECTION-SORT(A) n ← length[A] for j ← 1 to n - 1 do smallest ← j for i ← j + 1 to n do if A[i] < A[smallest] then smallest ← i exchange A[j] ↔ A[smallest] cost times c1 1 c2 n c3 n-1 c4 c5 c6 c7 n-1 ≈n2/2 comparisons ≈n exchanges T(n) = Θ(n2) CS 477/677 - Lecture 6

Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size Conquer the sub-problems Solve the sub-problems recursively Sub-problem size small enough ⇒ solve the problems in straightforward manner Combine the solutions to the sub-problems Obtain the solution for the original problem CS 477/677 - Lecture 6

Merge Sort Approach To sort an array A[p . . r]: Divide Conquer 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 6

Merge Sort Alg.: MERGE-SORT(A, p, r) if p < r Check for base case q r Alg.: MERGE-SORT(A, p, r) if p < r Check for base case then q ← ⎣(p + r)/2⎦ Divide MERGE-SORT(A, p, q) Conquer MERGE-SORT(A, q + 1, r) Conquer MERGE(A, p, q, r) Combine Initial call: MERGE-SORT(A, 1, n) 1 2 3 4 5 6 7 8 5 2 4 7 1 3 2 6 CS 477/677 - Lecture 6

Example – n Power of 2 Example 7 5 5 7 5 4 7 q = 4 1 2 3 4 5 6 7 8 q = 4 Example 1 2 3 4 7 5 6 8 1 2 5 3 4 7 6 8 1 5 2 3 4 7 6 8 CS 477/677 - Lecture 6

Merging Input: Array A and indices p, q, r such that p ≤ q < r 1 2 3 4 5 6 7 8 p r q Input: Array A and indices p, q, r such that p ≤ q < r Subarrays A[p . . q] and A[q + 1 . . r] are sorted Output: One single sorted subarray A[p . . r] CS 477/677 - Lecture 6

Merging Idea for merging: Two piles of sorted cards Choose the smaller of the two top cards Remove it and place it in the output pile Repeat the process until one pile is empty Take the remaining input pile and place it face-down onto the output pile CS 477/677 - Lecture 6

Merge - Pseudocode Alg.: MERGE(A, p, q, r) Compute n1 and n2 3 4 5 6 7 8 p r q n1 n2 Alg.: MERGE(A, p, q, r) Compute n1 and n2 Copy the first n1 elements into L[1 . . n1 + 1] and the next n2 elements into R[1 . . n2 + 1] L[n1 + 1] ← ∞; R[n2 + 1] ← ∞ i ← 1; j ← 1 for k ← p to r do if L[ i ] ≤ R[ j ] then A[k] ← L[ i ] i ←i + 1 else A[k] ← R[ j ] j ← j + 1 p q 7 5 4 2 6 3 1 r q + 1 L R ∞ CS 477/677 - Lecture 6

Running Time of Merge Initialization (copying into temporary arrays): Θ(n1 + n2) = Θ(n) Adding the elements to the final array (the for loop): n iterations, each taking constant time ⇒ Θ(n) Total time for Merge: Θ(n) CS 477/677 - Lecture 6

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 6

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 6

Solve the Recurrence T(n) = c if n = 1 2T(n/2) + cn if n > 1 Use Master’s Theorem: Compare n with f(n) = cn Case 2: T(n) = Θ(nlgn) CS 477/677 - Lecture 6

Merge Sort - Discussion Running time insensitive of the input Advantages: Guaranteed to run in Θ(nlgn) Disadvantage Requires extra space ≈N Applications Maintain a large ordered data file How would you use Merge sort to do this? CS 477/677 - Lecture 6

Quicksort Sort an array A[p…r] Divide Conquer Combine A[p…q] A[q+1…r] ≤ 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 CS 477/677 - Lecture 6

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 6

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] ≤ x x ≤ A[j…r] i j CS 477/677 - Lecture 6

Example 7 3 1 4 6 2 5 i j A[p…r] 7 3 1 4 6 2 5 i j 7 5 1 4 6 2 3 i j 7 5 1 4 6 2 3 i j 7 5 6 4 1 2 3 i j A[p…q] A[q+1…r] 7 5 6 4 1 2 3 i j CS 477/677 - Lecture 6

Partitioning the Array Alg. PARTITION (A, p, r) x ←A[p] i ←p – 1 j ←r + 1 while TRUE do repeat j ←j – 1 until A[j] ≤ x repeat i ←i + 1 until A[i] ≥ x if i < j then exchange A[i] ⟺A[j] else return j p r A: 7 3 1 4 6 2 5 i j ar ap i j=q A: A[p…q] A[q+1…r] ≤ Running time: Θ(n) n = r – p + 1 CS 477/677 - Lecture 6

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 - 3 2 1 3 Θ(n2) CS 477/677 - Lecture 6

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 6

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 6

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 n n - 1 1 combined cost: 2n-1 = Θ(n) n (n – 1)/2 (n – 1)/2 + 1 combined cost: n = Θ(n) (n – 1)/2 Alternation of a bad and a good split Nearly well balanced split Running time of Quicksort when levels alternate between good and bad splits is O(nlgn) CS 477/677 - Lecture 6

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 6

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 6

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 6

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 6

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(n2) Guess T(n) = O(n2) Induction goal: T(n) ≤ cn2 Induction hypothesis: T(k) ≤ ck2 for any k ≤ n CS 477/677 - Lecture 6

Worst-Case Analysis of Quicksort Proof of induction goal: T(n) ≤ max (cq2 + c(n-q)2) + Θ(n) = 1 ≤ q ≤ n-1 = c × max (q2 + (n-q)2) + Θ(n) The expression q2 + (n-q)2 achieves a maximum over the range 1 ≤ q ≤ n-1 at the endpoints of this interval max (q2 + (n - q)2) = 12 + (n - 1)2 = n2 – 2(n – 1) T(n) ≤ cn2 – 2c(n – 1) + Θ(n) ≤ cn2 CS 477/677 - Lecture 6

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] ≤ x A[i+1…j-1] > x p i i+1 r j-1 unknown pivot j CS 477/677 - Lecture 6

Readings Chapter 4 CS 477/677 - Lecture 6