Module le 5 5: P Predic icate L Logic ic Pre-class quiz #6 is - - PowerPoint PPT Presentation

module le 5 5 p predic icate l logic ic
SMART_READER_LITE
LIVE PREVIEW

Module le 5 5: P Predic icate L Logic ic Pre-class quiz #6 is - - PowerPoint PPT Presentation

2/5/17 Admin Ad Midterm 1: read the Piazza post on the two-stage exam. Assignment #2 is due Thursday Feb 2 at 4pm. Module le 5 5: P Predic icate L Logic ic Pre-class quiz #6 is due Thursday Feb 2 at 7pm. Assigned reading


slide-1
SLIDE 1

1

2/5/17 1

Module le 5 5: P Predic icate L Logic ic

1

Ad 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

Le Learni ning ng goals: pr pre-cl 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

CP CPSC C 121 121 - th the BI BIG que questions ns

  • 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

slide-2
SLIDE 2

2

2/5/17 2

Le Learni ning ng goals: in in-cl 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

Mo Module 5 outline

  • Predicates vs Propositions
  • Examples
  • More examples: sorted lists
  • Algorithm efficiency revisited.
  • Additional examples to consider.

6

Li Limitations ns of Propo positiona nal Lo Logic as a Mo 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

Wh What is predicate logic go good for mo 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

  • ther.

9

slide-3
SLIDE 3

3

2/5/17 3

Ex Exampl ples of pr predi dicate logi gic us 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

Qu Quantifier sy 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

Qu 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

An 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

slide-4
SLIDE 4

4

2/5/17 4

Qu Quantifier scope

A quantifier applies to everything to its right, until a closing parenthesis stops it.

14

Qu Quantifier scope

A quantifier applies to everything to its right, until a closing parenthesis stops it. ∀𝑦 ∈ 𝐸, (∃𝑧 ∈ 𝐹, 𝑅 𝑦, 𝑧 → ∀𝑨 ∈ 𝐺, 𝑆 𝑧, 𝑨 ฀ ฀ )∧ 𝑄 𝑦 ฀

∀𝑦 ∈ 𝐸 applies to the entire expression.

15

Qu Quantifier scope

A quantifier applies to everything to its right, until a closing parenthesis stops it. ∀𝑦 ∈ 𝐸, (∃𝑧 ∈ 𝐹, 𝑅 𝑦, 𝑧 → ∀𝑨 ∈ 𝐺, 𝑆 𝑧, 𝑨 ฀ ฀ )∧ 𝑄 𝑦 ฀

∃𝑧 ∈ 𝐹 applies to this subexpression.

16

Qu Quantifier scope

A quantifier applies to everything to its right, until a closing parenthesis stops it. ∀𝑦 ∈ 𝐸, (∃𝑧 ∈ 𝐹, 𝑅 𝑦, 𝑧 → ∀𝑨 ∈ 𝐺, 𝑆 𝑧, 𝑨 ฀ ฀ )∧ 𝑄 𝑦 ฀

∀𝑨 ∈ 𝐺 applies to this subexpression.

17

slide-5
SLIDE 5

5

2/5/17 5

Qu 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

Ne Negation n sc 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

Wh 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

Wh 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

  • f the predicate.

23

slide-6
SLIDE 6

6

2/5/17 6

Wh Which variables ar 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

Wh 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 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 In contrast, what is a proposition?

  • Can a proposition have unbound variables?
  • a. Yes.
  • b. No.
  • c. I don’t know.

28

slide-7
SLIDE 7

7

2/5/17 7

In 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

Dif ifference be between n a pr propo position an and a a pr predi dicate

  • 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

Qu 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

Wh 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

slide-8
SLIDE 8

8

2/5/17 8

De Descri ribi bing ng 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

De Descri ribi bing ng 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.)

35

“…” is an ellipsis

Mo Module 5 outline

  • Predicates vs Propositions
  • Examples
  • More examples: sorted lists
  • Algorithm efficiency revisited.
  • Additional examples to consider.

36

Tr Translating between English and pr predi dicate logi gic

  • 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

slide-9
SLIDE 9

9

2/5/17 9

Tr Translating between English and pr predi dicate logi gic

  • 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 38

Ma 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

Re 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

Re 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

slide-10
SLIDE 10

10

2/5/17 10

Re 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

Re 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

Re 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

Re 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

slide-11
SLIDE 11

11

2/5/17 11

Re 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

Re 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

Re 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

Re 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

slide-12
SLIDE 12

12

2/5/17 12

Re 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

  • f animals.
  • d. No, for another reason.
  • e. I don’t know.

50

Mi 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

Mi 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

Mi 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

slide-13
SLIDE 13

13

2/5/17 13

At 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 At least…

55

“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.” ∃𝑦 ∈ 𝐵, ∃𝑧 ∈ 𝐵, 𝑀 𝑦 ∧ 𝑀 𝑧 ∧ 𝑦 ≠ 𝑧

At At most…

56

“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.”

Ex 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

slide-14
SLIDE 14

14

2/5/17 14

Ex 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

Mo Module 5 outline

  • Predicates vs Propositions
  • Examples
  • More examples: sorted lists
  • Algorithm efficiency revisited.
  • Additional examples to consider.

59

A 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 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

slide-15
SLIDE 15

15

2/5/17 15

A 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

An 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

Ho How do we get a working predicate?

Sorted L ≡ ∀𝑗 ∈ 𝑂, ∀𝑘 ∈ 𝑂, 𝑗 < 𝑘 → (list − ref L i) < (list − ref L j)

66

Mo Module 5 outline

  • Predicates vs Propositions
  • Examples
  • More examples: sorted lists
  • Algorithm efficiency revisited.
  • Additional examples to consider.

68

slide-16
SLIDE 16

16

2/5/17 16

A 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

Co Comparing two search algorithms

Assumptions: Reading the name after the current name takes 1s

  • n 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

Co 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

  • f their inputs.

71

The The Big-O 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

slide-17
SLIDE 17

17

2/5/17 17

The The Big-O O Notation in quantifiers

  • 𝑔 is in 𝑃(𝑕) if

∃𝑑 ∈ 𝑆f, ∃𝑜0 ∈ 𝑂, ∀𝑜 ∈ 𝑂, 𝑜 ≥ 𝑜0 → 𝑔(𝑜) ≤ 𝑑𝑕(𝑜)

73

Wh 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

Wh When th the input t is is suffic icie iently ly la 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 The thr hresho eshold d value ue 𝑜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

slide-18
SLIDE 18

18

2/5/17 18

Le Let’s unde understand nd c

  • 𝑔 is in 𝑃(𝑕) if

∃𝑑 ∈ 𝑆f, ∃𝑜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

Le Let’s unde understand nd c

  • 𝑔 is in 𝑃(𝑕) if

∃𝑑 ∈ 𝑆f, ∃𝑜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

Le Let’s unde understand nd c

  • 𝑔 is in 𝑃(𝑕) if

∃𝑑 ∈ 𝑆f, ∃𝑜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

Co Comparing linear and binary search

  • The running time of binary search is 𝑔 𝑜 = log 𝑜 .
  • The running time of linear search is 𝑕 𝑜 = 𝑜.
  • 𝑔 is in 𝑃(𝑕) if

∃𝑑 ∈ 𝑆f, ∃𝑜0 ∈ 𝑂, ∀𝑜 ∈ 𝑂, 𝑜 ≥ 𝑜0 → 𝑔(𝑜) ≤ 𝑑𝑕(𝑜)

  • Is 𝑔(𝑜) ∈ 𝑃(𝑕)? Prove it.

80

slide-19
SLIDE 19

19

2/5/17 19

86

Mo Module 5.4: Algorithm efficiency re revisited

Which of the following functions f(n) is the last

  • ne 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

Mo Module 5.4: Algorithm efficiency re 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

Mo Module 5.4: Algorithm efficiency re revisited

Is the following predicate true for f(n) = n? ∃c ∈ R+ ∀n ∈ N f(n) ≤ cnlog2n a.Yes b.No

93

slide-20
SLIDE 20

20

2/5/17 20

Mo Module 5.4: Algorithm efficiency re 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

Mo Module 5.4: Algorithm efficiency re 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

Mo Module 5.4: Algorithm efficiency re revisited

Pictorially:

n0

c

98

Mo Module 5.4: Algorithm efficiency re revisited

Some common running times:

1 log n n nlog n n2 2n

99

slide-21
SLIDE 21

21

2/5/17 21

Mo Module 5.4: Algorithm efficiency re revisited

Revisiting sorted lists: Recall Sorted(L) ≡ ∀i ∈ N ∀j ∈ N

(0 ≤ i) ^ (i < j) ^ (j < (length L)) → (list-ref L i) ≤ (list- ref L j)

If we verify that L is sorted using this definition, how many comparisons will we need? Can we do better?

100

Mo Module 5.4: Algorithm efficiency re 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

Mo Module 5 outline

  • Predicates vs Propositions
  • Examples
  • More examples: sorted lists
  • Algorithm efficiency revisited.
  • Additional examples to consider.

102

Ad 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