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;

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

Lower bound: Decision tree and adversary argument
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
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:
1 Sorting in Linear Time How can we do better?  CountingSort  RadixSort  BucketSort.
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.
1 Today’s Material Medians & Order Statistics – Ch. 9.
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.
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
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.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Lecture 5: Linear Time Sorting Shang-Hua Teng. Sorting Input: Array A[1...n], of elements in arbitrary order; array size n Output: Array A[1...n] of the.
Comp 122, Spring 2004 Lower Bounds & Sorting in Linear Time.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS421 - Course Information Website Syllabus Schedule The Book:
Tirgul 4 Sorting: – Quicksort – Average vs. Randomized – Bucket Sort Heaps – Overview – Heapify – Build-Heap.
Lecture 5: Master Theorem and Linear Time Sorting
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
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]
10/15/2002CSE More on Sorting CSE Algorithms Sorting-related topics 1.Lower bound on comparison sorting 2.Beating the lower bound 3.Finding.
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?
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!
File Organization and Processing Week 13 Divide and Conquer.
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.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Fall 2015 Lecture 4: Sorting in linear time
Getting Started Introduction to Algorithms Jeff Chastine.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Sorting What makes it hard? Chapter 7 in DS&AA Chapter 8 in DS&PS.
Probabilistic Analysis and Randomized Algorithm. Average-Case Analysis  In practice, many algorithms perform better than their worse case  The average.
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.
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.
Linear Sorting. Comparison based sorting Any sorting algorithm which is based on comparing the input elements has a lower bound of Proof, since there.
1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Lower Bounds & Sorting in Linear Time
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Order Statistics Comp 122, Spring 2004.
QuickSort QuickSort Best, Worst Average Cases K-th Ordered Statistic
Analysis of Algorithms CS 477/677
Linear Sorting Sorting in O(n) Jeff Chastine.
Chapter 5: Probabilistic Analysis and Randomized Algorithms
Lower Bounds & Sorting in Linear Time
Order Statistics Comp 122, Spring 2004.
Analysis of Algorithms
Algorithms Sorting.
Chapter 5: Probabilistic Analysis and Randomized Algorithms
Randomized Algorithms
Randomized Algorithms
Presentation transcript:

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; 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?

Intro - 3 Comp 122, 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).

Intro - 4 Comp 122, 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)?

Intro - 5 Comp 122, 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(?)]

Intro - 6 Comp 122, 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  Run algorithm backwards: permutation determines choices! 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)]

Intro - 7 Comp 122, 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)]