Discrete Mathematics and Combinatorics

The Inclusion-Exclusion Principle With Worked Problems

Count overlapping sets without double counting, with worked problems on divisibility, primes, passwords, card hands and onto functions.

Key takeaways

  • For two sets, |A ∪ B| = |A| + |B| − |A ∩ B|.
  • For more sets, add the single sets, subtract the pairs, add the triples and continue with alternating signs.
  • To count objects with none of the properties, subtract the union from the total number of objects.
  • The method works well when intersections are easy to count even though the union is not.

The inclusion-exclusion principle counts the objects that have at least one of several properties. Add up the objects with each property, subtract those with each pair of properties, add back those with each triple, and keep alternating. Overlaps that were counted too often are corrected step by step until every object counts exactly once.

Here |A| means the number of elements in the set A, the union A ∪ B holds everything in either set, and the intersection A ∩ B holds what is in both.

Two and three sets

Adding |A| and |B| counts every element of the overlap twice, so you subtract the overlap once. With three sets the corrections go one level further:

|A ∪ B ∪ C| = |A| + |B| + |C|
              − |A ∩ B| − |A ∩ C| − |B ∩ C|
              + |A ∩ B ∩ C|

Why add back the triple overlap? An element in all three sets is added 3 times by the single sets and subtracted 3 times by the pairs, leaving it counted 0 times. The last term restores it. You can trace this region by region on a Venn diagram.

A language survey, region by region

Say an invented survey of 100 students finds that 45 study French, 38 Spanish and 30 German. The pairs are 15 for French and Spanish, 12 for French and German, and 10 for Spanish and German. Five study all three. Each pair figure includes those five.

at least one = (45 + 38 + 30) − (15 + 12 + 10) + 5
             = 113 − 37 + 5
             = 81
none         = 100 − 81 = 19

You can also fill in the diagram from the center outward. Five sit in the middle. French and Spanish only is 15 − 5 = 10, French and German only is 7, and Spanish and German only is 5. French only is then 45 − 10 − 7 − 5 = 23, Spanish only 18 and German only 13.

The seven regions add to 5 + 10 + 7 + 5 + 23 + 18 + 13 = 81, matching the formula. If any region had come out negative, the survey figures would have been inconsistent.

Counting exactly one or exactly two

Sometimes you want the objects with exactly a given number of properties. Let S₁ be the sum of the single set sizes, S₂ the sum over pairs and S₃ the triple overlap. For three sets:

exactly one    = S₁ − 2·S₂ + 3·S₃ = 113 − 74 + 15 = 54
exactly two    = S₂ − 3·S₃        = 37 − 15       = 22
exactly three  = S₃                                =  5

The coefficients correct for repetition. A student studying two languages appears twice in S₁ and once in S₂, so S₁ − 2·S₂ removes that student completely. The check 54 + 22 + 5 = 81 confirms the totals.

Divisibility up to 1,000

How many whole numbers from 1 to 1,000 are divisible by 2, 3 or 5? The number of multiples of d in that range is 1000/d rounded down. Since 2, 3 and 5 share no factors in pairs, a number divisible by two of them is divisible by their product.

singles:  500 + 333 + 200            = 1,033
pairs:    166 (by 6) + 100 (by 10)
          + 66 (by 15)               =   332
triple:   33 (by 30)
union:    1,033 − 332 + 33           =   734

So 1,000 − 734 = 266 numbers are divisible by none of the three. That second step, total minus union, is how the principle is used most often.

Example: numbers up to 1,000 divisible by 2, 3 or 5
1 Add singles Running total 1,033 2 Subtract pairs Remove 332, leaving 701 3 Add the triple Restore 33, giving 734 4 Take complement 266 avoid all three Add singles Running total 1,033 1 Subtract pairs Remove 332, leaving 701 2 Add the triple Restore 33, giving 734 3 Take complement 266 avoid all three 4

Why the signs always work

The general formula says that the size of A₁ ∪ ... ∪ Aₙ is the sum over all nonempty groups of the sets of the size of their intersection, with a plus sign for groups of odd size and a minus sign for even size.

To prove it, follow one element that lies in exactly m of the sets, with m ≥ 1. The formula counts it this many times:

C(m,1) − C(m,2) + C(m,3) − ... ± C(m,m)

Here C(m, j) is the number of ways to choose j of those m sets. By the binomial theorem, (1 − 1)ᵐ = 0, which rearranges to say that the sum above equals C(m, 0) = 1. Each element in the union is counted exactly once, and elements outside every set are never counted.

Sifting out primes

How many primes are there up to 100? Any composite number in that range has a prime factor of at most 10. So count the numbers divisible by 2, 3, 5 or 7.

singles:   50 + 33 + 20 + 14                 = 117
pairs:     16 + 10 + 7 + 6 + 4 + 2           =  45
triples:   3 (30) + 2 (42) + 1 (70) + 0 (105) =  6
all four:  0 (210)
union:     117 − 45 + 6 − 0                  =  78

That leaves 22 numbers with none of these factors. They include 1, which is not prime, and they omit the primes 2, 3, 5 and 7 themselves. So the number of primes up to 100 is 22 − 1 + 4 = 25. This is an arithmetic version of the sieve of Eratosthenes.

The same idea gives Euler's totient, the count of numbers from 1 to n sharing no factor above 1 with n. For 60, whose primes are 2, 3 and 5, it comes to 60 × (1/2) × (2/3) × (4/5) = 16. See the totient function.

Passwords with required characters

Suppose a password has 6 characters drawn from 26 lowercase letters and 10 digits, and must contain at least one of each kind. Count all strings, then remove the bad ones.

all strings         36⁶ = 2,176,782,336
no digit            26⁶ =   308,915,776
no letter           10⁶ =     1,000,000
both missing                          0
valid    36⁶ − 26⁶ − 10⁶ = 1,866,866,560

The overlap is zero because a string of length 6 must use something. With three or four required kinds of character, the overlaps become real and the alternating pattern is needed in full.

Card hands using every suit

How many five-card hands from a standard deck contain all four suits? Count the hands missing at least one suit. Missing one chosen suit leaves 39 cards, missing two leaves 26, and missing three leaves 13.

4 × C(39, 5) = 4 × 575,757 = 2,303,028
6 × C(26, 5) = 6 ×  65,780 =   394,680
4 × C(13, 5) = 4 ×   1,287 =     5,148
missing some suit = 2,303,028 − 394,680 + 5,148 = 1,913,496
all four suits    = 2,598,960 − 1,913,496       =   685,464

A direct count agrees. Pick the suit that appears twice (4), two of its cards (78), and one card from each other suit (13³ = 2,197). The product is 685,464. When two independent methods match, you can trust the result.

Onto functions

In how many ways can 5 different tasks be given to 3 people so that everyone gets at least one? Without the condition there are 3⁵ = 243 ways. Subtract those that leave out a chosen person, then restore those that leave out two.

3⁵ − 3 × 2⁵ + 3 × 1⁵ = 243 − 96 + 3 = 150

A function that uses every output is called onto, or surjective. For k inputs and n outputs the count is the sum over j from 0 to n of (−1)ʲ C(n, j)(n − j)ᵏ. Dividing by n! gives the Stirling numbers of the second kind.

The most famous application counts arrangements where nothing stays in its own place, treated in derangements.

Seating restrictions

Line up five people, A, B, C, D and E, so that A is not next to B and C is not next to D. There are 120 lineups in all. Call it a fault when a forbidden pair stands together.

Gluing A and B into one block gives 4! × 2 = 48 lineups with that fault, and the same for C and D. With both blocks, there are 3 objects and two ways to read each block: 3! × 2 × 2 = 24.

at least one fault = 48 + 48 − 24 = 72
no fault           = 120 − 72     = 48

Notice how each ingredient was easy. Requiring things to be together is simple, requiring them to be apart is awkward, and inclusion-exclusion converts one into the other.

The probability version

The same formula holds for probabilities of events, with P in place of set size. Roll three fair dice. What is the chance of at least one six?

P = 3 × (1/6) − 3 × (1/36) + 1/216
  = (108 − 18 + 1)/216
  = 91/216

The complement gives the same: the chance of no six is (5/6)³ = 125/216, and 216 − 125 = 91. For independent events like these, the complement is faster. Inclusion-exclusion earns its place when events depend on one another, as with cards dealt from one deck.

Using it well

  1. Name each property you want to avoid, such as "no digit appears".
  2. Check that you can count any combination of those properties.
  3. Use symmetry: if all pairs have the same size, count one and multiply.
  4. Test on a small case you can list.

Stopping early also tells you something. The sum of the single sets alone is never too small. Subtracting the pairs gives a total that is never too big. These alternating bounds, named for Carlo Bonferroni, are widely used in probability and statistics, where the exact overlaps are often unknown.

Symmetry cuts the work

When every group of j sets has an intersection of the same size, you do not need to compute each term separately. Multiply one representative by C(n, j), the number of such groups.

That is what happened with the suits and with the onto functions: the terms depended only on how many properties were involved. The whole formula then shrinks from 2ⁿ − 1 terms to just n. Before expanding a large problem, look for this kind of interchangeability among the properties. It often turns an impractical calculation into a few lines.

Where students slip

The most common error is mixing up "exactly" and "at least". A figure such as 15 students taking French and Spanish normally includes those who also take German. If a problem instead gives "French and Spanish only", add the triple group back before using the formula.

A second error is forgetting the objects with no property at all. The union is never the whole population unless you know that every object has some property.

A third is computing intersections of divisibility conditions by multiplying carelessly. The numbers divisible by both 4 and 6 are the multiples of 12, the least common multiple, and not the multiples of 24. Multiplication is right only when the divisors share no common factor.

Finally, check the last term. With four sets there are 4 singles, 6 pairs, 4 triples and 1 quadruple, 15 intersections in all, and the quadruple carries a minus sign.

Limits of the method

The main limitation is size. With n sets there are 2ⁿ − 1 intersections to consider, so 20 properties already mean more than a million terms unless symmetry collapses them.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 45 articles in Discrete Mathematics and Combinatorics →

Latest news on this topic