Loading in 5 sec....

Perfect ShufflePowerPoint Presentation

Perfect Shuffle

Download Presentation

TECHNICAL INTERVIEW QUESTION on DATA STRUCTURES & ALGORITHMS Perfect unbiased shuffle algorithm

Loading in 2 Seconds...

- 41 Views
- Uploaded on
- Presentation posted in: Education / Career

Perfect Shuffle

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

CodeGround Online Testing Platform is an online assessment and evaluation system that helps Recruiters conduct online screening tests to filter candidates before the interview process. CodeGround Recruitment Tests can be used during Campus Recruitment or screening walk-in candidates. CodeGround supports Aptitude Tests, English Communication Skills Assessments and Online Coding Contests in C, C++, Java, PHP, Ruby, Python and JavaScript. CodeGround also supports asynchronous automated interviews.

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:

- 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!
- 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

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

}

}

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

}

}

rand = 3

rand = 1

rand = 2

{1,2,3}

{3,2,1}

{2,1,3}

{1,2,3}

rand = 1

rand = 1

rand = 1

{2,1,3}

{2,3,1}

{1,2,3}

rand = 1

rand = 1

rand = 1

{3,1,2}

{1,3,2}

{3,2,1}

rand = 2

rand = 2

rand = 2

{2,3,1}

{2,1,3}

{1,3,2}

rand = 3

rand = 3

rand = 3

{2,1,3}

{2,3,1}

{1,2,3}

rand = 2

rand = 2

rand = 2

{1,2,3}

{3,2,1}

{2,1,3}

rand = 1

rand = 1

rand = 1

{3,2,1}

{1,2,3}

{3,1,2}

rand = 2

rand = 2

rand = 2

{1,3,2}

{3,1,2}

{2,3,1}

rand = 3

rand = 3

rand = 3

{1,2,3}

{3,2,1}

{2,1,3}

rand = 3

rand = 3

rand = 3

{1,3,2}

{3,1,2}

{2,3,1}

rand = 1

rand = 1

rand = 1

{2,3,1}

{2,1,3}

{1,3,2}

rand = 2

rand = 2

rand = 2

{1,2,3}

{3,2,1}

{2,1,3}

rand = 3

rand = 3

rand = 3

{1,3,2}

{3,1,2}

{2,3,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];

}

}

Count of permutations:

{1,2,3} – count 4

{1,3,2} – count 5

{2,1,3} – count 5

{2,3,1} – count 5

{3,1,2} – count 4

{3,2,1} – count 4

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

}

}

rand = 3

rand = 1

rand = 2

{1,2,3}

{3,2,1}

{2,1,3}

{1,2,3}

rand = 2

rand = 2

rand = 2

{1,2,3}

{3,2,1}

{2,1,3}

rand = 3

rand = 3

rand = 3

{1,2,3}

{3,2,1}

{2,1,3}

rand = 3

rand = 3

rand = 3

{1,3,2}

{3,1,2}

{2,3,1}

rand = 3

rand = 3

rand = 3

{1,3,2}

{3,1,2}

{2,3,1}

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

}

}

Count of permutations:

{1,2,3} – count 1

{1,3,2} – count 1

{2,1,3} – count 1

{2,3,1} – count 1

{3,1,2} – count 1

{3,2,1} – count 1

FISHER-YATES SHUFFLE

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

}

}

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

Comment

Share

Like

CodeGround Online Testing Platform is an online assessment and evaluation system that helps Recruiters conduct online screening tests to filter candidates before the interview process. CodeGround Recruitment Tests can be used during Campus Recruitment or screening walk-in candidates. CodeGround supports Aptitude Tests, English Communication Skills Assessments and Online Coding Contests in C, C++, Java, PHP, Ruby, Python and JavaScript. CodeGround also supports asynchronous automated interviews.