Set Theory — Venn Diagrams

Computational Mathematics

02

Important Notice

This is an unofficial support material, created by students with the help of artificial intelligence. It does not represent the official position of the institution or its professors.
Do not question or bother the professors about this content. The material is based on notes taken during classes and supplementary research done by the students themselves.
This material may contain inaccuracies. When in doubt, always refer to the official material provided by the professor and the references listed at the end.
Suggestions and Corrections
Found an error or have a suggestion? Send your contribution through the class WhatsApp group. All collaboration is welcome to improve the material for everyone.
03
TABLE OF CONTENTS

Contents

04
INTRO

What are Sets?

A set is a collection of distinct objects — each element appears only once. Order doesn't matter: {1, 2, 3} and {3, 1, 2} are the same set. In programming, sets work like Set data structures, where duplicates are automatically removed.

Sets are written with curly braces. We can list elements directly — A = {1, 2, 3} — or describe them with a rule: A = {x | x is even}, read as "A is the set of all x such that x is even". The Universal Set (U) contains all elements under consideration in a given context: everything that "exists" in our problem.

Venn Diagrams are the classic visual way to represent sets: each set is a circle, and the surrounding rectangle is the universe (U). When two circles overlap, the overlapping region represents elements that belong to both sets at the same time. The following sections show each operation with colored diagrams and practical software examples.

05
SET OPERATIONS

Example 1 — Sets Defined by List

A = {1, 3, 5, 7, 9, 11, 13, 15}
B = {2, 3, 5, 7, 11, 13}
A ∪ BUnion
UAB191535711132
A ∪ B = {1, 2, 3, 5, 7, 9, 11, 13, 15}

ALL elements from A and B combined (no duplicates).

Merging two lists of user IDs from different databases — you want ALL unique users.

A ∩ BIntersection
UAB191535711132
A ∩ B = {3, 5, 7, 11, 13}

ONLY elements that are in BOTH A and B at the same time.

Finding common friends between two social media profiles.

06
SET OPERATIONS

Example 1 — Sets Defined by List

A \ B • B \ A

A \ BDifference (A minus B)
UAB191535711132
A \ B = {1, 9, 15}

Elements in A that are NOT in B.

Users who have an account but never made a purchase.

B \ ADifference (B minus A)
UAB191535711132
B \ A = {2}

Elements in B that are NOT in A.

Features requested by clients that are not yet in the product backlog.

07
SET OPERATIONS

Example 1 — Sets Defined by List

A' • B'

A'Complement of A
UAB191535711132
A' = {2}

Everything in the universe that is NOT in A (= elements only in B, not shared).

All inactive users (complement of the active users set).

B'Complement of B
UAB191535711132
B' = {1, 9, 15}

Everything in the universe that is NOT in B (= elements only in A, not shared).

Products not in the discount category.

08
SET OPERATIONS

Example 1 — Sets Defined by List

(A ∪ B)'

(A ∪ B)'Complement of Union
UAB191535711132
(A ∪ B)' = ∅ (empty)

Everything NOT in A and NOT in B (here: empty — all elements belong to at least one set).

Users matching NO filter criteria — completely unclassified records.

09
SET OPERATIONS

Example 2 — Sets Defined by Rule

U = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}(Integers from 1 to 20)
A = {x | x is even} = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
B = {x | x ≥ 10} = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
U = A ∪ B ∪ {1, 3, 5, 7, 9}20 elements
A ∪ BUnion
UAB2468101214161820111315171913579
A ∪ B = {2, 4, 6, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}

All even numbers OR numbers ≥ 10 (or both).

A ∩ BIntersection
UAB2468101214161820111315171913579
A ∩ B = {10, 12, 14, 16, 18, 20}

Numbers that are even AND ≥ 10.

10
SET OPERATIONS

Example 2 — Sets Defined by Rule

B' • (A ∪ B)'

B'Complement of B
UAB2468101214161820111315171913579
B' = {1, 2, 3, 4, 5, 6, 7, 8, 9}

All integers from 1 to 20 that are NOT ≥ 10 (i.e., numbers < 10).

(A ∪ B)'Complement of Union
UAB2468101214161820111315171913579
(A ∪ B)' = {1, 3, 5, 7, 9}

Numbers that are NOT even AND NOT ≥ 10 (odd numbers less than 10).

11
SET OPERATIONS

Example 2 — Sets Defined by Rule

(A \ B)'

(A \ B)'Complement of Difference
UAB2468101214161820111315171913579
(A \ B)' = {1, 3, 5, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}

Everything EXCEPT elements only in A (keeps intersection, B-only, and outside).

12
CODE

Java — HashSet: Sets in Practice

How sets work in real code

Sets.java
import java.util.HashSet;
import java.util.Set;

public class Sets {
  public static void main(String[] args) {

    // Creates an empty set of integers
    Set<Integer> numSet = new HashSet<>();
    System.out.println(numSet.size());  // → 0 (empty)

    numSet.add(5);                       // Adds 5
    System.out.println(numSet.size());  // → 1

    numSet.add(6);                       // Adds 6
    System.out.println(numSet.size());  // → 2

    numSet.add(5);                       // Tries to add 5 again
    System.out.println(numSet.size());  // → 2 (UNCHANGED! Set ignores duplicates)

    System.out.println(numSet.add(4)); // → true  (4 was new, added)
    System.out.println(numSet.size());  // → 3

    System.out.println(numSet.add(4)); // → false (4 already existed, rejected)
    System.out.println(numSet.size());  // → 3 (still 3)

    // Prints the entire set — order may vary!
    System.out.println(numSet);         // → [4, 5, 6]
  }
}
13
CODE

Java — Custom Objects in Sets

Why equals() and hashCode() matter

CoolClass.java
// Simple class with two attributes
public class CoolClass {
    int value;        // a number
    String text;      // a text

    public CoolClass(int value, String text) {
        this.value = value;
        this.text = text;
    }
}
Usage in Sets.java
// Creating objects with the SAME values
CoolClass object1     = new CoolClass(1, "One");
CoolClass object2     = new CoolClass(2, "Two");
CoolClass objectOther1 = new CoolClass(1, "One"); // same value as object1!

Set<CoolClass> coolSet = new HashSet<>();
System.out.println(coolSet.size());    // → 0

coolSet.add(object1);
coolSet.add(objectOther1);              // SAME value, but different object
System.out.println(coolSet.size());    // → 2 (!!) — Shouldn't it be 1?

// ⚠ WARNING: Without overriding equals() and hashCode(),
// Java compares REFERENCES (memory address), not content.
// object1 and objectOther1 have equal values but are different
// objects → the Set treats them as "different".
14
SUMMARY

Quick Reference

OperationSymbolMeaning
UnionA ∪ BEverything in A or B (or both)
IntersectionA ∩ BOnly what is in both A and B
DifferenceA \ BWhat is in A but not in B
ComplementA'Everything in the universe not in A
Comp. of Union(A ∪ B)'Everything not in either set
15
EXERCISES

Practice Exercises

Solve using the operations you learned

01
Easy — Given C = {2, 4, 6, 8} and D = {1, 2, 3, 4, 5}. What is C ∪ D?
Combine all elements without repeating.
02
Easy — Using the same C and D, what is C ∩ D?
Which numbers appear in BOTH sets?
03
Easy — What is C \ D (difference of C minus D)?
Which numbers are in C but NOT in D?
04
Easy — If U = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, what is C'?
Everything in the universe that is NOT in C.
05
Easy — Using C = {2, 4, 6, 8} and D = {1, 2, 3, 4, 5} from earlier, what is D \ C?
Which numbers are in D but NOT in C? Note: D \ C is different from C \ D.
06
Easy — If |A| = 12, |B| = 8, and |A ∩ B| = 5, what is |A ∪ B|?
Use the inclusion–exclusion formula: |A ∪ B| = |A| + |B| − |A ∩ B|.
07
Medium — Given E = {a, b, c} and F = {b, c, d, e}. What is (E ∪ F)' if U = {a, b, c, d, e, f}?
First compute E ∪ F, then take the complement.
08
Medium — Let G = {1, 2, 3, 4, 5} and H = {3, 4, 5, 6, 7} with U = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. What is (G ∩ H)'?
First find G ∩ H, then take everything in U that is NOT in that result.
09
Medium — Given P = {10, 20, 30, 40, 50} and Q = {20, 40, 60, 80} with U = {10, 20, 30, 40, 50, 60, 70, 80}. Show that P \ Q = P ∩ Q' by computing both sides.
Left side: elements in P not in Q. Right side: first find Q', then intersect with P.
10
Medium — The intersection of two sets is {5, 10} and their union is {1, 5, 7, 10, 12}. Find one possible pair of sets A and B.
Elements in the intersection must be in BOTH sets. The remaining elements of the union can be distributed between A and B.
11
Hard — Let A = {1, 2, 3, 4}, B = {3, 4, 5, 6}, and C = {2, 4, 6, 8} with U = {1, 2, 3, 4, 5, 6, 7, 8}. What is (A ∪ B) ∩ C'?
Three steps: (1) find A ∪ B, (2) find C', (3) intersect the two results.
12
Hard — Let A = {1, 2, 3, 4, 5} and B = {4, 5, 6, 7, 8} with U = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. Verify De Morgan's Law: (A ∪ B)' = A' ∩ B'.
Compute each side separately: left side needs A ∪ B then its complement; right side needs A' and B' then their intersection.
13
Challenge — In a class of 40 students: 22 study Maths (M), 18 study Science (S), and 15 study Art (R). Also: |M ∩ S| = 8, |M ∩ R| = 6, |S ∩ R| = 5, and |M ∩ S ∩ R| = 3. (a) How many students study at least one subject? (b) How many study none of the three? (c) How many study exactly one subject?
Use the three-set inclusion–exclusion formula: |M ∪ S ∪ R| = |M| + |S| + |R| − |M∩S| − |M∩R| − |S∩R| + |M∩S∩R|.
14
Code — In Java, what does the code below print?
Set<Integer> numbers = new HashSet<>();
numbers.add(3);
numbers.add(3);
System.out.println(numbers.size());
Remember: Set does not accept duplicates.
16
ANSWERS

Answers

01
C ∪ D = {1, 2, 3, 4, 5, 6, 8}
Union: all elements from C and D combined.
02
C ∩ D = {2, 4}
Intersection: only 2 and 4 are in both.
03
C \ D = {6, 8}
6 and 8 are in C but do not appear in D.
04
C' = {1, 3, 5, 7, 9, 10}
Complement: all of U except {2, 4, 6, 8}.
05
D \ C = {1, 3, 5}
Difference is not symmetric: C \ D = {6, 8} but D \ C = {1, 3, 5}. The elements 1, 3 and 5 are in D but do not appear in C.
06
|A ∪ B| = 15
|A ∪ B| = 12 + 8 − 5 = 15. We subtract the intersection to avoid counting shared elements twice.
07
(E ∪ F)' = {f}
E ∪ F = {a,b,c,d,e}. Complement in the universe: only {f} remains.
08
(G ∩ H)' = {1, 2, 6, 7, 8, 9, 10}
G ∩ H = {3, 4, 5}. The complement is everything in U except {3, 4, 5}: {1, 2, 6, 7, 8, 9, 10}.
09
P \ Q = {10, 30, 50} and P ∩ Q' = {10, 30, 50} — they are equal.
P \ Q = {10, 30, 50} (elements in P not in Q). Q' = {10, 30, 50, 70} (elements in U not in Q). P ∩ Q' = {10, 30, 50} (elements in both P and Q'). Both sides give the same result — this identity always holds.
10
One valid pair: A = {1, 5, 10, 12} and B = {5, 7, 10}
Both 5 and 10 must be in A and B (they are in the intersection). The remaining elements {1, 7, 12} can be split between the sets in any way, as long as every element appears in at least one set. Another valid pair: A = {5, 7, 10} and B = {1, 5, 10, 12}.
11
(A ∪ B) ∩ C' = {1, 3, 5}
Step 1: A ∪ B = {1, 2, 3, 4, 5, 6}. Step 2: C' = U \ C = {1, 3, 5, 7}. Step 3: {1, 2, 3, 4, 5, 6} ∩ {1, 3, 5, 7} = {1, 3, 5}.
12
(A ∪ B)' = {9, 10} and A' ∩ B' = {9, 10} — De Morgan's Law holds.
Left side: A ∪ B = {1, 2, 3, 4, 5, 6, 7, 8}, so (A ∪ B)' = {9, 10}. Right side: A' = {6, 7, 8, 9, 10}, B' = {1, 2, 3, 9, 10}, so A' ∩ B' = {9, 10}. Both sides are equal — this identity is called De Morgan's Law and always holds for any sets.
13
(a) 39 students study at least one subject. (b) 1 student studies none. (c) 26 students study exactly one subject.
(a) |M ∪ S ∪ R| = 22 + 18 + 15 − 8 − 6 − 5 + 3 = 39. (b) Students studying none = 40 − 39 = 1. (c) M only = 22 − 8 − 6 + 3 = 11. S only = 18 − 8 − 5 + 3 = 8. R only = 15 − 6 − 5 + 3 = 7. Exactly one = 11 + 8 + 7 = 26. Verification: exactly two (not three) = (8−3) + (6−3) + (5−3) = 5 + 3 + 2 = 10. Total = 26 + 10 + 3 = 39 ✔.
14
Prints: 1
The second add(3) is ignored — 3 already existed in the 'numbers' set.
17
REFERENCES

References

Sources used to build this material

01
Set Operations (Union, Intersection, Complement, Difference)
Comprehensive set operations reference with formal definitions
Accessed: 21 March 2026
02
Union, Intersection, and Complement — Mathematics for the Liberal Arts
Beginner-friendly explanations with visual examples
Accessed: 21 March 2026
03
Set Operations — GeeksforGeeks
Set operations with formulas, properties, and worked examples
Accessed: 21 March 2026
04
Sets and Venn Diagrams — Math is Fun
Interactive Venn diagram tool with visual explanations
Accessed: 21 March 2026
05
Sets and Venn Diagrams — AMSI Teacher Modules
Comprehensive teaching module on sets and Venn diagrams
Accessed: 21 March 2026
06
Set Operations with Venn Diagrams — Mometrix
Video + text tutorial on set operations visualization
Accessed: 21 March 2026