Predicate Logic

Module 5: Predicate Logic
1
Admin
• Midterm 1: read the Piazza post on the two-stage
exam.
• Assignment #2 is due Thursday Feb 2 at 4pm.
• Pre-class quiz #6 is due Thursday Feb 2 at 7pm.
• Assigned reading for the quiz:
• Epp, 4th edition: 3.2, 3.4
• Epp, 3rd edition: 2.2, 2.4
2
Learning goals: pre-class
• By the start of class, you should be able to
• Evaluate the truth of predicates applied to particular
values.
• Show a predicate logic statement is true by
enumerating examples, i.e. one (all) in the domain for
an existential (universal) quantifier.
• Show a predicate logic statement is false by
enumerating counterexamples, i.e. all (one) in the
domain for an existential (universal) quantifier.
• Translate between statements in formal predicate
logic notation and equivalent statements in closely
matching informal language, i.e., informal statements
with clear and explicitly stated quantifiers.
3
CPSC 121 - the BIG questions
• How can we convince ourselves that an algorithm
does what it's supposed to do?
• We need to prove that it works.
• We have done a few proofs in the last week or so.
• Many statements (that we need to prove) involve
quantifiers.
• How do we determine whether or not one
algorithm is better than another one?
• We can finally answer that question!
4
Learning goals: in-class
• By the end of this module, you should be able to:
• Build statements about the relationships between
properties of various objects using predicate logic.
These may be
• real-world like “every candidate got votes from at
least two people in every province” or
• computing related like “on the ith repetition of this
algorithm, the variable min contains the smallest
element in the list between element 0 and element
i”.
5
Module 5 outline
• Predicates vs Propositions
• Examples
• More examples: sorted lists
• Algorithm efficiency revisited.
• Additional examples to consider.
6
Limitations of Propositional Logic as a
Model
• Which of the following can it model effectively?
a. Relationship among different people, e.g. Alice is
married to Bob and Bob is married to Alice.
b. Defining what it means for a number to be
prime.
c. Generalizing from examples to abstract patterns
like “everyone takes off their shoes at airport
security”.
d. Prop logic can model all of these effectively.
e. Prop logic can not model any of these effectively.
7
What is predicate logic good for
modeling?
• Relationships among real-world objects.
• Generalizations about patterns
• Infinite domains
• Generally, problems where the properties of the
different concepts, or parts, depend on each
other.
9
Examples of predicate logic use:
• Data structures: Every key stored in the left
subtree of a node N is smaller than the key stored
at N.
• Language definition: No path via references exists
from any variable in scope to any memory
location available for garbage collection...
• Algorithms: in the worst case, every comparison
sort requires at least cnlog2n comparisons to sort
n values, for some constant c > 0.
10
Quantifier syntax
A quantifier in CPSC 121 is always of the form:
_____ _____  ______, _______________
 or 
variable
set
subexpression that can
mention the variable
Together, this forms a single subexpression.
For example, x  N, (x > 3)  (x + 4 < 12).
11
Quantifiers syntax
You cannot treat any part of this (like x  Z or x)
as a subexpression. It just isn’t.
So, no saying: (x  Z)  p.
12
Announcements
• Assignment 1 being handed back in tutorials
• Assignment 2 and pre-class quiz due tomorrow.
• Midterm 1 cutoff:
• Roughly by the end of today’s lecture (Wednesday)
• Predicate logic will be on it but only lightly tested.
• Major topics:
• Logical equivalence proofs
• Tautologies, contradictions, and contingencies
• Circuit design
• Number representation
• Propositional logic proofs
• Translating between predicate logic and English
• A little bit of set and function theory
13
Quantifier scope
A quantifier applies to everything to its right, until
a closing parenthesis stops it.
14
Quantifier scope
A quantifier applies to everything to its right, until
a closing parenthesis stops it.
∀𝑥 ∈ 𝐷, (∃𝑦 ∈ 𝐸, 𝑄 𝑥, 𝑦 → ∀𝑧 ∈ 𝐹, 𝑅 𝑦, 𝑧 ∧ 𝑃 𝑥
∀𝑥 ∈ 𝐷 applies to the entire expression.
15
Quantifier scope
A quantifier applies to everything to its right, until
a closing parenthesis stops it.
∀𝑥 ∈ 𝐷, (∃𝑦 ∈ 𝐸, 𝑄 𝑥, 𝑦 → ∀𝑧 ∈ 𝐹, 𝑅 𝑦, 𝑧 ∧ 𝑃 𝑥
∃𝑦 ∈ 𝐸 applies to this subexpression.
16
Quantifier scope
A quantifier applies to everything to its right, until
a closing parenthesis stops it.
∀𝑥 ∈ 𝐷, (∃𝑦 ∈ 𝐸, 𝑄 𝑥, 𝑦 → ∀𝑧 ∈ 𝐹, 𝑅 𝑦, 𝑧 ∧ 𝑃 𝑥
∀𝑧 ∈ 𝐹 applies to this subexpression.
17
Quantifier scope
Which of the following placements of
parentheses yields the same meaning as:
∀x ∈ Z, ∃y ∈ Z, x < y ∧ Even(y) ?
a.(∀)x ∈ Z, ∃y ∈ Z, x < y ∧ Even(y)
b.(∀x) ∈ Z, ∃y ∈ Z, x < y ∧ Even(y)
c.(∀x ∈ Z), ∃y ∈ Z, x < y ∧ Even(y)
d.(∀x ∈ Z, ∃y ∈ Z, x < y) ∧ Even(y)
e.(∀x ∈ Z, ∃y ∈ Z, x < y ∧ Even(y))
18
Negation scope
Which of the following placements of parentheses
yields the same meaning as:
~∃x ∈ Z+, ∀y ∈ Z+, x < y ∧ Even(y) ?
a.(~(∃x)) ∈ Z+, ∀y ∈ Z+, x < y ∧ Even(y)
b.(~(∃x ∈ Z+)), ∀y ∈ Z+, x < y ∧ Even(y)
c.(~(∃x ∈ Z+, ∀y ∈ Z+, x < y)) ∧ Even(y)
d.(~(∃x ∈ Z+, ∀y ∈ Z+, x < y ∧ Even(y)))
e.None of the above.
20
What is a predicate?
• A logical expression with ”unbound variables”
• We cannot determine the truth value of a
predicate until we know the truth values of the
unbound variables.
• Example: PerfectSquare(y): ∃𝑥 ∈ 𝑍, 𝑥 ∗ 𝑥 = 𝑦
22
What is a predicate?
• PerfectSquare(y): ∃𝑥 ∈ 𝑍, 𝑥 ∗ 𝑥 = 𝑦
• PerfectSquare(y): unknown truth value
• PerfectSquare(10): false
• ∀𝑦 ∈ 𝑍, 𝑃𝑒𝑟𝑓𝑒𝑐𝑡𝑆𝑞𝑢𝑎𝑟𝑒(𝑦 : false
• It’s like a function --- a mapping from the truth
values of its unbound variables to the truth values
of the predicate.
23
Which variables are unbound?
∀i ∈ Z+, (i ≥ n) ↔ ~∃v ∈ Z+, HasValue(l, i, v)
a.i and v
b.l and n
c.n and v
d.i and n
e.None of these are correct.
24
What is a predicate?
• Does every predicate have quantifier(s)?
a. Yes.
b. No.
c. I don’t know
• Examples:
1. PerfectSquare(y): ∃𝑥 ∈ 𝑍, 𝑥 ∗ 𝑥 = 𝑦
2. IsSquare(x,y): 𝑥 ∗ 𝑥 = 𝑦
26
In contrast, what is a proposition?
• A logical expression that is either true or false
• Given a proposition, we can always evaluate its
truth value.
27
In contrast, what is a proposition?
• Can a proposition have unbound variables?
a. Yes.
b. No.
c. I don’t know.
28
In contrast, what is a proposition?
• Can a proposition have quantifiers?
a. Yes.
b. No.
c. I don’t know.
• Examples of propositions:
1. PerfectSquare(4) (4 is a perfect square.)
2. ∀𝑦 ∈ 𝑍, 𝑃𝑒𝑟𝑓𝑒𝑐𝑡𝑆𝑞𝑢𝑎𝑟𝑒 𝑦 (Every integer is a
perfect square.)
29
Difference between a proposition and
a predicate
a. A predicate may contain one or more
quantifiers, but a proposition never does.
b. A proposition's name is a lowercase letter,
whereas a predicate's name is an uppercase
letter.
c. A predicate must contain unbound variables,
but a proposition does not.
d. They are the same thing, using different names.
e. None of the above.
30
Quick Review (?) of Sets
A set is a collection of elements:
• the set of students in this class
• the set of lowercase letters in English
• the set of natural numbers (N)
• the set of all left-handed students in this class
An element is either in the set (x  S) or not (x 
S).
32
What is a Set?
A set is an unordered collection of objects.
The objects in a set are called members.
(a  S indicates a is a member of S;
a  S indicates a is not a member of S)
A set contains its members.
33
Describing Sets (1/4)
Some sets…
A = {1, 3, 9}
B = {1, 3, 9, 27, snow}
C = {1, 1, 3, 3, 9, 9} (the same as A!)
D = {A, B}
D = { {1, 3, 9}, {1, 3, 9, 27,
snow} }
E = { } (the empty set, with nothing in it)
34
Describing Sets (2/4)
Some sets…
A = {1, 5, 25, 125, …}
B = {…, -2, -1, 0, 1, 2, …}
C = {1, 2, 3, …, 98, 99, 100}
(The set of powers of 5, the set of integers, and the
set of integers between 1 and 100.)
“…” is an ellipsis
35
Module 5 outline
• Predicates vs Propositions
• Examples
• More examples: sorted lists
• Algorithm efficiency revisited.
• Additional examples to consider.
36
Translating between English and
predicate logic
• All lions are fierce.
• Some lion(s) do not drink coffee.
• L: the set of all lions.
• F(x): x is fierce.
• C(x): x drinks coffee.
(c) animal.discovery.com
37
Translating between English and
predicate logic
• All lions are fierce.
• ∀𝑥 ∈ 𝐿, 𝐹 𝑥 .
• Some lion(s) do not drink coffee.
• ∃𝑥 ∈ 𝐿, ~𝐶 𝑥 .
(c) animal.discovery.com
• L: the set of all lions.
• F(x): x is fierce.
• C(x): x drinks coffee.
38
Making your life a bit more difficult…
• All lions are fierce.
• Some lions do not drink coffee.
• A: the set of all animals.
• L(x): x is a lion.
• F(x): x is fierce.
• C(x): x drinks coffee.
(c) animal.discovery.com
39
Restricting the Domain of a Universal
• Translate “All lions are fierce.” into predicate logic.
a. ∀𝑥 ∈ 𝐴, 𝐹 𝑥 → 𝐿(𝑥
b. ∀𝑥 ∈ 𝐴, 𝐿 𝑥 → 𝐹 𝑥
c. ∀𝑥 ∈ 𝐴, 𝐿 𝑥 ∧ 𝐹 𝑥
d. ∀𝑥 ∈ 𝐴, 𝐿 𝑥 ∨ 𝐹 𝑥
• A: the set of all animals.
• L(x): x is a lion.
• F(x): x is fierce.
40
Restricting the Domain of a Universal
• All lions are fierce.
• We’d like to say: ∀𝑥 ∈ 𝑆𝑒𝑡𝑂𝑓𝐿𝑖𝑜𝑛𝑠, 𝐹 𝑥 .
• But we don’t have the set of lions…
• How do we restrict the set of animals to just lions for
this universal?
41
Restricting the Domain of a Universal
• All lions are fierce.
• Is the above proposition true for the following
set?
1. {a timid rabbit, a fierce lion}
42
Restricting the Domain of a Universal
• All lions are fierce.
• Every animal is either not a lion or fierce.
∀𝑥 ∈ 𝐴, ~𝐿 𝑥 ∨ 𝐹 𝑥
≡ ∀𝑥 ∈ 𝐴, 𝐿 𝑥 → 𝐹 𝑥
Use an implication to restrict the domain of a
universal!
43
Restricting the Domain of a Universal
• The general formula:
• Given set D and predicates P(x) and Q(x),
“All <things in D for which P is true> have the
property Q.”
∀𝑥 ∈ 𝐷, 𝑃 𝑥 → 𝑄 𝑥
All <things in D for which P is true>…
44
Restricting the Domain of a Universal
• Is ∀𝑥 ∈ 𝐴, 𝐿 𝑥 ∧ 𝐹 𝑥 another valid translation of
“All lions are fierce.”?
• Try this example A = {a timid rabbit, a fierce lion}
a. Yes, it is logically equivalent to using an
implication.
b. Yes, for another reason.
c. No, this translation requires every animal in the
set to be a lion.
d. No, for another reason.
e. I don’t know.
45
Restricting the Domain of an Existential
• Translate “Some lions do not drink coffee.” into
predicate logic.
a. ∃ 𝑥 ∈ 𝐴, ~𝐶 𝑥 → 𝐿(𝑥
b. ∃ 𝑥 ∈ 𝐴, 𝐿 𝑥 → ~𝐶 𝑥
c. ∃ 𝑥 ∈ 𝐴, 𝐿 𝑥 ∧ ~𝐶 𝑥
d. ∃ 𝑥 ∈ 𝐴, 𝐿 𝑥 ∨ ~𝐶 𝑥
• A: the set of all animals.
• L(x): x is a lion.
• C(x): x drinks coffee.
46
Restricting the Domain of an Existential
• Some lions do not drink coffee.
• We’d like to say: ∃𝑥 ∈ 𝑆𝑒𝑡𝑂𝑓𝐿𝑖𝑜𝑛𝑠, ~𝐶 𝑥 .
• But we don’t have the set of lions…
• How do we restrict the set of animals to just lions for
this existential?
47
Restricting the Domain of an Existential
• Some lions do not drink coffee.
• First, we need to be able to find a lion.
• Then, we need to find a non-coffee-drinking lion.
• Some animal is a lion and does not drink coffee.
∃𝑥 ∈ 𝐴, 𝐿 𝑥 ∧ ~𝐶 𝑥
Use a conjunction to restrict the domain of an
existential!
48
Restricting the Domain of an Existential
• The general formula:
• Given set D and predicates P(x) and Q(x),
“Some <thing in D for which P is true> has the
property Q.”
∃𝑥 ∈ 𝐷, 𝑃 𝑥 ∧ 𝑄 𝑥
Some <thing in D for which P is true>…
49
Restricting the Domain of an Existential
• Is ∃ 𝑥 ∈ 𝐴, 𝐿 𝑥 → ~𝐶 𝑥 another valid translation of
“Some lions do not drink coffee.”?
• Try this example A = {a rabbit}
a. Yes, it is logically equivalent to ∃ 𝑥 ∈ 𝐴, 𝐿 𝑥 ∧
~𝐶 𝑥 .
b. Yes, because the implication worked for the
universal, so it should also work for the existential.
c. No, ∃ 𝑥 ∈ 𝐴, 𝐿 𝑥 → ~𝐶 𝑥 can be true even if
there is no lion in the set of animals, but our English
statement requires at least one lion to be in the set
of animals.
d. No, for another reason.
e. I don’t know.
50
Mixed quantifiers
• Consider the following two propositions:
1. ∀𝑥 ∈ 𝑃, ∃𝑦 ∈ 𝐿, Enjoys(𝑥, 𝑦
2. ∃𝑦 ∈ 𝐿, ∀𝑥 ∈ 𝑃, Enjoys(𝑥, 𝑦
• P: set of people, L: set of locations
• Enjoys(x,y): person x enjoys spending time at location y.
• Are these two propositions logically equivalent?
a. Yes
b. No
c. I don’t know.
51
Mixed quantifiers
• Consider the following two propositions:
1. ∀𝑥 ∈ 𝑃, ∃𝑦 ∈ 𝐿, Enjoys(𝑥, 𝑦
2. ∃𝑦 ∈ 𝐿, ∀𝑥 ∈ 𝑃, Enjoys(𝑥, 𝑦
• P: set of people, L: set of locations
• Enjoys(x,y): person x enjoys spending time at location y.
• We read quantifiers from left to right.
• The order of quantifiers matters!
• The two propositions are not logically equivalent.
52
Mixed quantifiers
• Consider the following two propositions:
1. ∀𝑥 ∈ 𝑃, ∃𝑦 ∈ 𝐿, Enjoys(𝑥, 𝑦
2. ∃𝑦 ∈ 𝐿, ∀𝑥 ∈ 𝑃, Enjoys(𝑥, 𝑦
• P: set of people, L: set of locations
• Enjoys(x,y): person x enjoys spending time at location y.
• Let’s look at a few examples.
53
At least, at most, and exactly
• How do we translate…
• There is at least one lion.
• There is at most one lion.
• There is exactly one lion.
54
At least…
“There is at least one lion.” means
“Some animal is a lion”.
∃𝑥 ∈ 𝐴, 𝐿 𝑥
“There are at least two lions.” means
“Some animal and some other animal are lions.” or
“Some animal and some animal that is not the
same animal are lions.”
∃𝑥 ∈ 𝐴, ∃𝑦 ∈ 𝐴, 𝐿 𝑥 ∧ 𝐿 𝑦 ∧ 𝑥 ≠ 𝑦
55
At most…
“There is at most one lion in A…” means the same as
“It is not the case that there are at least two lions in
A…”
~∃𝑥 ∈ 𝐴, ∃𝑦 ∈ 𝐴, 𝐿 𝑥 ∧ 𝐿 𝑦 ∧ 𝑥 ≠ 𝑦
Let’s transform this.
≡ ~∃𝑥 ∈ 𝐴, ∃𝑦 ∈ 𝐴, (𝐿 𝑥 ∧ 𝐿 𝑦 ∧ 𝑥 ≠ 𝑦
≡ ∀𝑥 ∈ 𝐴, ∀𝑦 ∈ 𝐴, ~ 𝐿 𝑥 ∧ 𝐿 𝑦 ∨ 𝑥 = 𝑦
≡ ∀𝑥 ∈ 𝐴, ∀𝑦 ∈ 𝐴, 𝐿 𝑥 ∧ 𝐿 𝑦 → (𝑥 = 𝑦
This also means the same as
“If I can find two lions in A, they must be the same.”
56
Exactly…
“There is exactly one lion in A…” means that
“there is at least one lion in A” and
“there is at most one lion in A…”
57
Exactly…
“There is at least one lion in A”
∃𝑥 ∈ 𝐴, 𝐿 𝑥
“There is at most one lion in A”
~∃𝑥 ∈ 𝐴, ∃𝑦 ∈ 𝐴, 𝐿 𝑥 ∧ 𝐿 𝑦 ∧ 𝑥 ≠ 𝑦
“There is exactly one lion in A”
(∃𝑥 ∈ 𝐴, 𝐿 𝑥
∧ (~∃𝑥 ∈ 𝐴, ∃𝑦 ∈ 𝐴, 𝐿 𝑥 ∧ 𝐿 𝑦 ∧ 𝑥 ≠ 𝑦
58
Module 5 outline
• Predicates vs Propositions
• Examples
• More examples: sorted lists
• Algorithm efficiency revisited.
• Additional examples to consider.
59
A sorted list predicate
• Define a predicate Sorted(L) whose value is true if
and only if L is sorted in non-decreasing order.
• Two useful functions:
• (length L) returns the length of L.
• (list-ref L i) returns the ith element of L.
• The first element of L is (list-ref L 0).
60
A sorted list predicate
• Two useful functions:
• (length L) returns the length of L.
• (list-ref L i) returns the ith element of L.
• Are length and list-ref predicates?
a. Yes.
b. No, because they don’t start with capital letters.
c. No, because they have parentheses around them
instead of after.
d. No, because they don’t produce true or false.
61
A problematic predicate
Sorted L ≡
∀𝑖 ∈ 𝑁, ∀𝑗 ∈ 𝑁, (list − ref L i ∧ (list − ref L j ∧ 𝑣𝑖 < 𝑣𝑗
a. There is no quantifier for L.
b. There are no quantifiers for vi and vj.
c. We can not use a conjunction with (list-ref L i)
and (list-ref L j)
d. Both (a) and (b)
e. Both (b) and (c)
62
Another problematic predicate
Sorted L ≡ ∀𝑖 ∈ 𝑁, ∀𝑗 ∈ 𝑁,
𝑖 < 𝑗 → (list − ref L i < (list − ref L j
a. It is too restrictive (it does not allow for equal
values).
b. The ranges of i and j need to be restricted
further.
c. It is missing quantifiers.
d. Both (a) and (b)
e. Both (b) and (c)
64
How do we get a working predicate?
Sorted L ≡ ∀𝑖 ∈ 𝑁, ∀𝑗 ∈ 𝑁,
𝑖<𝑗
→ (list − ref L i < (list − ref L j
66
Module 5 outline
• Predicates vs Propositions
• Examples
• More examples: sorted lists
• Algorithm efficiency revisited.
• Additional examples to consider.
68
A search problem
• We are given a sorted list of names with phone numbers.
• We want to find the phone number for a given name N.
• Two algorithms:
• Algorithm L: check the first name. If it's not N, then
check the second name. Then the third name, etc.
• Algorithm B: check the name in the middle of the list.
If N comes earlier alphabetically then search the first
half of the list using B. If it comes later search the
second half of the list instead. Repeat until you have
found N or you're looking at an empty sublist.
69
Comparing two search algorithms
Assumptions:
Reading the name after the current name takes 1s
on average.
Reading a name given its position takes 10s on
average.
For a list with 15 names:
Algorithm L takes 15 * 1s = 15s in the worst case.
Algorithm B takes 4 * 10s = 40s in the worst case.
70
Comparing two search algorithms
For a list with 63 names:
Algorithm L takes 63 * 1s = 1m 3s in the worst case.
Algorithm B takes 6 * 10s = 1m in the worst case.
For a list with 1048575 names:
Algorithm L takes 1048575 * 1s = 12d 3h 16m 15s
in the worst case.
Algorithm B takes 20 * 10s = 3m 20s in the worst
case.
The running time of algorithms depends on the size
of their inputs.
71
The Big-O Notation
• Consider two functions f(n) and g(n) for some
integer n. Think of these two functions as the
running times of two algorithms.
• We say that 𝑓(𝑛 ∈ 𝑂(𝑔 if 𝑓(𝑛) is less than 𝑔(𝑛)
when 𝑛 is sufficiently large.
• Other ways of saying the same thing:
• f(n) is upper bounded by g(n).
• f(n) grows slower than g(n).
• Pictorially, g(n) is above f(n) for large inputs.
72
The Big-O Notation in quantifiers
• 𝑓 is in 𝑂(𝑔 if
∃𝑐 ∈ 𝑅+ , ∃𝑛0 ∈ 𝑁, ∀𝑛 ∈ 𝑁, 𝑛 ≥ 𝑛0 → 𝑓(𝑛 ≤ 𝑐𝑔(𝑛
73
When the input is sufficiently large
• What does ∃𝑛0 ∈ 𝑁, ∀𝑛 ∈ 𝑁, 𝑛 ≥ 𝑛0 → 𝑓(𝑛 ≤ 𝑐𝑔(𝑛
mean?
a. 𝑓(𝑛 is less than 𝑐𝑔(𝑛 for any input that is larger
than 𝑛0, where 𝑛0 is one particular integer.
b. 𝑓(𝑛 is less than 𝑐𝑔(𝑛 for any input that is larger
than 𝑛0, where 𝑛0 is any arbitrary integer.
c. 𝑓(𝑛 is less than 𝑐𝑔(𝑛 for a particular input that is
larger than 𝑛0, where 𝑛0 is one particular integer.
d. 𝑓(𝑛 is less than 𝑐𝑔(𝑛 for a particular input that is
larger than 𝑛0, where 𝑛0 is any arbitrary integer.
74
When the input is sufficiently large
• What does ∃𝑛0 ∈ 𝑁, ∀𝑛 ∈ 𝑁, 𝑛 ≥ 𝑛0 → 𝑓(𝑛 ≤ 𝑐𝑔(𝑛
mean?
a. 𝑓(𝑛 is less than 𝑐𝑔(𝑛 for any input that is larger
than 𝑛0, where 𝑛0 is one particular integer.
b. 𝑓(𝑛 is less than 𝑐𝑔(𝑛 for any input that is larger
than 𝑛0, where 𝑛0 is any arbitrary integer.
c. 𝑓(𝑛 is less than 𝑐𝑔(𝑛 for a particular input that is
larger than 𝑛0, where 𝑛0 is one particular integer.
d. 𝑓(𝑛 is less than 𝑐𝑔(𝑛 for a particular input that is
larger than 𝑛0, where 𝑛0 is any arbitrary integer.
75
The threshold value 𝑛0
• 𝑓 is in 𝑂 𝑔 if 𝑓(𝑛 is less than 𝑔 𝑛 for any
input that is larger than some natural number 𝑛0.
• Think of 𝑛0 as a threshold value.
• An input 𝑛 is ”sufficiently large” if it is “larger than
𝑛0”.
• 𝑓(𝑛) is less than 𝑔(𝑛) when 𝑛 is sufficiently large.
76
Let’s understand c
• 𝑓 is in 𝑂(𝑔 if
∃𝑐 ∈ 𝑅+ , ∃𝑛0 ∈ 𝑁, ∀𝑛 ∈ 𝑁, 𝑛 ≥ 𝑛0 → 𝑓(𝑛 ≤ 𝑐𝑔(𝑛
• Let f(n) = 2n and g(n) = n. Is 𝑓(𝑛 ∈ 𝑂(𝑔 ?
a. No, because g(n) is smaller than f(n) for any n.
b. Yes, choosing any value for c and n0 will make the
predicate true.
c. Yes, because we can find values for c and n0 to make
the predicate true.
77
Let’s understand c
• 𝑓 is in 𝑂(𝑔 if
∃𝑐 ∈ 𝑅+ , ∃𝑛0 ∈ 𝑁, ∀𝑛 ∈ 𝑁, 𝑛 ≥ 𝑛0 → 𝑓(𝑛 ≤ 𝑐𝑔(𝑛
• Let f(n) = 2n and g(n) = n. Is 𝑓(𝑛 ∈ 𝑂(𝑔 ?
a. No, because g(n) is smaller than f(n) for any n.
b. Yes, choosing any value for c and n0 will make the
predicate true.
c. Yes, because we can find values for c and n0 to make
the predicate true.
78
Let’s understand c
• 𝑓 is in 𝑂(𝑔 if
∃𝑐 ∈ 𝑅+ , ∃𝑛0 ∈ 𝑁, ∀𝑛 ∈ 𝑁, 𝑛 ≥ 𝑛0 → 𝑓(𝑛 ≤ 𝑐𝑔(𝑛
• Let f(n) = 2n and g(n) = n. Is 𝑓(𝑛 ∈ 𝑂(𝑔 ?
• We can show that f(n) is in O(g).
• We can also show that g(n) is in O(f).
• Big-O notation doesn’t care about the constant
multipliers in front of the functions.
• It only contains about the order of magnitudes of the
functions. In this sense, f(n) and g(n) are
“equivalent” because they are both on the order of n.
79
Comparing linear and binary search
• The running time of binary search is 𝑓 𝑛 = log 𝑛 .
• The running time of linear search is 𝑔 𝑛 = 𝑛.
• 𝑓 is in 𝑂(𝑔 if
∃𝑐 ∈ 𝑅+ , ∃𝑛0 ∈ 𝑁, ∀𝑛 ∈ 𝑁, 𝑛 ≥ 𝑛0 → 𝑓(𝑛 ≤ 𝑐𝑔(𝑛
• Is 𝑓(𝑛 ∈ 𝑂(𝑔 ? Prove it.
80
86
Module 5.4: Algorithm efficiency
revisited
Which of the following functions f(n) is the last
one for which the (correct) predicate from the
previous slide is true?
a.f(n) = n
b.f(n) = n2/2
c.f(n) = 3n2
d.f(n) = n3
e.f(n) = 2n
89
Module 5.4: Algorithm efficiency
revisited
Which of the following two functions grows
faster?
a.f(n) = n
b.f(n) = nlog2n
c.Neither; they both grow equally fast.
91
Module 5.4: Algorithm efficiency
revisited
Is the following predicate true for f(n) = n?
∃c ∈ R+ ∀n ∈ N f(n) ≤ cnlog2n
a.Yes
b.No
93
Module 5.4: Algorithm efficiency
revisited
Is the following predicate true for f(n) = n?
∃c ∈ R+ ∃n0 ∈ N ∀n ∈ N n ≥ n0 → f(n) ≤
cnlog2n
a.Yes
b.No
95
Module 5.4: Algorithm efficiency
revisited
So we define O(g) by:
f is in O(g) if
∃c ∈ R+ ∃n0 ∈ N ∀n ∈ N n ≥ n0 → f(n) ≤
cg(n)
97
Module 5.4: Algorithm efficiency
revisited
Pictorially:
c
n0
98
Module 5.4: Algorithm efficiency
revisited
Some common running times:
1
log n
n
nlog n
n2
2n
99
Module 5.4: Algorithm efficiency
revisited
Revisiting sorted lists:
Recall
Sorted(L) ≡ ∀i ∈ N ∀j ∈ N
(0 ≤ i) ^ (i < j) ^ (j < (length L)) → (list-ref L i) ≤ (listref L j)
If we verify that L is sorted using this definition,
how many comparisons will we need?
Can we do better?
100
Module 5.4: Algorithm efficiency
revisited
Here is another definition:
Sorted(L) ≡ ∀i ∈ N
(0 ≤ i) ^ (i < (length L) - 1) → (list-ref L i) ≤ (list-ref
L i+1)
These two definitions are logically equivalent.
If we verify that L is sorted using this definition,
how many comparisons will we need?
101
Module 5 outline
• Predicates vs Propositions
• Examples
• More examples: sorted lists
• Algorithm efficiency revisited.
• Additional examples to consider.
102
Additional examples
Specifying the behaviour of a function/method
that takes a list l and a value x:
Translate “returns true if and only if either l and x
are both equal to null, or l contains at least one
element e that is equal to x.
Define a predicate Prime(x) that evaluates to true
if and only if x is a prime. Assume that you have a
predicate | such that x | y is true if and only if x
divides y (that is, y/x is an integer).
103