Randomized Algorithms

Slides:



Advertisements
Similar presentations
©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 6 Prof. Erik Demaine.
Advertisements

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.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
1 More Sorting; Searching Dan Barrish-Flood. 2 Bucket Sort Put keys into n buckets, then sort each bucket, then concatenate. If keys are uniformly distributed.
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.
CS 3343: Analysis of Algorithms Lecture 14: Order Statistics.
Medians and Order Statistics
1 Selection --Medians and Order Statistics (Chap. 9) The ith order statistic of n elements S={a 1, a 2,…, a n } : ith smallest elements Also called selection.
Introduction to Algorithms
Introduction to Algorithms Jiafen Liu Sept
Spring 2015 Lecture 5: QuickSort & Selection
Median/Order Statistics Algorithms
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Median, order statistics. Problem Find the i-th smallest of n elements.  i=1: minimum  i=n: maximum  i= or i= : median Sol: sort and index the i-th.
Selection: Find the ith number
Tirgul 4 Order Statistics Heaps minimum/maximum Selection Overview
David Luebke 1 7/2/2015 Linear-Time Sorting Algorithms.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
David Luebke 1 8/17/2015 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
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.
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.
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.
Order Statistics(Selection Problem)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
1 Medians and Order Statistics CLRS Chapter 9. upper median lower median The lower median is the -th order statistic The upper median.
1 Algorithms CSCI 235, Fall 2015 Lecture 19 Order Statistics II.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
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.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
Randomized Quicksort (8.4.2/7.4.2) Randomized Quicksort –i = Random(p, r) –swap A[p]  A[i] –partition A(p, r) Average analysis = Expected runtime –solving.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Chapter 9: Selection of Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Many slides here are based on E. Demaine , D. Luebke slides
Order Statistics.
Order Statistics Comp 122, Spring 2004.
Introduction to Algorithms Prof. Charles E. Leiserson
Analysis of Algorithms
Linear-Time Sorting Continued Medians and Order Statistics
Order Statistics(Selection Problem)
CS 3343: Analysis of Algorithms
Randomized Algorithms
Data Structures Review Session
Medians and Order Statistics
Topic: Divide and Conquer
CS 3343: Analysis of Algorithms
Order Statistics Comp 550, Spring 2015.
CS 3343: 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.
CS 332: Algorithms Quicksort David Luebke /9/2019.
Chapter 7 Quicksort.
Chapter 9: Medians and Order Statistics
Topic: Divide and Conquer
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Order Statistics Comp 122, Spring 2004.
Chapter 9: Selection of Order Statistics
The Selection Problem.
Quicksort and Randomized Algs
Algorithms CSCI 235, Spring 2019 Lecture 19 Order Statistics
CS200: Algorithm Analysis
Analysis of Algorithms CS 477/677
Divide and Conquer Merge sort and quick sort Binary search
Medians and Order Statistics
Presentation transcript:

Randomized Algorithms 2018年8月27日4时11分 Randomized Algorithms

2018年8月27日4时11分 Upper Bounds We’d like to say “Algorithm A never takes more than f(n) steps for an input of size n” “Big-O” Notation gives worst-case, i.e., maximum, running times. A correct algorithm is a constructive upper bound on the complexity of the problem that it solves.

2018年8月27日4时11分 Lower Bounds Establishe the minimum amount of time needed to solve a given computational problem: Searching a sorted list takes at least O(log N) time. Note that such arguments also classify the problem; need not be constructive!

Randomized vs Deterministic Algorithms 2018年8月27日4时11分 Randomized vs Deterministic Algorithms Deterministic algorithms Take at most x steps Assuming input distribution, analyze expected running time. Randomized algorithms Take x steps on average Take x steps with high probability Output “correct” answer with probability p

Randomized Quicksort Always output correct answer 2018年8月27日4时11分 Randomized Quicksort Always output correct answer Takes O(N log N) time on average Likelihood of running O(N log N) time? Running time is O(N log N) with probability 1-N^(-6).

2018年8月27日4时11分 Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is the 1st order statistic The maximum is the nth order statistic The median is the n/2th order statistic If n is even, there are 2 medians How can we calculate order statistics? What is the running time?

2018年8月27日4时11分 Order Statistics How many comparisons are needed to find the minimum element in a set? The maximum? Can we find the minimum and maximum with less than twice the cost? Yes: Walk through elements by pairs Compare each element in pair to the other Compare the largest to maximum, smallest to minimum Total cost: 3 comparisons per 2 elements, 3n/2 comparisons in total

Finding Order Statistics: The Selection Problem 2018年8月27日4时11分 Finding Order Statistics: The Selection Problem A more interesting problem is selection: finding the ith smallest element of a set We will show: A practical randomized algorithm with O(n) expected running time A cool algorithm of theoretical interest only with O(n) worst-case running time

Randomized Selection Key idea: use partition() from quicksort 2018年8月27日4时11分 Randomized Selection Key idea: use partition() from quicksort But, only need to examine one subarray This savings shows up in running time: O(n) q = RandomizedPartition(A, p, r)  A[q]  A[q] p q r

Randomized Selection RandomizedSelect(A, p, r, i) 2018年8月27日4时11分 Randomized Selection RandomizedSelect(A, p, r, i) if (p == r) then return A[p]; q = RandomizedPartition(A, p, r) k = q - p + 1; if (i == k) then return A[q]; if (i < k) then return RandomizedSelect(A, p, q-1, i); else return RandomizedSelect(A, q+1, r, i-k); k  A[q]  A[q] p q r

Randomized Selection Analyzing RandomizedSelect() 2018年8月27日4时11分 Randomized Selection Analyzing RandomizedSelect() Worst case: partition always 0:n-1 T(n) = T(n-1) + O(n) = ??? = O(n2) (arithmetic series) No better than sorting! “Best” case: suppose a 9:1 partition T(n) = T(9n/10) + O(n) = ??? = O(n) (Master Theorem, case 3) Better than sorting! What if this had been a 99:1 split?

Randomized Selection Average case What happened here? 2018年8月27日4时11分 Randomized Selection Average case For upper bound, assume ith element always falls in larger side of partition: Let’s show that T(n) = O(n) by substitution What happened here?

Randomized Selection Assume T(n)  cn for sufficiently large c: 2018年8月27日4时11分 Randomized Selection Assume T(n)  cn for sufficiently large c: The recurrence we started with Substitute T(n)  cn for T(k) What happened here? “Split” the recurrence What happened here? Expand arithmetic series What happened here? What happened here? Multiply it out

Randomized Selection Assume T(n)  cn for sufficiently large c: 2018年8月27日4时11分 Randomized Selection Assume T(n)  cn for sufficiently large c: The recurrence so far Multiply it out What happened here? Subtract c/2 What happened here? Rearrange the arithmetic What happened here? What happened here? What we set out to prove

Worst-Case Linear-Time Selection 2018年8月27日4时11分 Worst-Case Linear-Time Selection Randomized algorithm works well in practice What follows is a worst-case linear time algorithm, really of theoretical interest only Basic idea: Generate a good partitioning element Call this element x

Worst-Case Linear-Time Selection 2018年8月27日4时11分 Worst-Case Linear-Time Selection The algorithm in words: 1. Divide n elements into groups of 5 2. Find median of each group (How? How long?) 3. Use Select() recursively to find median x of the n/5 medians 4. Partition the n elements around x. Let k = rank(x) 5. if (i == k) then return x if (i < k) then use Select() recursively to find ith smalles element in first partition else (i > k) use Select() recursively to find (i-k)th smallest element in last partition

Worst-Case Linear-Time Selection 2018年8月27日4时11分 Worst-Case Linear-Time Selection How many of the 5-element medians are  x? At least 1/2 of the medians = n/5 / 2 = n/10 How many elements are  x? At least 3 n/10  elements For large n, 3 n/10   n/4 (How large?) So at least n/4 elements  x Similarly: at least n/4 elements  x

Worst-Case Linear-Time Selection 2018年8月27日4时11分 Worst-Case Linear-Time Selection Thus after partitioning around x, step 5 will call Select() on at most 3n/4 elements The recurrence is therefore: n/5   n/5 ??? Substitute T(n) = cn ??? Combine fractions ??? Express in desired form ??? What we set out to prove ???

Worst-Case Linear-Time Selection 2018年8月27日4时11分 Worst-Case Linear-Time Selection Intuitively: Work at each level is a constant fraction (19/20) smaller Geometric progression! Thus the O(n) work at the root dominates

Selection Algorithm Using sorting, takes O(nlogn) time 2018年8月27日4时11分 Selection Algorithm Using sorting, takes O(nlogn) time A randomized algorithm Takes O(n) time on average Takes O(n) time with high probability A deterministic algorithm Takes O(n) time

An Example of Randomized Algorithm 2018年8月27日4时11分 An Example of Randomized Algorithm A group of N nodes want to know their value, but nobody wants to reveal their own value. Node Ai’s value is ai We'll compute the sum mod m, m is a sufficient large number A2 A3 A1 AN A4

An Example of Randomized Algorithm – the protocol 2018年8月27日4时11分 An Example of Randomized Algorithm – the protocol Node Ai chooses N-1 random numbers X1i, X2i, ..., XN-1i in the range 0,...,M-1 and a number pi Each node Ai distributes the N-1 Xji to the N-1 other nodes and keeps the number pi to his own Each node computes si like this Each node Ai distributes the si to others All nodes can know the sum by

Many Other Randomized Algorithms 2018年8月27日4时11分 Many Other Randomized Algorithms Hashing: Universal Hash Function Resource Sharing Ethernet collision avoidance Load Balancing Packet Routing Primality Test