Contents / Discrete Mathematics / Combinatorics
Chapter 4
Combinatorics
The sum and product rules with counterexamples when their hypotheses fail; permutations and combinations derived by uniform overcounting; Pascal's identity proved combinatorially; the binomial theorem with Vandermonde and the hockey-stick identity; stars and bars as a bijection; the pigeonhole principle with its generalised form and classic applications; and inclusion-exclusion with derangements as a corollary.
Introduction
Counting sounds like the first thing a person learns and the last thing worth a chapter. But the sets a computer scientist wants to count are never small enough to count one at a time. How many passwords of length eight are there over a -character keyboard? How many distinct ways can a hash function scatter keys into buckets without a collision? How many subsets of a -flag configuration space must a test suite cover? Each of these is a number no one will ever enumerate, and each is computed in one line by the rules of this chapter.
The subject has a characteristic style of argument. To count a set , you do not inspect ; you build a bijection between and a set whose size you already know, or you decompose into pieces you can count separately, or you count something larger and divide by the exact factor by which you overcounted. Almost every result below is one of those three moves, and the value of learning them is that they compose: the count of poker hands, of binary strings with a fixed number of ones, and of the ways to distribute identical tasks over distinct servers are all the same calculation wearing different clothes.
We begin with the two rules everything rests on — the sum rule and the product rule — stated carefully enough that their hypotheses are visible, since almost every wrong count in practice is one of those two rules applied where its hypothesis fails. From there we get functions, injections and permutations, then combinations and the binomial coefficients, whose identities are proved twice over: once by algebra and once by counting a set in two ways, which is the more instructive proof and often the shorter one. The pigeonhole principle then shows that counting proves existence, not just size. We finish with inclusion–exclusion, the systematic way to count a union whose pieces overlap.
Throughout, all sets are finite, denotes the number of elements of , and . The deeper theory — generating functions, the transfer-matrix method, enumerative and extremal combinatorics — belongs to the separate subject Combinatorics; here we build the vocabulary it presumes.
4.1Basic Counting Principles
Two rules generate the entire chapter. They correspond to the two ways a decision can be structured: a choice between alternatives, and a sequence of choices made one after another. Getting a count wrong almost always means one of these was applied when its hypothesis did not hold, so we state the hypotheses explicitly and then break each rule on purpose to see what the hypothesis was doing.
Definition 4.1 (Disjoint sets and a partition). Two sets and are disjoint if . A family is pairwise disjoint if whenever , and it is a partition of if in addition .
Theorem 4.2 (Sum rule). Let be pairwise disjoint finite sets. Then
Proof. Induct on . For there is nothing to prove.
For , suppose and , witnessed by bijections and . Define by for and for . This is well defined precisely because : no receives two values. It is injective, since and are injective and their images and are disjoint, and it is surjective because every element of lies in one of those two images. So .
For the induction step, write . The set is disjoint from each with , hence from their union, so the case applies and the inductive hypothesis finishes the sum.∎
Pitfall. Drop disjointness and the rule fails immediately. Among the integers in , let be the even ones and the multiples of . Then and , but , not — the element was counted twice. Repairing that double count in general is exactly what the section Inclusion-Exclusion is for.
Theorem 4.3 (Product rule). Suppose an object is built by a sequence of choices, where the first choice has options and, for each , the number of options at step is no matter how steps through turned out. Suppose also that distinct sequences of choices produce distinct objects. Then the number of objects built is
In particular, for finite sets , .
Proof. Induct on . For the claim is the definition of .
Suppose it holds for and consider a process of steps. Partition the objects by the outcome of the first step: for each of the options , let be the set of objects whose first choice was . These sets are pairwise disjoint, since distinct choice sequences give distinct objects and an object records its first choice. Within the remaining steps have options, independent of prior outcomes, so the inductive hypothesis gives . The theorem Sum rule now gives
as claimed. The product form for Cartesian products is the special case where step chooses a coordinate from .∎
The hypothesis that the number of options at step is constant is weaker than it first looks, and weaker than it needs to be in practice. The options themselves may change wildly; only the count must not. Choosing an ordered pair of distinct letters from has options at step one and at step two — a different three letters each time, but always three — so the product rule applies and gives .
Pitfall. When the count at step two genuinely depends on step one, the product rule is simply false. Count the strings of length two over in which the second character differs from the first and, if the first character is , the second must also avoid . Step one has options; step two has options after or after , but only after (namely ). The true count is , whereas . The repair is always the same: split into cases with the theorem Sum rule first, then apply the product rule inside each case, where the counts are constant.
Theorem 4.4 (Counting functions). Let and . The number of functions is .
Proof. List the elements of as . A function is determined by, and determines, the sequence of values , so the set of functions is in bijection with . Each coordinate is chosen from freely and independently of the others, so the theorem Product rule applies with , giving .∎
Corollary 4.5 (Strings and subsets). Over an alphabet of symbols there are strings of length . A set with elements has exactly subsets.
Proof. A string of length is a function from the positions to the alphabet, so the theorem Counting functions gives .
For subsets, fix and map each subset to its indicator string , whose -th bit is exactly when . Distinct subsets differ at some and so have different strings, making injective; and any bit string is the image of , making surjective. So subsets are in bijection with binary strings of length , of which there are .∎
Intuition. The indicator string is the reason a set of feature flags has configurations, and the reason a subset is stored as a bitmask. "Choose a subset" and "answer independent yes/no questions" are the same act.
Example 4.6 (Counting passwords). A system requires passwords of length exactly over the alphanumeric characters. How many are there, and how many contain at least one digit?
Solution.
- Each of the positions is filled independently from symbols, so by the corollary Strings and subsets there are passwords.
- Counting those with at least one digit directly would require splitting on how many digits appear. Count the complement instead: a password with no digit uses only the letters, and there are of those.
- The digit-free passwords and the rest partition the whole set, so by the theorem Sum rule the answer is .
Remark. Step 2 is the complement trick, and it is worth naming because it recurs: "at least one" is almost always easier to count as "total minus none". The alternative — summing over exactly one digit, exactly two, and so on — is the same number obtained with seven times the work.
Theorem 4.7 (Counting injections). Let and . The number of injective functions is
which is when .
Proof. Order as and build by choosing in turn. Injectivity says exactly that each value must avoid the values already used. When is chosen, values have been used and they are distinct, so exactly elements of remain available — a count that does not depend on which values were used, only how many. The theorem Product rule therefore applies and yields . Distinct choice sequences give distinct functions, since the sequence is the list of values. If the factor with is zero, matching the fact that no injection exists.∎
Example 4.8 (Hash collisions). A hash function maps distinct keys into a table of slots, each key landing in a slot independently and uniformly. How many of the possible outcomes place every key in a different slot, and what fraction of all outcomes is that?
Solution.
- An outcome is a function from the keys to the slots, so by the theorem Counting functions there are outcomes.
- A collision-free outcome is an injection, so by the theorem Counting injections there are of them.
- The fraction is . So even with only keys and slots, a collision occurs about of the time — this is the birthday problem, and it is why hash tables are designed to handle collisions rather than to avoid them.
Example 4.9 (Splitting before multiplying). How many strings of length over begin with a vowel or end with the letter ?
Solution.
- Let be the set of such strings beginning with or , and those ending with . The two sets are not disjoint, so the theorem Sum rule does not apply directly to .
- Partition instead by the first letter. If the first letter is or ( ways), the remaining three positions are free: strings.
- If the first letter is one of the three consonants, the string must end in : strings.
- These two cases are disjoint and exhaust , so the count is .
4.2Permutations
An arrangement is an injection whose target is the same set as its source, or a list of distinct items drawn from a pool. The counting was already done in the theorem Counting injections; this section names the result, packages it in factorials, and then handles the case that the items are not all distinguishable.
Definition 4.10 (Factorial). For an integer define
The convention is forced, not arbitrary: there is exactly one way to arrange nothing, namely the empty arrangement, and every formula below would need a special case without it.
Definition 4.11 (Permutation and -permutation). A permutation of a finite set is a linear arrangement of all its elements, equivalently a bijection . A -permutation of is an ordered list of distinct elements of . The number of -permutations of an -set is written .
Theorem 4.12 (The -permutation count). For ,
Proof. A -permutation of is exactly an injection , read as the list , so the theorem Counting injections gives .
For the factorial form, multiply and divide by the missing tail:
Corollary 4.13 (Permutations of an -set). A set with elements has exactly permutations.
Proof. A permutation uses all elements, so it is a -permutation with . The theorem The -permutation count gives .∎
Intuition. The factorial grows faster than any exponential you are likely to meet: , but and exceeds the number of atoms in the observable universe. This is precisely why brute-force search over all orderings — the naive travelling-salesman algorithm — is hopeless past about twelve cities, and why Combinatorics devotes a whole subject to smarter enumeration.
Example 4.14 (Ordered and unordered officers). A club of members elects a president, a secretary and a treasurer, no member holding two posts. In how many ways? And how many ways to choose three members to form an unranked steering group?
Solution.
- The three posts are distinguishable, so an outcome is an ordered list of three distinct members: .
- For the unranked group, each set of three members was counted once for each of its orderings in step 1, so the count is . That division is the argument formalised in the theorem The combination formula.
Theorem 4.15 (Permutations with repeated objects). Suppose a multiset consists of objects of types, with copies of type , …, copies of type , where and copies of the same type are indistinguishable. The number of distinguishable arrangements in a row is
Proof. Temporarily label the copies of each type to make all objects distinct — write the three copies of a type as . The corollary Permutations of an -set says the labelled objects have arrangements.
Now group the labelled arrangements by the unlabelled arrangement they produce when the superscripts are erased. Fix one unlabelled arrangement . Which labelled arrangements erase to ? Precisely those obtained from one of them by permuting the superscripts within each type independently, and by the theorem Product rule there are of these, a count that does not depend on .
So the labelled arrangements fall into groups of equal size , one group per unlabelled arrangement, and the theorem Sum rule gives . Divide.∎
Example 4.16 (Arrangements of a word). How many distinguishable strings can be made from all the letters of ? How many of those begin with ?
Solution.
- The word has letters: one , four , four , two . By the theorem Permutations with repeated objects the count is
- Fixing in position one leaves the multiset of letters to arrange, giving .
- As a check, is equally likely to occupy any of the positions by symmetry, and indeed .
Example 4.17 (Lattice paths). A robot on a grid moves only right or up, from to , one unit per step. How many routes are there?
Solution.
- Every route consists of exactly right-steps and up-steps in some order, and the route is determined by that order — so routes correspond bijectively to strings of seven s and four s.
- By the theorem Permutations with repeated objects, the number of such strings is .
Remark. Circular arrangements need one adjustment: seating people around a round table, where only the cyclic order matters, gives arrangements, because each cyclic arrangement corresponds to linear ones — one per choice of who is listed first. The same divide-by-the-overcount reasoning as in the theorem Permutations with repeated objects.
4.3Combinations
A permutation records which items were taken and in what order. Discard the order and you have a combination — a subset. Because the discarded information is the same amount for every selection, the count follows by dividing, and that single division is the most used formula in the subject.
Definition 4.18 (Binomial coefficient). For integers , the binomial coefficient , read " choose ", is the number of -element subsets of an -element set. For or we set , since no such subset exists.
Theorem 4.19 (The combination formula). For ,
Proof. Count the -permutations of an -set in two ways.
Directly, the theorem The -permutation count gives .
Alternatively, build a -permutation in two steps: first choose the set of elements that will appear, then choose the order in which to list them. There are ways to do the first step by definition, and whichever set is chosen, the corollary Permutations of an -set gives exactly orders — a number independent of the set chosen, which is what the theorem Product rule requires. Distinct pairs (set, order) give distinct lists, and every list arises this way, so the count is .
Equating the two counts, , and dividing by gives .∎
Pitfall. The formula divides by because every -subset is overcounted by exactly the same factor . Division is legitimate only when the overcounting is uniform. If you try the same move on arrangements of by writing , you are lucky; on a problem where different objects are overcounted different numbers of times, dividing gives a non-integer and a wrong answer. When in doubt, partition into classes of equal size and divide inside each class, as the theorem Permutations with repeated objects does.
Proposition 4.20 (Symmetry). For , .
Proof. Let be an -set and let be its family of -subsets, its family of -subsets. Define by complementation, . This lands in because .
It is a bijection because it is its own inverse: for every . A map with a two-sided inverse is a bijection, so .∎
Intuition. Choosing which of features to enable is the same act as choosing which to disable. Symmetry also says where the work is: to compute , compute instead — three multiplications rather than ninety-seven.
Example 4.21 (Committees with constraints). From developers and designers, how many teams of contain at least one designer?
Solution.
- All teams of from the people: .
- Teams with no designer are teams of chosen from the developers: .
- By the complement trick, teams with at least one designer number .
- The direct route confirms it: .
Example 4.22 (Bit strings with a fixed weight). How many bit strings of length contain exactly ones? How many contain at most ones?
Solution.
- A string is determined by the set of positions carrying a one, so strings of weight correspond bijectively to -subsets of : .
- "At most two" splits into three disjoint cases by the theorem Sum rule: .
Sometimes the items being selected may be taken more than once — three coffees of four available kinds, or ten identical background jobs assigned to four servers. Order still does not matter; only how many of each kind were taken. The count is again a binomial coefficient, and the reason is a bijection worth seeing in full.
Theorem 4.23 (Combinations with repetition (stars and bars)). The number of ways to choose items from types, with repetition allowed and order irrelevant, is
Equivalently, this is the number of solutions in non-negative integers of .
Proof. A selection is recorded by the multiplicities , where counts the copies taken of type ; two selections are the same exactly when their multiplicity vectors agree, and the total is . So the two statements count the same set of solutions.
Let be the set of strings of length over containing exactly stars and bars. Define by sending to
that is, stars, a bar, stars, a bar, and so on, ending with stars. The string has stars and exactly bars, so .
is injective: from the string one recovers as the number of stars strictly between bar and bar (with the ends of the string playing the role of bar and bar ). Two distinct vectors differ in some , hence the strings differ in that block.
is surjective: given any , the bars cut into blocks of stars; let be the length of block . Each , the total is the number of stars, which is , so , and applying to it rebuilds block by block.
Hence . Finally, a string in is determined by which of its positions hold stars, so by the definition Binomial coefficient.∎
Example 4.25 (Distributing identical jobs). Ten identical background jobs are assigned to four distinct servers. How many assignments are there? How many give every server at least one job?
Solution.
- An assignment is a solution of in non-negative integers, so the theorem Combinations with repetition (stars and bars) gives .
- For "at least one each", set . Then , and is a bijection between the two solution sets. So the count is .
Pitfall. Identical objects into distinct boxes is stars and bars; distinct objects into distinct boxes is the theorem Counting functions, giving instead of . Ask first whether swapping two of the objects changes the outcome. Identical objects into identical boxes is a harder problem — the partition function — and belongs to the subject Combinatorics.
4.4Binomial Coefficients and the Binomial Theorem
The numbers satisfy a dense web of identities. Each can be proved by manipulating factorials, and each can also be proved by counting one set in two different ways — a double counting or combinatorial proof. The second kind is preferred here, and not only for elegance: an algebraic proof verifies that two expressions agree, while a combinatorial proof explains why they agree, and the explanation generalises where the algebra does not.
Theorem 4.26 (Pascal's identity). For ,
Proof. Let be a set of elements and distinguish one of them, say ; write , so . The left side counts the -subsets of .
Partition those -subsets by whether they contain .
Subsets containing . Such a subset is where is a -subset of , and distinct give distinct subsets, while every subset containing arises from exactly one , namely . So there are of them.
Subsets not containing . Such a subset is exactly a -subset of , so there are of them.
The two classes are disjoint and together are all -subsets of , so the theorem Sum rule gives the identity.∎
Remark. The algebraic proof is available too, and is worth doing once to see how much less it tells you. With ,
using and to put the two fractions over a common denominator. Correct, but it does not mention the distinguished element , and it is the distinguished element that makes the identity memorable and transplantable.
Pascal's identity is a recurrence, so the whole table of binomial coefficients can be built from the boundary values by additions alone — no factorials, no division, no overflow from intermediate values that dwarf the answer. That table is Pascal's triangle.
Example 4.28 (Building a row by addition). Use Pascal's identity to extend the triangle to the row , and read off .
Solution.
- Row is .
- Each interior entry of row is the sum of its two neighbours above: , giving .
- So , which matches .
Theorem 4.29 (Binomial theorem). For any numbers and any integer ,
Proof. Write the power as a product of identical factors:
Expanding by distributivity without collecting terms means: from each factor choose either the or the , multiply the chosen symbols, and sum over all choices. By the theorem Counting functions there are such choices, and each contributes one term.
A choice is determined by the set of factors from which was taken. If that set has size , the term is , since the other factors contributed a . So the terms equal to are in bijection with the -subsets of the factors, of which there are by the definition Binomial coefficient.
Grouping the terms by the value of — a partition, so the theorem Sum rule applies — the coefficient of is , and ranges over .∎
Example 4.30 (Extracting one coefficient). Find the coefficient of in .
Solution.
- Apply the theorem Binomial theorem with the two summands and : .
- The term with is : .
- Compute: , , , so the coefficient is .
Corollary 4.31 (Row sums). For every ,
Proof. First identity, algebraically. Put in the theorem Binomial theorem: the left side is and the right side is .
First identity, combinatorially. Count the subsets of an -set in two ways. The corollary Strings and subsets gives . Alternatively, partition the subsets by their size ; the class of size has members by the definition Binomial coefficient, and ranges over to , so the theorem Sum rule gives . The two counts must agree.
Second identity. Put , in the theorem Binomial theorem: the left side is for , and the right side is .∎
Intuition. The second identity says an -set has as many even-sized subsets as odd-sized ones, for . There is a bijection that proves it directly: fix an element and toggle it — add if absent, remove it if present. This changes the size by one, hence changes its parity, and applying it twice returns the original set, so it is a bijection between the even and odd families.
Theorem 4.32 (Vandermonde's identity). For non-negative integers ,
Proof. Let be a set of elements and a disjoint set of elements, so by the theorem Sum rule. Count the -subsets of in two ways.
Directly there are of them.
Alternatively, partition them by , the number of chosen elements coming from ; then . For a fixed , such a is determined by a -subset of together with a -subset of , freely and independently chosen, so the theorem Product rule gives such subsets. Since can be anything from to (terms where or contribute by the convention in the definition Binomial coefficient), the theorem Sum rule gives the sum.∎
Corollary 4.33 (Sum of squares of a row). For every , .
Proof. Take and in the theorem Vandermonde's identity: . By the proposition Symmetry, , so each summand is .∎
Theorem 4.34 (Hockey-stick identity). For integers ,
Proof. Count the -subsets of by conditioning on their largest element.
If the largest element is , the remaining elements form an -subset of , and conversely any such -subset together with gives an -subset with largest element . So there are subsets with largest element .
The largest element is at least (there must be smaller elements available) and at most . These classes are disjoint and exhaust the -subsets, so the theorem Sum rule gives
after substituting .∎
Remark. The name comes from the picture: the terms lie along a diagonal of Pascal's triangle and the answer sits just off the end of it, so the shaded cells look like a hockey stick. With it reads , the schoolbook sum.
Example 4.35 (Summing a diagonal). Evaluate .
Solution.
- This is the theorem Hockey-stick identity with and , so the sum is .
- .
- Check term by term: .
Example 4.36 (Counting comparisons in a double loop). A program runs for i in 1..n: for j in i+1..n: and does one comparison in the body. How many comparisons are made?
Solution.
- The body runs once for each pair with — that is, once for each -element subset of , since a set determines the ordered pair with uniquely.
- So the count is , the familiar quadratic cost of a naive all-pairs scan.
4.5The Pigeonhole Principle
So far counting has answered "how many?". The pigeonhole principle turns counting into a tool for proving that something exists, without producing it. It is almost insultingly obvious in statement, and the skill it demands lies entirely in choosing what the pigeons are and what the holes are.
Theorem 4.37 (Pigeonhole principle). If or more objects are placed into boxes, then some box contains at least two objects. Equivalently: if then no function is injective.
Proof. Suppose, for contradiction, that every box contains at most one object. Let be the sets of objects in each box. These are pairwise disjoint and their union is the whole collection, so the theorem Sum rule gives
That contradicts there being at least objects. Hence some box holds two or more.
For the restatement, take the boxes to be the elements of and place each into box . A box with two objects is a pair with , which is a failure of injectivity.∎
Theorem 4.38 (Generalised pigeonhole principle). If objects are placed into boxes, then some box contains at least objects.
Proof. Suppose every box held at most objects. Summing over the boxes as in the theorem Pigeonhole principle,
using the bound . This gives , a contradiction.∎
Example 4.39 (Shared birth months). A lecture hall holds students. What is the largest for which you can guarantee that of them share a birth month?
Solution.
- Take the students as objects and the months as boxes.
- The theorem Generalised pigeonhole principle gives a month shared by at least students.
- No better guarantee is possible: four months with students and eight with totals with no month exceeding . So .
Pitfall. The principle guarantees existence, never location. It says some month is shared by nine students; it does not say which, and it gives no way to find it short of looking. This is the price of an argument that never examines the objects.
The next two results are the reason the principle is worth a name. In each, the difficulty is entirely in inventing the boxes.
Proposition 4.40 (Two people with the same number of friends). At any gathering of people, where friendship is mutual and nobody is their own friend, two people have exactly the same number of friends present.
Proof. Assign to each person the number of friends they have at the gathering. Each such number lies in , which is possible values for people — not yet a pigeonhole.
But the values and cannot both occur. A person with friends is a friend of everybody else present, so nobody present has friends; conversely a person with friends is nobody's friend, so nobody has . Hence the friend counts actually lie in one of the two sets or , each of size .
Now people are assigned values from a set of values, so the theorem Pigeonhole principle gives two people with the same count.∎
Proposition 4.41 (Coprime pairs among n+1 integers). Let and choose any distinct integers from . Then two of the chosen integers are coprime.
Proof. Partition into the consecutive pairs
and take these as the boxes, placing each chosen integer into the box containing it. With chosen integers and boxes, the theorem Pigeonhole principle puts two chosen integers in the same box.
Two integers in the same box are consecutive, say and . Any common divisor of and divides their difference , so and the two are coprime.∎
Remark. The same set with choices yields a second gem by a different choice of box: write each chosen integer as with odd. There are only odd numbers available in , so two chosen integers share the same odd part , say and with — and then the first divides the second. Both statements are sharp: the set of size contains no coprime pair, so choices really are needed.
Example 4.42 (Sock drawer and a stronger guarantee). A drawer holds socks of colours, mixed at random in the dark. How many socks must be drawn to guarantee a matching pair? To guarantee three of one colour?
Solution.
- Four socks might be four different colours, so four is not enough; with socks and colours the theorem Pigeonhole principle forces a repeated colour. The answer is .
- For three of one colour, socks might be two of each colour. With the theorem Generalised pigeonhole principle gives a colour used at least times. The answer is .
- The pattern: to force of one of types, draw .
4.6Inclusion-Exclusion Principle
The theorem Sum rule needs disjointness. When the sets overlap, adding their sizes counts each overlap more than once, and inclusion–exclusion is the exact correction: add the sets, subtract the pairwise intersections, add back the triples, and so on in alternating signs.
Theorem 4.43 (Inclusion-exclusion for two sets). For finite sets and ,
Proof. Split into three pairwise disjoint pieces: , , and . Every element of the union lies in exactly one of them, according to whether it belongs to only, to only, or to both, so the theorem Sum rule gives
Also is the disjoint union of and , so , and likewise . Substituting,
Example 4.45 (Divisible by 3 or by 5). How many integers in are divisible by or by ?
Solution.
- Let be the multiples of and the multiples of . Then and .
- An integer lies in exactly when it is divisible by , so .
- By the theorem Inclusion-exclusion for two sets, .
Theorem 4.46 (Inclusion-exclusion for three sets). For finite sets , , ,
Proof. Apply the theorem Inclusion-exclusion for two sets to the two sets and :
Expand the first term by the same theorem: .
For the last term, distributivity gives , and applying the two-set theorem once more,
Substituting both expansions and collecting signs gives the stated formula.∎
Example 4.47 (Three-language survey). Of students, know Python, know Java, know C; know Python and Java, Python and C, Java and C, and know all three. How many know none of the three?
Solution.
- By the theorem Inclusion-exclusion for three sets, the number knowing at least one is
- The remaining students know none, so the answer is .
- Sanity check on the innermost regions: all three, ; exactly Python and Java, ; exactly Python and C, ; exactly Java and C, ; Python only, ; Java only, ; C only, . These seven disjoint regions total .
Theorem 4.48 (Inclusion-exclusion, general form). For finite sets ,
Proof. Both sides count elements of the union, so it suffices to show that each element of the union contributes the same amount to each side.
Fix and let be the number of indices with ; write for that set of indices, so . On the left, contributes exactly .
On the right, is counted by the term for precisely when , which happens exactly when . The non-empty subsets of size number by the definition Binomial coefficient, and each contributes . So the total contribution of to the right side is
where the alternating sum vanishes by the corollary Row sums, valid since .
Every element of the union therefore contributes to each side, and elements outside the union contribute to both, so the two sides are equal.∎
Corollary 4.49 (Counting derangements). Let be the number of permutations of with no fixed point — no element mapped to itself. Then
Proof. Let be the set of permutations fixing . A permutation has some fixed point exactly when it lies in , so .
For with , a permutation in fixes every element of and permutes the remaining elements arbitrarily, so by the corollary Permutations of an -set. There are such , so the theorem Inclusion-exclusion, general form gives
Therefore
the last step absorbing into the sum.∎
Intuition. The sum is the truncated series for , so very fast — already correct to four decimals at . Shuffle a deck of any size and hand the cards back to their owners at random: the chance that nobody gets their own card is essentially , independent of the number of people.
Example 4.50 (A derangement count). Five students hand in five assignments, which are then returned at random, one to each student. In how many ways does nobody get their own back?
Solution.
- This is . By the corollary Counting derangements, .
- The bracket is , so .
- So of the possible returns are deranged, a fraction , already close to .
Example 4.51 (Counting surjections). How many functions from a -element set onto a -element set are surjective?
Solution.
- Let be the functions missing the -th target value. A function fails to be surjective exactly when it lies in .
- A function avoiding a fixed set of targets maps into the remaining values, so by the theorem Counting functions there are of them, and there are such index sets.
- The theorem Inclusion-exclusion, general form gives .
- Total functions: . Surjections: .
Remark. The same computation in general gives the number of surjections from an -set onto a -set as , and dividing by gives the Stirling number of the second kind — the count of ways to partition an -set into unlabelled non-empty blocks. Those numbers, and the generating functions that tame them, belong to the subject Combinatorics.
- Applying the sum rule to overlapping cases. "Divisible by or by " is not . If the cases can both happen, either make them disjoint by conditioning, or use the theorem Inclusion-exclusion for two sets.
- Applying the product rule when the option count varies. If the number of choices at step two depends on what happened at step one, split into cases first and multiply inside each case. A product of averages is not a count.
- Confusing ordered with unordered. "President, secretary, treasurer" is ; "three-member committee" is . The tell is whether swapping two chosen items gives a different outcome.
- Dividing away a non-uniform overcount. Dividing by is legitimate only when every outcome was produced exactly times. When different outcomes are overcounted different numbers of times, the division is meaningless — and a non-integer answer is the symptom.
- Confusing with . Distinct objects into distinct boxes is the theorem Counting functions; identical objects into distinct boxes is the theorem Combinations with repetition (stars and bars). Ask whether swapping two objects changes anything.
- Forgetting the complement trick. "At least one" almost always means total minus none. Summing over exactly one, exactly two, … is correct but invites arithmetic slips.
- Double-counting in stars and bars. The bars are indistinguishable from one another and so are the stars; what is distinguishable is the positions. If you find yourself multiplying by to order the bars, you have left the bijection.
- Reading the pigeonhole principle as a construction. It proves a box with two objects exists; it never says which box, and no amount of restating it will produce one.
- Off-by-one in pigeonhole thresholds. To force objects in one of boxes you need objects, not . Always check the extremal arrangement that just fails.
- Misusing . It is not a special case to be worked around; formulas like and depend on it. Writing makes undefined.