COMP Analysis of Algorithms & Data Structures

Similar documents
COMP Analysis of Algorithms & Data Structures

Module 3: Sorting and Randomized Algorithms

Module 3: Sorting and Randomized Algorithms. Selection vs. Sorting. Crucial Subroutines. CS Data Structures and Data Management

Selection (deterministic & randomized): finding the median in linear time

COMP Analysis of Algorithms & Data Structures

Module 3: Sorting and Randomized Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

COMP Data Structures

Algorithms and Data Structures for Mathematicians

Randomized Algorithms: Selection

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Introduction to Algorithms

1 Probabilistic analysis and randomized algorithms

COMP Analysis of Algorithms & Data Structures

Sorting and Selection

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

COMP Analysis of Algorithms & Data Structures

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

Lecture 2: Divide&Conquer Paradigm, Merge sort and Quicksort

Data Structures and Algorithms Running time, Divide and Conquer January 25, 2018

COMP Analysis of Algorithms & Data Structures

Divide-and-Conquer CSE 680

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017

Analysis of Algorithms - Quicksort -

4.4 Algorithm Design Technique: Randomization

EECS 2011M: Fundamentals of Data Structures

Analysis of Algorithms - Medians and order statistic -

Randomized Algorithms, Quicksort and Randomized Selection

Divide and Conquer. Algorithm D-and-C(n: input size)

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

4. Sorting and Order-Statistics

Other techniques for sorting exist, such as Linear Sorting which is not based on comparisons. Linear Sorting techniques include:

CSC 273 Data Structures

Lecture 9: Sorting Algorithms

Practical Session #11 - Sort properties, QuickSort algorithm, Selection

The Substitution method

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

Randomized Algorithms, Hash Functions

COSC242 Lecture 7 Mergesort and Quicksort

Quicksort (CLRS 7) We saw the divide-and-conquer technique at work resulting in Mergesort. Mergesort summary:

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

BM267 - Introduction to Data Structures

Divide and Conquer 4-0

CSE Winter 2015 Quiz 2 Solutions

CSC Design and Analysis of Algorithms

Fast Sorting and Selection. A Lower Bound for Worst Case

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Data Structures and Algorithms Week 4

II (Sorting and) Order Statistics

Divide and Conquer Algorithms

The divide and conquer strategy has three basic parts. For a given problem of size n,

Lecture 4. The Substitution Method and Median and Selection

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Lecture 19 Sorting Goodrich, Tamassia

Introduction. e.g., the item could be an entire block of information about a student, while the search key might be only the student's name

COMP Analysis of Algorithms & Data Structures

1 (15 points) LexicoSort

Deterministic and Randomized Quicksort. Andreas Klappenecker

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

CS 171: Introduction to Computer Science II. Quicksort

Searching, Sorting. part 1

Unit-2 Divide and conquer 2016

Data Structures and Algorithms Chapter 4

1 The sorting problem

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Lower bound for comparison-based sorting

15 150: Principles of Functional Programming Sorting Integer Lists

Divide-and-Conquer. Dr. Yingwu Zhu

Lecture 2: Divide and Conquer

Quicksort Using Median of Medians as Pivot

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Copyright 2009, Artur Czumaj 1

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

CS61B Lectures # Purposes of Sorting. Some Definitions. Classifications. Sorting supports searching Binary search standard example

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Design and Analysis of Algorithms

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

COMP Analysis of Algorithms & Data Structures

Searching Algorithms/Time Analysis

11/8/2016. Chapter 7 Sorting. Introduction. Introduction. Sorting Algorithms. Sorting. Sorting

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

Lecture 4 Median and Selection

Lecture 2: Getting Started

We can use a max-heap to sort data.

Shell s sort. CS61B Lecture #25. Sorting by Selection: Heapsort. Example of Shell s Sort

Data Structures and Algorithms

Randomized Quickselect and Randomized Quicksort. Nishant Mehta September 14 th, 2017

Problem Set 4 Solutions

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

Test 1 Review Questions with Solutions

Midterm 2. Replace by cover page.

Recurrences and Divide-and-Conquer

Deliverables. Quick Sort. Randomized Quick Sort. Median Order statistics. Heap Sort. External Merge Sort

Lower Bound on Comparison-based Sorting

CS S-11 Sorting in Θ(nlgn) 1. Base Case: A list of length 1 or length 0 is already sorted. Recursive Case:

Transcription:

COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 7 - Jan. 17, 2018 CLRS 7.1, 7-4, 9.1, 9.3 University of Manitoba COMP 3170 - Analysis of Algorithms & Data Structures 1 / 11

QuickSelect Review quick-select1(a, i) A: array of size n, i: integer s.t. 0 i < n 1. p choose-pivot1(a) 2. j partition(a, p) 3. if j = i then 4. return A[j] 5. else if j > i then 6. return quick-select1(a[0, 1,..., j 1], i) 7. else if j < i then 8. return quick-select1(a[j + 1, j + 2,..., n 1], i j 1) If pivot is at position j, the cost of recursive call parameters will be: None if j = i. (j, i) if j > i (recursing on the left subarray). (n j 1, i j 1) if j < i (recursing on the right subarray). COMP 3170 - Analysis of Algorithms & Data Structures 2 / 11

Average-case analysis of quick-select1 Assume all n! permutations are equally likely. Define T (n, i) as average cost for selecting ith item from size-n array: The cost for recursive calls (RC) is 0 j = i RC = T (j, i), j > i T (n j 1, i j 1) j < i Shuffled input it is equally likely for the pivot to be at any position: T (n, i) = cn + 1 ( ) (RC if j=0) + (RC if j=1) +... + (RC if j=n-1) n partition = cn + 1 n partition i 1 T (n j 1, i j 1) + j=0 For simplicity, define T (n) = max T (n, k). 0 k<n n 1 j=i+1 T (j, i) COMP 3170 - Analysis of Algorithms & Data Structures 3 / 11

Average-case analysis of quick-select1 T (n) cn + 1 n partition i 1 T (n j 1) + j=0 n 1 j=i+1 T (j) We say that a pivot is good if the arrays on both sides have size at least n/4 This happens when pivot index j is in [n/4, 3n/4). Half of possible pivots are good and the rest are bad. The recursive cost for a good pivot is at most T (3n/4). The recursive cost for a bad pivot is at most T (n). The average cost is then given by: ( cn + 1 2 T (n) + T ( 3n/4 ) ), n 2 T (n) bad pivot good pivot d n = 1 COMP 3170 - Analysis of Algorithms & Data Structures 4 / 11

Average-case analysis of quick-select1 The average cost is then given by: { ( cn + 1 T (n) 2 T (n) + T ( 3n/4 )), n 2 d, n = 1 Rearranging gives: T (n) 2cn + T ( 3n/4 ) 2cn + 2c(3n/4) + 2c(9n/16) + + d ( ) 3 i d + 2cn O(n) 4 i=0 Since T (n) must be Ω(n) (why?), T (n) Θ(n). COMP 3170 - Analysis of Algorithms & Data Structures 5 / 11

Linear-time selection Although Quick-select runs in O(n) on average, in the worst-case it is still super-linear. Is there any selection algorithm that runs in O(n) in the worst-case? The answer is Yes; Median of medians algorithms! It is a twist to Quick-select in which the pivot is selected a bit smarter! COMP 3170 - Analysis of Algorithms & Data Structures 6 / 11

Median of five algorithm A variant of Quick-select in which the pivot is selected more carefully. The input is an array A of n objects (assume n is divisible by 5). Divide A into n/5 blocks of size 5. Recursively find the median of the medians; denote it by x. x will be the pivot for quick-select Partition the whole array using x as the pivot Recurs on the corresponding subarray as in Quick-select COMP 3170 - Analysis of Algorithms & Data Structures 7 / 11

Median of five example COMP 3170 - Analysis of Algorithms & Data Structures 8 / 11

Median of five algorithm Pivot x is median of medians half of blocks have median < x. This implies half of blocks include at least 3 elements < x. So, there will be at least n/5 1/2 3 = 3n/10 elements smaller than x Similarly, there will be at least 3n/10 elements larger than x. We assume distinct items; when pivot is equal to multiple items, you can update the partition algorithm so that the pivot is the best among items with the same key Hence, the size of recursive call is always in the range (3n/10, 7n/10). x is always a good pivot In the worst case, the size of recursive call is always 7n/10. T (n/5) + cn + T (7n/10), n 2 T (n) find x partition around x recursive call d, n = 1 COMP 3170 - Analysis of Algorithms & Data Structures 9 / 11

Median of five algorithm T (n/5) + cn + T (7n/10), n 2 T (n) find x partition around x recursive call d, n = 1 We guess that T (n) O(n) and use strong induction to prove it. We prove there is a value M s.t. T (n) Mn for all n 1. For the base we have T (1) = d M as long as M d. For any value of n we can state: T (n) T (n/5) + T (7n/10) + cn M n/5 + M 7n/10 + cn = (9M/10 + c)n M n (definition) (induction hypothesis) as log as M 9M/10 + c, i.e., M 10c so, we showed for M = max{10c, d} we have T (n) M n for n 1. So, T (n) O(n). COMP 3170 - Analysis of Algorithms & Data Structures 10 / 11

Quick-sort revisit Theorem It is possible to select the i th smallest item in a list of n numbers in time Θ(n) Quick-sort in O(n log n) time: Using select algorithm to choose the pivot as the median of n items in O(n) time Partition around pivot in O(n) time (selecting pivot as n/c th smallest item for constant c gives the same result) Sort the two sides of pivot recursively in time 2T (n/2). The cost will be T (n) = 2T (n/2) + Θ(n), which gives T (n) = Θ(n log n) [case II of Master theorem] Theorem A smart selection of pivot, using linear-time select, results in quick-sort running in Θ(n log n) COMP 3170 - Analysis of Algorithms & Data Structures 11 / 11