0% found this document useful (0 votes)
201 views5 pages

Derangements

This document discusses algorithms for sorting lists of numbers from lowest to highest value. It introduces bubblesort, which compares and swaps adjacent elements until the list is fully sorted, requiring O(n2) comparisons. It also introduces mergesort, which recursively splits the list in half, sorts each half, and then merges the sorted halves together with fewer than n + m - 1 comparisons of split lists of lengths n and m. The average number of elements in the correct position in a random permutation is shown to be 1.

Uploaded by

Murat Yoğurtçu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
201 views5 pages

Derangements

This document discusses algorithms for sorting lists of numbers from lowest to highest value. It introduces bubblesort, which compares and swaps adjacent elements until the list is fully sorted, requiring O(n2) comparisons. It also introduces mergesort, which recursively splits the list in half, sorts each half, and then merges the sorted halves together with fewer than n + m - 1 comparisons of split lists of lengths n and m. The average number of elements in the correct position in a random permutation is shown to be 1.

Uploaded by

Murat Yoğurtçu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

differentiating gives

--1 - 1 + 2 2 + 3x2 + ... ,


( 1 - 2)'
so that
1 =1+2.2~+3.2~~~+.... '

( 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 + . . - .

= n.2,-' - ( n - 1)2n-2 = ( n + 1)2,-'

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

Using this recurrence we get

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

If we now sum the identities

-
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

One interesting consequence of (2.10) is that, as n + oo,

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

So the average number of fixed numbers is 1.

Alternative proofs of (2.10)


A proof of ('2.10)using the inclusion-exclusion principle will be given in Chapter
6. Here we give yet another proof, a simple application of the inversion principle,
as in Corollary 1.15, applied to (2.11).
In (2.11), put a, for n! and bn for d,. Then (2.11) is

so that, by Corollary 1.15,


32 Discrete Mathematics

2.5 Sorting Algorithms


Given a pile of exam scripts, we might want to sort them, i.e. put them in
increasing or decreasing order of marks. Are there any efficient ways of doing
this? We start with a simple but not very efficient procedure. $5

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

You might also like