Probabilistic Analysis and Randomized Algorithm. Average-Case Analysis  In practice, many algorithms perform better than their worse case  The average.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Sorting in Linear Time Comp 550, Spring Linear-time Sorting Depends on a key assumption: numbers to be sorted are integers in {0, 1, 2, …, k}. Input:
CSE 3101: Introduction to the Design and Analysis of Algorithms
Mudasser Naseer 1 5/1/2015 CSC 201: Design and Analysis of Algorithms Lecture # 9 Linear-Time Sorting Continued.
Counting Sort Non-comparison sort. Precondition: n numbers in the range 1..k. Key ideas: For each x count the number C(x) of elements ≤ x Insert x at output.
CSC 2300 Data Structures & Algorithms March 16, 2007 Chapter 7. Sorting.
CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Chapter 5. Probabilistic Analysis and Randomized Algorithms
UMass Lowell Computer Science Analysis of Algorithms Spring, 2002 Chapter 5 Lecture Randomized Algorithms Sections 5.1 – 5.3 source: textbook.
Probabilistic (Average-Case) Analysis and Randomized Algorithms Two different approaches –Probabilistic analysis of a deterministic algorithm –Randomized.
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.
Analysis of Algorithms
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
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]
WS Algorithmentheorie 03 – Randomized Algorithms (Overview and randomised Quicksort) Prof. Dr. Th. Ottmann.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
1 Hiring Problem and Generating Random Permutations Andreas Klappenecker Partially based on slides by Prof. Welch.
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.
Comp 122, Spring 2004 Lower Bounds & Sorting in Linear Time.
CS421 - Course Information Website Syllabus Schedule The Book:
Complexity Analysis (Part I)
CSE 830: Design and Theory of Algorithms
How should a computer shuffle?. Intro - 2 Comp 122, Goal  Input: Given n items to shuffle (cards, …)  Output: Return some list of exactly those n items;
Analysis of Algorithms CS 477/677
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 5 Linear-time sorting Can we do better than comparison sorting? Linear-time sorting.
1 Randomized Algorithms Andreas Klappenecker [using some slides by Prof. Welch]
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.
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner.
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Lecture 2 We have given O(n 3 ), O(n 2 ), O(nlogn) algorithms for the max sub-range problem. This time, a linear time algorithm! The idea is as follows:
CSCE 2100: Computing Foundations 1 Probability Theory Tamara Schneider Summer 2013.
Randomized Algorithms (Probabilistic algorithm) Flip a coin, when you do not know how to make a decision!
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.
Elementary Sorting Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Sorting Fun1 Chapter 4: Sorting     29  9.
BY Lecturer: Aisha Dawood. The hiring problem:  You are using an employment agency to hire a new office assistant.  The agency sends you one candidate.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Introduction to Algorithms Jiafen Liu Sept
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
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.
Algorithms IS 320 Spring 2015 Sorting. 2 The Sorting Problem Input: –A sequence of n numbers a 1, a 2,..., a n Output: –A permutation (reordering) a 1.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
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.
1 Computer Algorithms Lecture 8 Sorting Algorithms Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
1 Algorithms CSCI 235, Fall 2015 Lecture 17 Linear Sorting.
Introduction to Algorithms Randomized Algorithms – Ch5 Lecture 5 CIS 670.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Linear Sorting. Comparison based sorting Any sorting algorithm which is based on comparing the input elements has a lower bound of Proof, since there.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Introduction to Algorithms Prof. Charles E. Leiserson
Analysis of Algorithms CS 477/677
Linear Sorting Sorting in O(n) Jeff Chastine.
Chapter 5: Probabilistic Analysis and Randomized Algorithms
Analysis of Algorithms
Chapter 5: Probabilistic Analysis and Randomized Algorithms
Randomized Algorithms
Randomized Algorithms
Presentation transcript:

Probabilistic Analysis and Randomized Algorithm

Average-Case Analysis  In practice, many algorithms perform better than their worse case  The average case is analyzed by  Construct a probabilistic model of their input  Determine the probabilities and running times (or costs) of alternate executions  Calculate expected running time (or cost) COT5407

Example 1 COT5407

Probabilistic Model  Assume A has n distinct numbers. (What is the effect of duplicates?)  Assume each permutation of the numbers is equally likely (How can we guarantee this? through randomization)  How many permutations are there?  What is the probability of the best case?  What is the probability of the worst case? COT5407

Example 1: Analysis COT5407

Randomized Algorithm  We might not know the distribution of inputs, or we might not be able to model it computationally  Instead we use randomization within the algorithm in order to impose a distribution on the inputs  An algorithm is randomized if its behavior is determined in part by values produced by a random-number generator  How to compute a shuffle? COT5407

Goal  Input: Given n items to shuffle (cards, …)  Output: Return some list of exactly those n items; all n! lists should be equally likely.  Not the same as saying “each card is equally likely at each position!” Why not?  Possible methods?  Swap a pair of randomly chosen cards?  Choose keys and sort?  Swap each card with a randomly chosen card?

Choose key and sort  Book suggests: Assign each card a key number from [1..K]. Sort keys to permute cards  What is the probability that…  the second card gets the same key as the first? 1/K  the third gets the first or second, assuming that the first and second have different keys? 2/K  That we have some duplicate key among n cards?  1/K + 2/K + … + n/K = n(n+1)/(2K)  Choose K = n^3 and the probability is < 1/n  Expected time: T(n) = O(n lg n) + T(n)/n = O(n lg n).

Random Shuffle?  Goal: uniform random permutation of an array.  RANDOM(n) – returns an integer 1  r  n with each of the n values of r being equally likely.  In iteration i, choose A[i] randomly from A[1..?].  A[i] is never altered after iteration i.  Running Time: O(n) Shuffle(A) n  length[A] for i  n downto 2 do swap A[i] ↔ A[RANDOM(?)] Shuffle(A) n  length[A] for i  n downto 2 do swap A[i] ↔ A[RANDOM(?)] n? i? (i-1)?

Finding the correct shuffle  (i-1) forces change in each element.  n has n n-1 possible outcomes, but since n! does not divide n n-1, some must occur more frequently than others.  i works … we should prove it. Shuffle(A) n  length[A] for i  n downto 2 do swap A[i] ↔ A[RANDOM(?)] Shuffle(A) n  length[A] for i  n downto 2 do swap A[i] ↔ A[RANDOM(?)]

Proving the shuffle correct  Consider the random numbers chosen by a run of the algorithm: RANDOM(n), RANDOM(n-1), …, RANDOM(2), RANDOM(1)  Choices are independent: n·(n-1) ···2·1 = n! choices  We have chosen one uniformly at random.  Claim: Each choice produces to a unique permutation  By running algorithm, choices determine the permutation Shuffle(A) n  length[A] for i  n downto 2 do swap A[i] ↔ A[RANDOM(i)] Shuffle(A) n  length[A] for i  n downto 2 do swap A[i] ↔ A[RANDOM(i)]

Random Shuffle  Goal: uniform random permutation of an array.  RANDOM(n) – returns an integer 1  r  n with each of the n values of r being equally likely.  In iteration i, choose A[i] randomly from A[1..i].  A[i] is never altered after iteration i.  Running Time: O(n) Shuffle(A) n  length[A] for i  n downto 1 do swap A[i] ↔ A[RANDOM(i)] Shuffle(A) n  length[A] for i  n downto 1 do swap A[i] ↔ A[RANDOM(i)]