Chuyển đến nội dung

Bài 6 · Sets and Operations

Complement and Difference

Learn set difference A \ B, the complement of a set inside a universal set, and how they relate to union and intersection, with worked examples and Python.

Union and intersection combine sets. The operations in this lesson remove elements: the difference takes away one set from another, and the complement takes a set away from the whole universe of discussion. Together with union and intersection they let you describe nearly any region of a Venn diagram.

Difference

The difference A \ B (also written A − B) is the set of elements that are in A but not in B:

Text
A \ B = { x : x ∈ A and x ∉ B }

Example. If A = {1, 2, 3, 4, 5} and B = {4, 5, 6}, then A \ B = {1, 2, 3} and B \ A = {6}. The difference is not symmetric: A \ B and B \ A are usually different sets. You do not need B ⊆ A; elements of B that are not in A are simply ignored.

Useful facts:

  • A \ B ⊆ A.
  • A \ B and B are always disjoint.
  • A \ B = ∅ exactly when A ⊆ B.
  • A \ A = ∅ and A \ ∅ = A.

Complement

Suppose all sets live inside a universal set U. The complement of A, written Aᶜ (or A' or Ā), is everything in U that is not in A:

Text
Aᶜ = U \ A = { x ∈ U : x ∉ A }

Example. Let U = {1, 2, ..., 10} and A = {2, 4, 6, 8, 10}. Then Aᶜ = {1, 3, 5, 7, 9}.

The complement depends on the universe. The complement of the set of even integers is the set of odd integers if U = ℤ, but it also contains every non-integer if U = ℝ. Always say what U is.

Basic properties:

Statement Reading
(Aᶜ)ᶜ = A complementing twice gives back A
A ∪ Aᶜ = U A and its complement cover the universe
A ∩ Aᶜ = ∅ A and its complement never overlap
∅ᶜ = U and Uᶜ = ∅ the extreme cases

The difference can be written with the complement: A \ B = A ∩ Bᶜ. In words, "in A but not in B" equals "in A and in the complement of B".

flowchart TB
    U["Universe U"] --> A["A"]
    U --> AC["Aᶜ = U minus A"]

The universe splits into A and its complement. They share no element, and together they fill U.

Python Difference and complement

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.

Complement and subsets

Taking complements reverses inclusion: if A ⊆ B then Bᶜ ⊆ Aᶜ. Proof: let x ∈ Bᶜ. If x were in A, it would be in B (because A ⊆ B), contradicting x ∈ Bᶜ. So x ∉ A, that is, x ∈ Aᶜ.

This reversal is the idea behind the contrapositive in logic. "If an element is in A it is in B" is equivalent to "If it is not in B it is not in A".

Regions of a Venn diagram

Two overlapping sets split the universe into four regions. Each region has a short formula:

Region Formula Example with U = {1..8}, A = {1,2,3,4}, B = {3,4,5,6}
only in A A ∩ Bᶜ = A \ B {1, 2}
in both A ∩ B {3, 4}
only in B B ∩ Aᶜ = B \ A {5, 6}
in neither Aᶜ ∩ Bᶜ {7, 8}

The four regions are disjoint and their union is U, so their sizes add to |U|. This is the bookkeeping behind many survey problems: "In a class of 30, 18 play football, 12 play tennis, and 5 play both. How many play neither?" The union has 18 + 12 − 5 = 25 students, so 5 play neither.

Python The four 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.

A word on notation

In plain text the complement is awkward to type, so many calculations write A' or not A. In code we use U - A. Whatever the notation, the meaning is always "relative to the universal set".

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. If A = {1, 2, 3, 4, 5} and B = {4, 5, 6}, what is A \ B?

    Remove from A everything that is in B: 4 and 5 go, leaving 1, 2, 3.
  2. 2. Which expression equals A \ B?

    'In A and not in B' means in A and in the complement of B.
  3. 3. If A ⊆ B, which statement is true?

    Complements reverse inclusion. Whatever is outside the bigger set B is also outside the smaller set A.
  4. 4. In a class of 40, 25 take French, 20 take Spanish and 8 take both. How many take neither?

    The union has 25 + 20 - 8 = 37 students. 40 - 37 = 3 take neither.

Summary

  • A \ B keeps the elements of A that are not in B; it is not symmetric.
  • Aᶜ = U \ A depends on the chosen universe U.
  • A \ B = A ∩ Bᶜ; A ∪ Aᶜ = U; A ∩ Aᶜ = ∅; (Aᶜ)ᶜ = A.
  • Complements reverse inclusion.
  • Two sets split the universe into four disjoint regions.

Next: Symmetric Difference and Venn Diagrams

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.