Set Theory
We can define sets in 3 ways.
-
Listing elements.
-
Set Builder notation (set comprehension).
-
A list with implied pattern.
Common Sets
Description | Notation |
---|---|
Integers | |
Positive Integers | |
Non-Negative Integers | |
Rationals | |
Real Numbers | |
Set of Numbers. 1 through | |
Empty Set (no elements) |
0 is not positive.
Membership
How to check whether an element is in a set ?
- Checking whether in the list, if is given explicitly.
- Checking whether satisfies the conditions, if is given in Set Builder Notation.
- Checking whether satisfies the implied condition, for sets given like
Membership Examples:
- because 12 is an integer & 12 divides 60.
- the implied condition is "odd numbers".
implied conditions are bad!
Equality of Sets
Two sets are considered to be equal if they contain the same elements.
e.g. , when:
- Every element is in . aka. Every element of is also in .
- Every element is in . aka. Every element of is also in .
Another equivalent definition using subset:
Are these the same?
- is the same set as (order doesn't matter).
- is the same set as (don't count multiples).
- is the same as .
Size of Sets
The size of a set is denoted as which is the number of distinct elements in the set.
Subset
is a subset of , if every is in .
is a proper subset of , if is a subset of and not equal to .
The set of all subsets of a set is called the power set of , written .
- The empty set is a subset of all set.
- The set itself is also a subset of itself.
Set Builder Notation
There are two general forms that we can use for Set Builder Notation.
Set Comprehension
Specify a base set, and test if the element match the condition. (Array.filter()
)
Replacement
Apply a function to each member of a set and collect the results. (like Array.map()
)
These two constructions cannot generate sets than are larger than the original sets.
Set Builder Examples
-
is every integer that is an integer multiples by 2, i.e. the even numbers.
-
is every integer times 2. i.e. the even numbers.
-
are the real numbers that becomes integer after multiplying by 2.
i.e.
Set Operations
Set operations consists of union, intersection, and difference.
Union
Include every element in both sets.
The real definition: we define to be the smallest set such that and .
Intersection
Only include elements that exist in the both sets.
Difference
Remove elements in that are in .
Set and . Then:
Universe Set
The universe set is the set of all elements that we care about.
Benefits:
- Allow us to define complements.
- Allow us to use set comprehensions without specifying the set to draw elements from.
With :
Venn Diagram
Venn diagrams are used for showing the relationship between sets.
For more about Venn diagrams, visit Math is Fun or lecture notes
Characteristic Vector
Sets in Python
S = {1, 2, 3}
T = {1, 3, 5}
S.add(4)
S.remove(4)
S.union(T)
S.intersection(T)
S.issubset({1, 2})
print(1 in S)
print(S - T) # { 2 }
print(T <= S) # Is T a subset of S?
print(S.isdisjoint(T)) # Is S intersect T empty?
Python uses a combined comprehension and replacement notation for Set Builder notation.
S = {0, 2, 4, 6, 8}
def filter(x): return x % 2 == 0
def transform(x): return x * 5
new_S = { transform(x) for x in S if filter(x) }
temp_S = { x for x in range(0, 10) if x % 2 == 0 } # {0, 2, 4, 6, 8}
new_S = { x * 5 for x in range(0, 10) if x % 2 == 0 } # {0, 40, 10, 20, 30}