Lecture 3: Randomized Algorithm and Divide and Conquer II: Shang-Hua Teng.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Order Statistics. order - 2 Lin / Devi Comp 122 Order Statistic i th order statistic: i th smallest element of a set of n elements.
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
CSE 3101: Introduction to the Design and Analysis of Algorithms
Order Statistics(Selection Problem) A more interesting problem is selection:  finding the i th smallest element of a set We will show: –A practical randomized.
Medians and Order Statistics
Introduction to Algorithms Jiafen Liu Sept
1 Today’s Material Medians & Order Statistics – Ch. 9.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Chapter 3: Divide and Conquer
Spring 2015 Lecture 5: QuickSort & Selection
CSE 2331/5331 Topic 5: Prob. Analysis Randomized Alg.
Probabilistic (Average-Case) Analysis and Randomized Algorithms Two different approaches –Probabilistic analysis of a deterministic algorithm –Randomized.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
Median/Order Statistics Algorithms
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Analysis of Algorithms
Finding the Median Algorithm : Design & Analysis [11]
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
1 Introduction to Randomized Algorithms Md. Aashikur Rahman Azim.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
Probabilistic (Average-Case) Analysis and Randomized Algorithms Two different but similar analyses –Probabilistic analysis of a deterministic algorithm.
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.
Tutorial 4 The Quicksort Algorithm. QuickSort  Divide: Choose a pivot, P Form a subarray with all elements ≤ P Form a subarray with all elements > P.
Selection1. 2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken from a total order, find the k-th smallest element in this.
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.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
10 Algorithms in 20th Century Science, Vol. 287, No. 5454, p. 799, February 2000 Computing in Science & Engineering, January/February : The Metropolis.
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.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Introduction to Algorithms Jiafen Liu Sept
Chapter 9: Selection Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
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.
Order Statistics(Selection Problem)
Deterministic and Randomized Quicksort Andreas Klappenecker.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
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.
Computer Sciences Department1. Sorting algorithm 4 Computer Sciences Department3.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Order Statistics Comp 122, Spring 2004.
Randomized Algorithms
Order Statistics(Selection Problem)
Ch 7: Quicksort Ming-Te Chi
Randomized Algorithms
Topic: Divide and Conquer
Order Statistics Comp 550, Spring 2015.
CS 583 Analysis of Algorithms
Order Statistics Def: Let A be an ordered set containing n elements. The i-th order statistic is the i-th smallest element. Minimum: 1st order statistic.
Tomado del libro Cormen
Topic: Divide and Conquer
Order Statistics Comp 122, Spring 2004.
The Selection Problem.
Quicksort and Randomized Algs
CS200: Algorithm Analysis
Sorting We have actually seen already two efficient ways to sort:
Medians and Order Statistics
Presentation transcript:

Lecture 3: Randomized Algorithm and Divide and Conquer II: Shang-Hua Teng

Randomized Algorithm Random Number Generator (RNG) –RANDOM(a,b) returns an integer from {a,a+1,…, b-1,b}, with each such integer being equally likely. –Special case (random coin flip): RANDOM(0,1) chooses 0 and 1 with probability 1/2 each. A Randomized Algorithm uses a random number generator –its behavior is determined by not only by its input but also the values chosen by RNG

Why Randomized Algorithms? Efficiency Simplicity Reduction of the impact of bad cases!

The Selection Problem Input: Array A[1...n] of the elements in the an arbitrary order, and an index k Output: the kth smallest element in A[1..n]. If k = 1, we are asking for the smallest element If k = n, we are asking for the largest element If k = n/2, we are asking for the median

Quick-Selection Choose a random element, split the array, eliminate the fraction that does not contain the desired element and recursively apply the procedure. Quick-Selection(A,1,n,k) –More generally, Quick-Selection(A,p,r,k) –A procedure that selects the kth smallest element of elements in sub-array A[p..r]

Quick-Selection(A,p,r,k) –if p =r and k = 1, do return A(p) –if p< r then t = Partition(A,p,r,q) if t = k do return A[t] else if t > k do return Quick-Selection(A,p,t-1,k) else if t < k do return Quick-Selection(A,t+1,r,k-t)

Partition(A,p,r,q) Suppose there are t-1 elements in A that is smaller than s = A[q]. Then return t and reorder A so that A[p..p+t-1] < A[t] = s <= A[t+1..r] Partition(A,p,r,q) – –for j p to r-1 do if –then – –return i+1 SWAP(A[q],A[r])

Expected Time Complexity

Time Complexity Let G(n) = E(T(n)), we have

Time Complexity Conjecture: G(n) =  (n). Use the substitution method –Assuming for all m < n, –Need to show

Time Complexity (if c>=24)

Quick-Sort Quick-Sort(A,1,n) –Divide t = Partition(A,1,n,q) –Conquer Quick-Sort(A,1,t-1) Quick-Sort(A,t+1,n)

Quick-Sort(A,p,r) –if p< r then t = Partition(A,p,r,q) Quick-Sort(A,p,t-1) Quick-Sort(A,t+1,r) Theorem: Worst Case: Theorem: Expected Case: O(nlg n)

Partition(A,p,r,q) Suppose there are t-1 elements in A that is smaller than s = A[q]. Then return t and reorder A so that A[p..p+t-1] < A[t] = s <= A[t+1..r] Partition(A,p,r,q) – –for j p to r-1 do if –then – –return i+1 SWAP(A[q],A[r])

Complexity of Quick-Sort The Element chosen by RANDOM is called the pivot element Each number can be a pivot element at most once So totally, at most n calls to Partition procedure So the total steps is bounded by a constant factor of the number of comparisons in Partition.

Compute the total number of comparison in calls to Partition When does the algorithm compare two elements? When does not the algorithm compare two elements? Suppose (Z[1],Z[2],…,Z[n]) are the sorted array of elements in A that is, Z[k] is the kth smallest element of A.

Compute the total number of comparison in calls to Partition The reason to use Z rather than A directly is that is it hard to locate elements in A during Quick- Sort because elements are moving around. But it is easier to identify Z[k] because they are in a sorted order. We call this type of the analysis scheme: the backward analysis.

Compute the total number of comparisons in calls to Partition Under what condition does Quick-Sort compare Z[i] and Z[j]? What is the probability of comparison? First: Z[i] and Z[j] are compared at most once!!! Let E ij be the random event that Z[i] is compared to Z[j]. Let X ij be the indicator random variable of E ij. –X ij = I{Z[i] is compared to Z[j]}

Compute the total number of comparisons in calls to Partition So the total number of comparisons is We are interested in

Compute the total number of comparisons in calls to Partition By linearity of expectation, we have So what is Pr(Z[i] is compared to Z[j])?

Compute the total number of comparisons in calls to Partition So what is Pr(Z[i] is compared to Z[j])? What is the condition that –Z[i] is compared to Z[j]? What is the condition that –Z[i] is not compared to Z[j]? –Answer: no element is chosen from Z[i+1] … Z[j-1] before Z[i] or Z[j] is chosen as a pivot in Quick-Sort therefore...

Compute the total number of comparisons in calls to Partition Therefore

Compute the total number of comparisons in calls to Partition By linearity of expectation, we have

Original Quick-Sort (Tony Hoare) Partition with the first element Average-Case Complexity: –Assume inputs come from uniform permutations. Our analysis of the Expected time analysis of Random Quick-Sort extends directly. Notice the difference of randomized algorithm and average-case complexity of a deterministic algorithm