Felipi
Felipi
Course Notes
Felipe Balbi
Week 1 5
1.101 Introduction to propositional logic . . . . . . . . . . . . . . . . . . . . . . 5
1.103 Building blocks of logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.105 Truth Table: examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.202 Tautology and consistency . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Week 2 12
1.301 Equivalences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.304 First-order logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Week 3 16
2.01 What is a proof? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.101 Direct proof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.102 Direct proof examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.202 Proof by contradiction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.203 Proof by contraposive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Week 4 22
2.301 Proof by induction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.303 Example of a correct proof . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.305 Example of an incorrect proof . . . . . . . . . . . . . . . . . . . . . . . . 24
2.401 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Week 5 26
3.01 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.101 Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.102 Complex counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.201 The Pigeonhole Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.202 The Pigeonhole Principle: examples . . . . . . . . . . . . . . . . . . . . . 29
Week 6 31
3.301 Permutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.304 Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Week 7 35
4.01 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.101 Basic definitions, letters, strings . . . . . . . . . . . . . . . . . . . . . . . 35
2
Contents
Week 8 45
4.301 Deterministic finite automata (DFA) vs nondeterministic finite automata
(NFA) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.303 Computation by NFA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Week 9 49
5.101 Regular expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
5.103 Design regular expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 52
5.201 Regular expressions and finite automata . . . . . . . . . . . . . . . . . . . 53
5.203 Regular expressions and finite automata: examples . . . . . . . . . . . . . 54
Week 10 57
5.301 Regular or non-regular? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.303 Pumping lemma . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Week 11 60
6.01 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
6.101 Grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
6.201 Language of a grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
6.203 Designing a grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Week 12 65
6.301 Regular expression to context-free grammar . . . . . . . . . . . . . . . . . 65
6.304 Chomsky Normal Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Week 13 71
7.101 Turing machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Week 14 74
7.301 The power of Turing machines . . . . . . . . . . . . . . . . . . . . . . . . 74
7.303 The language of Turing machines . . . . . . . . . . . . . . . . . . . . . . . 75
Week 15 77
8.101 What is an algorithm ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
8.103 Representing algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
8.201 Simple algorithms: insertion sort . . . . . . . . . . . . . . . . . . . . . . . 79
8.203 Simple algorithms: bubble sort . . . . . . . . . . . . . . . . . . . . . . . . 80
Week 16 81
8.301 Binary search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
3
Contents
Week 17 91
9.101 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
9.103 Quick sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
9.201 Merging lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
9.203 Merge sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Week 18 102
9.301 The Algorithm of Happiness . . . . . . . . . . . . . . . . . . . . . . . . . 102
9.303 The Gale-Shapley algorithm: example and pseudocode . . . . . . . . . . . 104
Week 19 106
10.101 Efficiency: insertion sort (time complexity) . . . . . . . . . . . . . . . . 106
10.103 Efficiency: bubble sort and binary search . . . . . . . . . . . . . . . . . . 106
10.201 Asymptotic complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
10.203 Big O notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Week 20 109
10.301 Recursion complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
10.304 Efficiency: Quick sort, merge sort . . . . . . . . . . . . . . . . . . . . . . 110
4
Week 1
Learning Objectives:
• Understand logical arguments and apply basic concepts of formal proof.
Essential Reading
• Rosen, K.H. Discrete mathematics and its applications (New York: McGraw-Hill,
2012) 7th edition, Chapter 1.1-1.2, pp.1-22.
5
Week 1
• x is a prime number
In this case, it can be made into a proposition by assigning a value to x.
• Are you going to school?
Because this is a question, we can’t assign a truth value to the sentence.
• Do your homework now
Being an order, it has no truth value.
6
Week 1
6. Exclusive or (⊕)
p ⊕ q is true if and only if p or q is true but not both.
P = UK exits the EU
Q = Skilled nurses will leave the NHS
R = Research grants will remain intact
¬P → ¬Q ∧ R
Note that we removed any connectives from our propositions as that’s a good practice.
This makes the logical statement easier to follow.
7
Week 1
8
Week 1
Operator Precedence
When formulae are written without parenthesis, we must rely on rules of operator prece-
dence. Logic operator precende rules are as follows:
¬ ∧ ∨ →↔
1. Example
If we have the logical statement p → p ∧ ¬q ∨ s, we can parse it following the steps
below:
p → p ∧ ¬q ∨ s
p → p ∧ (¬q) ∨ s
p → (p ∧ (¬q)) ∨ s
p → ((p ∧ (¬q)) ∨ s)
(p → ((p ∧ (¬q)) ∨ s))
9
Week 1
Consistent
A formula that is true at least for one scenario. All connectives are consistent.
The formula p ∧ ¬p is inconsistent because it can never be true. Inconsistent formulae
are also called contradictions.
10
Week 1
11
Week 2
Learning Objectives:
• Understand logical arguments and apply basic concepts of formal proof.
Essential Reading
• Rosen, K.H. Discrete mathematics and its applications (New York: McGraw-Hill,
2012) 7th edition, Chapter 1.3–1.4, pp.22–52.
1.301 Equivalences
Formulae A and B are equivalent if they have identical truth tables. Equivalence is
denoted by the symbol ≡
In other words, A ≡ B means that A and B have the same truth values, regardless of
how variables are assigned.
One thing to note is that ≡ is NOT a connective.
De Morgan’s Laws
¬(p ∧ q) ≡ ¬p ∨ ¬q
¬(p ∨ q) ≡ ¬p ∧ ¬q
Truth Tables
1. ¬(p ∧ q) ≡ ¬p ∨ ¬q
p q ¬p ¬q p∧q ¬(p ∧ q) ¬p ∨ ¬q
0 0 1 1 0 1 1
0 1 1 0 0 1 1
1 0 0 1 0 1 1
1 1 0 0 1 0 0
2. ¬(p ∨ q) ≡ ¬p ∧ ¬q
12
Week 2
p q ¬p ¬q p∨q ¬(p ∨ q) ¬p ∧ ¬q
0 0 1 1 0 1 1
0 1 1 0 1 0 0
1 0 0 1 1 0 0
1 1 0 0 1 0 0
3. (p → q) ≡ (¬p ∨ q) ≡ ¬(p ∧ ¬q)
p q ¬p ¬q p→q ¬p ∨ q ¬(p ∧ ¬q)
0 0 1 1 1 1 1
0 1 1 0 1 1 1
1 0 0 1 0 0 0
1 1 0 0 1 1 1
4. Contrapositive: (p → q) ≡ (¬q → ¬p)
p q ¬p ¬q p→q ¬q → ¬p
0 0 1 1 1 1
0 1 1 0 1 1
1 0 0 1 0 0
1 1 0 0 1 1
Odd(3) ∧ P rime(3) = T
13
Week 2
In order to satisfy the formula, we must prove that every x satisfies the
formula.
Note that a single counterexample is enough to disprove a universally quan-
tified statement. In other words, universally quantified statements are true
unless there is a false example.
Quantifiers to connectives
• Existential Quantifier
∃xP (x) and domain D = {x1 , x2 , . . . , xn }. This is equivalent to saying P (x1 ) ∨ P (x2 ) ∨
. . . ∨ P (xn )
• Universal Quantifier
∀xP (x) and domain D = {x1 , x2 , . . . , xn }. This is equivalent to saying P (x1 ) ∧ P (x2 ) ∧
. . . ∧ P (xn )
Negation of Quantifiers
• Existential Quantifier
14
Week 2
15
Week 3
Learning Objectives:
• Correctly follow a sequence of justified steps to reach a conclusion statement.
• Prove a conclusion statement by first assuming it is false.
• Describe inductive steps.
• Understand logical arguments and apply basic concepts of formal proof.
16
Week 3
Example 1:
Theorem 1. If n and m are even numbers, then n + m is also even
n = 2k
m = 2l
n + m = 2k + 2l
= 2(k + l)
Let k + l = t,
n + m = 2t
∴ n + m is even.
Example 2:
Theorem 2. ∀n ∈ N, n2 + n is even.
n2 + n = (2k)2 + 2k Even
If n is odd, then n = 2k + 1.
n2 + n = (2k + 1)2 + 2k + 1
= (2k)2 + 2 · 2k + 12 + 2k + 12
= 4k 2 + 6k + 2 Even
17
Week 3
Proof. Assume a < b and a < 0. Multiplying both sides of the inequality by a gives:
a·a<b·a
a2 > b · a
Assume a < b and b < 0. Multiplying both sides of the inequality by b gives:
a·b<b·b
a · b > b2
a2 > a · b > b2
∴ a2 > b2
Example 2:
Theorem 4. ∀x ∈ N, 2x3 + x is a multiple of 3.
If x = 3k + 2, then:
18
Week 3
Example 1:
p
Theorem 5. The square-root of two, (2) is irrational
p
Proof. Assume (2) is rational. This means it can be written as a fraction, in lowest
terms, of the form pq for p, q ∈ N, q 6= 0.
If pq is in lowest terms, it means the fraction cannot be further simplified. Therefore, we
have:
p p
(2) =
q
p 2 p 2
(2) =
q
p 2
2= 2
q
2q 2 = p2
From this, we can see that p must be even. Which means p = 2k. Therefore:
2q 2 = p2
2q 2 = (2k)2
2q 2 = 4k 2
q 2 = 2k 2
19
Week 3
From this, we can see that q must also be even. Which means our fraction pq cannot be
p
in lowest terms. Therefore, (2) cannot be a rational number, so it is irrational.
Example 2:
Theorem 6. There is an infinite number of prime numbers.
Proof. Assume there are finitely many prime numbers. Let the set of prime numbers be
P = {p1 , p2 , . . . , pn }. Let N = (p1 · p2 · . . . · pn ) + 1.
If we divide N by any of the prime numbers in our list of prime numbers, it will have a
remainder of 1. Therefore, N is, itself, a prime number.
Example 1:
Theorem 7. ∀n ∈ N, Odd(n3 + 1) → Even(n)
n = 2k + 1∀k ∈ N
n3 + 1 = (2k + 1)3 + 1
= (2k + 1)(2k + 1)(2k + 1) + 1
= 8k 3 + 12k 2 + 6k + 2
= 2(4k 3 + 6k 2 + 3k + 1)
Example 2:
Theorem 8. Suppose x, y ∈ R, y 3 + yx2 ≤ x3 + xy 2 → y ≤ x
20
Week 3
21
Week 4
Learning Objectives:
• Correctly follow a sequence of justified steps to reach a conclusion statement.
• Prove a conclusion statement by first assuming it is false.
• Describe inductive steps.
• Understand logical arguments and apply basic concepts of formal proof.
Essential Reading
• Rosen, K.H. Discrete mathematics and its applications. (New York: McGraw-Hill,
2012) 7th edition, Chapter 1.7, pp.76–83.
22
Week 4
Proof. Let P (n) = 20 + 21 + . . . + 2n−1 = 2n − 1, prove that P (n) is valid for all n.
Basis: Prove that P (1) is true.
P (1) = 21−1
= 20
=1
= 21 − 1
P (k + 1) = 20 + 21 + . . . + 2k−1 + 2k
P (k + 1) = 2k − 1 + 2k
= 2k + 2k − 1
= 2k+1 − 1
23
Week 4
∀n n < 3n
Theorem 10. n < 3n , for all n ∈ N
Proof. Let P (n) = n < 3n , prove by induction that P (n) is true for all n.
Basis: Prove P (0) is true.
P (0) = 0 < 30
=0<1
k < 3k
k + 1 < 3k + 1
k + 1 < 3k + 1 < 3k + 3k + 3k
k + 1 < 3k + 1 < 3 · 3k
k + 1 < 3k + 1 < 3k+1
k + 1 < 3k+1
n + 1 < n∀n ∈ N
Theorem 11. n + 1 < n, for all n ∈ N.
Proof. INCORRECT!!!
Let P (n) = n + 1 < n∀n ∈ N.
Prove P (k) → P (k + 1). Assuming P (k) is true, so k + 1 < k. Show P (k + 1) is true.
Adding 1 to both sides of the inequality we get k + 1 + 1 < k + 1. Let l = k + 1 we get
l + 1 < l, so P (k + 1) is also true. Therefore P (n) is true.
In this proof, we didn’t prove the base case, so the proof in invalid.
24
Week 4
2.401 Conclusion
We have learned about several powerful proof techniques. We explored Proof by Induc-
tion, which exploits the fact that natural numbers are like a chain.
We have also seen how contrapositive proofs are, sometimes, easier then proving the
original statement. We have also witnessed how proofs can go wrong if we miss an
important step.
25
Week 5
Learning Objectives
• Explore finite or countable discrete structures in the context of computer science.
• Consider how different rules can be applied to appreciate the number of possible
outcomes for an event.
• Explore relationships between sets and elements within or across sets.
• Consider how elements in a set can be counted.
3.01 Introduction
During this topic, we study key principles in counting. We study the Pigeon-hole principle
and learn to apply to prove theorems.
3.101 Counting
How many outfits can we pick from a collection of 5 pairs of trousers and 7 shirts?
Essentially this translates to:
7 5
·
1 1
7! 5!
= ·
1! · (7 − 1)! 1! · (5 − 1)!
7 · 6! 5 · 4!
= ·
6! 4!
=7·5
= 35
Product Rule
The product rule says that if a job can be split into two separate tasks, if there are n
ways of doing task 1 and m ways of doing task 2 then the job can be done in n · m ways.
26
Week 5
A generalization of this is to state that if there are k tasks and each task can be achieved
in ni ways, then there are n1 · n2 · . . . · nk ways of achieving the task.
1. Example 1
How many strings of length 5 can we make with uppercase English letters?
We have 26 uppercase letters in the English alphabet and there are no restrictions
to repetition. For each letter we have 26 options, therefore we can make as many
as 26 · 26 · 26 · 26 · 26 = 265 = 11881376.
2. Example 2
How many strings of length 5 can we make with 3 uppercase English letters and 2
digits?
This is going to be 26 · 26 · 26 · 10 · 10 = 263 · 102 = 1757600
Sum Rule
The sum rule states that if a job can be done in n ways or m ways, then it can be done
in n + m ways.
1. Example 1
A teacher is choosing a student to be her assistent from 5 different classes. The
classes contain 28, 21, 24, 25, and 27 students. How many possible ways are there
to pick an assistant?
There are 28 + 21 + 24 + 25 + 27 = 125 ways of picking an assistant.
Example 1
For most accounts, you need to choose a password. In this example, the password must
be five to seven characters long. Each drawn from uppercase letters or digits. The
password must contain at least one upper case letter.
Let’s split this work by length:
Passwords Length 5 Length 6 Length 7
All passwords (1) 365 366 367
No Letters (2) 105 106 107
Valid Passwords (1 - 2) 60 366 176 2 175 782 336 78 354 164 096
Total 80 590 312 608
27
Week 5
Subtraction Rule
The subtraction rule applies when lists have items in common. This rule is also known
as Inclusion-Exclusion Principle.
This rule states that if a choice can be made from two lists containing n and m items, then
the number of ways to make a choice from these two lists is n + m − items in common.
1. Example 1
How many integers less than 100 are divisible by either 2 or 3. In other words,
we’re talking about the cardinality of the union of the set of numbers divisible by
2 and less than 100 and the set of numbers divisible by 3 and less than 100.
However, this would be cumbersome to calculate. A simpler way is to first calculate
how many numbers between 1 and 99 are divisible by 2 using ⌊ 99
2 ⌋ = 49. Similarly,
we can calculate how many numbers between 1 and 99 are divisible by 3 using
⌊ 99
3 ⌋ = 33.
We must remember to decrement numbers that are divisible by both 2 and 3. Such
numbers are divisible by 6, therefore ⌊ 99
6 ⌋ = 16.
Proof. By contradiction
Assume none of the boxes contains more than ⌈ Nk ⌉ − 1 objects. Since we have k boxes,
we can conclude that Number of Objects ≤ k(⌈ Nk ⌉ − 1) < k( Nk + 1 − 1) = N .
From that we conclude that Number of Objects ≤ N , which contradicts our original
statement that there are exactly N objects.
Example 1
How many cards from a standard deck of 52 cards must be selected to guarantee that 5
cards are from the same suit?
28
Week 5
There are 4 suits in a standard deck of cards. If we pick 16 cards and spread them evenly
among the suits, we will end up with 4 cards for each suit. At the moment we pick the
17th card, it must go to one of the 4 suits, therefore giving 5 cards from the same suit.
We can verify this with ⌈ 17
4 ⌉ = 5.
Example 2
A bag contains 7 blue balls and 4 red balls, how many must be selected to guarantee
that three balls are of the same color.
In this case, there are 2 boxes (the colors) and 11 objects. In the worst case, we pick
colors evenly. Assuming we have picked 4 balls (2 blue and 2 red), when we pick the 5th
ball, it must be either blue or red, therefore giving us our 3 balls of the same color.
In other words, we want to find the number which satisfies ⌈ x2 ⌉ = 3.
Example 3
Select 5 integers from the set S = {1, 2, 3, 4, 5, 6, 7, 8}; show that at least two integers
add up to 9.
The pairs making up 9 are (1, 8), (2, 7), (3, 6), (4, 5). If we label those pairs A, B, C, D,
we can see that all numbers in the set belong to one of 4 boxes.
There are 4 boxes and 5 objects, therefore at least one box will have more than 1 object.
29
Week 5
Example 4
There are n people in the room; every pair is either friends or not friends. Show that
there are at least two people with the same number of friends.
We know that there are n in the room and the amount of friends people can have are
limited to 1, 2, 3, . . . , n − 1 or 0, 1, 2, . . . , n − 2. In both of these cases we will have n − 1
boxes and n people. Consequently, at least one box will have more than one person.
Example 5
√ if there are 10 dots on a square of 6cm × 6cm, there are at least two dots
Show that
within 8 cm.
2cm
√
8
6cm
We can see that a 6cm × 6cm square divided into √ √ squares of 2cm × 2cm.
9 equal smaller
2 2
The smaller squares have a hypotenuse of hyp = 2 + 2 = 8 (indicated by the blue
line).
√
We have√ 9 boxes that are 8 apart, but have 10 objects. Therefore at least two will be
within 8 distance from each other.
30
Week 6
Learning Objectives
• Explore finite or countable discrete structures in the context of computer science.
• Consider how different rules can be applied to appreciate the number of possible
outcomes for an event.
• Explore relationships between sets and elements within or across sets.
• Consider how elements in a set can be counted.
Essential Reading
• Sipser, M. Introduction to the theory of computation. (Boston: Cengage Learning,
2014) 3rd edition, Chapters 1.1 and 1.2, pp.31–63
– PDF
3.301 Permutations
Permutation relates to the arrangement of objects where order does matter. For example,
how many ways are there for 5 people to form a queue? For the first position in the queue,
we can have any of the five people, for second position we can have one out of four people;
for the third position, we can have one out of three people and so on. The answer here
is that there are 5! = 120 ways for 5 people to form a queue.
Definition 1 (Permutation). A permutation of a set of distinct objects is an ordered
arrangement of these objects.
There are n! permutations of n objects. This is a simplification of the definition of
Permutation of n objects taken r at a time. If we set k = r we get:
n n!
Pn =
(n − n)!
n!
=
0!
= n!
31
Week 6
n n!
Pr = P (n, r) = n(n − 1) + . . . + (n − r + 1) =
(n − r)!
3.304 Combinations
Combination relates to the arrangement of objects where does not matter. For example,
if we have 4 animals (mouse, cat, dog, rabbit) and we want to take a side-by-side photo
of two animals, how many ways can we choose two animals? Note that photos with the
same animals in different order count as equivalent ((cat, dog) ≡ (dog, cat)).
Definition 2 (Combination). A combination of a set of distinct objects is an unordered
arrangement of these objects.
There is only one combination of n elements. This is because upon shuffling, only the
order of elements change, however with combinations the order does not matter.
An r-combination of elements of a set is an unordered selection of r elements from this
set and is denoted by n Cr = C(n, r).
Theorem 14. For two integers n, r, 0 ≤ r ≤ n. There are:
n n n!
Cr = C(n, r) = =
k k! · (n − k)!
Example 1
How many hands of 7 cards can be dealt from a standard deck of 52 cards? First we
need to figure out if the order matters. In this case it doesn’t because being dealt (5, 7)
is equivalent to being dealt (7, 5).
n n
Cr =
r
52
=
7
52!
=
7!(52 − 7)!
52 · 51 · 50 · 49 · 48 · 47 · 46 · 45!
=
7!45!
52 · 51 · 50 · 49 · 48 · 47 · 46 · 45!
=
7·6·5·4·3·2·1
= 133 784 560
32
Week 6
Example 2
How many ways are there to choose 11 players from a group of 16 in order to form a
team? First we need to decide if the order matters. In case, it doesn’t. Therefore it’s a
combination.
n n
Cr =
r
16
=
11
16!
=
11!(16 − 11)!
16 · 15 · 14 · 13 · 13 · 12 · 11!
=
11! · 5!
16 · 15 · 14 · 13 · 13 · 12
=
5!
= 4368
Example 3
How many binary words of length 8 contain equal number of zeroes and ones?
Does the order matter? No. Therefore it’s a combination.
33
Week 6
n n
Cr =
r
8
=
4
8!
=
4!(8 − 4)!
8!
=
4!4!
8 · 7 · 6 · 5 · 4!
=
4!4!
8·7·6·5
=
4!
= 70
Example 4
How many binary words of length 8 contain at most 3 ones?
At most means 0, 1, 2, or 3. This can be solved as a sum of combinations:
8 8 8 8
= + + +
0 1 2 3
8! 8! 8! 8!
= + + +
0!8! 1!7! 2!6! 3!5!
= 1 + 8 + 28 + 56
= 93
Note that 0! = 1.
Example 5
How many binary words of length 8 contain at least 5 ones?
At least means 5, 6, 7 or 8. This can be solved as a sum of combinations:
8 8 8 8
= + + +
5 6 7 8
8! 8! 8! 8!
= + + +
5!3! 6!2! 7!1! 8!0!
= 56 + 28 + 8 + 1
= 93
34
Week 7
Learning Objectives
• Understand the basic terminologies of automata theory.
• Describe finite automata and what it can represent.
• Build deterministic and nondeterministic finite automata.
• Understand and apply various concepts in automata theory, such as deterministic
automata, regular languages and context-free grammar.
4.01 Introduction
We will study simple mathematical machines knows as Automata. The next few topics
will introduce the prerequisites for understanding automata and later we will learn about
how these machines work and process input data.
Riddle:
There is a farmer who has a mouse, a cat, and a loaf of bread. He has to
cross the river from the north to the south by a small boat. He can take
up to one of his possessions with him on the boat. The boat cannot operate
without the farmer. The cat and the mouse cannot be left alone, as the cast
will eat the mouse, and the mouse cannot be left alone with the loaf of bread
as it will eat it. How can the farmer cross deliver without losing one of his
possessions?
35
Week 7
Examples
1. If Σ = {a, b, c} then what is Σ2 ?
Σ2 = {aa, ab, ac, ba, bb, bc, ca, cb, cc}
2. If Σ = {a, b, c} then what is Σ1 ?
Σ1 = {a, b, c}
Note that Σ1 6= Σ. Elements in Σ are called symbols while elements in Σ1 are
called strings, the strings just happen to have length 1.
1
The length of the set of all strings of length k from alphabet Σ is equal to the length of the alphabet
Σ to the power k.
36
Week 7
1
A B
1 0
0 1 0
0
C D
37
Week 7
E D
1
0 1
0 1
1 B C 0
1 0
38
Week 7
E D
1
0 1
0 1
1 B C 0
1 0
What follows is input 1 which makes the automaton switch to state D as in figure 7.
Finally, we process another 1 which causes the automaton to switch to state B as in
figure 8, which is not and accept state and, therefore, the output of the computation is
REJECT.
The next input is 0, which causes to transition from B to E.
One important detail to keep in mind that we don’t ACCEPT when passing through an
accept state, only when the input ends at an accept state.
39
Week 7
E D
1
0 1
0 1
1 B C 0
1 0
E D
1
0 1
0 1
1 B C 0
1 0
40
Week 7
E D
1
0 1
0 1
1 B C 0
1 0
E D
1
0 1
0 1
1 B C 0
1 0
41
Week 7
E D
1
0 1
0 1
1 B C 0
1 0
E D
1
0 1
0 1
1 B C 0
1 0
42
Week 7
0, 1
1 0
1
B A
0
1 0 0,1
0 1
A B C
43
Week 7
0 0
A 1 0 D 0,1
1 1
44
Week 8
Learning Objectives
• Understand the basic terminologies of automata theory.
• Describe finite automata and what it can represent.
• Build deterministic and nondeterministic finite automata.
• Understand and apply various concepts in automata theory, such as deterministic
automata, regular languages and context-free grammar.
Essential Reading
• Sipser, M. Introduction to the theory of computation. (Boston: Cengage Learning,
2014) 3rd edition, Chapters 1.1 and 1.2, pp.31–63
– PDF
45
Week 8
1 0,1
0 1
A B C
1 0 0,1
0 0,1
A B C
46
Week 8
1 1 0,1
0,1 0
A B C
0,1
1
A C
0 1
to B. With the next 1, we loop back to B. With the following 0 we transition to C and
with the final 1 we loop back to C which is an accept state.
Language of NFA
Again, we’re going to use 17 as our example. We would like to study different inputs.
If input starts with 1, then we transition to accept state C and anything that follows
keeps us in state C by looping back.
If the input starts with 01, then we, again, transtion to accept state C (through B) and
anything that follows keeps us in state C by looping back.
Any other strings will cause us to get stuck.
47
Week 8
1 0,1
0,1 0
A B C
The language of this NFA is, therefore, all binary strings starting with 1 or 01.
48
Week 9
Learning Objectives
• Describe formal languages in the context of regular expressions.
• Identify examples of regular expressions and finite automaton.
• Write regular expressions with and without the use of finite automaton.
Essential reading
• Sipser, M. Introduction to the theory of computation. (Boston: Cengage Learning,
2014) 3rd edition, Chapter 1.3, pp.63–76
– PDF
49
Week 9
Example
A = {red, green, pink}
B = {apple, banana, kiwi}
A ∪ B = {red, green, pink, apple, banana, kiwi}
A ◦ B = {redapple, redbanana, redkiwi, greenapple, greenbanana,
greenkiwi, pinkapple, pinkbanana, pinkkiwi}
∗
A = {ε, red, green, pink, redred, redgreen,
redpink, greenred, greengreen, greenpink,
pinkred, pinkgreen, pinkpink, . . .}
50
Week 9
• Concatenation
If R1 and R2 are regular expressions, so is R1 ◦ R2 .
• Union
If R1 and R2 are regular expressions, so is R1 ∪ R2 .
• Kleene Star
If R is a regular expression, so is R∗ .
51
Week 9
The answer is 4: a∪bc∗ = a∪b(c∗ ) = a∪(b(c∗ )). The language is {a, b, bc, bcc, bccc, . . .}.
52
Week 9
53
Week 9
1 0
0
A B
(1 ∪ 0+ 1)∗ 0+
C D
the transitions accordingly. When we have multiple transitions from one state to another,
we can take the union of all transitions and collapse into a single transition.
Also note that 00∗ is the same as 0+ , so we can can simplify the transition from A to
D. Finally, we can remove state A by realising that we have a single path from C to D
equivalent to (1 ∪ 0+ 1)∗ 0+ . The result is shown in figure 20.
1 0 0,1
0 1
A B C
54
Week 9
1 0 0,1
0 1
A B C
1 0 0,1
0 1
A B C
ε ε
D E
1 0
0 1Σ∗
A B E
55
Week 9
00∗ 1Σ∗
A E
1∗ 0+ 1Σ∗
D E
56
Week 10
Learning Objectives
• Describe formal languages in the context of regular expressions
• Identify examples of regular expressions and finite automaton.
• Write regular expressions with and without the use of finite automaton.
Essential Reading
• Sipser, M. Introduction to the theory of computation. (Boston: Cengage Learning,
2012) 3rd edition, Chapter 1.4, pp.77–82.
– PDF
Closure properties
Theorem 15. If L1 and L2 are regular languages on alphabet Σ, then the following
languages are also regular:
• U − L1 : This means Σ∗ − L1 or the complement of L1
• L1 ∪ L2 : The union of L1 and L2
• L1 ∩ L2 : The intersection of L1 and L2
• L1 L2 : The product of L1 and L2
• L1 ∗ : The Kleene star of L1
57
Week 10
Proof. By contradiction.
Let’s assume L to be regular. We know that L′ = {x ∈ a∗ b∗ } is regular. We also know
that the intersection of two regular languages is also regular, therefore L ∩ L′ = {an bn |
n ∈ N} must be regular. We know this language to not be regular, therefore L cannot
be a regular language.
58
Week 10
Proof. By contradiction.
Let’s assume L to be regular, therefore ¬L must also be regular.
We know ¬L = {an bn } ∪ non-bitonic is regular. We also know L′ = {x ∈ a∗ b∗ } is regular.
Moreover, we know that the intersection of two regular languages is also regular, therefore
¬L ∩ L′ must be regular.
However, ¬L ∩ L′ = {an bn | n ∈ N} is non-regular.
59
Week 11
Learning Objectives
• Consider context-free grammar and it utility in computer science.
• Explore the language of grammar and designing a grammar.
• Be able to convert between regular expressions and context-free grammar.
• Understand conversion to normal form.
• Understand Chomsky Normal Form.
6.01 Introduction
We discuss Grammars, which is a set of rules which recursively defines the structure of
strings.
6.101 Grammar
As we’ve seen before, regular languages can be implemented by Finite Automata while
irregular languages, cannot.
When we want to generate irregular languages, we must employ a more powerful tech-
nique called Context-free Grammar. Simply put, a grammar is a set of rules for connect-
ing strings together. They describe the structure of strings recursively.
A Context-free grammar is a type of grammar where rules are independent of the context
in which variables occur.
A context-free grammar G is a 4-tuple (V, Σ, R, S) where:
Variables (V) Also referred to as non-terminals. A finite set of symbols usually denoted
by capital letters.
Terminals (Σ) A finite set of symbols denoted by capital sigma.
Rules (R) A finite set of mappings from a variable to a string consisting of variables and
terminals.
Start Variable (S) A member of V, positioned on the left-hand side of the top rule.
60
Week 11
Example
S → bSa
S → ba
This grammar has two production rules. S is the non-terminal and the start variable.
There are two terminals a and b.
Generating strings
1. Starting from start symbol, read its rule
2. Find a variable in the rule of the start symbol and replace it with the rule for
that variable
3. Repeat step 2 until there are no variables left
A few notes:
• A derivation is a sequence of substitutions in generating a string
• There may be more than one rule for a variable. We can use the symbol | to indicate
or : S → bSa | ba
With this rule, we have a recursively generated string that looks like so:
S
ba bSa
bbaa bbSaa
bbbaaa bbbSaaa
bbbbaaaa ...
We say that u derives v or u ⇒∗ v if there is a derivation from u to v.
Example
S → aS | T
T →b|ε
Variables S, T
Terminals a, b
Starting variable S
61
Week 11
We want to find 3 strings derived from S. We can use the following derivation tree.
S
aS T
aT b
a ab
Of course, there are many more strings that can be generated from this grammar. For
example the S in the first aS, could be replaced with aaS and so on.
Let’s find strings which can’t be generated from this grammar:
bb b comes from T which comes from S. To get bb we would need to have SS somewhere.
But there’s no way to produce it.
abb ab comes from aS, which produces aT which produces ab. To produce abb we would
have to produce aTT somewhere.
aba Similar case to previous one.
Example
What is the language of the following grammar G1 :
S → bSa
S → ba
If we produce a few strings from this grammar, we can build the following tree:
62
Week 11
ba bSa
bbaa bbSaa
bbbaaa bbbSaaa
bbbbaaaa ...
It becomes clear that all strings will have the same amount of b and a, always starting
with b. So the language is L(G1 ) = {bn an | n ≥ 1}.
Writing the grammar as a 4-tuple, we have G1 = ({S}, {a, b}, {S → bSa, S → ba}, S).
S → aSa | bSb | a | b | ε
63
Week 11
Checklist
After designing a grammar, we can apply this checklist:
Consistency if all strings generated by the grammar fit the description of the language
Completeness all strings in the description can be generated by the grammar
Terminating recursion all recursions used in the grammar terminate
S → 1S | 0S0S | ε
64
Week 12
Learning Objectives
• Consider context-free grammar and its utility in computer science.
• Explore the language of grammar and designing a grammar.
• Be able to convert between regular expressions and context-free grammar.
• Understand conversion to normal form.
• Understand Chomsky Normal Form.
Essential Reading
• Hopcroft, J., R. Motwani and J.D. Ullman Introduction to automata theory, lan-
guages and computation. (Harlow: Pearson Education Ltd, 2013) 3rd edition,
Chapter 5, pp.171–224.
– PDF
Example 1
Let’s convert the regular expression ab∗ to a Context-free Grammar (CFG).
We can see that the expression b∗ can be written as U → bU | ε.
The expression ab∗ can be written as S → aU .
The final CFG is:
65
Week 12
S → aU
U → bU | ε
Example 2
Convert the regular expression ab∗ ∪ b∗ to a CFG.
• b∗ can be expressed as U → bU | ε
• ab∗ can be expressed as S → aU
The final CFG is:
S → aU | U
U → bU | ε
Example 3
Convert the regular expression ab+ ∪ b+ b to a CFG.
• b+ can be written as U → bU | b
• b+ b can be written as T → U b
• ab+ can be written as S → aU
The final CFG is:
S → aU | T
T → Ub
U → bU | b
S → aU | uB
U → bU | b
Example 4
Convert Σ∗ aΣ∗ to a CFG, Σ = {a, b}
• Σ∗ can be written as U → aU | bU | ε
66
Week 12
S → U aU
U → aU | bU | ε
Example 5
Binary strings of length at least 3: ΣΣΣ+
• Σ+ can be written as U → aU | bU | a | b
• ΣΣ+ can be written as V → aU | bU
• ΣΣΣ+ can be written as S → aV | bV
The final CFG is:
S → aV | bV
V → aU | bU
U → aU | bU | a | b
S → XU
S→a
• a is a terminal symbol
• X, U are not the start variable
• S → ε is permitted if S is the start variable
Conversely, the following grammars are not in Chomsky Normal Form:
S → 1S | 0S0S | ε Because S appears on the right-hand side
V → ε where V is not a start variable Due to ε rules
U → V where V is a variable Due to unit rules
X → 1U V The length of the rule is > 2. Referred to as Improper Rule
We can substitute rules that are not in Chomsky Normal Form with other rules that are
in Chomsky Normal Form.
67
Week 12
Example 1
1. Starting CFG
U → XU X | 0Y
X→Y |U
Y →1|ε
S→U
U → XU X | 0Y
X→Y |U
Y →1|ε
3. Remove ε rule Y → ε
S→U
U → XU X | 0Y
X→Y |U |ε
Y →1
4. Remove ε rule X → ε
68
Week 12
S→U
U → XU X | 0Y | U X | XU | U
X→Y |U
Y →1
69
Week 12
70
Week 13
Learning Objectives
• Understand the process of computation through Turing machines.
• Consider the design and utility of Turing machines.
• Explore the power and language of Turing machines.
• Discuss non-context-free languages.
Essential Reading
• Forbes, M. A theoretical introduction to Turing Machine. (World Technologies,
2014), Chapter 1, pp.4-21.
71
Week 13
qk
q3 ..
.
q2 qn
q1 q0
The transition function δ takes two arguments: a state, and a letter from the alphabet
Γ; and returns a state, a letter to be written on the current cell of the tape, the direction
to which the tape head should move (L for left, R for right).
DFA vs TM
• TMs may not terminate when the input is completely processed
72
Week 13
Acc
→ , R
b → , R
→ , R
q1 q2 Rej
a → , R
a → , R
73
Week 14
Learning Objectives
• Understand the process of computation through Turing machines.
• Consider the design and utility of Turing machines.
• Explore the power and language of Turing machines.
• Discuss non-context-free languages.
Essential Reading
• Kozen, D.C. Automata and Computability. (New York: Springer, 2007), Lecture
32, pp.235–238.
– PDF
74
Week 14
1 → 1, R 0 → 0, R 0 → 0, L
0 → 0, R 1 → 0, L
q1 q2 q3
→ , R
→ , R
,R
1 → 1, R
0
→
→
1,
R
Acc q4
Halting problem
• Every decider is a Turing Machine
• R ⊂ RE
• Halting Problem: Given a Turing Machine, does is halt?
75
Week 14
Hailstone Sequence
1 function hailstone(n)
2 {
3 while (n != 1) {
4 if ((n & 1) == 0) {
5 n = n / 2
6 } else if ((n & 1) == 1) {
7 n = 3 * n + 1
8 } else {
9 return n;
10 }
11 }
Nobody knows whether the Hailstone Sequence always terminates. Collatz Conjecture
states that it does, but it’s yet to be proven.
What we know
• Every regular language is context-free (FSA and RE)
• Every context-free languages is Turing-decidable (CFGs)
• Every decidable language is recognisable (Decide Turing Machine)
Chomsky Hierarchy
76
Week 15
Learning Objectives
• Understand the process of algorithmic thinking and a number of proof techniques,
and apply this knowledge to solve a range of computer science problems.
• Explore different techniques for sorting and searching using algorithms.
• Identify the value of using different types of algorithmic techniques in different
contexts.
Essential Reading
• Rosen, K.H. Discrete mathematics and its applications. (New York: McGraw-Hill,
2012) 7th edition, Chapter 3.1, pp.191–204
Informal definition
Problem What is the problem we’re trying to solve?
Input What information do we have available?
Output What is the output of the algorithm applied to the inputs?
An algorithm is the set of steps required to take the input and achieve the outcome.
77
Week 15
A formal definition
An ordered set of unambiguous, executable steps that form a terminating process.
Ordered After each step we know what to do next
Unambiguous Each operation is sufficiently clear
Executable Each operation must be doable (e.g. dividing by 0 is not doable)
Terminating There is a finite number of executions. The process should halt eventually.
Pseudocode
A series of notations that can describe ideas and operations. It’s made to be intuitive
and informal while also being easily readable by humans.
Algorithms written in pseudocode are not made to processed by machines.
Pseudocode rules are not unique.
Primitives of pseudocode
• Assignment
78
Week 15
1 x = 10
2 sum = a + b
• Conditional
1 if (a < b):
2 b = b - 2
or
1 if (a < b):
2 b = b - 2
3 else:
4 b = b + 1
• Loops
1 while (raining):
2 read a book
• Functions
1 def add(a, b):
2 a + b
Real techniques
Divide and Conquer Divide problem into smaller problems and solve recursively
Greedy Best solution at each moment without looking at the entire problem
Backtracking Begin with one solution and move to next level. If it’s not a good solution,
go back to previous step.
79
Week 15
4 tmp = array[i]
5 j = i
6 while (array[i] < array[j-1] and (j > 0)):
7 j = j - 1
8
13 i = i + 1
14 return array
6 def bubbleSort(array):
7 n = len(array)
8 count = 0
9
16 if (count == 0):
17 break
18 return array
80
Week 16
Learning Objectives
• Understand the process of algorithmic thinking and a number of proof techniques,
and apply this knowledge to solve a range of computer science problems.
• Explore different techniques for sorting and searching using algorithms.
• Identify the value of using different types of algorithmic techniques in different
contexts.
81
Week 16
Max Heap
• A complete binary tree
• Each internal node has a value greater than or equal to its children
Min Heap
• A complete binary tree
• Each internal node has a value less than or equal to its children
Heapify
The process of transforming a complete binary tree into a heap tree.
Heap sort
1. Represent list in the form of a complete binary tree
82
Week 16
45 21 3 56 10 7 41 9
45
21 3
56 10 7 41
45
21 3
9 10 7 41
56
Once we’ve done that, we move up the tree. Do 21, 9 and 10 for a Min Heap? No,
then we swap:
83
Week 16
45
9 3
21 10 7 41
56
Moving to the right we look at 3, 7, and 41. Do they form a Min Heap? Yes, we
don’t need to touch them.
Moving up again we encounter 45, 9, and 3. Do they form a Min Heap? No, then
we swap:
9 45
21 10 7 41
56
84
Week 16
9 45
21 10 7 41
56
Next we take the last item in the bottom right of the tree and place it in the root:
56
9 45
21 10 7 41
The resulting tree is no longer a Min Heap. We must heapify again. The figures
that follow, show the rest of the process.
56
9 7
21 10 45 41
85
Week 16
9 56
21 10 45 41
9 56
21 10 45 41
41
9 56
21 10 45
41
9 56
21 10 45
86
Week 16
41
9 45
21 10 56
41 45
21 10 56
41 45
21 10 56
56
41 45
21 10
87
Week 16
56
10 45
21 41
10
56 45
21 41
56 45
21 41
41
56 45
21
88
Week 16
41
21 45
56
21
41 45
56
41 45
56
56
41 45
89
Week 16
41
56 45
56 45
45
56
56
56
3 7 9 10 21 41 45 56
90
Week 17
Learning Objectives
• Explore more advanced algorithmic techniques, including recursion and sorting
algorithms.
• Consider merging lists, merge sorts and how they work.
• Understand and apply algorithms in the context of Shapley proofs and stable
matching.
Essential Reading
• Rosen, K.H. Discrete mathematics and its applications. (New York: McGraw-Hill,
2012) 7th edition, Chapter 5.1, pp.307–327.
9.101 Recursion
A simple example
Here’s the pseudocode for a function called LessThan
1 def LessThan(n):
2 Print n
3 If n-2 > 0:
4 LessThan(n-1)
5 LessThan(n-2)
What’s the output when the input if 4? From line 2, we print 4. Then on line 3, we check
if n-2 is greater than 0, which it is; therefore we call LessThan(n-1) which evaluates to
LessThan(3).
Following we print 3, check if n-2 is greater than 0, then call LessThan(n-1) which
evaluates to LessThan(2). Then we print 2, check if n-2 is greater than 0. This time
it isn’t, then we can call exit our original function and call LessThan(n-2) where n is
3. We print 1 and exit this function. Now n is 4 and we call LessThan(n-2) which will
print 2 and exit the function.
Our complete output is 4 3 2 1 2.
91
Week 17
92
Week 17
6 }
7
8 return a
9 }
The second version of the algorithm is recursive. Below we can find its pseudocode:
1 def GCD(a, b):
2 if b = 0:
3 return a
4 return GCD(b, a mod b)
And a javascript implementation follows:
1 function GCD(a, b) {
2 if (b === 0)
3 return a
4 return GCD(b, a % b)
5 }
9 5 4 1 1 1
9 5 4 1 1 1
1 1 1 4 9 5
93
Week 17
1 1 1 4 9 5
1 1 1 4 5 9
1 1 1 4 5 9
Below, we can see the pseudocode for Quicksort. One thing to note is that since Quicksort
is implemented recursively, we need a base case. That base case is a list with a single
item, which is already sorted.
1 def QuickSort(List):
2 If List has one item:
3 Return List
4 Else:
5 Pivot = the middle entry of the List
6 Delete Pivot from the List
7 For item in List:
8 If Pivot > Item:
9 ListLeft.append(item)
10 Else:
11 ListRight.append(item)
12 C = QuickSort(ListLeft) + Pivot + QuickSort(ListRight)
13 Return C
94
Week 17
1 3 5 2 4 6
1 3 5 2 4 6
i=1 j=1
1 3 5 2 4 6
k=1
i=1 j=1
1 3 5 2 4 6
k=1
1
95
Week 17
i=2 j=1
1 3 5 2 4 6
k=2
1
i=2 j=1
1 3 5 2 4 6
k=2
1
i=2 j=1
1 3 5 2 4 6
k=2
1 2
i=2 j=2
1 3 5 2 4 6
k=3
1 2
96
Week 17
i=2 j=2
1 3 5 2 4 6
k=3
1 2
i=2 j=2
1 3 5 2 4 6
k=3
1 2 3
i=3 j=2
1 3 5 2 4 6
k=4
1 2 3
i=3 j=2
1 3 5 2 4 6
k=4
1 2 3
97
Week 17
i=3 j=2
1 3 5 2 4 6
k=4
1 2 3 4
i=3 j=3
1 3 5 2 4 6
k=5
1 2 3 4
i=3 j=3
1 3 5 2 4 6
k=5
1 2 3 4
i=3 j=3
1 3 5 2 4 6
k=5
1 2 3 4 5
98
Week 17
i=4 j=3
1 3 5 2 4 6
k=6
1 2 3 4 5
i=4 j=3
1 3 5 2 4 6
k=6
1 2 3 4 5
i=4 j=3
1 3 5 2 4 6
k=6
1 2 3 4 5 6
i=4 j=4
1 3 5 2 4 6
k=7
1 2 3 4 5 6
99
Week 17
i=4 j=4
1 3 5 2 4 6
k=7
1 2 3 4 5 6
100
Week 17
101
Week 18
Learning Objectives
• Explore more advanced algorithmic techniques, including recursion and sorting
algorithms.
• Consider merging lists, merge sorts and how they work.
• Understand and apply algorithms in the context of Shapley proofs and stable
matching.
Problem Statement
There are n hospitals and n medical students. There are lists of preferences for the
students and the hospitals. Pair students to hospitals so that the matching is stable.
A pair student s and hospital h is called unstable when:
• Student s prefers hospital h to the one assigned to him/her
• Hospital h prefers student s to the one assigned to it
A stable matching is a matching without any unstable pairs.
The input to our algorithm will be:
• List of hospitals
• List of students
And a few rules:
1. Number of students is equal to number of hospitals
1
Numberphile Video on Stable Marriage Problem
2
Gale-Shapley Paper
3
Mathematical Details of Stable Marriage Problem
102
Week 18
Definitions
• H is the list of hospitals
• S is the list of students
• A matching M is a set of pairs (h, s) where h ∈ H and s ∈ S
– each hospital h appears at most one pair of M
– each student s appears at most one pair of M
• A matching is perfect if
– each hospital h appears in at least one pair of M
– each student s appears in at least one pair of M
– |M| = |H| = |S|
A perfect match
From the previous example, a possible perfect match would be as shown on by the blue
cells below:
Mohammed UCLH Whittington Royal Free
Elena Whittington UCLH Royal Free
Sara Whittington UCLH Royal Free
Whittington Mohammed Elena Sara
UCLH Elena Mohammed Sara
Royal Free Mohammed Elena Sara
However, Elena and UCLH form an unstable pair because Elena prefers UCLH over her
current hospital and UCLH prefers Elena over its current student. The existence of an
unstable pair, makes the matching unstable.
103
Week 18
Proof of correctness
In order to show that this algorithm terminates with a stable matching, we first show
that it, indeed, terminates; then we show that it terminates with a stable matching.
• Prove that it terminates
Look at the lines of pseudocode. We can see a single loop that runs for as long as
there are students to offer. A hospital never offers to the same student twice and
the student who rejects a hospital is removed from the list.
Therefore, each hospital of the n offers at most n students. The loop runs at most
n2 iterations.
• Prove that it returns a perfect matching
104
Week 18
Look at the definition of perfect matching. Perfect matching is when each hospital
appears in the match at least and at most once (i.e. exactly once).
– At most
Every hospital makes an offer only if it’s unmatched. Every hospital appears
in the match at most once.
Every student accepts the best offer and rejects the others. Every student
appears in the match at most once.
– At least (by contradiction)
Assuming there is a hospital h that is not in the matching, then there must
be at least one student s who is unmatched. This student has never received
an offer. This means that h never made an offer to this student s.
If the algorithm has terminated, then h has gone through all its list and it’s
still unmatched, however s must be in h’s list, this is a contradiction.
• Prove that the match is stable
No unstable pair!
Let’s assume there is an unstable pair (h, s). This means (h, s′ ) ∈ M and (h′ , s) ∈
M. There are few cases:
1. s never got an offer from h
Hospitals make offers in rank-decreasing order, which means that the rank of
s′ is higher than s, so (h, s) is stable.
2. h made an offer to s
s must have rejected the offer. Students only reject offers when they have
better offers, which means rank h′ must be higher than h, so (h, s) is stable.
105
Week 19
Learning Objectives
• Explore the concepts of efficiency, average, best and worst.
• Explore the usage of bubble sorts and binary searches.
• Consider asymptotic complexity and Big O notation.
• Explore recursion complexity and master theorem in the context of large problems.
• Consider efficient methods for problem solving.
106
Week 19
Graphs
Constant parallel to the x axis
logarithmic slow growth
Linear growth at a constant rate
Quadratic grows with the square of the input size
Exponential very fast growth
Asymptotic complexity
Refers to the growth of the function when the input size n is very large. It serves as a
simple way of estimating how slow the algorithm is.
When doing such analyses, we ignore the lower order terms. For example, both f (n) =
2n2 + 5n and g(n) = 4n2 + 7n have the same asymptotic behavior.
107
Week 19
108
Week 20
Learning Objectives
• Explore the concepts of efficiency, average, best and worst.
• Explore the usage of bubble sorts and binary searches.
• Consider asymptotic complexity and Big O notation.
• Explore recursion complexity and master theorem in the context of large problems.
• Consider efficient methods for problem solving.
Essential Reading
• Chang, S. (ed) Data structures and algorithms. (New Jersey: World Scientific
Publishing, 2003), Chapters 8 and 9, pp.161–200.
– Direct Link
109
Week 20
110