Generating Random Derangements

Report 22 Downloads 164 Views
Generating Random Derangements 1

Conrado Martínez 1

Alois Panholzer

2

Univ. Politècnica de Catalunya, Spain Tech. Univ. Wien, Austria Univ. Stellenbosch, South Africa 2

3

3

Helmut Prodinger

Derangements

Le Problème des Derangements: A number of gentlemen, say

n, surrender their top hats in the

cloakroom and proceed to the evening's enjoyment. After wining and dining (and wining some more), they stumble back to the cloakroom and confusedly take the rst top-hat they see. What is the probability that no gentleman gets his own hat?

Derangements I

A derangement is a permutation without xed points:

i, 1  i  n I The number Dn of derangements of size n is

 (i ) 6= i

for any

Dn = n !  I

As



1

1

0!

1!

+

1

1

2!

3!

+

   + n!

n ! 1, Dn =n!  1=e  0:36788.

(

1)

n

 =



n! + 1 : e

Derangements I

A derangement is a permutation without xed points:

i, 1  i  n I The number Dn of derangements of size n is

 (i ) 6= i

for any

Dn = n !  I

As



1

1

0!

1!

+

1

1

2!

3!

+

   + n!

n ! 1, Dn =n!  1=e  0:36788.

(

1)

n

 =



n! + 1 : e

Derangements I

A derangement is a permutation without xed points:

i, 1  i  n I The number Dn of derangements of size n is

 (i ) 6= i

for any

Dn = n !  I

As



1

1

0!

1!

+

1

1

2!

3!

+

   + n!

n ! 1, Dn =n!  1=e  0:36788.

(

1)

n

 =



n! + 1 : e

Break #1: Fisher-Yates' shue

n

procedure RandomPermutation( )

i 1 to n do A[i ] i for i n downto 1 do j Uniform(1; i ) A[i ] $ A[j ] return A for

Break #2: Sattolo's algorithm

n

procedure RandomCyclicPermutation( )

i 1 to n do A[i ] i for i n downto 1 do j Uniform(1; i 1) A[i ] $ A[j ] return A for

A recurrence for the number of derangements

D0 = 1 ; D1 = 0 D n = ( n 1 ) Dn 1 + ( n

1)

Dn 2

A recurrence for the number of derangements D0 = 1 ; D1 = 0 D n = ( n 1 ) Dn 1 + ( n

1)

Dn 2

n belongs to a cycle of length > 2. n is built by constructing a derangement of size n 1 and then n is inserted into any of the cycles (of length  2); there are (n 1) possible ways to do that Choice #1:

The derangement of size

A recurrence for the number of derangements D0 = 1 ; D1 = 0 D n = ( n 1 ) Dn 1 + ( n

1)

Dn 2

n belongs to a cycle of length 2. n is built by constructing a cycle of size 2 with n and some j , 1  j  n 1; then we build a derangement of size n 2 with the remaining elements Choice #2:

The derangement of size

The rejection method Require:

n 6= 1

n

procedure RandomDerangement( ) repeat

A

n A) return A

RandomPermutation( )

until Is-Derangement(

P[A is a derangement]  e E[# of calls to Random] = e  n + O (1) 1

The rejection method Require:

n 6= 1

n

procedure RandomDerangement( ) repeat

A

n A) return A

RandomPermutation( )

until Is-Derangement(

P[A is a derangement]  e E[# of calls to Random] = e  n + O (1) 1

The recursive method f1; 2; : : : ; ng

C

RandomDerangement-Rec(

n; C )

n 6= 1

Require:

n C)

procedure RandomDerangement-Rec( , if

j p

if

n  1 then return

a random element from

;

C

Uniform(0 1)

p < (n

1)

Dn 2 =Dn then

RandomDerangement-Rec(

 (n )

j ;  (j )

n

else

RandomDerangement-Rec(

 (n )

 (j );  (j )

n

n

2

;C

n f j ; n g)

n

1

;C

n f n g)

Our algorithm Require:

n 6= 1

n i ; mark [i ]

procedure RandomDerangement( )

1 to n do A[i ] false n; u n while u  2 do if :mark [i ] then j pick a random unmarked element in A[1::i for

i

i

A[i ] $ A[j ] if

j

shall be marked

mark [j ] u u 1 i i 1 return A

true;

 close the cycle

u

u

1

then

1]

Our algorithm Require:

n 6= 1

n i ; mark [i ]

procedure RandomDerangement( )

1 to n do A[i ] n; u n while u  2 do if :mark [i ] then repeat j Random(1; i until :mark [j ] A[i ] $ A[j ] p Uniform(0; 1) if p < (u 1 ) Du 2 = Du mark [j ] true; u u u u 1 i i 1 return A for

i

i

false

1)

1

Our algorithm procedure RandomDerangement(n) for i 1 to n do A[i ] i ; mark [i ]

i

false

n; u n while u  2 do if :mark [i ] then repeat j Random(1; i 1) until :mark [j ] A[i ] $ A[j ] p Uniform(0; 1) if p < (u 1)Du 2 =Du then mark [j ] true; u u u u 1 i i 1 return A

1 1

2

3

4

5

6

7

8

"

Our algorithm procedure RandomDerangement(n) for i 1 to n do A[i ] i ; mark [i ]

i

false

n; u n while u  2 do if :mark [i ] then repeat j Random(1; i 1) until :mark [j ] A[i ] $ A[j ] p Uniform(0; 1) if p < (u 1)Du 2 =Du then mark [j ] true; u u u u 1 i i 1 return A

1 1

2

3

4

8

6

7

5

Our algorithm procedure RandomDerangement(n) for i 1 to n do A[i ] i ; mark [i ]

i

false

n; u n while u  2 do if :mark [i ] then repeat j Random(1; i 1) until :mark [j ] A[i ] $ A[j ] p Uniform(0; 1) if p < (u 1)Du 2 =Du then mark [j ] true; u u u u 1 i i 1 return A

1 1

2

3

4

8

6

7

"

5

Our algorithm procedure RandomDerangement(n) for i 1 to n do A[i ] i ; mark [i ]

i

false

n; u n while u  2 do if :mark [i ] then repeat j Random(1; i 1) until :mark [j ] A[i ] $ A[j ] p Uniform(0; 1) if p < (u 1)Du 2 =Du then mark [j ] true; u u u u 1 i i 1 return A

1 1

7

3

4

8

6

2

5

Our algorithm procedure RandomDerangement(n) for i 1 to n do A[i ] i ; mark [i ]

i

false

n; u n while u  2 do if :mark [i ] then repeat j Random(1; i 1) until :mark [j ] A[i ] $ A[j ] p Uniform(0; 1) if p < (u 1)Du 2 =Du then mark [j ] true; u u u u 1 i i 1 return A

1 1

7

3

4

8

6

"

2

5

Our algorithm procedure RandomDerangement(n) for i 1 to n do A[i ] i ; mark [i ]

i

false

n; u n while u  2 do if :mark [i ] then repeat j Random(1; i 1) until :mark [j ] A[i ] $ A[j ] p Uniform(0; 1) if p < (u 1)Du 2 =Du then mark [j ] true; u u u u 1 i i 1 return A

1 1

7

3

4

6

8

2

5

Our algorithm procedure RandomDerangement(n) for i 1 to n do A[i ] i ; mark [i ]

i

false

n; u n while u  2 do if :mark [i ] then repeat j Random(1; i 1) until :mark [j ] A[i ] $ A[j ] p Uniform(0; 1) if p < (u 1)Du 2 =Du then mark [j ] true; u u u u 1 i i 1 return A

1 1

7

3

4

6

"

8

2

5

Our algorithm procedure RandomDerangement(n) for i 1 to n do A[i ] i ; mark [i ]

i

false

n; u n while u  2 do if :mark [i ] then repeat j Random(1; i 1) until :mark [j ] A[i ] $ A[j ] p Uniform(0; 1) if p < (u 1)Du 2 =Du then mark [j ] true; u u u u 1 i i 1 return A

1 1

7

6

4

3

8

2

5

Our algorithm procedure RandomDerangement(n) for i 1 to n do A[i ] i ; mark [i ]

i

false

n; u n while u  2 do if :mark [i ] then repeat j Random(1; i 1) until :mark [j ] A[i ] $ A[j ] p Uniform(0; 1) if p < (u 1)Du 2 =Du then mark [j ] true; u u u u 1 i i 1 return A

1 1

7

6

4

"

3

8

2

5

Our algorithm procedure RandomDerangement(n) for i 1 to n do A[i ] i ; mark [i ]

i

false

n; u n while u  2 do if :mark [i ] then repeat j Random(1; i 1) until :mark [j ] A[i ] $ A[j ] p Uniform(0; 1) if p < (u 1)Du 2 =Du then mark [j ] true; u u u u 1 i i 1 return A

4 1

7

6

1

3

8

2

5

The analysis I # of marked elements = # of cycles (Cn ) I # of iterations = # of calls to Uniform = n I G = # of calls to Random I Gi = # of calls to Random at iteration i I

Cn

E[cost] = n E[Cn ] + E[G ] X = n E[Cn ] + E[Gi ] 1