Mathematical Foundations of Computing
Unit 4
SETS
What is a set?
A set is a unordered collection of “objects”
People in a class: {Ahmed, Omar, Samy }
Cities in Jordan: {Amman, Tafila, Irbid, … }
Sets can contain non-related elements: {3, a, red, Sameer}
We will most often use sets of numbers
All positive numbers less than or equal to 5:
{1, 2, 3, 4, 5}
A few selected real numbers:
{ 2.1, π, 0, -6.32, e }
2
Properties of sets
Order does not matter
{1, 2, 3, 4, 5} is equivalent to {3, 5, 2, 4, 1}
Sets do not have duplicate elements
Consider the list of students in a class
It does not make sense to list somebody twice
Specifying a Set (i)
Capital letters (A, B, S…) for sets
Italic lower-case letter for elements (a, x, y…)
1) Easiest way: list all the elements (roster method)
A = {1, 2, 3, 4, 5}, Not always feasible!
2) Using ellipsis (…)
B = {0, 1, 2, 3, …}
May cause confusion. C = {3, 5, 7, …}. What’s next?
Answer:
If the set is all odd integers greater than 2, it is 9
If the set is all prime numbers greater than 2, it is 11
3) Using the set-builder notation
D = {x | x is prime and x > 2}
E = {x | x is odd and x > 2}
The vertical bar means “such that”
4
Specifying a Set (ii)
A set “contains” the various “members” or “elements”
that make up the set
If an element a is a member (element) of a set S, we use then
notation
aS
4 {1, 2, 3, 4}
If not, we use the notation
aS
7 {1, 2, 3, 4}
Often Used Sets
N = {0, 1, 2, 3, …} is the set of natural numbers
Z = {…, -2, -1, 0, 1, 2, …} is the set of integers
Z+ = {1, 2, 3, …} is the set of positive integers (whole numbers)
R is the set of real numbers (it includes all the whole numbers,
rational numbers, irrational numbers such as √2 (1.41421356...)
What is Q ? If Q = {p/q | p Z, q Z, q ≠ 0}
Q is the set of rational numbers
Any number that can be expressed as a fraction of two integers
(where the bottom one is not zero)
6
The Universal Set
U is the universal set – the set of all of elements
(or the “universe”) from which any given set is
drawn
For the set {-2, 0.4, 2}, U would be
the real numbers (R)
For the set {0, 1, 2}, U could be
the N, Z, Q, R depending on the context
For the set of the vowels of the alphabet, U would be
all the letters of the alphabet
Venn Diagrams
Represents sets graphically
The rectangle represents the universal set
Circles represent the set(s)
Consider set V, which is the set of all vowels in the alphabet
The individual elements
are usually not written b c d f
in a Venn diagram g h j
k l m
n p q a e i
r s t o u
v w x
y z
8
Sets of Sets
Sets can contain other sets
S = { {1}, {2}, {3} }
T = { {1}, {{2}}, {{{3}}} }
V = { {{1}, {{2}}}, {{{3}}}, { {1}, {{2}}, {{{3}}} } }
How many elements does the set V have ?
only 3 elements!
Note that 1 ≠ {1} ≠ {{1}} ≠ {{{1}}}
They are all different
The Empty Set and Singleton
If a set has zero elements (no elements), it is
called the empty (or null) set
Written using the symbol
Thus, = { } VERY IMPORTANT
It can be an element of other sets
{ , 1, 2, 3, x } is a valid set
Singleton: A set with one element
≠ { } why ?
The first is a set of zero elements (Empty)
The second is a set of 1 element (Singleton)
Replace by { }, and you get: { } ≠ {{ }}
It’s easier to see that they are not equal that way
10
Set Equality, Subsets
Two sets are equal if and only if they have the same elements
x (x A x B)
{1, 2, 3, 4, 5} = {5, 4, 3, 2, 1}
{1, 2, 3, 2, 4, 3, 2, 1} = {4, 3, 2, 1}
Two sets are not equal if they do not have the same elements
{1, 2, 3, 4, 5} ≠ {1, 2, 3, 4}
If all the elements of a set S are also elements of a set T, then S is a
subset of T
If S = {2, 4, 6}, T = {1, 2, 3, 4, 5, 6, 7}, S is a subset of T
This is specified by S T meaning that x (x S x T)
For any set S, S S (S S S) [any set is a subset of itself]
For any set S, S (S S) [the null set is a subset of any
other set]
11
Proper Subsets
If S is a subset of T, and S is not equal to T, then S is a
proper subset of T
A proper subset is written as S T
Can be written as: R T and R ≠ T
Let T = {0, 1, 2, 3, 4, 5}
If S = {1, 2, 3},
S is not equal to T, and S is a subset of T
Let Q = {4, 5, 6}. Q is neither a subset of T nor a proper
subset of T. WHY?
The difference between
“subset ” and “proper subset ”
is like the difference between
“less than or equal to ” and “less than <” for numbers
12
Example: Answer the following questions by Yes or No
{1, 2, 3}?
Yes
{1, 2, 3}?
No
{, 1, 2, 3}?
Yes
{, 1, 2, 3}?
Yes
{x} {x}?
No
Slide 13
Set Cardinality
The cardinality of a set is the number of elements
in a set, written as |A|
Examples
Let R = {1, 2, 3, 4, 5}. What’s |R|
|R| = 5
|| =
0
Let S = {, {a}, {b}, {a, b}}. What’s |S|
Then |S| = 4
14
Power Sets
Given the set S = {0, 1}. List all the possible subsets of S?
(as it is a subset of all sets), {0}, {1}, and {0, 1}
The power set of S (written as P(S)) is the set of all the subsets
of S
P(S) = { , {0}, {1}, {0,1} }
Note that |S| = 2 and |P(S)| = 4
If a set has n elements, then the power set will have 2n elements
Let set T = {0, 1, 2}. What is P (T)?
P(T) = { , {0}, {1}, {2}, {0,1}, {0,2}, {1,2}, {0,1,2} }
Note that |T| = 3 and |P(T)| = 8
What’s P()
P() = { }
Note that || = 0 and |P()| = 1
15
Cartesian Product
A Cartesian product is a set of all ordered 2-tuples where each “part” is from
a given set.
Denoted by A x B, and uses parenthesis (not curly brackets)
Formal definition of a Cartesian product:
A x B = { (a,b) | a A and b B }
Example: Given A = { a, b } and B = { 0, 1 }, what is their
Cartesian product (A x B)?
A x B = { (a,0), (a,1), (b,0), (b,1) }
NOTE THAT A x B ≠ B x A
Since B x A = { (0, a), (0, b), (1, a), (1, b) }
Note that |A x B|= |A| x |B|
16
Cartesian Product of many sets
The Cartesian product of the sets A1, A2, ... , An is
A1 A2 … An = {(a1, a2, ... , an) | ai Ai for i = 1,2, ...,n}
Example
A = {0, 1}
B = {1, 2}
C = {0, 1 2}
ABC={ (0, 1, 0), (0, 1, 1), (0, 1, 2),
(0, 2, 0), (0, 2, 1), (0, 2, 2),
(1, 1, 0), (1, 1, 1), (1, 1, 2),
(1, 2, 0), (1, 2, 1), (1, 2, 2) }
17
Using Set Notation with Quantifiers
x S ( P( x)) denotes the universal quantification P(x)
over all elements in the set S
Shorthand for x( x S ( P( x))
x S ( P( x)) is shorthand for x( x S P( x))
What does this mean? x R( x 2 0)
The square of every real number is non-negative
What does this mean? x Z ( x 2 1)
There is an integer whose square is 1
18
Set Operations: Union
Formal definition for the union of two sets:
AUB={x|xA˅xB}
Further examples
{1, 2, 3} U {3, 4, 5} =
{1, 2, 3, 4, 5}
{a, b} U {3, 4} =
{a, b, 3, 4}
{1, 2} U =
{1, 2}
19
Properties of the union operation
AU =
A Identity law
AU U=
U Domination law
AU A=
A Idempotent law
AU B=BUA Commutative law
AU (B U C) = (A U B) U C Associative law
=AUBUC
20
Set Operations: Intersection
Formal definition for the intersection of two sets:
A∩B={x|xA˄xB}
Examples
{1, 2, 3} ∩ {3, 4, 5} =
{3}
{a, b} ∩ {3, 4} =
{1, 2} ∩ =
21
Properties of the intersection operation
A∩U=
A Identity law
A∩=
Domination law
A∩A=
A Idempotent law
A∩B=B∩A Commutative law
A ∩ (B ∩ C) = (A ∩ B) ∩ C = A ∩ B ∩ C Associative law
22
Disjoint Sets
Formal definition for disjoint sets:
two sets are disjoint if their intersection is the empty
set (have no common elements)
Example: which of the following sets are disjoint ?
{1, 2, 3} and {3, 4, 5} are
not disjoint
{a, b} and {3, 4} are
disjoint
{1, 2} and are
Disjoint because their intersection is the empty set
and are
disjoint! because their intersection is the empty set
23
Set Operations: Difference
Formal definition for the difference of two sets:
A-B={x|xA˄xB}
In other words, A – B is the set containing those
elements that are in A but not in B
Example: find the difference of the following sets
{1, 2, 3} - {3, 4, 5} =
{1, 2}
{a, b} - {3, 4} =
{a, b}
{1, 2} - =
{1, 2}
The difference of any set S with
the empty set will be the set S
24
Complement Sets
Formal definition for the complement of a set:
A = Ac = { x | x A }
Or Ac = U – A, where U is the universal set
Example:
Assuming U = Z, find:
{1, 2, 3}c =
{ …, -2, -1, 0, 4, 5, 6, … }
{a, b} =
c
Z
Let U is the set of all positive integers and A = {x | x > 10} find AC
Ā = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
25
Properties of complement sets
(Ac)c =
A Complementation law
A U Ac =
U Complement law
A ∩ Ac =
Complement law
26
Symmetric difference
Symmetric Difference of two sets A & B is the set of
elements that belong to either set A or set B but not
both.
AB = {x | (x Є A) (x Є B)} .
It can be expressed also in the following way:
A B = (A U B) − (A ∩ B)
= (A − B) U (B − A)
27
Set identities
A = A AU = U
Identity Law Domination law
AU = A A =
AA = A Idempotent
(Ac)c = A Complement Law
AA = A Law
AB = BA Commutative (AB)c = AcBc
De Morgan’s Law
AB = BA Law (AB)c = AcBc
A(BC) A(BC) =
= (AB)C Associative (AB)(AC)
Distributive Law
A(BC) Law A(BC) =
= (AB)C (AB)(AC)
A(AB) = A Absorption A Ac = U
Complement Law
A(AB) = A Law A Ac =
A - B = A Bc
28
How to Prove a Set Identity
Four methods:
1. Use the set identities
2. Prove each set is a subset of each other
3. Use set builder notation and logical equivalences
4. Use membership tables
29
What we are going to prove?
A∩B=B-(B-A)
A B
A∩B
B-(B-A) B-A
30
Proof by Set Identities
Use set identities to proof that A B = A - (A - B)
Proof:
A - (A - B)
= A - (A Bc)
= A (A Bc)c
= A (Ac B)
= (A Ac) (A B)
= (A B)
=AB
31
Showing each set is a subset of the others
Prove that: (A B)c = Ac Bc
x (A B)c
x (A B)
(x A B)
(x A x B)
(x A) (x B)
xAxB
x A c x Bc
x A c Bc
32
Proof by Builder Notation
Prove A B A B using builder notation
A B {x | x A B} (def of complement)
{x | ( x ( A B ))} (def of not belong to)
{x | ( x A x B )} (def of intersecti on)
{x | ( x A) ( x B )} (De Morgan' s law)
{x | x A x B} (def of not belong to)
{x | x A x B} (def of complement)
{x | x A B} (def of union)
A B
33
Proof by Membership Table
To indicate that an element is in a set, a 1 is used
To indicate that an element is not in a set, a 0 is used
Example: Use the membership table to proof that
A(BC) = (AB)(AC)
34
Generalized Union and Intersection
A={0,2,4,6,8}, B={0,1,2,3,4}, C={0,3,6,9}
A⋃B⋃C=
{0,1,2,3,4,6,8,9}
A⋂B⋂C=
{0}
35
General Cases
n
A1 A2 An Ai
Union: i 1
n
Intersection A1 A2 An Ai
i 1
Union: A1 A2 An Ai
i 1
Intersection: A1 A2 An Ai
i 1
Example: Suppose Ai={1, 2, 3,…, i} for i=1,2,3,… Find:
36
Computer Representation of Sets
We will present a method for storing elements using an
arbitrary ordering of the elements of the universal set.
This method of representing sets makes computing
combinations of sets easy.
If U is a finite set, then the number of elements in U cannot be
larger than the memory size of the computer being used
If the ordering of the elements of U is a1, a2, ... , an, and if A is a
subset of U with the bit string of length n, then the string that
represent A has its ith bit as
1 if ai A and
0 if ai A
Slide 37
Example 1
Let U = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} and the
ordering of elements of U is in the increasing order,
that is, ai = i
What bit strings represent the following subsets of U:
1. All odd integers in U 12 3456 78910
10 1010 1010
2. All even integers in U
01 0101 0101
3. Integers not exceeding 5 in U
11 1110 0000
Slide 38
Example 1 (cont.)
1. The bit string that represents the set of odd integers
in U, namely, {1, 3, 5, 7, 9}, has bits in positions first,
third, fifth, seventh, and ninth set to 1, and to zero
elsewhere.
10 1010 1010
2. The string representing the subset of all even integers
in U, namely, {2, 4, 6, 8, 10}, is
01 0101 0101
3. The string representing the set of all integers in U that
do not exceed 5, namely, {1, 2, 3, 4, 5}, is
11 1110 0000
Slide 39
Example 2
let U = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. If the bit string for
set A = {1, 3, 5, 7, 9} is 10 1010 1010, what is the bit
string for the complement of this set?
Solution
The bit string for the complement of this set is
01 0101 0101,
which corresponds to the set {2, 4, 6, 8, 10}
Slide 40
Example 3
Let U = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. If the bit strings for
the sets {1, 2, 3, 4, 5} and {1, 3, 5, 7, 9} are 11 1110 0000
and 10 1010 1010, respectively.
Find the bit string for union and intersection of these sets.
Solution
The bit string for the union of these sets is
11 1110 0000 10 1010 1010 = 11 1110 1010,
corresponds to the set {1, 2, 3,4, 5, 7, 9}
The bit string for the intersection of these sets is
11 11100000 10 1010 1010 = 10 1010 0000,
corresponds to the set {1, 3, 5}
Slide 41