0% found this document useful (0 votes)
6 views70 pages

CS112 Week3

The document provides an introduction to sets, their definitions, and operations, including subsets, cardinality, and set algebra. It also discusses the application of sets in Python programming, demonstrating how to use sets and perform operations like union and intersection. Additionally, the document outlines combinatorial principles such as the addition rule and correspondence principle, using examples involving dice and meal combinations.

Uploaded by

peachypaimon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views70 pages

CS112 Week3

The document provides an introduction to sets, their definitions, and operations, including subsets, cardinality, and set algebra. It also discusses the application of sets in Python programming, demonstrating how to use sets and perform operations like union and intersection. Additionally, the document outlines combinatorial principles such as the addition rule and correspondence principle, using examples involving dice and meal combinations.

Uploaded by

peachypaimon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 70

Fall 2025

Prof. Dr. Hasan F. ATEŞ


Özyeğin University, İstanbul, Turkey
[email protected]
Chapter 1: roadmap

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:

S = {x ∈ U: “a property that defines x” },


where U is a larger set (“universe”). This defines
the elements by abstracting a property of them.
Examples:
Z+ = { x ∈ Z: x ≥ 0 }
B = { c ∈ P: c is a boy } = { Bob, Frank, George }

7
Abstraction
{x | P(x) } = {y | P(y)}, i.e., abstraction does not
depend on the variable x or y.

Python (with lists):


naturals = [n for n in range(1,1001)]
evens = [n for n in naturals if n%2==0]

We can also write: {f(x) | P(x) }, e.g., {2n | n∈N }


or, in Python (with lists):
evens = [2*n for n in naturals]

8
Set Abstraction
{n2 | n ∈ {1,…,999}} = {n2 | n ∈ N and n < 1000}

Python (with lists):


small_squares1 = [n**2 for n in
range(1,1000)]

small_squares2 = [n**2 for n in naturals


if n<1000]

Note: naturals = [1..]

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.

Now, see this interaction:


>>> 'R' in "Russell"
True
>>> 'R' in "Cantor"
False
>>> "Rus" in "Russell"
True
>>> [1,2] in [1,2,3]
False
>>> [1] in [1,2,3]
False

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:

ow to check correctness of complex set equatio


Venn Diagrams
A∪(B∩C) ≟ (A∪B) ∩ (A ∪ C)

A B A B

C C

15
Addition Rule:

Introducing SETS to Python…


Sets vs. Lists in Python
In Python, sets are lists where duplicates are removed
and there is no specific order of elements:
>>> s1 = {"Russell", 1, 'a', 2.1, 'a’}
>>> s1
{1, 2.1, 'Russell', 'a’}

>>> s2 = set(["Russell", 1, 'a', 2.1, 'a'])


>>> s2
{1, 2.1, 'Russell', 'a’}

>>> 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'}

cross_product = {(i,j) for i in setA for j in setB}

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| = ∞

but, |R| > |Z|!

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),

|A∪B| = |A| + |B|

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 ≜ {outcomes where the dice show different values}

|S|=?

a. 30
b. 12
c. 10
d. 36
Let’s count some outcomes…

S ≜ {outcomes where the dice show different values}

|S|=?

Ai ≜ { outcomes where the black dice says i and


the white dice says something else }
eg., black is 6, white is something else

A6 = , , , ,

| A6 | = 5
Let’s count some outcomes…

S = A1∪A2∪A3∪A4∪A5∪A6

and, Ai’s are disjoint


therefore,

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 ≜ { outcomes where the black dice shows a


smaller number than the white dice }
|S| = ?
a. 30
b. 18
c. 15
d. 12
A somewhat more difficult one…

S ≜ { outcomes where the black dice shows a


smaller number than the white dice }
|S| = ?

Ai ≜ { outcomes where the black dice says i and


the white dice says something larger }

S A 1  A 2  A 3  A 4  A 5  A 6
S 5  4  3  2  1 0 15
Addition Rule

Divide the set into “manageable” parts


• The cardinality of each part should be easy to
determine
• The number of parts should not be very large, e.g., if
each part contains only one element, we go back to
enumeration!
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
Dice revisited

35
Dice revisited
S ≜ { outcomes where the black dice shows a
smaller number than the white dice }
|S| = ?

L ≜ { outcomes where the black dice shows a


larger number than the white dice }
S + L = 30
It is clear by symmetry that S = L.

Therefore S = 15
It is clear by symmetry that S = L ?
Pinning down the idea of symmetry by exhibiting a
correspondence

Let’s put each outcome in S in


correspondence with an outcome in L
by swapping the color of the dice.

S L
1-1 Onto Correspondence (bijection or
just “correspondence” for short)

A B
Correspondence Principle

If two finite sets can be placed


into 1-1 onto correspondence,
then they have the same size.
Correspondence Principle
If two finite sets can be placed into 1-1 onto
correspondence, then they have the same size.

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.

How many ways to order a meal if I might not


have some of the courses (i.e., no appetizer, no
entrée, no salad or no desert)?
a. 630
b. 1260
c. 1344
d. 315
Strings of different objects
A restaurant has a menu with
5 appetizers, 6 entrees, 3 salads, and 7 desserts.

How many ways to order a meal if I might not


have some of the courses (i.e., no appetizer, no
entrée, no salad or no desert)?

6 * 7 * 4 * 8 = 1344
Choice Trees

A choice tree is a rooted, directed tree with


an object called a “choice” associated with
each edge and a label on each leaf.
Choice Trees representing Sets

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

Proof: The leaves of the choice tree are in 1-1


onto correspondence with the elements of S.
Product Rule
Suppose that all objects of a type S can be constructed by
a sequence of choices with P1 possibilities for the first
choice, P2 for the second, and so on.
IF
1) Each sequence of choices constructs an
object of type S
AND
2) No two different sequences create the
same object
THEN
there are P1P2P3…Pn objects of type S.
E.g., how many different orderings of deck with 52
cards?
• What type of objects are we making?
– Orderings of a deck

Construct an ordering of a deck by a sequence of 52 choices:

52 possible choices for the first card;


51 possible choices for the second card;
50 possible choices for the third card;

1 possible choice for the 52nd card.
Computing large factorials

By the product rule:

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.

24 ways to order a meal if I might not have


some of the courses.

Same as number of subsets of the 4-element


set:
{Appetizer, Entrée, Salad, Dessert}
Selecting from different objects
Appetizer
Y N

Entree 2 children Entree


Y N Y N

Salad Salad Salad Salad leaf (node)


Y N Y N Y N Y N

Desert Desert Desert Desert Desert Desert Desert Desert


Y N Y N Y N Y NY N Y N Y NY N

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)

Desert Desert Desert Desert Desert Desert Desert Desert


Y N Y N Y N Y NY N Y N Y NY N P4 = 2 (i = 3)

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.

Example: Consider the set: S = {a, b, c}

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

{a,b,c} {a,b} {a,c} {a} {b,c} {b} {c} ∅ 23 = 8 subsets

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!

Example: {a,d,e} ⊂ {a,b,c,d,e,f,g}


1 0 0 1 1 0 0 = 76th Subset

Or, for any given binary number, the corresponding subset


can be determined, e.g., for 00111010012 corresponding
subset of a 10 element set: {a10,a9,a8,a7,a6,a5,a4,a3,a2,a1}
is: {a8,a7,a6,a4,a1}.

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.

Set with 11 elements: {a11,a10,a9,a8,a7,a6,a5,a4,a3,a2,a1}


Subset 11-bit binary number
{a11,a10,a8, a1} 11010000001
{a11,a3,a2,a1} 10000000111

{a8,a7,a2} 00011000010
67
Choice Tree for 2n n-bit sequences

0 1

0 1 0 1

0 1 0 1 0 1 0 1

We can use a “choice tree” to represent the


construction of objects of the desired type.
2n n-bit sequences
0 1

0 1 0 1

0 1 0 1 0 1 0 1

000 001 010 011 100 101 110 111

Label each leaf with the object constructed by the


choices along the path to the leaf
2n n-bit sequences

0 1

0 1 0 1

0 1 0 1 0 1 0 1

2 choices for first bit


X 2 choices for second bit
X 2 choices for third bit 2n leaves

X 2 choices for the nth

You might also like