CPSC 121: Models of Computation Module 5: Predicate Logic Module 5: - - PowerPoint PPT Presentation

cpsc 121 models of computation
SMART_READER_LITE
LIVE PREVIEW

CPSC 121: Models of Computation Module 5: Predicate Logic Module 5: - - PowerPoint PPT Presentation

CPSC 121: Models of Computation Module 5: Predicate Logic Module 5: Predicate Logic Midterm 1: Friday October 12 th , 17:30 to 18:45 A to G (by last name): room FSC 1005 H to O: room ESB 1013 P to Z: room CIRS 1250 Modules 1 to 5 (up to


slide-1
SLIDE 1

CPSC 121: Models of Computation

Module 5: Predicate Logic

slide-2
SLIDE 2

CPSC 121 – 2018W T1 2

Module 5: Predicate Logic

Midterm 1:

Friday October 12th, 17:30 to 18:45

A to G (by last name): room FSC 1005 H to O: room ESB 1013 P to Z: room CIRS 1250

Modules 1 to 5 (up to about the end of next Tuesday’s class), plus

Labs 1 to 3.

You can bring

Nothing but we will provide the parts of Dave’s excellent handout that you need.

slide-3
SLIDE 3

CPSC 121 – 2018W T1 3

Module 5: Predicate Logic

Assignment #2 is due Wednesday October 10th at 19:00. Pre-class quiz #6 is due Monday October 15th at 19:00

Assigned reading for the quiz:

Epp, 4th edition: 3.2, 3.4 Epp, 3rd edition: 2.2, 2.4 Rosen, 6th edition: 1.3, 1.4 Rosen, 7th edition: 1.4, 1.5

slide-4
SLIDE 4

CPSC 121 – 2018W T1 4

Module 5: Predicate Logic

Pre-class quiz #7 is tentatively due Wednesday October 24th at 19:00.

Assigned reading for the quiz:

Epp, 4th edition: 4.1, 4.6, Theorem 4.4.1 Epp, 3rd edition: 3.1, 3.6, Theorem 3.4.1. Rosen, 6th edition: 1.6, 1.7. 3.4 (theorem 2 only). Rosen 7th edition: 1.7, 1.8, 4.1 (theorem 2 only).

slide-5
SLIDE 5

CPSC 121 – 2018W T1 5

Module 5: Predicate Logic

Quiz 5 feedback:

Very well done; only one question below 90%:

If D is the set of 8 bit signed integers, is ∀x D, y D, ((x > 0) ^ (y > 0)) → (x + y) > 0 ∈ ∀ ∈ true? Answer: NO. For instance 100 + 100 = -56.

As usual, we will discuss the open-ended question (what it means for an algorithm to be faster or slower than another one) in class.

slide-6
SLIDE 6

CPSC 121 – 2018W T1 6

Module 5: Predicate Logic

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.

slide-7
SLIDE 7

CPSC 121 – 2018W T1 7

Module 5: Predicate Logic

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

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!

slide-8
SLIDE 8

CPSC 121 – 2018W T1 8

Module 5: Predicate Logic

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

slide-9
SLIDE 9

CPSC 121 – 2018W T1 9

Module 5: Predicate Logic

Module Summary

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

slide-10
SLIDE 10

CPSC 121 – 2018W T1 10

Module 5.1: Predicates vs Propositions

Is Propositional Logic a complete model? Can it be used to model every real-world situation? Which of the following can it model effectively?

a) Specializing abstract patterns to specific instances, like “... so Donald Trump was born in the United States of America”. 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) It can model all of these effectively. e) It can not model any of these effectively.

slide-11
SLIDE 11

CPSC 121 – 2018W T1 12

Module 5.1: Predicates vs Propositions

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.

slide-12
SLIDE 12

CPSC 121 – 2018W T1 13

Module 5.1: Predicates vs Propositions

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. [CPSC 221] Language definition: No path via references exists from any variable in scope to any memory location available for garbage collection... [CPSC 311 or 312] Databases: the relational model is based on predicate

  • logic. [CPSC 304]

Algorithms: in the worst case, every comparison sort requires at least cnlog2n comparisons to sort n values, for some constant c > 0. [CPSC 320]

slide-13
SLIDE 13

CPSC 121 – 2018W T1 14

Module 5.1: Predicates vs Propositions

Quantifiers scope:

A quantifier applies to everything to its right, up to the closing parenthesis of the () pair that “contains” it. Example:

∀ x∈ D ,∃ y∈E ,Q x , y ∀ z∈ F , R y , z

∧ Px

slide-14
SLIDE 14

CPSC 121 – 2018W T1 15

Module 5.1: Predicates vs Propositions

Quantifiers scope (continued)

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) Both a and b. e) Both b and c.

slide-15
SLIDE 15

CPSC 121 – 2018W T1 17

Module 5.1: Predicates vs Propositions

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) None of the above.

slide-16
SLIDE 16

CPSC 121 – 2018W T1 19

Module 5.1: Predicates vs Propositions

What is the 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 may contain unbound variables, but a proposition does not. d) They are the same thing, using different names. e) None of the above.

slide-17
SLIDE 17

CPSC 121 – 2018W T1 21

Module 5.1: Predicates vs Propositions

What is the truth value of the proposition:

∃ x ∈ Z, x*x = y ? a) True because (for example) 5 * 5 = 25 b) True because every y = √ y * √ y c) False, because of counterexamples like no integer multiplied by itself equals 3 d) It depends on y, but given a value for y, we could calculate a truth value. e) None of the above.

slide-18
SLIDE 18

CPSC 121 – 2018W T1 23

Module 5.1: Predicates vs Propositions

Which variables do we need values for in order to determine this formula's truth value?

∀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.

slide-19
SLIDE 19

CPSC 121 – 2018W T1 25

Module 5.1: Predicates vs Propositions

A predicate is a logic formula with unbound variables, such as

Perfect Square(y): ∃x ∈ Z, x*x = y

Then

PerfectSquare(25) is PerfectSquare(27) is ∃y ∈ Z, PerfectSquare(y) is ∀y ∈ Z, PerfectSquare(y) is

slide-20
SLIDE 20

CPSC 121 – 2018W T1 26

Module 5: Predicate Logic

Module Summary

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

slide-21
SLIDE 21

CPSC 121 – 2018W T1 27

Module 5.2: Examples

Given the definitions:

F: the set of foods. E(x): Alice eats food x. g: Alice grows. s: Alice shrinks.

(c) Walt Disney Co.

slide-22
SLIDE 22

CPSC 121 – 2018W T1 28

Module 5.2: Examples

Express these statements using predicate logic:

Eating food causes Alice to grow or shrink. Alice shrank when she ate some food.

slide-23
SLIDE 23

CPSC 121 – 2018W T1 29

Module 5.2: Examples

Given the definitions:

F(x): x is a fierce creature. L(x): x is a lion C(x): x drinks coffee D: the set of all creatures. T(x,y): creature x has “tasted” creature y.

(c) animal.discovery.com

slide-24
SLIDE 24

CPSC 121 – 2018W T1 30

Module 5.2: Examples

Express these statements using predicate logic:

All lions are fierce. Some lions do not drink coffee.

slide-25
SLIDE 25

CPSC 121 – 2018W T1 31

Module 5.2: Examples

Compare and contrast the following: Some creature drinks coffee Some lion drinks coffee Some fierce lion drinks coffee (restricting the domain of an existential stmt)

slide-26
SLIDE 26

CPSC 121 – 2018W T1 32

Module 5.2: Examples

Compare and contrast the following: Every creature drinks coffee Every lion drinks coffee Every fierce lion drinks coffee (restricting the domain of an universal stmt)

slide-27
SLIDE 27

CPSC 121 – 2018W T1 33

Module 5.2: Examples

Consider the statement All fierce creatures are not lions

Give two different (not logically equivalent) translations into predicate logic. Why did we end up with two translations?

slide-28
SLIDE 28

CPSC 121 – 2018W T1 34

Module 5.2: Examples

Express these two propositions in English:

∀x D, ∈ y D, T(x,y) ∃ ∈ ∃y D, x ∈ ∀ D, T(x,y) ∈

Give an example where one of the propositions is true, and the other proposition is false.

slide-29
SLIDE 29

CPSC 121 – 2018W T1 35

Module 5: Predicate Logic

Module Summary

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

slide-30
SLIDE 30

CPSC 121 – 2018W T1 36

Module 5.3: More examples: sorted lists

Definitions:

Assume that L represents a list of values. The length of L is denoted by (length L). The ith element of L is denoted by (list-ref L i).

The first element of L is (list-ref L 0).

Are length and list-ref predicates?

No: a predicate is a function that returns true or false. What do these functions return?

length: an integer. list-ref: a value whose type depends on the contents of L.

slide-31
SLIDE 31

CPSC 121 – 2018W T1 37

Module 5.3: More examples: sorted lists

Problem:

Define a predicate Sorted(L) whose value is true if and only if L is sorted in non-decreasing order. We can use the functions length and list-ref.

Assumption:

The call (list-ref L i) returns an undefined value if i is negative, or greater than or equal to (length L).

Recall the first element of L is (list-ref L 0), so L's last element is (list-ref L (- (length L) 1)).

slide-32
SLIDE 32

CPSC 121 – 2018W T1 38

Module 5.3: More examples: sorted lists

Which of the following is/are a problem with this definition?

Sorted(L) ≡ i N j N (list-ref L i) ^ (list-ref L j) ^ ∀ ∈ ∀ ∈ vi < vj a) There is no quantifier for L. b) There are no quantifiers for vi and vj. c) We can not use ^ with (list-ref L i) and (list-ref L j) d) Both (a) and (b) e) Both (b) and (c)

slide-33
SLIDE 33

CPSC 121 – 2018W T1 40

Module 5.3: More examples: sorted lists

Which of the following is a problem with this definition?

Sorted(L) ≡ i N j N i < j →(list-ref L i) < ∀ ∈ ∀ ∈ (list-ref L j) a) It is too restrictive (it does not allow for equal values). b) It does not restrict the ranges of i and j. c) It is missing quantifiers. d) Both (a) and (b) e) Both (b) and (c)

slide-34
SLIDE 34

CPSC 121 – 2018W T1 42

Module 5.3: More examples: sorted lists

How do we modify the attempt on the previous slide to get a working predicate?

Sorted(L) ≡ i N j N ∀ ∈ ∀ ∈ i < j → (list-ref L i) < (list-ref L j)

slide-35
SLIDE 35

CPSC 121 – 2018W T1 43

Module 5.3: More examples: sorted lists

There exists means there is at least one. How do we write there is exactly one?

lists have exactly one element at each valid index.

Definitions:

There is exactly one ≡ There is at most one with property P ≡ There are at least two ≡

slide-36
SLIDE 36

CPSC 121 – 2018W T1 44

Module 5.3: More examples: sorted lists

Soon we will use English more often than we will write every predicate explicitly using logic. However the ability to use predicate logic will help us think things through and not overlook minor (but important) details.

“when we become comfortable with formal manipulations, we can use them to check our intuition, and then we can use our intuition to check

  • ur formal manipulations.” -- Epp, (4th ed), p. 127
slide-37
SLIDE 37

CPSC 121 – 2018W T1 45

Module 5: Predicate Logic

Module Summary

Predicates vs Propositions Examples More examples: sorted lists Algorithm efficiency revisited

Additional examples to consider

slide-38
SLIDE 38

CPSC 121 – 2018W T1 46

Module 5.4: Algorithm efficiency revisited

What does it mean for one algorithm to be generally faster than another algorithm? Here are some answers we saw on the quiz:

For one algorithm to be faster I think it might mean that that algorithm is faster and scales better as the problem gets larger... I think that an algorithm is generally faster if it involves less amounts of steps and if all those steps take less amounts of time to complete. An example

  • f this was encountered in the CPSC 121 lab... AND

trees were faster because they completed a lot more steps simultaneously...

slide-39
SLIDE 39

CPSC 121 – 2018W T1 47

Module 5.4: Algorithm efficiency revisited

More responses:

Algorithms describe processes that run on real computers with finite resources. So in order to determine which algorithm is faster, we need to look for two factors: consume an amount of time, memory. An algorithm can be designed to gain faster run times at the cost of using extra space. Or some users might be willing to pay for more memory to get a faster algorithm, whereas others would rather settle for a slower algorithm that economizes on memory.

slide-40
SLIDE 40

CPSC 121 – 2018W T1 48

Module 5.4: Algorithm efficiency revisited

More responses:

One way might to be analyzing the average speed of all of the inputs for each algorithm, or at least looking at the most commonly used inputs that you’ll be using for the algorithm. If we know something about the answer we’re looking for (for example with the phonebook seeing what letter the name starts with and starting the search from the closest side) we could use that information and analyze which algorithm would make the most sense for dealing with the specific answer you’re looking for.

slide-41
SLIDE 41

CPSC 121 – 2018W T1 49

Module 5.4: Algorithm efficiency revisited

Consider the following problem:

Given: a sorted list of names with phone numbers. Wanted: the phone number for a given name N.

Which algorithm is generally faster?

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.

slide-42
SLIDE 42

CPSC 121 – 2018W T1 50

Module 5.4: Algorithm efficiency revisited

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.

slide-43
SLIDE 43

CPSC 121 – 2018W T1 51

Module 5.4: Algorithm efficiency revisited

For a list with 63 names:

Algorithm L takes 63 * 1s = 1m 3s in the worst case. Algorithm B takes 6 * 10s = 1m 0s 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.

slide-44
SLIDE 44

CPSC 121 – 2018W T1 52

Module 5.4: Algorithm efficiency revisited

How do we determine whether or not an algorithm is generally faster than another?

We want to measure how good the algorithm is, in a way that does not depend on

the programming language used to implement it. the quality of the compiler or interpreter. the speed of the computer it is executed on.

 One idea is to count the number of elementary steps
  • f the algorithm as a function of the size of its input n.

A step is anything that can be computed in constant time, that is, independent from n.

slide-45
SLIDE 45

CPSC 121 – 2018W T1 53

Module 5.4: Algorithm efficiency revisited

Is an algorithm with 3n steps faster than one with 6n steps?

a) Yes, always. b) No, never. c) Sometimes. d) None of the above.

slide-46
SLIDE 46

CPSC 121 – 2018W T1 55

Module 5.4: Algorithm efficiency revisited

Example:

One algorithm performs 6n steps of the following type (only the first 6 are written): The other algorithm performs 3n steps of the following type (only the first 3 are written): Which one is faster?

slide-47
SLIDE 47

CPSC 121 – 2018W T1 56

Module 5.4: Algorithm efficiency revisited

Facts about execution times:

we can not rely on the values of the constants in front of the functions describing the number of steps. it's almost impossible to compute the number of steps exactly.

So we want to come up with

a way to count steps that ignores these constants. an approximation of the correct number of steps.

slide-48
SLIDE 48

CPSC 121 – 2018W T1 57

Module 5.4: Algorithm efficiency revisited

Terminology: an algorithm runs in O(g) time, stated “big-Oh of g time”, if it performs at most g(n) steps (approximately). Examples:

Algorithm L runs in O(n) time. Algorithm B runs in O(log2 n) time. The algorithm we used to order students by date of birth runs in O(n2) time.

Let's see how we can define O using quantifiers.

slide-49
SLIDE 49

CPSC 121 – 2018W T1 58

Module 5.4: Algorithm efficiency revisited

Which of the following predicates says that the number of steps f(n) is (approximately) at most n2 ?

a) ∀c R ∈

+ n

N f(n) ∀ ∈ ≤ cn2 b) ∃c R ∈

+ n

N f(n) ∃ ∈ ≤ cn2 c) ∃c R ∈

+ n

N f(n) ∀ ∈ ≤ cn2 d) ∀c R ∈

+ n

N f(n) ∃ ∈ ≤ cn2 e) None of the above.

slide-50
SLIDE 50

CPSC 121 – 2018W T1 60

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

slide-51
SLIDE 51

CPSC 121 – 2018W T1 62

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.

slide-52
SLIDE 52

CPSC 121 – 2018W T1 64

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

slide-53
SLIDE 53

CPSC 121 – 2018W T1 66

Module 5.4: Algorithm efficiency revisited

Is the following predicate true for f(n) = n?

∃c R ∈

+ n

∃ N n N n ∈ ∀ ∈ ≥ n0 → f(n) ≤ cnlog2n a) Yes b) No

slide-54
SLIDE 54

CPSC 121 – 2018W T1 68

Module 5.4: Algorithm efficiency revisited

So we define O(g) by:

f is in O(g) if ∃c R ∈

+ n

∃ N n N n ∈ ∀ ∈ ≥ n0 → f(n) ≤ cg(n)

slide-55
SLIDE 55

CPSC 121 – 2018W T1 69

Module 5.4: Algorithm efficiency revisited

Pictorially:

n0

c

slide-56
SLIDE 56

CPSC 121 – 2018W T1 70

Module 5.4: Algorithm efficiency revisited

Some common running times:

1 log n n nlog n n2 2n

slide-57
SLIDE 57

CPSC 121 – 2018W T1 71

Module 5.4: Algorithm efficiency revisited

How long will my algorithm take to execute?

Time n = 10 n = 20 n = 100 n = 1000 n 0.00001 sec 0.00002 sec 0.0001 sec 0.001 sec n2 0.0001 sec 0.0004 sec 0.01 sec 1 sec n3 0.001 sec 0.008 sec 1 sec 16 min 40 sec n5 0.1 sec 3.2 sec 2h 47 min 31.7 years 2n 0.001 sec 1 sec 4 x 1016 yrs way too long 3n 0.059 sec 58 min 1034 yrs eons

slide-58
SLIDE 58

CPSC 121 – 2018W T1 72

Module 5.4: Algorithm efficiency revisited

How big a problem can I solve if my computer is k times faster?

Time k = 1 k = 10 k = 100 k = 1000 n N 10N 100N 1000N n2 N 3.16N 10N 31.6N n3 N 2.15N 4.64N 10N n5 N 1.58N 2.51N 3.98N 2n N N + 3 N + 6 N + 9 3n N N + 2 N + 4 N + 6

slide-59
SLIDE 59

CPSC 121 – 2018W T1 73

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) ≤ (list-ref L j)

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

slide-60
SLIDE 60

CPSC 121 – 2018W T1 74

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?

slide-61
SLIDE 61

CPSC 121 – 2018W T1 75

Module 5: Predicate Logic

Module Summary

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

slide-62
SLIDE 62

CPSC 121 – 2018W T1 76

Module 5.5: Additional examples to consider

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

  • nly if x divides y (that is, y/x is an integer).