Hello! It's great to see you again. In our previous lesson, we saw how the Principle of Inclusion-Exclusion (PIE) can be combined with the stars and bars method to solve complex distribution problems involving upper limits. The core strategy was to count the total possibilities and then subtract the "bad" cases—those that violated our constraints.
Today, we'll apply this exact same strategic thinking to a completely different, and very famous, problem in combinatorics: counting permutations. Our goal is to use PIE to determine the number of derangements, which are permutations where no element ends up in its original position. This is a classic application of PIE and a cornerstone of enumerative combinatorics, frequently appearing in Olympiad-style problems.
What is a Derangement?
Imagine a hat-check attendant who has collected hats from guests. At the end of the night, the attendant hands back the hats randomly. A derangement occurs if no single guest receives their own hat back.
Formally, a derangement of a set of elements is a permutation of those elements such that no element appears in its original position. The number of derangements of elements is often denoted as or .
Let's look at a small example with elements, say . There are total ways to arrange these elements.

To see how we can count these without listing them all, we'll turn to the Principle of Inclusion-Exclusion.
Setting up the PIE Framework
Our goal is to count permutations where no element is in its original place. This "no" condition is a strong clue that PIE will be useful. As we did in the last lesson, we will count the total number of permutations and subtract the "bad" ones.
- The universe () is the set of all possible permutations. For elements, .
- A permutation is "bad" if at least one element is in its original position (i.e., it is not a derangement).
- Let's define a property to mean that element is in its original position. Let be the set of all permutations that have property .
The set of all "bad" permutations is the union . The number of derangements, , is the number of permutations with none of these properties. Therefore:
Now, our task is to calculate the size of that union using PIE.
Applying PIE to Count Derangements
The video below provides a clear, step-by-step walkthrough of how to use PIE to count derangements. It first demonstrates the method for and then generalizes the result.
[Discrete Mathematics] Derangements
This video from TrevTutor, titled "Derangements", gives a fantastic explanation of how to derive the derangement formula using PIE.
First, watch the calculation for n=4 from this section. The logic directly mirrors what we're about to discuss. Next, watch the derivation of the general formula from this segment. This is the core of today's lesson. Pay close attention to how the binomial coefficient terms from PIE simplify.
Let's formalize the steps shown in the video. The PIE formula for the size of the union is:
-
First term:
This represents the sum of the sizes of sets where at least one element is fixed.- How many permutations fix element ? If we fix element , we are free to permute the remaining elements in ways. So, .
- There are ways to choose which single element to fix.
- Thus, the first term is .
-
Second term:
This represents permutations where at least two elements are fixed.- How many permutations fix elements and ? We fix those two and permute the remaining elements in ways. So, .
- There are ways to choose which pair of elements to fix.
- The second term is .
-
General term for fixed elements:
The size of the intersection of such sets is . There are ways to choose the elements to fix. So, the -th term in the PIE sum is .
This logic is explained in the following resource, which presents the derivation from a textual perspective.
The Inclusion-Exclusion Principle
This document provides a concise mathematical derivation of the formula for D(n).
Read the section titled Now back to (3). It clearly lays out how to compute the size of the intersections and assembles the full PIE formula to find the number of non-derangements.
The General Formula and a Surprising Connection
As the resources show, when we assemble the PIE formula, a beautiful simplification occurs. The -th term of the sum for non-derangements is:
So, the number of non-derangements is:
The number of derangements is :
Factoring out gives us the standard formula for derangements:
This sum is the first terms of the Taylor series for evaluated at . Recall that:
For even moderately large , the sum in our formula is extremely close to . This gives us a fantastic approximation:
In fact, is the closest integer to for . This means the probability that a randomly chosen permutation is a derangement, , quickly converges to . It's quite remarkable that for any large group of people, the chance that no one gets their own hat back is about 36.8%!
Practice Problems
To master this concept, you must be able to apply it not just to find , but also to related problems, like counting permutations with a specific number of fixed points.
- The Hat-Check Problem: Five gentlemen attend a party and leave their hats at the door. At the end, they hastily grab hats on their way out. In how many ways could it happen so that none of the gentlemen leaves with his own hat? (This is Example 1.6.5 from the "Advanced Counting Using PIE" text).
- Partially Correct Permutations: How many permutations of the set fix exactly 2 elements?
- The Grinch's Mischief: The Grinch sneaks into a room with 7 presents for 7 different people and switches the name labels. (This is based on Exercise 10 in the same text).
a. In how many ways can he do this so that no present ends up with its original label?
b. In how many ways can he do this so that exactly 3 presents keep their original labels?
c. In how many ways can he do this so that exactly 6 presents keep their original labels? - A Sneaky Derangement: Let . How many injective functions have the property that for every , ?
Hint for Problem 2: This is a two-step problem. First, choose which elements will be fixed. Then, figure out what must happen to the remaining elements and count the ways.
Conclusion
Today, we've added a powerful and elegant tool to our combinatorial arsenal. By applying the Principle of Inclusion-Exclusion, we moved from a brute-force listing of permutations to a general and efficient formula for counting derangements.
Key Takeaways:
- A derangement is a permutation where no element is in its original position.
- We use PIE to count derangements by defining the "bad" properties as elements that are in their original positions (fixed points).
- The number of derangements of elements is given by the formula .
- For , is the closest integer to , meaning the probability of a random permutation being a derangement is approximately .
- Many problems can be solved by combining derangements with other counting principles, such as choosing a subset of elements to be fixed and deranging the rest.
In our next lesson, we will continue our exploration of PIE's applications. We will tackle another classic counting problem: finding the number of surjective (or "onto") functions between two finite sets. You will see that the underlying strategy remains the same: identify the total, define the "bad" properties, and use PIE to subtract what you don't want.