Derangements
Derangements
( 1 - 2x)2
Thus
f (x) = (X - x2)(1 + 2.22 + 3.2'~' + 4 . 2 3 ~ +3 . . - )
whence
a , = coefficient of x n P 1 in 1+ 2.22 + . . .
- coefficient of x " - ~ in 1 + 2.22 + . . - .
as before.
2.4 Derangements 1
Suppose that n people at a party leave their coats in the cloakroom. After the
party, they each take a coat at random. How likely is it that no person gets the
correct coat?
A derangement of 1 , . . . , n is a permutation 7r of 1 , . . . ,n such that ~ ( i#) i
for each i. For example, there are nine derangements of 1 , 2 , 3 , 4 :
In each of these 1 is not in the first place, 2 is not in the second, and so on.
Let d, denote the number of derangements of 1 , . . . ,n . Then (check!)
2. Recurrence 29
Our aim is to obtain a recurrence relation for the d, and then use it to obtain
a formula for d,. Before proceeding to the recurrence relation, note that dn is
the number of ways of assigning n objects to n boxes, where, for each object,
there is one prohibited box, and where each box is prohibited to just one object.
Above, the objects and boxes are both labelled by 1,. . . ,n, with box (position)
i prohibited to object (number) i, but the labelling of the boxes and objects is
of course arbitrary and does not affect the problem.
Next note that in three of the nine derangements of 1,2,3,4 listed above,
4 swaps places with another number: this happens in 2143,3412 and 4321. In
the remaining derangements 4 does not swap places with another. With this
in mind, we put
dn = e n + f n
where en, fn denote the numbers of derangements of 1 , . . . ,n in which n swaps,
does not swap, places with another. Now if n swaps places with i (and there are
n - 1 possible choices for i), the remaining n - 2 numbers have to be deranged,
and this can be done in dn-2 ways; SO
If n does not swap places with any other, then some r goes to place n (and
there are n - 1choices of r ) , while n does not go to place r. So we have to assign
places to 1,. . . ,n, excluding r , where the available are 1,. . . ,n - 1, and
where each has precisely one forbidden place (for i # r, n, place i is forbidden;
for i = n, place r is forbidden). So there are dn-1 possible arrangements, and
SO
fn = (n - l)dn-l.
Thus by the addition principle, we have
and so on.
The recurrence (2.8) does not permit the use of the auxiliary equation
method, since the coefficients of dn-1 and dn-2 are not constants. However,
we can manipulate (2.8) into a more manageable form. Equation (2.8) can be
rewritten as
dn - ndn-l = -(dn-1 - (n - l)dn-n),
where the expression on the right is the negative of the expression on the left,
with n replaced by n - 1. So iteration gives
30 Discrete Mathematics
Thus
-
dm - dm-1 -
-
m! (m- 1 m!
over m = 2,3,. . . , n, we get cancellations on the left, giving
But dl = 0, so we obtain
so the ~ r o b a b i l i of
t ~ no one getting their own coat back after the party tends
1
to - = 0.36788 as n - iIndeed, for n as small as 6,
oo.
e
1
agreeing with - to 3 decimal places.
e
Example 2.7
(a) Find the number of permutations of 1,. . . , n in which exactly k of the
numbers are in their correct position, and deduce that
(b) What is the average number of numbers in their correct position in a random
permutation of 1, . . . , n?
2. Recurrence 31
Solution
(a) There are (); ways of choosing the k numbers to be fixed. The remaining
n - k have to be deranged, and this can be done in dn-k ways. So there are
(;)dnPk permutations with exactly k fixed numbers.
But any of the n! permutations fixes k numbers for some k between 0 and n.
on putting L = n - k.
(b) The average number of fixed numbers in a permutation of 1 , . . . ,n is
- -
1
(n - I)!
C (n ;l) dl (on putting t = n - k)
n-l
f=O
Bubblesort
Take a list of n numbers, in random order. Compare the first two, swapping
them round if they are not in increasing order. Then compare the second and
third numbers, again swapping if necessary. In this way proceed up the se-
quence; the largest number will then be at the end. Next repeat the whole
process for the first n - 1 numbers: this will take the second largest to the
second last position. Repeat for the first n - 2, and so on.
The total number of comparisons involved in this procedure is
1 1 2 1
(n-l)+(n-2)+...+2+1=-n(n-l)=-n -in,
2 2
so we say that the bubblesort algorithm has O(n2) complexity.
Example 2.8
Start with 7,10,4,6,3.
After the first 4 comparisons we have 7,4,6,3,10.
After the next 3 comparisons we have 4,6,3,7,10.
After the next 2, we have 4,3,6,7,10.
After the final comparison we have 3,4,6,7,10.
Mergesort
The idea here is to split the given list into two (roughly) equal parts, sort each
separately, and then merge (combine) them.
The process of combining two sorted lists of lengths L and m into one list can
+
be accomplished by L m - 1 comparisons. For suppose we have two such lists,
both in increasing order. Compare the first (smallest) numbers in the lists, and
take the smaller as the first member of a new list L, crossing it out of its original