Data Structures and Algorithm Analysis (CSC317) Randomized Algorithms (part 3)

Similar documents
Data Structures and Algorithm Analysis (CSC317) Randomized algorithms

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort

Quicksort. Where Average and Worst Case Differ. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

Data Structures and Algorithms CSE 465

Lecture 4. Quicksort

1 Quick Sort LECTURE 7. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Analysis of Algorithms. Randomizing Quicksort

ITEC2620 Introduction to Data Structures

Analysis of Algorithms CMPSC 565

Divide and Conquer Strategy

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Algorithms And Programming I. Lecture 5 Quicksort

Average Case Analysis. October 11, 2011

Data Structures and Algorithms

CSE 421, Spring 2017, W.L.Ruzzo. 8. Average-Case Analysis of Algorithms + Randomized Algorithms

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text.

Design and Analysis of Algorithms PART II-- Median & Runtime Analysis Recorded by Chandrasekar Vijayarenu and Shridharan Muthu.

Divide-and-conquer. Curs 2015

Quicksort algorithm Average case analysis

Partition and Select

Fundamental Algorithms

Fundamental Algorithms

CSE 312, Winter 2011, W.L.Ruzzo. 8. Average-Case Analysis of Algorithms + Randomized Algorithms

Bucket-Sort. Have seen lower bound of Ω(nlog n) for comparisonbased. Some cheating algorithms achieve O(n), given certain assumptions re input

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

COMP 250: Quicksort. Carlos G. Oliver. February 13, Carlos G. Oliver COMP 250: Quicksort February 13, / 21

Review Of Topics. Review: Induction

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

CSCI 3110 Assignment 6 Solutions

1. [10 marks] Consider the following two algorithms that find the two largest elements in an array A[1..n], where n >= 2.

COMP Analysis of Algorithms & Data Structures

CSE 613: Parallel Programming. Lecture 9 ( Divide-and-Conquer: Partitioning for Selection and Sorting )

Randomized algorithms. Inge Li Gørtz

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

On Partial Sorting. Conrado Martínez. Univ. Politècnica de Catalunya, Spain

Divide and conquer. Philip II of Macedon

R ij = 2. Using all of these facts together, you can solve problem number 9.

Recap: Prefix Sums. Given A: set of n integers Find B: prefix sums 1 / 86

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1.

Linear Time Selection

Selection and Adversary Arguments. COMP 215 Lecture 19

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Analysis of Approximate Quickselect and Related Problems

Asymptotic Running Time of Algorithms

Lecture 2: Randomized Algorithms

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015

Week 5: Quicksort, Lower bound, Greedy

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Quick Sort Notes , Spring 2010

1 Divide and Conquer (September 3)

Introduction to Randomized Algorithms: Quick Sort and Quick Selection

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

Bin Sort. Sorting integers in Range [1,...,n] Add all elements to table and then

Divide-Conquer-Glue. Divide-Conquer-Glue Algorithm Strategy. Skyline Problem as an Example of Divide-Conquer-Glue

Kartsuba s Algorithm and Linear Time Selection

Searching. Sorting. Lambdas

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Algorithms and Data Structures

Find an Element x in an Unsorted Array

Sorting algorithms. Sorting algorithms

Find Min and Max. Find them independantly: 2n 2. Can easily modify to get 2n 3. Should be able to do better(?) Try divide and conquer.

Sorting DS 2017/2018

Divide-and-Conquer. a technique for designing algorithms

N/4 + N/2 + N = 2N 2.

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

CS 344 Design and Analysis of Algorithms. Tarek El-Gaaly Course website:

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Advanced Analysis of Algorithms - Midterm (Solutions)

Design and Analysis of Algorithms

Asymptotic Algorithm Analysis & Sorting

CPS 616 DIVIDE-AND-CONQUER 6-1

Extended Algorithms Courses COMP3821/9801

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

Divide and Conquer. Arash Rafiey. 27 October, 2016

CS361 Homework #3 Solutions

CSE 4502/5717: Big Data Analytics

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

CS 361, Lecture 14. Outline

CS 161 Summer 2009 Homework #2 Sample Solutions

5. DIVIDE AND CONQUER I

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

Chunksort: A Generalized Partial Sort Algorithm

CSE 591 Homework 3 Sample Solutions. Problem 1

Data selection. Lower complexity bound for sorting

On my honor I affirm that I have neither given nor received inappropriate aid in the completion of this exercise.

Problem Set 2 Solutions

MIDTERM I CMPS Winter 2013 Warmuth

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

DM507 - Algoritmer og Datastrukturer Project 1

Design and Analysis of Algorithms

CS Analysis of Recursive Algorithms and Brute Force

Mergesort and Recurrences (CLRS 2.3, 4.4)

Divide and Conquer. Recurrence Relations

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

Transcription:

Data Structures and Algorithm Analysis (CSC317) Randomized Algorithms (part 3)

Quicksort p r Quicksort(A, p, r) 1. If p<r 2. q = Partition(A,p,r) 3. Quicksort(A,p,q-1) 4. Quicksort(A,q+1,r)

Quicksort p q r Quicksort(A, p, r) 1. If p<r 2. q = Partition(A,p,r) 3. Quicksort(A,p,q-1) 4. Quicksort(A,q+1,r)

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 pivot Average as good as best case!

Quicksort on average runtime Prelims: Most work of Quicksort in comparisons Each call to partition is constant plus number of comparisons in for loop Let X = total number of comparisons in all calls to Partition

Quicksort on average runtime Let X = total number of comparisons in all calls to Partition Rename smallest and largest elements in A as z 1, z 2,..z n Example: [4 1 2 3 ] z1=1;; z2=2;; z3=3;; z4=4 This is for analysis, not that we presort!

Quicksort on average runtime Let X = total number of comparisons in all calls to Partition Rename smallest and largest elements in A as z 1, z 2,..z n z i ;z j Consider with indices i<j X ij = z i z j 1 if and compared 0 if not compared

Quicksort on average runtime How many times will zi and zj be compared? Example: A=[8 1 6 4 0 3 9 5];; zi=3;; zj=9 When will two elements be compared? Only if one of them (3 or 9) is chosen as a pivot, since in Partition, each element compared only with pivot. They will then never be compared again, since pivot is not in the subsequent recursions to Partition

Quicksort on average runtime How many times will zi and zj be compared? Example: A=[8 1 6 4 0 3 9 5];; zi=3;; zj=9 When will two elements not be compared? If pivot=5, then none of [8 6 9] will be compared to [1 4 0 3], so zi and zj not compared. Not in this call of partition, or any other call, since these sets will then be separated

Quicksort on average runtime Probability that zi and zj be compared? Consider i<j and set Z ij = z i, z i+1,..z j Not necessarily in order in array A This set will remain together in calls to Partition, unless one of them is a pivot Because otherwise pivot will be smaller than i or larger than j, so these will remain together

Quicksort on average runtime Probability that zi and zj be compared? Consider i<j and set Z ij = z i, z i+1,..z j If i or j chosen first as pivot within this set, they ll be compared Otherwise, another element will be chosen as pivot, and they will be separated Since (j-i+1) elements in this set, the prob of any element as pivot: 1 j i +1

Quicksort on average runtime Pr { zi compared to zj } = Pr { zi or zj chosen first as pivot in Zij } = (since mutually exclusive) Pr { zi first element chosen from Zij } + Pr { zj first element chosen from Zij } = 1 j i +1 + 1 j i +1 = 2 j i +1

Quicksort on average runtime Let X = total number of comparisons in all calls to Partition X = 1 if z and z i j compared ij 0 if not compared X = n 1 i=1 n j=i+1 X ij All possible comparisons i and j

Quicksort on average runtime Let X = total number of comparisons in all calls to Partition X ij = n 1 n j=i+1 E[X] = E[ X ] = E[X ] ij ij i=1 z i z j 1 if and compared 0 if not compared n 1 i=1 n j=i+1 Prob zi compared to zj

Quicksort on average runtime n 1 n j=i+1 E[X] = E[ X ] = E[X ] = ij ij n 1 i=1 n j=i+1 2 i=1 j i +1 n 1 i=1 n j=i+1 Prob zi compared to zj

Quicksort on average runtime n 1 n j=i+1 E[X] = E[ X ij ] = E[X ij ] = i=1 n 1 n 2 = j i +1 i=1 n 1 i=1 n 1 i=1 n 1 i=1 j=i+1 n i 2 < k +1 k=1 n i 2 = k k=1 O(logn) = O(nlogn) n 1 i=1 n j=i+1 (Change of var: k = j-i)

Quicksort on average run time We ll shown that average run time with random pivots for any input array is O(n log n) Randomness is in choosing pivot Average as good as best case!

Order statistics: another use of partition! Array n unsorted Find kth smallest k=1: Minimum

Order statistics: another use of partition! Array n unsorted Find kth smallest element k=1: Minimum k = n +1? 2 ; n +1 2

Order statistics: another use of partition! Array n unsorted Find kth smallest element k=1: Minimum k = n +1 : Median 2 ; n +1 2

Order statistics Array n unsorted 1 st, 2 nd, 3 rd, smallest or largest;; median Another use of Partition

Order statistics: start simple (1 st order) Minimum(A) 1. Min = A[1] 2. for i=2 to A.length 3. if min>a[i] 4. min = A[i] 5. return min Worst case? Best case?

Order statistics: start simple (1 st order) Minimum(A) 1. Min = A[1] 2. for i=2 to A.length 3. if min>a[i] 4. min = A[i] 5. return min Worst case? Best case? Θ(n) Θ(n)

Order statistics Selection problem more general problem Input: set A with n distinct numbers Output: find ith smallest element

Order statistics Selection problem more general problem Input: set A with n distinct numbers Output: find ith smallest element Upper bound?

Order statistics Selection problem more general problem Input: set A with n distinct numbers Output: find ith smallest element Upper bound? We can solve in O(n logn) and find ith index in array since we can always sort

Order statistics Selection problem more general problem Input: set A with n distinct numbers Output: find ith smallest element Upper bound? We can solve in O(n logn) since we can always sort and find ith index in array. We would like to do better!

Selection problem Selection problem more general problem Input: set A with n distinct numbers Output: find ith smallest element O(n) on average with randomized algorithm Amazing that (at least on average) similar to finding just minimum!

Selection problem Randomized- Select(A,p,r,i) 1 if p==r // base case 2 return A[p] 3 q=randomized- Partition(A,p,r) 4 k=q- p+1 // number elements from left up to pivot 5 if i==k // pivot is the ith smallest! 6 return A[q] 7 elseif i<k 8 return Randomized- Select(A,p,q- 1,i) // ith smallest left 9 else return Randomized- Select(A,q+1,r,i- k) //on right p < pivot q > pivot r

Selection problem Randomized- Select(A,p,r,i) p < pivot q > pivot r Example: A = [4 1 6 5 3] i=3 (find 2 nd smallest; 3 rd smallest element On the board )

Selection problem p < pivot q > pivot r How is this different from randomized version of Quicksort?

Selection problem p < pivot q > pivot r How is this different from randomized version of Quicksort? Answer: Only one recursion (left or right); not two

Selection problem p q r Analysis Worst case: T(n) = T ( n 1) + Θ(n) = Θ(n 2 ) always partition around largest remaining element, and recursion on array size n- 1 Worse than a good sorting scheme!

Selection problem p q r Analysis: But 1/10 to 9/10 good T(n) = T 9n 10 + Θ(n) = Θ(n)

Selection problem p q r Analysis: But 1/10 to 9/10 good T(n) = T 9n 10 + Θ(n) Θ(n) Master theorem. n log b a = n log 10/91 = n 0 = 1 f (n) = Θ(n) T(n) = Θ(n)

Selection problem Average case solution also good! Θ(n) We won t prove, but similar to Quicksort.