Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Number System and Arithmetic
  • Algebra
  • Set Theory
  • Probability
  • Statistics
  • Geometry
  • Calculus
  • Logarithms
  • Mensuration
  • Matrices
  • Trigonometry
  • Mathematics
Open In App
Next Article:
Principle of Inclusion and Exclusion
Next article icon

Principle of Inclusion and Exclusion

Last Updated : 27 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Principle of Inclusion and Exclusion (PIE) is a smart way to count the number of items in overlapping groups without accidentally counting some items more than once. It works by first adding the number of items in each group, then subtracting the number of items in any two groups that overlap, and so on.

For example, if you have three groups of students studying different subjects, PIE helps figure out how many students study at least one subject by adjusting for those who study more than one, so nobody is counted twice.

In this article, we will discuss Principle of Inclusion and Exclusion in detail.

Table of Content

  • What is the Principle of Inclusion and Exclusion?
  • Formula for Principle of Inclusion and Exclusion
    • Formula for Two Sets
    • Formula for three sets:
  • Derivation of Inclusion and Exclusion Principle Using Venn Diagrams
    • Venn Diagram for Three Sets
  • FAQs

What is the Principle of Inclusion and Exclusion?

Principle of Inclusion and Exclusion (PIE) is a combinatorial method used to calculate the cardinality (size) of the union of multiple sets. The principle is particularly useful when sets overlap, and we want to avoid double-counting elements that belong to more than one set.

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 intersection of four set, as it was added twice in the last step.
  • and so on.

We can continue this till intersection of all the element is either added or subtracted at the end.

Formula for Principle of Inclusion and 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 element sin set B.
  • ∣A∩B∣ is the number of elements in both set A and B.

Formula for three sets:

For three sets A,B,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∣, ∣C∣ are number of elements in the sets A,B and C respectively.
  • ∣A∩B∣,∣A∩C∣,∣B∩C∣ are the numbers of elements in the pairwise intersections of the sets.
  • ∣A∩B∩C∣ s the number of elements that are in all three sets.

Derivation of Inclusion and Exclusion Principle Using Venn Diagrams

We can visualize this using Venn diagram, for finite number of sets. Here we will discuss formula for Principle of Inclusion and Exclusion for two and three sets and derive it using Venn diagram,

Venn Diagram for Two Sets

Venn-Diagram-of-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 A∩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 ∪ B∣, is given by:

∣A∪B∣ = ∣A∣ + ∣B∣ − ∣A∩B∣

Venn Diagram for Three Sets

Venn-Diagram-of-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 (Inclusion of Intersection of All Three Sets): 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∣

Solved Examples

Problem 1: In a class of 100 students:

  • 60 study Math.
  • 45 study Science.
  • 20 study both Math and Science.

How many students study either Math or Science?

Solution:

As we know, n(A⋃B) = n(A) + n(B) – n(A⋂B)

Given:

  • n(A) = 60
  • n(B) = 45
  • n(A∩B) = 20

n(A⋃B) = n(A) + n(B) – n(A⋂B) = 60 + 45 - 20 = 85

Thus, 85 students study either Math or Science.

Problem 2 :The probability of getting at least one head is Problem: In a survey of 120 people:

  • 80 people like tea.
  • 70 people like coffee.
  • 50 people like both tea and coffee.

How many people like either tea or coffee?

Solution:

As we know, n(A⋃B) = n(A) + n(B) – n(A⋂B)

Given:

  • IAI = 80
  • IBI = 70
  • IA∩BI = 50

Thus, n(A⋃B) = 80 + 70 - 50 = 100

Thus, 100 people like either tea or coffee.

Problem 3: A company has 30 employees, 10 are assigned to Task A, 15 to Task B, and 5 are assigned to both. How many employees are assigned to at least one task?

Solution:

As we know, n(A⋃B) = n(A) + n(B) – n(A⋂B)

Given:

  • IAI = 10 Task A
  • IBI = 15 Task B
  • IA∩BI = 5 Both tasks

Substitute the Values, We get

n(A⋃B) = 10 + 15 - 5 = 10

Thus, 20 employees are assigned to at least one task.

Problem 4 : In a survey of 200 people:

  • 120 like pizza.
  • 100 like burgers.
  • 80 like tacos.
  • 60 like both pizza and burgers.
  • 40 like both burgers and tacos.
  • 30 like both pizza and tacos.
  • 20 like all three: pizza, burgers, and tacos.

How many people like at least one of these three foods?

Solution:

As we know, ∣AUBUC∣ = ∣A∣ + ∣B∣ + ∣C∣ - ∣A∩B∣ - ∣A∩C∣ - ∣B∩C∣ + ∣A∩B∩C∣

Given:

  • ∣A∣ = 120 Pizza lovers
  • ∣B∣ = 100 Burger lovers
  • ∣C∣ = 80 Taco lovers
  • ∣A∩B∣ = 60
  • ∣A∩C∣ = 30
  • ∣B∩C∣ = 40
  • ∣A∩B∩C∣ = 20

Substituting in the formula, we get,

∣AUBUC∣ = 120 + 100 + 80 - 60 - 30 - 40 + 20 = 190

Thus, 190 people like at least one of the three foods.

Problem 5: In a university of 300 students:

  • 150 students take Math.
  • 120 students take Physics.
  • 100 students take Chemistry.
  • 80 students take both Math and Physics.
  • 60 students take both Physics and Chemistry.
  • 50 students take both Math and Chemistry.
  • 30 students take all three subjects.

How many students are taking at least one subject?

Solution:

As we know, ∣AUBUC∣ = ∣A∣ + ∣B∣ + ∣C∣ - ∣A∩B∣ - ∣A∩C∣ - ∣B∩C∣ + ∣A∩B∩C∣

Given:

  • ∣A∣ = 150 (Math),
  • ∣B∣ = 120 (Physics),
  • ∣C∣ = 100 (Chemistry),
  • ∣A∩B∣ = 80
  • ∣A∩C∣ = 60
  • ∣B∩C∣ = 50
  • ∣A∩B∩C∣ = 30

Substituting values, we get

∣AUBUC∣ = 150 + 120 + 100 - 80 - 60 - 50 + 30 = 210

Thus, 210 students are taking at least one subject.

Practice Problems

Problem 1: In a group of 200 people:

  • 120 people like chocolate.
  • 90 people like vanilla.
  • 50 people like both chocolate and vanilla.

How many people like either chocolate or vanilla?

Problem 2: In a class of 100 students:

  • 70 students play football.
  • 60 students play basketball.
  • 50 students play cricket.
  • 30 students play both football and basketball.
  • 25 students play both basketball and cricket.
  • 20 students play both football and cricket.
  • 15 students play all three sports.

How many students play at least one of these three sports?

Problem 3: Out of 150 attendees at an event:

  • 80 attended Workshop A.
  • 70 attended Workshop B.
  • 40 attended both Workshop A and Workshop B.

How many people attended at least one workshop?

Problem 4: A card is drawn from a deck of 52 cards. What is the probability that it is:

  • A heart,
  • A face card (King, Queen, Jack),
  • Or a red card (hearts or diamonds)?

There are 13 hearts, 12 face cards, and 26 red cards.

Note that:

  • 3 face cards are also hearts,
  • 6 face cards are red.

Problem 5: In a school of 300 students:

  • 180 students are enrolled in Math.
  • 150 students are enrolled in Science.
  • 120 students are enrolled in English.
  • 90 students are enrolled in both Math and Science.
  • 80 students are enrolled in both Science and English.
  • 70 students are enrolled in both Math and English.
  • 50 students are enrolled in all three subjects.

How many students are enrolled in at least one subject?

Answer Key

  1. 160 people like either chocolate or vanilla.
  2. 120 students play at least one of these sports.
  3. 110 people attended at least one workshop.
  4. The probability is 8/13.
  5. 260 students are enrolled in at least one subject.

Conclusion

In conclusion, the Principle of Inclusion and Exclusion (PIE) is a handy tool for counting items in overlapping groups. It helps ensure that we don’t count anything more than once by carefully adding and subtracting the sizes of the groups and their overlaps. Whether you're working with two sets or many, PIE gives you an accurate way to find the total without making mistakes.

Read More,

  • Set Theory
  • Set Theory Formulas 
  • Set Theory Symbols
  • Union of Sets
  • Intersection of Sets
  • Cardinality of Sets

Next Article
Principle of Inclusion and Exclusion

M

maityavighyw
Improve
Article Tags :
  • Mathematics
  • School Learning
  • Set Theory
  • Maths

Similar Reads

    Pauli Exclusion Principle
    The Pauli Exclusion Principle plays an important role in the field of Chemistry. It helps us to understand the electron arrangements in atoms and molecules also it is very crucial to understand the Pauli Exclusion Principle in the study of an electron. In this article, we will do a detailed study of
    5 min read
    Inclusion-Exclusion and its various Applications
    In the field of Combinatorics, it is a counting method used to compute the cardinality of the union set. According to basic Inclusion-Exclusion principle: For 2 finite sets A_1 and A_2 , which are subsets of Universal set, then (A_1-A_2), (A_2-A_1) and (A_1\bigcap A_2) are disjoint sets. Hence it ca
    7 min read
    Competitive Exclusion Principle
    Competitive Exclusion Principle, also known as Gause’s law, states that two species that compete for the same resource cannot coexist; over time, one species may outcompete the other, leading to the elimination of the inferior species. In ecology, there are two types of competition: Intraspecies and
    6 min read
    Fundamental Principle of Counting
    The fundamental principle of counting is a basic concept used to determine the total number of possible outcomes in a situation where there are multiple independent events. It allows us to count a large number of possibilities without needing to list each one individually.For example, consider guess
    11 min read
    Practice Problems on Finite and Infinite Sets
    Finite and infinite sets are two different parts of "Set theory" in mathematics. When a set has a finite number of elements, it is called a "Finite set" and when a set has an infinite number of elements, it is called an "Infinite set". A finite set is countable, whereas an infinite set is uncountabl
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences