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:
The following is not a set.
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”.
is the set of lower case letters, its size is 26.
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 emoji to indicate “the programmer’s view”).
programmer’s view on containment: is like Python’s in
keyword.
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.
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:
The vertical bar in the middle, , separates the two parts, and is often pronounced “such that”.
For example, we can define a as the set of evens by writing:
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 here has size 1:
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.
- : the empty set (also could be written as ). We’ll use this often in CS340.
- : the set of reals (the number line). This comes up in other areas of math often, less so in CS340.
- : the set of integers .
- : the set of natural numbers . Note that some definitions define the natural numbers as not containing 0. Computer scientists, at least in my experience, lean more toward including it!
Set operations
Now that we’ve defined sets, we can do interesting things with them using set operations.
On sets and :
Subset:
is pronounced “ is a subset of ” and means every element of is also an element of .
This matches our intuitive English definition of a subset.
We say it’s a strict subset, , if there is at least one element in that is not in (that is, if is a subset of but is strictly smaller than ). 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.
My follow-up answer: this notation (), does denote the superset when used in that direction: means every element of is an element of . This does occasionally come up.
An important aside: subset and containment are different!
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:
is pronounced “ equals ” and means the sets have the same elements.
Exercise: define equality using only the operator.
(Click for answer)
if and only if and .
Union:
Note: is just or
. In CS240 notation: .
is pronounced “ union ” (or sometimes “ or ”)and the result is a new set that contains all of the elements that are in either or .
To remember: the word “union” sounds like bringing people together! In my CS240 intuitive framing, union is the more “permissive” operation.
Intersection:
Note: is just and
. In CS240 notation: .
is pronounced “ intersection ” (or sometimes “ and ”) and the result is a new set that contains all of the elements that are in both or .
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: , ,
is pronounced “the complement of ” or “not ” 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.
(Click for answer)
Set difference:
is pronounced “ minus ” or “ set difference ” and means the elements in and not in .
Note that this is equivalent to the previous exercise: . Another way to write this would be
Note that when we use “,” inside a set comprehension, this means “and”. It would be equivalent to write .
Ordered tuples/pairs
We can express ordered pairs or tuples using parenthesis.
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:
How many elements does have?
(Click for answer)
has size 3.
Relations
Relations are a useful special type of set.
Binary relations
Binary relations are sets of ordered tuples.
is a binary relation with two elements.
is .
“Student of” is a binary relation.
(you
, me
) “student of” is .
We can define relation “infix” by putting the name of the relation between the things in the pair:
you
“student of” me
is .
General relations
A general relation has elements that are -tuples, where 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 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:
- A Python dictionary’s elements are unordered.
- 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:
- A general relation is more like a spreadsheet in that they both can have more than two ordered components per element.
- Both a general relation and a spreadsheet should have the same size of tuple per element.
- Relations that are not functions (more on that in a moment), such as , 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 and is defined as:
That is, the cartesian product is all possible combinations of the elements in set and set .
Exercise: if has size (which we can write as ) and has size , what is the size of ?
(Click for answer)
has size .
We can have a cartesian product on a single set:
which is the same as:
Relation definition, again
Now that we have cartesian products, we can more formally define a relation.
A relation on set is any subset .
Relation properties
For a relation on , we can define useful properties:
Reflexive
is reflexive if for all .
Symmetric
is symmetric if for all .
“classmate in CS340” is symmetric.
“student of” is not symmetric.
Transitive
is transitive if for all .
“lives in the same city” is transitive.
Functions
Mathematical functions can be defined as a special kind of relation, where each value in has one distinct value .
Said more formally:
A function on sets and is a subset such that:
- For all , these exists a such that , and
- .
The first condition says that every element in maps to something, and the second condition says that it maps to only one distinct thing.
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 ). A unit circle is not a function, because it does not pass the vertical line test, but it is a relation on .
If is the unit circle:
is not a function because (visually, 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 !
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!
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:
The following is not a set.
Why not? (Click for answer)
Because it has duplicates!
Pattern notation
We use ellipses to show “this pattern in the set continues”.
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 emoji to indicate “the programmer’s view”).
programmer’s view on containment: is like Python’s
in
keyword.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.
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:
The vertical bar in the middle, , separates the two parts, and is often pronounced “such that”.
For example, we can define a as the set of evens by writing:
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 here has size 1:
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 and :
Subset:
This matches our intuitive English definition of a subset.
We say it’s a strict subset, , if there is at least one element in that is not in (that is, if is a subset of but is strictly smaller than ). 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.
My follow-up answer: this notation ( ), does denote the superset when used in that direction: means every element of is an element of . This does occasionally come up.
An important aside: subset and containment are different!
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 typeSet<Integer>
. An element ofSet<Integer>
would have typeInteger
.Equality:
Exercise: define equality using only the operator.
(Click for answer)
Union:
Note: is just .
or
. In CS240 notation:To remember: the word “union” sounds like bringing people together! In my CS240 intuitive framing, union is the more “permissive” operation.
Intersection:
Note: is just .
and
. In CS240 notation: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: , ,
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:
Find .
(Click for answer)
Set difference:
Note that this is equivalent to the previous exercise: . Another way to write this would be
Note that when we use “,” inside a set comprehension, this means “and”. It would be equivalent to write .
Ordered tuples/pairs
We can express ordered pairs or tuples using parenthesis.
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:
How many elements does have?
(Click for answer)
Relations
Relations are a useful special type of set.
Binary relations
Binary relations are sets of ordered tuples.
“Student of” is a binary relation.
( “student of” is .
you
,me
)We can define relation “infix” by putting the name of the relation between the things in the pair:
you
“student of”me
isGeneral relations
A general relation has elements that are -tuples, where 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 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:
A relation is more like a spreadsheet in these ways:
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 and is defined as:
That is, the cartesian product is all possible combinations of the elements in set and set .
Exercise: if has size (which we can write as ) and has size , what is the size of ?
(Click for answer)
We can have a cartesian product on a single set:
which is the same as:
Relation definition, again
Now that we have cartesian products, we can more formally define a relation.
A relation on set is any subset .
Relation properties
For a relation on , we can define useful properties:
Reflexive
“equal to” is reflexive.
Symmetric
“classmate in CS340” is symmetric.
“student of” is not symmetric.
Transitive
“lives in the same city” is transitive.
Functions
Mathematical functions can be defined as a special kind of relation, where each value in has one distinct value .
Said more formally:
A function on sets and is a subset such that:
The first condition says that every element in maps to something, and the second condition says that it maps to only one distinct thing.
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 ). A unit circle is not a function, because it does not pass the vertical line test, but it is a relation on .
If is the unit circle:
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:
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 !
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:
To be continued next week!