Presentation is loading. Please wait.

Presentation is loading. Please wait.

Permutations. Motivation Shuffling – Games – Music players Brute-force algorithms.

Similar presentations


Presentation on theme: "Permutations. Motivation Shuffling – Games – Music players Brute-force algorithms."— Presentation transcript:

1 Permutations

2 Motivation Shuffling – Games – Music players Brute-force algorithms

3 Shuffling Given a sequence of values, return a random reordering of them Want unbiased shuffle (usually) Assume access to a fast, unbiased random number generator

4 Fisher-Yates Given sequence of n elements, generate a random number r between 1 and n Remove the r-th element from the sequence and add it to the output sequence Repeat on the leftover elements, until every element has been selected and placed in the output sequence

5 Fisher-Yates Unbiased – Each element is equally likely to be picked first – Given the first selection, each remaining element is equally likely to be picked second Fast – Smart implementation takes O(n) time

6 Implementing Fisher-Yates If input is a linked list, iterating through the list to find the r-th element costs linear time, so runtime falters to O(n 2 ). Ouch. If input is an array, the r-th element cannot be removed. – But we can get around this – Instead of removing selected elements, swap them to front of array, then repeat process on last n-1 elements, and so on

7 Other shuffles Generate a random number for every element in the list, then sort the list – O(n log n) runtime, due to sorting step – O(n) space requirement, for random numbers – The Housing Office uses this – Damn roompicks Random swapping – Biased

8 Lexicographic Permutation Generation Given a sequence of comparable elements, return the lexicographically next permutation Sequences a 1, a 2, …, a n and b 1, b 2, …, b n ; sequence a is said to be lexicographically earlier than sequence b if a j < b j, where j is the first index at which the two sequences differ – e.g. “act” < “cat”, “loop” < “pool”

9 Lexicographic Permutation Generation Find latest position j such that a j < a j+1 – If no j exists, our sequence is the last permutation Find latest position k such that a j < a k – k must be later than j, since j+1 satisfies this Swap a j and a k Reverse the subsequence a j+1, …, a n

10 Lexicographic Permutation Generation Each generation could take as long as O(n) – As it turns out, this is actually O(1) amortized – Consider the average position of j If we want to generate all n! permutations, we only need O(n!) time – Provided n is very small (think single-digit or low 10s), we can brute-force over all permutations

11 Steinhaus-Johnson-Trotter Given a sorted sequence, generates a new permutation (not necessarily next-lex) with each call Strange algorithm – Difficult to prove correct

12 Steinhaus-Johnson-Trotter Create a “mobility array” of length n associated with the sequence, where each element can be “left” or “right” Define a “mobile value” as an element in the sequence whose adjacent value in the direction of its mobility is smaller than the element itself

13 Steinhaus-Johnson-Trotter Pseudocode follows: while there exists a mobile value let k be the largest mobile value swap k and its neighbor in the direction of k’s mobility for each value s in the sequence if s > k reverse the direction of mobility of s

14 Steinhaus-Johnson-Trotter Cons: – Requires O(n) time (not amortized) and O(n) space – Requires initial input to be sorted – Does not use lexicographic ordering – If mobility array is lost, must restart Pros: – Has an impressive-sounding name – Might possibly get you women


Download ppt "Permutations. Motivation Shuffling – Games – Music players Brute-force algorithms."

Similar presentations


Ads by Google