Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

A visual guide to counting unions without double-counting


The inclusion-exclusion principle is a fundamental counting technique that helps us determine the size of a union of sets. The challenge? When we naively add up the sizes of sets, we double-count elements that appear in multiple sets.

Venn diagrams make this principle crystal clear. Let’s build up our understanding starting with two sets.

The Problem: Naïve Counting Fails

Suppose we want to count how many elements are in “A or B” (written as ABA \cup B, the union).

Our first instinct might be:

AB=A+B(wrong) WRONG!|A \cup B| = |A| + |B| \quad \text{(wrong) WRONG!}

Why is this wrong? Because elements in both A and B (the intersection ABA \cap B) get counted twice!

The inclusion-exclusion principle fixes this:

AB=A+BAB(correct) CORRECT|A \cup B| = |A| + |B| - |A \cap B| \quad \text{(correct) CORRECT}

Let’s visualize why.


Two Sets: The Foundation

Matplotlib is building the font cache; this may take a moment.
<Figure size 1800x700 with 2 Axes>

The Two-Set Formula

AB=A+BAB|A \cup B| = |A| + |B| - |A \cap B|

Why it works:

  1. Start by adding A|A| and B|B| — this counts everything in both sets

  2. But wait! Elements in ABA \cap B were counted twice (once in A|A|, once in B|B|)

  3. Subtract AB|A \cap B| once to correct the double-counting

Note: AB=BAA \cap B = B \cap A (intersection is commutative) — order doesn’t matter!


Example 1: Student Clubs

Scenario: At a school:

Question: How many students are in at least one club?

Solution

Using inclusion-exclusion:

DC=D+CDC|D \cup C| = |D| + |C| - |D \cap C|
DC=45+3515=65|D \cup C| = 45 + 35 - 15 = 65

Answer: 65 students are in at least one club.

<Figure size 1200x800 with 1 Axes>

Extending to Three Sets

With three sets, the counting becomes trickier. We need to:

  1. Include all three sets individually

  2. Exclude all pairwise intersections (we double-counted them)

  3. Include the triple intersection back (we excluded it too many times!)

The formula:

ABC=A+B+CABACBC+ABC|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|

Let’s visualize why each term is necessary.

<Figure size 2000x1200 with 6 Axes>

Why Each Term in the Three-Set Formula?

ABC=A+B+CABACBC+ABC|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|

Understanding the counting:

RegionTimes in sumAfter subtracting pairsAfter adding triple
A only1x1x1x (correct)
B only1x1x1x (correct)
C only1x1x1x (correct)
A∩B only2x2x - 1x = 1x1x (correct)
A∩C only2x2x - 1x = 1x1x (correct)
B∩C only2x2x - 1x = 1x1x (correct)
A∩B∩C3x3x - 3x = 0x0x + 1x = 1x (correct)

The center region (A∩B∩C) is:


Example 2: Programming Languages Survey

Scenario: A survey of 100 programmers asks about language proficiency:

Overlaps:

Question: How many programmers know at least one of these languages?

Solution

PJR=P+J+RPJPRJR+PJR|P \cup J \cup R| = |P| + |J| + |R| - |P \cap J| - |P \cap R| - |J \cap R| + |P \cap J \cap R|
PJR=50+40+30201510+8|P \cup J \cup R| = 50 + 40 + 30 - 20 - 15 - 10 + 8
PJR=12045+8=83|P \cup J \cup R| = 120 - 45 + 8 = 83

Answer: 83 programmers know at least one of Python, JavaScript, or Rust.

<Figure size 1400x1000 with 1 Axes>

Example 3: Probability Version

The inclusion-exclusion principle also works for probabilities!

P(ABC)=P(A)+P(B)+P(C)P(AB)P(AC)P(BC)+P(ABC)P(A \cup B \cup C) = P(A) + P(B) + P(C) - P(A \cap B) - P(A \cap C) - P(B \cap C) + P(A \cap B \cap C)

Scenario: Rolling a fair six-sided die:

Question: What’s the probability of rolling a number that satisfies at least one of these conditions?

Solution

First, let’s identify the sets:

Intersections:

Apply inclusion-exclusion:

P(ABC)=12+13+12161616+0P(A \cup B \cup C) = \frac{1}{2} + \frac{1}{3} + \frac{1}{2} - \frac{1}{6} - \frac{1}{6} - \frac{1}{6} + 0
P(ABC)=36+26+36161616P(A \cup B \cup C) = \frac{3}{6} + \frac{2}{6} + \frac{3}{6} - \frac{1}{6} - \frac{1}{6} - \frac{1}{6}
P(ABC)=8636=56P(A \cup B \cup C) = \frac{8}{6} - \frac{3}{6} = \frac{5}{6}

Answer: The probability is 5/6.

Verification: The union is {1, 2, 3, 4, 6} — that’s 5 outcomes out of 6, confirming P = 5/6. (correct)

<Figure size 1400x1000 with 1 Axes>

The General Formula

For n sets A₁, A₂, ..., Aₙ, the inclusion-exclusion principle extends to:

A1A2An=iAii<jAiAj+i<j<kAiAjAk+(1)n+1A1A2An|A_1 \cup A_2 \cup \cdots \cup A_n| = \sum_{i} |A_i| - \sum_{i<j} |A_i \cap A_j| + \sum_{i<j<k} |A_i \cap A_j \cap A_k| - \cdots + (-1)^{n+1} |A_1 \cap A_2 \cap \cdots \cap A_n|

Pattern:

  1. Add all single sets

  2. Subtract all pairwise intersections

  3. Add all triple intersections

  4. Subtract all quadruple intersections

  5. Continue alternating + and -

The signs alternate because we’re correcting for over-counting and under-counting at each step!


Key Takeaways

  1. Naïve addition overcounts — elements in multiple sets get counted multiple times

  2. Two sets: AB=A+BAB|A \cup B| = |A| + |B| - |A \cap B|

    • Subtract the intersection once to fix double-counting

  3. Three sets: ABC=A+B+CABACBC+ABC|A \cup B \cup C| = |A| + |B| + |C| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|

    • Subtract pairwise intersections, then add back the triple intersection

  4. Intersection is commutative: AB=BAA \cap B = B \cap A (order doesn’t matter)

  5. Works for probabilities too: Replace set sizes with probabilities

  6. Pattern for n sets: Alternate between adding and subtracting, working through all possible intersections

  7. Venn diagrams are your friend — they make the over-counting and correction visible!


The inclusion-exclusion principle is fundamental in combinatorics, probability theory, and computer science. Master it, and you’ll have a powerful tool for solving counting problems!