CS112 Week3
CS112 Week3
Sets
Sets
Definition:
Any collection of distinct “things” (elements or
members) is a set.
Examples:
P = {Alice, Bob, Diane, Eve, Frank, George}
D={ }
3
Sets
Definition:
Sets that have the same elements are “equal”.
Note 1:
Order of the set’s elements is not important!
i.e., { }={ }
Note 2:
Repetition of the set’s elements is not important!
i.e., { }={ }
4
Sets
Is the set {{1, 2}, {0}, {2,1}} equal to the set
{{0}, {1, 2}}?
YES or NO ?
5
Sets
R = { real numbers} Q = { rational numbers}
Z = { integers } Z += { non-negative integers }
N = { natural numbers }
N0 = { natural numbers including 0 }
∅ = empty set = { }
Notation:
The sign ∈ is used to indicate element of a set
Example:
1 ∈ Z or 1 ∈ N but ½ ∉ N
6
Notation: Abstraction
Instead of listing the elements of a set explicitly,
we can use the notation:
7
Abstraction
{x | P(x) } = {y | P(y)}, i.e., abstraction does not
depend on the variable x or y.
8
Set Abstraction
{n2 | n ∈ {1,…,999}} = {n2 | n ∈ N and n < 1000}
9
Subsets
Definition:
A set A is called a subset of a set B if every
element of a A is also an element of B.
Notations:
The sign ⊂ is used to indicate “proper subset of ”,
if all elements of B are also elements of A (A = B),
then ⊆ is used.
Note: ∅ is a subset of all sets.
Examples:
B⊂P
∅ ⊂ N ⊆ Z+ ⊂ Z ⊂ Q ⊂ R
10
Subsets
Theorem:
1. A ⊆ A (reflexivity)
2. If A ⊆ B and B ⊆ A, then A = B (antisymmetry)
3. If A ⊆ B and B ⊆ C, then A ⊆ C (transitivity)
11
in keyword for Python
The in keyword checks whether an object is an element of a list.
This is the closest operation to ‘∈’ of the set theory.
12
Set Operations – Algebra of Sets
A ∩ B: intersection; A∩B = ∅ ⇒A & B are disjoint
A∪B: union
A \ B (or A – B): difference
A ∆ B: symmetric difference ≜ (A ∪ B) – (A ∩ B)
A’ : A complement ≜ U - A where U is the Universe.
∩ and ∪ are commutative and associative operators
i.e,, A∪B = B∪A; A∪(B∪C) = (A∪B)∪C
A∩B = B∩A; A∩(B∩C) = (A∩B)∩C
Also, they are distributive over each other.
i.e,, A∪(B ∩ C) = (A∪B)∩(A∪C)
A∩(B ∪ C) = (A ∩ B)∪(A ∩ C)
With ∩, ∪ and complement operations, and ∅ and U
as the identity elements, sets define an algebraic
field. 13
Addition Rule:
A B A B
C C
15
Addition Rule:
>>> s1 == s2
True
17
Union (∪) and Intersection (∩)
Union in Python:
>>> s1 = {"Russell", 1, 'a', 2.1, 'a'}
>>> s2 = {"a", 2.1, 'CS112'}
>>> s3 = s1.union(s2)
>>> s3
{1, 2.1, 'CS112', 'Russell', 'a'}
>>> s4 = s1.intersection(s2)
>>> s4
{2.1, 'a'}
18
List of all subsets (powerset)
def powerset(s):
x = len(s)
ls = list(s)
lsub = []
for i in range(1 << x):
lsub.append({ls[j] for j in range(x) if (i & (1 <<
j))})
return(lsub)
print(powerset({1,2,3}))
[set(), {1}, {2}, {1, 2}, {3}, {1, 3}, {2, 3}, {1, 2,
3}]
19
Cross Product
Cross product of two lists setA and setB can be
implemented as:
{(i,j) for i in setA for j in setB}
setA = {1, 2, 3}
setB = {'a', 'b'}
print(cross_product)
{(2, 'a'), (2, 'b'), (3, 'a'), (3, 'b'), (1, 'b'), (1,
'a')}
20
Cardinality (size) of a set
Definition:
The number of elements of a set is its cardinality.
Notation:
Signs | | are used to denote cardinality.
Examples:
| P | = 6, | ∅ | = 0, | Z | = ∞, |R| = ∞
21
Chapter 1: roadmap
1.1 Real life examples – A Party
1.2 Introduction to Python Programming Language
1.3 Sets and the Like
1.4 Addition Rule
1.5 The Number of Subsets
1.6 Correspondence Principle
1.7 Choice trees & Product Rule
1.8 Permutations
1.9 The Number of Ordered Subsets
2.0 The Number of Subsets of a Given Size
Addition Rule:
If A ∩ B = ∅ (i.e., if A & B are disjoint),
Corollary:
If A1, A2,…,An are disjoint,
n n
|∪
i=1
Ai | = ∑
i=1
|Ai|
23
Using the addition rule
Let’s look at some combinatorics examples.
Let’s roll a black dice and a white dice…
Let’s count some outcomes…
|S|=?
a. 30
b. 12
c. 10
d. 36
Let’s count some outcomes…
|S|=?
A6 = , , , ,
| A6 | = 5
Let’s count some outcomes…
S = A1∪A2∪A3∪A4∪A5∪A6
6 6 6
S A i A i 5 30
i 1 i=1 i=1
Another solution
S ≜ {outcomes where the dice show different values}
|S|=?
T ≜ { outcomes where dice agree }
S T # of outcomes 36
S T 36 T 6
S 36 6 30
With Python
dice = {1, 2, 3, 4, 5, 6}
setAll= {(i,j) for i in dice for j in dice}
print(setAll)
{(3, 4), (4, 3), (3, 1), (5, 4), (4, 6), (5, 1), (2, 2),
(1, 6), (2, 5), (1, 3), (6, 2), (6, 5), (4, 2), (4, 5), (3,
3), (5, 6), (3, 6), (5, 3), (2, 4), (1, 2), (2, 1), (1, 5),
(6, 1), (6, 4), (3, 2), (4, 1), (3, 5), (5, 2), (4, 4), (5,
5), (1, 1), (1, 4), (2, 3), (2, 6), (6, 6), (6, 3)}
dice = {1, 2, 3, 4, 5, 6}
setS= {(i,j) for i in dice for j in dice if i!=j}
print(len(setS))
setT =setAll.difference(setS)
print(setT)
{(4, 4), (5, 5), (1, 1), (3, 3), (2, 2), (6, 6)}
A somewhat more difficult one…
S A 1 A 2 A 3 A 4 A 5 A 6
S 5 4 3 2 1 0 15
Addition Rule
35
Dice revisited
S ≜ { outcomes where the black dice shows a
smaller number than the white dice }
|S| = ?
Therefore S = 15
It is clear by symmetry that S = L ?
Pinning down the idea of symmetry by exhibiting a
correspondence
S L
1-1 Onto Correspondence (bijection or
just “correspondence” for short)
A B
Correspondence Principle
It’s one of
the most
important
mathematic
al ideas of
all time!
Chapter 1: roadmap
1.1 Real life examples – A Party
1.2 Introduction to Python Programming Language
1.3 Sets and the Like
1.4 Addition Rule
1.5 Correspondence Principle
1.6 Choice trees & Product Rule
1.7 The Number of Subsets
1.8 Permutations
1.9 The Number of Ordered Subsets
2.0 The Number of Subsets of a Given Size
Strings of different objects
I own 3 beanies and 2
ties. How many
different ways can I
dress up in a beanie
and a tie?
a. 6
b. 5
c. 9
d. 12
Strings of different objects
Add or multiply?
A restaurant has a menu with
5 appetizers, 6 entrees, 3 salads, and 7 desserts.
How many items on the menu?
a. 630
b. 21
c. 315
d. 20
Add or multiply?
A restaurant has a menu with
5 appetizers, 6 entrees, 3 salads, and 7 desserts.
How many ways to choose a complete meal?
a. 630
b. 21
c. 315
d. 20
Strings of different objects
A restaurant has a menu with
5 appetizers, 6 entrees, 3 salads, and 7 desserts.
6 * 7 * 4 * 8 = 1344
Choice Trees
S ={ , , , , , }
A choice tree provides a “choice tree representation” of a set S, if
1) Each leaf label is in S, and each element of S is some leaf label
2) No two leaf labels are the same
Leaf Counting Lemma
Let T be a depth n tree when each node
at depth 0 i n-1 has Pi+1 children. The
number of leaves of T is given by:
P1P2…Pn
We will now combine
the correspondence
principle with the leaf
counting lemma to
make a powerful
counting rule for
choice tree
representation.
Product Rule
IF a set S has a choice tree representation with
P1 possibilities for the first choice,
P2 for the second, and so on,
THEN
there are P1P2P3…Pn elements in S
52 * 51 * 50 * … * 3 * 2 * 1 = 52!
52 “factorial” orderings
Chapter 1: roadmap
1.1 Real life examples – A Party
1.2 Introduction to Python Programming Language
1.3 Sets and the Like
1.4 Addition Rule
1.5 Correspondence Principle
1.6 Choice trees & Product Rule
1.7 The Number of Subsets
1.8 Permutations
1.9 The Number of Ordered Subsets
2.0 The Number of Subsets of a Given Size
Selecting from different objects
Hobson’s restaurant has only 1 appetizer, 1
entree, 1 salad, and 1 dessert.
A A A A A A A A
E E E E E E E E
S S S S S S S S
D D D D D D D D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
59
Selecting from different objects
Appetizer
Y N P1 = 2 (i = 0)
Entree Entree
Y N Y N P2 = 2 (i = 1)
Depth = 4
Salad Salad Salad Salad
Y N Y N Y N Y N P3 = 2 (i = 2)
A A A A A A A A 2x2x2x2=16
E E E E E E E E leaves
S S S S S S S S
D D D D D D D D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
60
The Number of Subsets
What is the number of subsets of a set with
n elements?
∅ has only 1 subset, itself: ∅⊆∅
{a} has 2: ∅ and {a}
{a, b} has 4: ∅, {a}, {b}, {a,b}
{a, b, c} has 8: ∅,{a},{b},{c},{a,b},{a,c},{b,c},{a,b,c}
A Python computation:
>>>len(powerset(set(range(1,11))))
1024
61
The Number of Subsets
Theorem: A set with n elements has 2n subsets.
a∈ S
Y N
b∈ S b∈ S
Y N Y N
c ∈S c ∈S c ∈S c ∈S
Y N Y N Y N Y N
62
A (one-to-one) relation
between:
n-bit binary numbers,
e.g., 011010 (6 bits)
&
subsets of an n-element set,
e.g. {a,b,c,d,e,f} (6
elements)
Mapping Subsets to Numbers
Construct an n-bit binary number by assigning a 1 to those
elements that belong to a subset, and 0 to the others. The
corresponding subset’s order will be the resulting binary
number!
64
Mapping Subsets to Numbers
What is the 512th subset of {a11,a10,a9,a8,a7,a6,a5,a4,a3,a2,a1}?
a. {a1}
b. {a2}
c. {a3}
d. {a10}
e. {a11}
65
Mapping Subsets to Numbers
Note that here the subscripts of a’s are not important. The
set with 11 members can be {a,b,c,d,e,f,g,h,I,j,k} instead
of {a11,a10,a9,a8,a7,a6,a5,a4,a3,a2,a1}. The subset number
512 of this set will be {b} corresponding to the binary
number: 010 0000 00002 = 2910 = 51210
66
Bijection
There is a one-to-one correspondence
(bijection) between the subsets of an n
element set and n-bit binary numbers.
0 1
0 1 0 1
0 1 0 1 0 1 0 1
0 1 0 1
0 1 0 1 0 1 0 1
0 1
0 1 0 1
0 1 0 1 0 1 0 1