#1  
Old 11-17-2016, 10:37 PM
CodeGroundOnlineTests CodeGroundOnlineTests is offline
Junior Member
 
Join Date: Nov 2016
Posts: 8
Default Perfect Shuffle

Hi All,
I found the comparison between these two random shuffling algorithms to be interesting.
Consider two shuffling algorithms
SHUFFLE 1
shuffle(A[1 … n])** {
*** for i = 1 to n** {
******* // Find a random integer between 1 and n inclusive
******* int rand= RANDOM(1,n);
******* swap A[i] with A[rand];
*** }
}
SHUFFLE 2
shuffle(A[1 … n])** {
*** for i = 1 to n** {
******* // Find a random integer between i and n inclusive
******* int rand= RANDOM(i,n);
******* swap A[i] with A[rand];
*** }
}
How do these two shuffling algorithms compare against each other?*
Which of these two is a perfect shuffling algorithm?
Consider an array with distinct elements A[1 … n]
A perfectly unbiased shuffle algorithm would randomly shuffle all elements in the array such that after shuffling:
1.The probability that the shuffling operation would produce any particular permutation of the original array is the same for all permutations (i.e.) since there are n! permutations, the probability that the shuffle operation would produce any particular permutation is 1/n!
2.For any element e in the array and for any position j (1<= j <= n), the probability that the element would end up in position A[j] is 1/n
Simulating Shuffle 1 and Shuffle 2 ( https://codeground.in/blog/index.php...rfect-shuffle/ ) clearly proves that Shuffle 1 is biased while Shuffle 2 is unbiased
Can we prove that Shuffle 2 will produce an unbiased shuffle in all cases?
For any element e, the probability that it will be shuffled into the first position
= probability that it is selected for swapping when i = 1
= 1/n
For any element e, the probability that it will be shuffled into the second position
= probability that it is NOT selected for the first position x probability that it is selected for swapping when i = 2
= (n-1)/n** x** 1/(n-1)
= 1/n

For any element e, the probability that it will be shuffled into any particular position = 1/n

Was this useful? If you’re interested, you can read more about Technical Interview questions on Data Structures and Algorithms ( https://codeground.in/blog/index.php...iew-questions/ ) or look at some programming challenges ( https://codeground.in/screening-test...-contests.html )
Reply With Quote
Reply

Tags
algorithm, data structure, puzzle

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 12:06 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

About Puzzle Baron

The Puzzle Baron family of web sites has served millions and millions of puzzle enthusiasts since its inception in 2006. From cryptograms to acrostics, logic puzzles to drop quotes, patchwords to wordtwist and even sudoku, we run the gamut in word puzzles, printable puzzles and logic games.