CPSC 121: Models of Computation Module 5: Predicate Logic
Module 5: Predicate Logic Midterm 1: Friday February 9 th, 17:00 to 18:15 A to C (by last name): room DMP 310 D to K: room MATH 100 L to P: room FSC 1005 Q to Z: room ESB 1013 Modules 1 to 5, plus Labs 1 to 4. You can bring Two 2-sided letter-size (8.5 x 11in, 21.59 x 27.94cm) piece of paper but NO CALCULATOR CPSC 121 2017W T2 2
Module 5: Predicate Logic Assignment #2 is due Tuesday February 6 th at 16:00. Pre-class quiz #6 is due Tuesday February 13 th at 19:00 Assigned reading for the quiz: Epp, 4 th edition: 3.2, 3.4 Epp, 3 rd edition: 2.2, 2.4 Rosen, 6 th edition: 1.3, 1.4 Rosen, 7 th edition: 1.4, 1.5 CPSC 121 2017W T2 3
Module 5: Predicate Logic Pre-class quiz #7 is tentatively due Thursday March 1 st at 19:00. Assigned reading for the quiz: Epp, 4 th edition: 4.1, 4.6, Theorem 4.4.1 Epp, 3 rd edition: 3.1, 3.6, Theorem 3.4.1. Rosen, 6 th edition: 1.6, 1.7. 3.4 (theorem 2 only). Rosen 7 th edition: 1.7, 1.8, 4.1 (theorem 2 only). CPSC 121 2017W T2 4
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. CPSC 121 2017W T2 5
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. CPSC 121 2017W T2 6
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!? CPSC 121 2017W T2 7
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 i th repetition of this algorithm, the variable min contains the smallest element in the list between element 0 and element i. CPSC 121 2017W T2 8
Module 5: Predicate Logic Module Summary Predicates vs Propositions Examples More examples: sorted lists Algorithm efficiency revisited. Additional examples to consider. CPSC 121 2017W T2 9
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. CPSC 121 2017W T2 10
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. CPSC 121 2017W T2 12
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 cnlog 2 n comparisons to sort n values, for some constant c > 0. [CPSC 320] CPSC 121 2017W T2 13
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 CPSC 121 2017W T2 14
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) None of the above. CPSC 121 2017W T2 15
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) (~( x Z +, y Z +, x < y Even(y) )) e) None of the above. CPSC 121 2017W T2 17
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. CPSC 121 2017W T2 19
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. CPSC 121 2017W T2 21
Module 5.1: Predicates vs Propositions Which variables do we need values for in order to determine this formula's truth value? i a) i and v b) l and n c) n and v d) i and n Z +, (i n) ~ v Z +, HasValue(l, i, v) e) None of these are correct. CPSC 121 2017W T2 23
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 CPSC 121 2017W T2 25
Module 5: Predicate Logic Module Summary Predicates vs Propositions Examples More examples: sorted lists Algorithm efficiency revisited. Additional examples to consider. CPSC 121 2017W T2 26
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. CPSC 121 2017W T2 27
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. CPSC 121 2017W T2 28
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 CPSC 121 2017W T2 29
Module 5.2: Examples Express these statements using predicate logic: All lions are fierce. Some lions do not drink coffee. CPSC 121 2017W T2 30
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) CPSC 121 2017W T2 31
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) CPSC 121 2017W T2 32
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? CPSC 121 2017W T2 33
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. CPSC 121 2017W T2 34
Module 5: Predicate Logic Module Summary Predicates vs Propositions Examples More examples: sorted lists Algorithm efficiency revisited. Additional examples to consider. CPSC 121 2017W T2 35
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 i th 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. CPSC 121 2017W T2 36
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)). CPSC 121 2017W T2 37
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) ^ v i < v j a) There is no quantifier for L. b) There are no quantifiers for v i and v j. 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) CPSC 121 2017W T2 38
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) CPSC 121 2017W T2 40
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) CPSC 121 2017W T2 42
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 CPSC 121 2017W T2 43
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 our formal manipulations. -- Epp, (4 th ed), p. 127 CPSC 121 2017W T2 44
Module 5: Predicate Logic Module Summary Predicates vs Propositions Examples More examples: sorted lists Algorithm efficiency revisited Additional examples to consider CPSC 121 2017W T2 45
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: Algorithm A is generally faster because as the problem size gets larger, A will only get slightly slower while B will get exponentially slower, so B is a slower algorithm in general. For an algorithm to be considered generally faster. It must be in most cases faster. Looking at the diagram above we see that algorithm B is generally faster up to a certain point.certain algorithms work best with a small set of data but then when scaling to larger collections of data get slower. CPSC 121 2017W T2 46
Module 5.4: Algorithm efficiency revisited More responses: For an algorithm to be faster than another, it could mean that the algorithm takes less time to produce an output than another. However, if the speed of an algorithm depends on the input it's given, then we can't necessarily say it is ""faster,"" because it isn't always so. We could then define an algorithm as faster if, on average, it takes the least amount of time to return an output as compared to other algorithms. We could also define faster based on the storage space used up by an algorithm; a simpler algorithm could be equivalent to a more complicated one but be more efficient with both time and space. CPSC 121 2017W T2 47
Module 5.4: Algorithm efficiency revisited More responses: There is likely a certain range of inputs for which Algorithm B is much faster at solving compared to Algorithm A, and vice versa. What would have to be looked at is which range of inputs are more commonly used. The easiest way to compare the two algorithms would be to compare the longest amount of time either algorithm could possibly take. It would also be useful to compare the average amount of time each algorithm would take. The easiest way to compare the two algorithms would be to compare the longest amount of time either algorithm could possibly take. It would also be useful to compare the average amount of time each algorithm would take. CPSC 121 2017W T2 48
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. CPSC 121 2017W T2 49
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. CPSC 121 2017W T2 50
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. CPSC 121 2017W T2 51
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 of 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. CPSC 121 2017W T2 52
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. CPSC 121 2017W T2 53
Module 5.4: Algorithm efficiency revisited Example: One algorithm performs 6n steps of the following type (only the first 6 are written): 3 + 8 2 + 4 6 + 9 2 + 11 5 + 6 7 + 1 The other algorithm performs 3n steps of the following type (only the first 3 are written): 2 5 x 4 x 2 2 dx 1 6 xcos( x)+sin (x)dx 1 1 x 2 dx Which one is faster? CPSC 121 2017W T2 55
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. CPSC 121 2017W T2 56
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(log 2 n) time. The algorithm we used to order students by date of birth runs in O(n 2 ) time. Let's see how we can define O using quantifiers. CPSC 121 2017W T2 57
Module 5.4: Algorithm efficiency revisited Which of the following predicates says that the number of steps f(n) is (approximately) at most n 2? a) c R + n N f(n) cn 2 b) c R + n N f(n) cn 2 c) c R + n N f(n) cn 2 d) c R + n N f(n) cn 2 e) None of the above. CPSC 121 2017W T2 58
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) = n 2 /2 c) f(n) = 3n 2 d) f(n) = n 3 e) f(n) = 2 n CPSC 121 2017W T2 60
Module 5.4: Algorithm efficiency revisited Which of the following two functions grows faster? a) f(n) = n b) f(n) = nlog 2 n c) Neither; they both grow equally fast. CPSC 121 2017W T2 62
Module 5.4: Algorithm efficiency revisited Is the following predicate true for f(n) = n? c a) Yes b) No R + n N f(n) cnlog 2 n CPSC 121 2017W T2 64
Module 5.4: Algorithm efficiency revisited Is the following predicate true for f(n) = n? c a) Yes b) No R + n 0 N n N n n 0 f(n) cnlog 2 n CPSC 121 2017W T2 66
Module 5.4: Algorithm efficiency revisited So we define O(g) by: f is in O(g) if c R + n 0 N n N n n 0 f(n) cg(n) CPSC 121 2017W T2 68
Module 5.4: Algorithm efficiency revisited Pictorially: c CPSC 121 2017W T2 69 n 0
Module 5.4: Algorithm efficiency revisited Some common running times: 1 log n n nlog n n 2 2 n CPSC 121 2017W T2 70
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 n 2 0.0001 sec 0.0004 sec 0.01 sec 1 sec n 3 0.001 sec 0.008 sec 1 sec 16 min 40 sec n 5 0.1 sec 3.2 sec 2h 47 min 31.7 years 2 n 0.001 sec 1 sec 4 x 10 16 yrs way too long 3 n 0.059 sec 58 min 10 34 yrs eons CPSC 121 2017W T2 71
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 n 2 N 3.16N 10N 31.6N n 3 N 2.15N 4.64N 10N n 5 N 1.58N 2.51N 3.98N 2 n N N + 3 N + 6 N + 9 3 n N N + 2 N + 4 N + 6 CPSC 121 2017W T2 72
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? CPSC 121 2017W T2 73
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? CPSC 121 2017W T2 74
Module 5: Predicate Logic Module Summary Predicates vs Propositions Examples More examples: sorted lists Algorithm efficiency revisited Additional examples to consider CPSC 121 2017W T2 75
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 only if x divides y (that is, y/x is an integer). CPSC 121 2017W T2 76