The Principle of Inclusion and Exclusion (PIE) is a counting technique used to find the size of the union of multiple sets by systematically including and excluding overlapping parts.
It corrects the overcounting that happens when you simply add the sizes of the sets. When sets have overlapping elements, counting them directly can lead to overcounting. The Principle of Inclusion and Exclusion fixes this by alternately adding and subtracting the sizes of the intersection to get the exact total.

For example, when calculating the number of people who own a dog or a cat, PIE adds up all dog owners and subtracts all cat owners. This includes:
- Adding the sizes of each individual set.
- Subtracting the sizes of the pairwise intersections to avoid double-counting.
- Adding back the size of the intersection of all three sets, as it was subtracted too many times.
- Subtracting the sizes of the intersection of four sets, as it was added twice in the last step.
- And so on.
Formula for Principle of Inclusion & Exclusion
In general, for n sets A1, A2, . . ., An:
|A_1 \cup A_2 \cup \dots \cup A_n| = \sum_{i=1}^{n} |A_i| - \sum_{1 \leq i < j \leq n} |A_i \cap A_j| + \sum_{1 \leq i < j < k \leq n} |A_i \cap A_j \cap A_k| - \dots + (-1)^{n+1} |A_1 \cap A_2 \cap \dots \cap A_n|
Where:
- The first term sums the sizes of all individual sets.
- The second term subtracts the sizes of all pairwise intersections.
- The third term adds back the sizes of all three-way intersections.
- This alternating pattern continues, with the signs changing for each subsequent term, until the intersection of all N sets is reached.
Note: This formula effectively accounts for all overlaps among the sets, ensuring that each unique element is counted exactly once.
Formula for Two Sets
For two sets A and B
∣AUB∣ = ∣A∣+∣B∣-∣A∩B∣
Where,
- ∣A∣ is the number of elements in set 𝐴.
- ∣B∣ is the number of elements in set B.
- ∣A∩B∣ is the number of elements in both set A and set B.
Formula for Three Sets
For three sets A,B, and C, the Principle of Inclusion and Exclusion (PIE) formula to find the size of the union is:
∣AUBUC∣ = ∣A∣+∣B∣+∣C∣-∣A∩B∣-∣A∩C∣-∣B∩C∣+∣A∩B∩C∣
Where,
- ∣A∣, ∣B∣, and ∣C∣ are the number of elements in the sets A, B, and C, respectively.
- ∣A∩B∣, ∣A∩C∣, ∣and B∩C∣ are the numbers of elements in the pairwise intersections of the sets.
- ∣A∩B∩C∣ is the number of elements that are in all three sets.
Derivation of the Inclusion and Exclusion Principle Using Venn Diagrams
We can visualize this using a Venn diagram for a finite number of sets. Here we will discuss the formula for the Principle of Inclusion and Exclusion for two and three sets and derive it using a Venn diagram.
Venn Diagram for Two Sets

- First Step (Inclusion): Add the sizes of the two sets ∣A∣ and ∣B∣. This counts all the elements in A and B, but elements in the intersection of A∩and B are counted twice (once in A and once in B).
- Second Step (Exclusion): Subtract the size of the intersection ∣A∩B∣|A \cap B|∣A∩B∣, because these elements were counted twice in the first step.
Thus, the number of elements in the union of these two sets, ∣A ∪ and B∣, is given by:
∣A∪B∣ = ∣A∣ + ∣B∣ − ∣A∩B∣
Venn Diagram for Three Sets

- First Step (Inclusion): Add the sizes of the individual sets ∣A∣, ∣B∣, and ∣C∣. This counts all the elements in A, B, and C, but it also counts the elements in the intersections more than once.
- Second Step (Exclusion): Subtract the pairwise intersections ∣A∩B∣, ∣B∩C∣, and ∣C∩A∣ because these elements were counted twice in the first step.
- Third Step (Re) Inclusion ): Add back the intersection of all three sets ∣A∩, B∩C∣, as it was subtracted three times (once for each pairwise intersection) in the previous step.
Thus, the number of elements in the union of these three sets, ∣A∪B∪C∣, is given by:
∣A∪B∪C∣ = ∣A∣ + ∣B∣ + ∣C∣ − ∣A∩B∣ − ∣B∩C∣ − ∣C∩A∣ + ∣A∩B∩C∣
Applications in Computer Science
Principle of Inclusion and Exclusion (PIE) is used quite a lot in Computer Science, especially in areas that deal with counting, probability, and combinatorics.
Here are some key applications:
- Counting problems in algorithms: When counting arrangements, subsets, or configurations with overlapping constraints.
- Cryptography & error-correcting codes: Counting possible keys or codewords that satisfy multiple constraints.
- Inclusion–Exclusion in bitmask DP: In dynamic programming on subsets, PIE is often combined with bitmasking to speed up solutions to problems involving all subsets.
- Graph theory & network problems: Counting the number of vertices/edges meeting certain properties.