Today, we’ll have an introduction to some of the essentials from discrete math that we’ll build on in this course.

This course is not a math class, and most assignments will involve programming, not written answers. But we’ll need shared language around some discrete math—specifically set operations and the foundations of logic—to analyze interesting systems.

This week was an exception in that the assignment and workshop material was a temperature check on material not previously taught in lecture. Future assignments and workshop sessions will happen after the lecture(s) in which we cover the material. Thank you for your patience with me as I determine what background information I need to cover!

Preliminaries

The notation we use here is not an important focus of this class, but I’ll try to show a few different notations that you may see outside of this class. The goal of notation is to help us be precise, I’ll try to not use more than necessary.


Sets

Set definition

A set is a unordered collection of elements. Set elements are distinct—an element is either contained in the set or not (in a programmer’s view, there are no duplicates).

There are many valid ways to define a set.

Often, we’ll define sets by using curly brackets ({}). We can define a small (finite) set by listing elements inside the brackets.

For example, this is the set containing the integers 1, 2, and 3:

{1,2,3} is a set

The following is not a set.

{1,2,1,2} is not a set (not a canonical description of one).

Why not? (Click for answer)
Because it has duplicates!


Pattern notation

We use ellipses to show “this pattern in the set continues”.

{a,b,c,,z} is the set of lower case letters, its size is 26.

{0,1,2,3,} is the set of natural numbers, its size is infinity.


Containment/element of

To show that an element is contained in a set, we’ll use the notation (pronounced “in”).

(Aside: in class, we voted on the :dark_sunglasses: emoji to indicate “the programmer’s view”).

:dark_sunglasses: programmer’s view on containment: is like Python’s in keyword.

1{1,2,3} is true.

4{1,2,3} is false.

4{1,2,3} is true.


Set builder/set comprehension notation

We can also define sets by specifying, in math notation, which elements are contained in the set. This is called set builder or set comprehension notation.

:dark_sunglasses: programmer’s view: This is like Python’s list or dictionary comprehension, for example: {x: x+1 for x in [1,2,3]}.

This notation still uses curly brackets, but has two parts: a variable name on the left, then the specification for which variables are in the set on the right:

S={x|<something>}

The vertical bar in the middle, |, separates the two parts, and is often pronounced “such that”.

For example, we can define a S as the set of evens by writing:

S={x|x%2=0} is the set of evens.

We would pronounce this as: “The set of integers x such that x mod two equals zero”.

Does any set defined by a set comprehension need to be infinite?

(Click for answer)

No, for example, the set S here has size 1:

A={3}
S={x|xA}


We’ll use set comprehension notation frequently in the Alloy section of this course.

Special sets

There are a few special sets in math that have their own names/symbols.


Set operations

Now that we’ve defined sets, we can do interesting things with them using set operations.

On sets S and T:


Subset:

ST is pronounced “S is a subset of T” and means every element of S is also an element of T.

This matches our intuitive English definition of a subset.

{1,2}{1,2,3} is true.

{1,4}{1,2,3} is false.

We say it’s a strict subset, ST, if there is at least one element in T that is not in S (that is, if S is a subset of T but is strictly smaller than T). You can remember this by comparing it to vs. < on numbers.

Student question: do people also write the subset the other direction, like and >?

My answer in class: yes, but not very often.

:star: My follow-up answer: this notation (), does denote the superset when used in that direction: ST means every element of T is an element of S. This does occasionally come up.

An important aside: subset and containment are different!

1{1,2,3} is true.

1{1,2,3} is false.

{1}{1,2,3} is true.

:dark_sunglasses: programmers view: the types are different. Subset works on things of the same type, contains needs an element type. For example, a subset of Set<Integer> would have type Set<Integer>. An element of Set<Integer> would have type Integer.


Equality: =

S=T is pronounced “S equals T” and means the sets have the same elements.

{1,2}={2,1} is true.

Exercise: define equality using only the operator.

(Click for answer)

S=T if and only if ST and TS.



Union:

ST={x|xS or xT}
ST={x|xSxT}

Note: is just or. In CS240 notation: +.

ST is pronounced “S union T” (or sometimes “S or T”)and the result is a new set that contains all of the elements that are in either S or T.

{1,2}{2,3}={1,2,3}

To remember: the word “union” sounds like bringing people together! In my CS240 intuitive framing, union is the more “permissive” operation.


Intersection:

ST={x|xS and xT}
ST={x|xSxT}

Note: is just and. In CS240 notation: .

ST is pronounced “S intersection T” (or sometimes “S and T”) and the result is a new set that contains all of the elements that are in both S or T.

{1,2}{2,3}={2}

To remember: street “intersections” are the parts of the road where both streets meet. In my CS240 intuitive framing, intersection is the more “strict” operation.


Complement: Sc, S, S

Sc is pronounced “the complement of S” or “not S” and means all of the elements that are not in S.

This could be an infinite number of elements if we are considering the entire universe to be our domain. Often, we’ll combine the complement operator with an intersection operator to talk about smaller, more useful sets.

Exercise:

S={1,2}
T={2,3}

Find TSc.

(Click for answer)

TSc={3}



Set difference:

TS is pronounced “T minus S” or “T set difference S” and means the elements in T and not in S.

Note that this is equivalent to the previous exercise: TS=TSc. Another way to write this would be {x|xT,xS}

Note that when we use “,” inside a set comprehension, this means “and”. It would be equivalent to write {x|xTxS}.


Ordered tuples/pairs

We can express ordered pairs or tuples using parenthesis.

:dark_sunglasses: programmer’s view: this is like Python tuples.

Aside: Like Python tuples, sets can be heterogenous (that is, have elements of seemingly different types)

So:

W={1,(3,4),(5,6,7)} is a valid set.

How many elements does W have?

(Click for answer)

W has size 3.



Relations

Relations are a useful special type of set.

Binary relations

Binary relations are sets of ordered tuples.

{(1,2),(3,4)} is a binary relation with two elements.

(1,2){(1,2),(3,4)} is true.

“Student of” is a binary relation.

(you, me) “student of” is true.

We can define relation “infix” by putting the name of the relation between the things in the pair:

you “student of” me is true.

General relations

A general relation has elements that are n-tuples, where n is fixed for a given relation.

{(1, 2, 3), (4, 5, 6)} is a relation on 3-tuples.

{(1, 2, 3), (4, 5)} is not a relation (but it is a set).

Back to the :dark_sunglasses: programmer’s view: we can make analogies to other data representations

Discuss: is a relation more similar to a Python dictionary or an Excel spreadsheet (/CSV file)? Why?

(Click for answer)

A relation is more like a Python dictionary in these ways:

  1. A Python dictionary’s elements are unordered.
  2. A binary relation is more like a Python dictionary in that they both can only have elements with two ordered components.

A relation is more like a spreadsheet in these ways:

  1. A general relation is more like a spreadsheet in that they both can have more than two ordered components per element.
  2. Both a general relation and a spreadsheet should have the same size of tuple per element.
  3. Relations that are not functions (more on that in a moment), such as {(1,1),(1,2)}, can be represented by a spreadsheet but not represented by a Python dictionary, because Python dictionaries cannot map the same key to multiple elements. For this reason, I tend to like the spreadsheet analogy better for my own mental model.

Note that one weakness of the spreadsheet mental model is that the order of rows could matter in a spreadsheet (and there could be a row that is totally a duplicate), whereas a relation is a set with distinct and unordered elements.



Cartesian Product

The cartesian product of sets A and B is defined as:

A×B={(a,b)|aA,bB}

That is, the cartesian product is all possible combinations of the elements in set A and set B.

Exercise: if A has size M (which we can write as |A|=M) and B has size N, what is the size of A×B?

(Click for answer)

A×B has size MN.


We can have a cartesian product on a single set:

S×S=S2={(x,y)|x,yS}

which is the same as:

S×S=S2={(x,y)|xS,yS}

Relation definition, again

Now that we have cartesian products, we can more formally define a relation.

A relation R on set A×B is any subset RA×B.


Relation properties

For a relation R on A×A, we can define useful properties:

Reflexive

R is reflexive if for all aA,(a,a)R.

“equal to” is reflexive.

Symmetric

R is symmetric if for all a,bA,(a,b)R(b,a)R.

“classmate in CS340” is symmetric.

“student of” is not symmetric.

Transitive

R is transitive if for all a,b,cA,(a,b)R(b,c)R(a,c)R.

“lives in the same city” is transitive.

Functions

Mathematical functions can be defined as a special kind of relation, where each value a in (a,b)R has one distinct value b.

Said more formally:

A function F on sets A and B is a subset FA×B such that:

The first condition says that every element in A maps to something, and the second condition says that it maps to only one distinct thing.

:dark_sunglasses: programmer’s view: Python dictionaries are a close mental model for functions.

In class, we drew out the 2D plane to relate this to the vertical line test for determining if a line in the plane is a function (the 2D plane is a visual representation of R2). A unit circle is not a function, because it does not pass the vertical line test, but it is a relation on R×R=R2.

If C is the unit circle:

(0,1)C
(0,1)C
C is a relation, CR2.

C is not a function because 11 (visually, C fails the vertical line test at x=0).


Logic

We’ll cover logic more in the next lecture, but for now, a quick note on logical implication.

Implication:

In short, this is the truth table for implication:

A B    A => B
-------------
T T    T
T F    F
F T    T
F F    T

For example, consider if I said:

“If pigs fly tomorrow, I will give you all an A in CS340 without coming to class”

Which we can also write as:

“Pigs flying tomorrow implies I will give you all an A in CS340 without coming to class”

These statement are both true!

The statements themselves are not a lie, since the first part of the conditional is never true. In logic, false implies anything. More on this on Monday.

This is the truth table for equality/bidirectional implication, which might feel more intuitive:

A B    A <=> B
-------------
T T    T
T F    F
F T    F
F F    T

To be continued next week!