Discrete MathematicsPigeonhole, Inclusion-Exclusion and Generating Functions3 of 4
Pigeonhole, Inclusion-Exclusion and Generating Functions · 3
General Inclusion-Exclusion and Derangements
We already know how to count around two or three overlapping conditions, and now we want to count arrangements that avoid any number of forbidden rules at once.
1
Why Avoiding Rules Is Hard
Suppose four friends (A, B, C, and D) write their names on four slips of paper and draw from a hat for Secret Santa. We want to find how many ways they can draw names so that nobody pulls their own name.
Secret Santa and Why Avoiding Rules Is Hard
2
General Inclusion-Exclusion
In the four-person Secret Santa puzzle, there are four overlapping bad sets (one for each person who draws their own name), and a problem with ten people has ten bad sets. When we have n bad sets A1,A2,…,An inside a universe of N total outcomes, we can no longer draw a Venn diagram to check every region by eye. We need the general alternating pattern for n sets, and a way to be sure that any bad outcome cancels out to zero no matter how many rules it breaks.
To see why the alternating counts cancel for an outcome that breaks m rules at once, we will use the Binomial Theorem, which expands (1+x)m by choosing x from k of the m factors at a time:
(1+x)m=(0m)+(1m)x+(2m)x2+(3m)x3+⋯+(mm)xm
General Inclusion-Exclusion and Why It Works
At each step k, we compute the overlap sum Sk by taking every group of k bad sets, counting how many outcomes sit in all k sets at the same time, and adding those counts together:
Note that Sk is a sum of overlap sizes, not the number of distinct outcomes that break k rules: an outcome that breaks m rules gets counted (km) separate times inside Sk, once for every group of k sets chosen from the m sets it belongs to.
Subtracting the odd steps and adding back the even steps leaves only the clean outcomes outside all n bad sets:
Clean items=totalN−singlesS1+pairsS2−triplesS3+…+(−1)nall n setsSn
When a problem instead asks for the union (the outcomes that break at least one rule), we drop N and flip the signs:
∣A1∪A2∪⋯∪An∣=S1−S2+S3−⋯+(−1)n−1Sn
For an item that belongs to m≥1 of the n bad sets, its count across the formula is (km) at step k and 0 past step m, so its alternating counts always cancel to (1−1)m=0:
An item in one or more bad sets has its counts across the total, the singles, pairs, triples, quads, and the five-set overlap cancel to 0, while a clean item is counted once.
3
Counting Derangements
Now we can finish the four-person Secret Santa problem and find a single formula for the number of derangementsDn, which are rearrangements of n items where no item ends up in its original spot.
When we simplify the overlap sum Sk for n people, we will rewrite the combination (kn) in terms of factorials so it cancels with the ways to shuffle the remaining people:
(kn)=k!(n−k)!n!
Counting Derangements
Text solution
Count all shuffles and pin k people to their own namesBecause all four people are symmetric, each Sk chooses k of the 4 people to get their own names in (k4) ways and shuffles the remaining 4−k names in (4−k)! ways.
Take the alternating sumSubtract the singles, add the pairs, subtract the triples, and add the four-way overlap.
D4=N−S1+S2−S3+S4=24−24+12−4+1=9
So out of 24 possible drawings, 9 are derangements where nobody pulls their own name, and the remaining 24−9=15 drawings have at least one person pull their own name.
For any number of items n, pinning k chosen items to their original spots and shuffling the other n−k items cancels (n−k)! at every step:
Sk=(kn)(n−k)!=k!(n−k)!n!(n−k)!=k!n!
Factoring n! out of the alternating sum gives the general derangement formula:
Because the alternating sum inside the parentheses gets close to e1≈0.368 very quickly (it is already 0.375 at n=4 and about 0.367 at n=5), about 36.8% of all shuffles are derangements once n is 5 or more. In fact Dn is always the nearest whole number to en!, but in practice you should write out the alternating terms.
4
Asymmetric Rules and Forbidden Blocks
In Secret Santa, every person was symmetric, so all (kn) overlaps at step k had the exact same size. Now suppose we arrange the five letters A, B, C, D, E in a row and want to avoid four different rules:
A1: A is in the first spot.
A2: B and C appear right next to each other in the order BC.
A3: C and D appear right next to each other in the order CD.
A4: D, E, and A appear glued together as the block DEA.
Here the bad sets have different sizes, and combining rules can leave blocks separate, fuse them into a longer chain, or contradict each other completely.
Asymmetric Rules and Forbidden Blocks
Text solution
Pin each single rule and count the free objectsFix A in spot 1 or glue each forbidden block into a single tile before shuffling the remaining objects.
NS1=5!=120=A1: A first4!+A2: [BC], A, D, E4!+A3: [CD], A, B, E4!+A4: [DEA], B, C3!=24+24+24+6=78
Inspect each pair of rules one by oneCheck all (24)=6 pairs to see whether the two rules stay separate, fuse at a shared letter, or contradict each other.
S2=A1∩A2: A first, [BC], D, E3!+A1∩A3: A first, [CD], B, E3!+A1∩A4: impossible0+A2∩A3: [BCD], A, E3!+A2∩A4: [BC], [DEA]2!+A3∩A4: [CDEA], B2!=6+6+0+6+2+2=22
Inspect the triples and the four-way overlapAny combination containing both A1 and A4 has 0 ways, while compatible blocks fuse further.
S3S4=A1∩A2∩A3: A first, [BCD], E2!+A1∩A2∩A40+A1∩A3∩A40+A2∩A3∩A4: [BCDEA]1!=2+0+0+1=3=A1∩A2∩A3∩A40
Take the alternating sumCombine the overlap sums to count the arrangements that break none of the four rules.
N−S1+S2−S3+S4=120−78+22−3+0=61
So 61 of the 120 arrangements of A, B, C, D, E avoid all four rules.
See how each pair and triple of rules interacts:
Combining different rules can leave blocks separate, join shared letters into a single chain, or make an arrangement impossible.
5
Onto Functions and Non-Empty Boxes
Our last problem looks different on the surface, but brings back the clean symmetry of (kn). We want to find how many ways to assign 6 distinct tasks to 4 coworkers so that every coworker receives at least one task. Assigning m distinct objects to n distinct boxes with no box left empty is the exact same problem as counting onto functions from a domain of m inputs to a codomain of n outputs.
Onto Functions and Non-Empty Boxes
Text solution
Define a bad set for each empty box and seal k boxes shutWith 4 coworkers as boxes and 6 tasks as objects, sealing k chosen boxes leaves 4−k open boxes for each of the 6 tasks.
So out of 4096 total functions, 1560 are onto functions where every coworker gets at least one task.
In general, when distributing m distinct inputs (objects) into n distinct outputs (boxes) with m≥n, sealing k of the n boxes leaves (n−k)m ways to place the objects:
Onto functions=k=0∑n(−1)k(kn)(n−k)m=all n opennm−1 box sealed(1n)(n−1)m+2 boxes sealed(2n)(n−2)m−…+(−1)n−1n−1 boxes sealed(n−1n)1m
Key concepts
Use General Inclusion-Exclusion whenever a problem asks you to avoid multiple rules at once, such as "nobody gets their own name," "none of these blocks appear," or "no box is left empty." If a rule is required rather than avoided (for instance, "A must be in the first spot" or "Task 1 must go to coworker A"), do not create a bad set for it; just lock that requirement in place from the very beginning when you count N and every Sk.
Once you flip the forbidden conditions into n bad sets A1,…,An, the whole problem comes down to finding the overlap sums Sk by pinning k rules in place and counting the remaining choices:
Symmetric fixed points (Derangements)
When n items cannot land in their own spots, pinning k items leaves (n−k)! shuffles for the rest, so Sk=(kn)(n−k)!=k!n! and
Dn=n!(1−1!1+2!1−3!1+⋯+(−1)nn!1)
For 4 people, D4=24(1−1+21−61+241)=9. If only r of the n people are forbidden from drawing their own names, there are only r bad sets, so you choose the pinned people from those r and still shuffle all n−k remaining people: Sk=(kr)(n−k)!.
Symmetric empty boxes (Onto functions)
When m distinct objects go into n distinct boxes (m≥n) with none empty, sealing k boxes leaves n−k choices for each of the m objects, so Sk=(kn)(n−k)m and
Onto functions=k=0∑n(−1)k(kn)(n−k)m
For 6 tasks and 4 coworkers, 46−(14)36+(24)26−(34)16=1560.
Asymmetric rules (Forbidden blocks and positions)
When the bad sets have different sizes or interact differently, do not multiply by (kn). List each pair and triple, fuse blocks that share letters (like [BC] and [CD] becoming [BCD]), and write 0 whenever two rules contradict each other (like A being first while also inside [DEA]).
In every case, finish with N−S1+S2−S3+⋯+(−1)nSn to count the clean outcomes, or S1−S2+S3−⋯+(−1)n−1Sn (which is N−Clean items) to count the outcomes that break at least one rule.
6
Practice
1
Five letters are placed at random into five addressed envelopes, one per envelope. In how many ways does no letter go into its own envelope, and in how many ways does at least one letter go into its own envelope?
Watch out: don't stop at the 4! term from the four-person example. Forgetting the final −5!5!=−1 term gives 45.
No letter in its own envelope is a derangement of n=5 items; once you have D5, how do the remaining shuffles out of 5! relate to the second question?
For no letter in its own envelope, use the derangement formula with n=5:
(Watch out: don't stop at the 4! term from the four-person example. Forgetting the final −5!5!=−1 term gives 45 instead of 44.)
Subtracting the 44 derangements from the 5!=120 total shuffles gives the ways where at least one letter goes into its own envelope:
120−44=76
44 ways where no letter goes into its own envelope, and 76 ways where at least one letter goes into its own envelope.
2
How many onto functions are there from {1,2,3,4,5} to {a,b,c,d}?
Watch out: each of the 5 inputs chooses among the open boxes, so the powers are (4−k)5, not (5−k)4; also remember to multiply each power by (k4) for the ways to choose which k boxes are empty. Omitting (k4) gives 1024−243+32−1=812.
Treat the m=5 inputs as distinct tasks and the n=4 outputs as distinct boxes: if you seal k boxes shut, how many open boxes can each of the 5 inputs choose from?
With m=5 inputs and n=4 outputs, define 4 bad sets for the 4 outputs that could be missed, and seal k outputs at step k:
(Watch out: each of the 5 inputs chooses among the open boxes, so the powers are (4−k)5, not (5−k)4; also remember to multiply each power by (k4) for the ways to choose which k boxes are empty. Omitting (k4) gives 1024−243+32−1=812.)
240
3
Six coworkers (four designers and two interns) put all six of their names into a hat and each draw one name at random. In how many ways can they draw names so that none of the four designers draws their own name (whether or not the interns draw their own names)?
Watch out: don't use D6=265, which also forbids the two interns from drawing their own names, and don't shuffle only the 4−k remaining designers with (4−k)!, which forgets the two interns and gives 9.Watch out: don't use D6=265, which also forbids the two interns from drawing their own names, and don't shuffle only the 4−k remaining designers with (4−k)!, which forgets the two interns and gives 9.
Only the four designers have a forbidden outcome, so how many bad sets Ai are there, and when k designers are pinned to their own names, how many remaining people shuffle freely?
Define 4 bad sets, one for each of the 4 designers drawing their own name. When k chosen designers are pinned to their own names in (k4) ways, all 6−k remaining coworkers (including both interns) shuffle the remaining names in (6−k)! ways:
(Watch out: don't use D6=265, which also forbids the two interns from drawing their own names, and don't shuffle only the 4−k remaining designers with (4−k)!, which forgets the two interns and gives 9.)
362
4
How many permutations of the six letters A, B, C, D, E, F avoid all four of these conditions: A1: F is in the last (6th) position; A2: the block AB appears; A3: the block BC appears; A4: the block FCA appears?
Watch out: in A1∩A4 and A3∩A4, check whether the rules can actually happen together before counting. Treating one of A1∩A4 and A3∩A4 as 3!=6 instead of 0 raises S2 and gives 414; treating both that way gives 420.Watch out: in A1∩A4 and A3∩A4, check whether the rules can actually happen together before counting. Treating one of A1∩A4 and A3∩A4 as 3!=6 instead of 0 raises S2 and gives 414; treating both that way gives 420.
Because the blocks have different lengths and share letters differently, write out the singles, the (24)=6 pairs, and the (34)=4 triples individually. Which pairs of rules contradict each other?
Start with N=6!=720 and count each single rule:
S1=A1: F is 6th5!+A2: [AB]5!+A3: [BC]5!+A4: [FCA]4!=120+120+120+24=384
Now check each of the 6 pairs:
∣A1∩A2∣∣A1∩A3∣∣A1∩A4∣∣A2∩A3∣∣A2∩A4∣∣A3∩A4∣=4!=24(F is 6th; shuffle [AB], C, D, E)=4!=24(F is 6th; shuffle [BC], A, D, E)=0(F cannot be 6th and alsoat the front of [FCA])=4!=24(fuses into [ABC] with D, E, F)=3!=6(fuses into [FCAB] with D, E)=0(C cannot come right afterboth B and F)
S2=24+24+0+24+6+0=78
(Watch out: in A1∩A4 and A3∩A4, check whether the rules can actually happen together before counting. Treating one of A1∩A4 and A3∩A4 as 3!=6 instead of 0 raises S2 and gives 414; treating both that way gives 420.)
Among the 4 triples, any triple containing A4 includes one of the impossible pairs above (A1∩A4 or A3∩A4), leaving only A1∩A2∩A3:
S3S4=A1∩A2∩A3: F is 6th, [ABC], D, E3!+0+0+0=6,=0
Combine the alternating sum:
720−384+78−6+0=408
408
5
Six distinct tasks (1 through 6) are assigned to four coworkers (A, B, C, D). Task 1 must be assigned to coworker A, and every coworker must receive at least one task. How many valid assignments are there?
Watch out: don't include coworker A as a fourth bad set with (k4), which tries to seal a box that already contains Task 1 and gives 240.
Once Task 1 is locked to coworker A, how many tasks are left to assign freely, and which coworkers can still end up with zero tasks?
Lock Task 1 to coworker A first. Each of the remaining 5 tasks (Tasks 2 through 6) has 4 choices of coworker, giving N=45=1024 total assignments.
Because coworker A already has Task 1, A can never have zero tasks. Only the other 3 coworkers (B, C, and D) can be left empty, giving 3 bad sets:
(Watch out: don't include coworker A as a fourth bad set with (k4), which tries to seal a box that already contains Task 1 and gives 240 instead of 390.)