Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 38 · Sets and Operations

Symmetric Difference and Venn Diagrams

Learn the symmetric difference of sets, its algebra (it behaves like XOR), and how to read and draw Venn diagrams for two and three sets.

So far the operations were built from "and", "or" and "not". This lesson adds a fourth, the symmetric difference, which means "one or the other but not both". It turns out to have beautiful algebra, and it is the set version of the XOR operation in computing. We then use Venn diagrams to organize all the regions of two and three sets.

Symmetric difference

The symmetric difference of A and B, written A △ B (or A ⊕ B), contains the elements that are in exactly one of the two sets:

Text
A △ B = (A \ B) ∪ (B \ A) = (A ∪ B) \ (A ∩ B)

Example. With A = {1, 2, 3, 4} and B = {3, 4, 5, 6}, we get A △ B = {1, 2, 5, 6}. The shared elements 3 and 4 are removed.

You can see it as a truth table, where 1 means "is in the set":

In A In B In A △ B
0 0 0
0 1 1
1 0 1
1 1 0

That is exactly the XOR (exclusive or) table, so the algebra of △ matches the algebra of XOR.

Algebra of the symmetric difference

For all sets A, B, C:

  • Commutative: A △ B = B △ A.
  • Associative: (A △ B) △ C = A △ (B △ C).
  • Identity: A △ ∅ = A.
  • Self-inverse: A △ A = ∅. Doing it twice cancels: (A △ B) △ B = A.
  • Distributes over intersection: A ∩ (B △ C) = (A ∩ B) △ (A ∩ C).

Because of associativity, A △ B △ C needs no brackets; it contains the elements that belong to an odd number of the three sets (one or all three). Because of the self-inverse property, △ can "undo" itself, which is how a simple parity or checksum works.

Python Symmetric difference as XOR

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Venn diagrams

A Venn diagram draws each set as a closed curve (usually a circle) inside a rectangle that stands for the universal set. Points inside a curve belong to that set. Two circles that overlap make four regions; three circles that overlap in every possible way make eight regions, including the outside.

In general, n sets drawn in the most general way create 2ⁿ regions, one for each in/out pattern: this is the same count as the number of subsets of an n-element set, and for the same reason.

For three sets A, B, C each region corresponds to a pattern of membership:

In A In B In C Region description
1 0 0 only A
0 1 0 only B
0 0 1 only C
1 1 0 A and B but not C
1 0 1 A and C but not B
0 1 1 B and C but not A
1 1 1 all three
0 0 0 none (outside all circles)
flowchart TB
    U["Universe: 8 regions"] --> S1["only one set: 3 regions"]
    U --> S2["exactly two sets: 3 regions"]
    U --> S3["all three: 1 region"]
    U --> S0["none: 1 region"]

How the 2³ = 8 regions of a three-set Venn diagram group by how many sets they belong to.

Using a Venn diagram to solve a problem

Fill the diagram from the inside out: start with the region where all sets meet, then the regions where two meet, then the single-set regions, then the outside.

Example. In a group of 50 learners, 28 study Math, 24 study Physics and 20 study Chemistry. 12 study Math and Physics, 9 study Physics and Chemistry, 10 study Math and Chemistry, and 5 study all three. How many study none of the three?

Start in the middle: 5 take all three. "Math and Physics" is 12 in total, so 12 − 5 = 7 take exactly Math and Physics. Similarly 9 − 5 = 4 take exactly Physics and Chemistry, and 10 − 5 = 5 take exactly Math and Chemistry. Only Math: 28 − 7 − 5 − 5 = 11. Only Physics: 24 − 7 − 4 − 5 = 8. Only Chemistry: 20 − 4 − 5 − 5 = 6. The union has 11 + 8 + 6 + 7 + 4 + 5 + 5 = 46 learners, so 50 − 46 = 4 study none.

The program below checks this arithmetic by building the regions directly.

Python Counting the eight regions

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Drawing the diagram and writing a number in each region is the safest approach. Do not try to carry a whole inclusion-exclusion calculation in your head until you have practiced the picture.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What is A △ B for A = {1, 2, 3} and B = {2, 3, 4}?

    Only 1 (in A only) and 4 (in B only) are in exactly one set.
  2. 2. What is (A △ B) △ B?

    The symmetric difference is self-inverse: applying △ B twice cancels out.
  3. 3. How many regions does a general three-set Venn diagram have, counting the outside?

    Each region is an in/out pattern for three sets: 2 x 2 x 2 = 8.
  4. 4. Which element belongs to A △ B △ C when it is in A and B but not C?

    A △ B △ C contains exactly the elements that lie in an odd number of the sets.

Summary

  • A △ B holds the elements in exactly one of A and B; it equals (A \ B) ∪ (B \ A).
  • It behaves like XOR: commutative, associative, A △ ∅ = A, A △ A = ∅.
  • A △ B △ C is the set of elements in an odd number of the sets.
  • n sets in a general Venn diagram give 2ⁿ regions; fill them from the inside out.

Next: Laws of Set Algebra and De Morgan