Download presentation
Presentation is loading. Please wait.
Published byBarry Patterson Modified over 9 years ago
1
snick snack CPSC 121: Models of Computation 2012 Summer Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville and others 1
2
Outline Prereqs, Learning Goals, and Quiz Notes Prelude: Scope and Predicate Definition Problems and Discussion –Liszt Etudes –Sorted Lists –Comparing Algorithms Next Lecture Notes 2
3
Learning Goals: Pre-Class By the start of class, you should be able to: –Evaluate the truth of predicates applied to particular values. –Show predicate logic statements are true by enumerating examples (i.e., all examples in the domain for a universal or one for an existential). –Show predicate logic statements are false by enumerating counterexamples (i.e., one counterexample for universals or all in the domain for existentials). –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
4
Learning Goals: In-Class By the end of this unit, you should be able to: –Build statements about the relationships between properties of various objects—which 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”)—using predicate logic. 4
5
Where We Are in The Big Stories Theory How do we model computational systems? Now: Finally getting a tool that we can use to model the complex systems we really want to reason about! Hardware How do we build devices to compute? Now: Still taking a bit of a vacation in lecture! 5
6
Quiz 5 Notes If x and y are drawn from the 8-bit signed ints, x = 127, and y = 1, what is x+y? a.-128 b.-127 c.0 d.128 e.None of these. 6 Is “for all positive 8-bit signed x and y, x + y is positive” true?
7
Quiz 5 Notes How would we say: “People who are afraid of ladders are also afraid of shadows?” We are making a universal statement, but only about people who match a particular quality: “For any person, if they’re afraid of ladders, then they’re also afraid of shadows.” What is it now? 7
8
Quiz 5 Notes Is Prime(10) Odd(2) true? Prime(10) means “10 is prime”, which is false. Odd(2) means “2 is odd”, which is false. Is F F true or false? 8
9
Outline Prereqs, Learning Goals, and Quiz Notes Prelude: Motivation, Scope & Defining Predicates Problems and Discussion –Liszt Etudes –Sorted Lists –Comparing Algorithms Next Lecture Notes 9
10
Problem: Translate to Propositional Logic Steve is married to Rachel. Steve is married to Rachel and Rachel is married to Steve. Steve is not married to anyone other than Rachel. 10 NO predicates allowed, just things like p, q, r, etc.
11
Limitations of Propositional Logic as a Model Which of the following can propositional logic model effectively? a.Relationships among factory production lines like “wheel assembly and frame welding both feed into the undercarriage line”. 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 cannot model any of these effectively. 11
12
What Does Predicate Logic Model? Relationships among real-world objects Generalizations about patterns Infinite (or even just big) domains 12 Problems where the properties of different concepts, ideas, parts, or entities depend on each other.
13
But... Would You Ever Really Use Pred Logic? Data Structures Example: “...every key is less than or equal to all of its children’s keys...” AI example: “...let h' be a ‘heuristic’ function evaluating game states and h be the true value of the state. For all nodes n, h'(n) h(n)...” Java example: “...there is no path via references from any variable in scope to any memory location available for garbage collection...” Economics/elections example: “...for any distinct pair of candidates c 1 and c 2, if all voters prefer c 1 to c 2, then society must rank c 1 above c 2...” 13
14
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). 14
15
Quantifier 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. 15
16
Quantifier Scope A quantifier applies to everything to its right until a closing parenthesis stops it. 16
17
Quantifier Scope A quantifier applies to everything to its right until a closing parenthesis stops it. x D, ( y E, Q(x,y) z F, R(y,z)) P(x). 17 One subexpression
18
Quantifier Scope A quantifier applies to everything to its right until a closing parenthesis stops it. x D, ( y E, Q(x,y) z F, R(y,z)) P(x). 18 A smaller subexpression
19
Quantifier Scope A quantifier applies to everything to its right until a closing parenthesis stops it. x D, ( y E, Q(x,y) z F, R(y,z)) P(x). 19 Another smaller subexpression
20
A Bit of Syntax: 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)). 20
21
Aside: Where Can You “Cut” a Propositional Logic Statement? This doesn’t make sense: (x w) ( y). Why not? Every use of looks just like every other use. They all look like p q. The left side must make sense as an equivalent to p: p x w here. The right side must make sense as an equivalent to q. Does q y make sense? 21
22
Aside: Where Can You “Cut” a Predicate Logic Statement? This doesn’t make sense: x Z Tasty(y). Why not? Every use of looks just like every other use. They all look like P(x, y, …) Q(x, y, …). The RIGHT side must make sense as an equivalent to Q( … ). Q(y) Tasty(y) here. The left side must make sense as an equivalent to P( … ): Does P(x) x Z make sense? 22
23
Aside: Where Can You “Cut” a Predicate Logic Statement? So does this make sense: x Z, Costs(y, x) Tasty(y)? Does it look like P(x, y, …) Q(x, y, …)? P(y) x Z, Costs(y, x)? Q(y) Tasty(y)? No! The is inside the universal quantifier. It applies to Costs(y, x) and Tasty(y). P(x, y) Costs(y, x). Q(y) Tasty(y). x Z, P(x, y) Q(y). 23
24
Aside: Where Can You “Cut” a Predicate Logic Statement? How about: ( x Z, Costs(y, x)) ( w F, Salty(w))? P(y) x Z, Costs(y, x). q w F, Salty(w). Note: q alone is essentially a predicate Q() with no arguments. 24
25
A Bit of Syntax: 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.(~( x Z +, y Z +, x < y Even(y))). 25
26
A Bit of Semantics: Unbound Variables What is the truth value of the following formula? x Z, x*x = y. a.True, because (for example) 5*5=25. b.True, because every y = (sqrt y)*(sqrt 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. 5 5 26
27
Defining a Predicate Using Expressions with “Unbound” Variables A pred. logic formula with only bound variables is a proposition, something that is either true or false: x Z, x*x = 25. true x Z, x*x = 3. false y Z, x Z, x*x = y. false 5 5 27
28
Defining a Predicate Using Expressions with “Unbound” Variables A pred. logic formula with unbound variables is itself a predicate, something whose truth depends on its unbound variables’ values: PerfectSquare(y): x Z, x*x = y, where y Z PerfectSquare(25). true PerfectSquare(3). false y Z, PerfectSquare(y). false 5 5 28 Notice that this is much like defining a function in programming: (1) The meaning of the predicate/function depends on the argument given. (2) You can use the predicate/function over and over again.
29
Unbound Variables Check Which variable(s) are unbound? (In other words, which variables does this formula’s truth depend on?) i N, (i > n) ~ v N, Elt(a, i, v). a.i and v b.a and n c.n and v d.i and n e.None of these is correct. 29 (DO NOT worry about what it means!)
30
Outline Prereqs, Learning Goals, and Quiz Notes Prelude: Scope and Predicate Definition Problems and Discussion –Liszt Etudes Intermezzo on Idioms –Sorted Lists –Comparing Algorithms Next Lecture Notes 30
31
Modeling ListOfInteger Let Elt(a, i, v) be a predicate indicating that list a at index i has the integer value v, where indexes must be 0. 2457610 012345 Elt(mylist, 3, 7) is true. Elt(mylist, 2, 0) and Elt(mylist, 7, 2) are false. mylist 31
32
ListOfInteger ;; ListOfInteger is one of: ;; - empty ;; - (cons Integer ListOfInteger) Also: (where index 0 gets us the first element) 32
33
List Element Warmup Elt(a, i, v) list a has value v at index i. Which of the following should describe a valid list (assume all other Elt(x, y, z) are false)? a.Elt(list, 0, 7), Elt(list, 1, 4), Elt(list, 2, 3) b.Elt(list, 0, 7), Elt(list, 1, 4), Elt(list, 3, 3) c.Elt(list, -1, 7), Elt(list, 0, 4), Elt(list, 1, 3) d.Elt(list, 0, 7), Elt(list, 1, 7), Elt(list, 1, 3) e.Nothing. (~ i N, v Z, Elt(list, i, v).) 33
34
List Element Exercise Define a predicate Contains(a, v) meaning “list a contains the value v ”. Contains(a, v): a. a A, i N, v Z, Elt(a,i,v) b. a A, i N, v Z, Elt(a,i,v) c. i N, Elt(a,i,v) d. i N, Elt(a,i,v) e.None of these 34 This really means the list contains the value “in at least one place”. It might contain the value once or twice or three times or …
35
List Length Properties Which of the following should not be true of the length of a list? a.Every list should have a length. b.No list should have two different lengths. c.No list should have elements at any index as large or larger than (“beyond”) its length. d.A list should have exactly one element at every index up to (not including) its length. e.All of these should be true. 35
36
Outline Prereqs, Learning Goals, and Quiz Notes Prelude: Scope and Predicate Definition Problems and Discussion –Liszt Etudes Intermezzo on Idioms –Sorted Lists –Comparing Algorithms Next Lecture Notes 36
37
Idioms: “a form of expression natural to a language” To define lists, we need to say things like: “No list has property P”, “Some list has…”, “Every list has…”, “Some non-empty lists have…”, “All non-empty lists have…” “At most one element in a list has…” “Exactly one element in a list has…” These are common predicate logic “idioms”. Let’s learn them. 37
38
Idioms: “None, some, not every, all” Let A be the set of animals. Honey(x) means “x likes honey” Bear(x) means “x is a bear” None of the animals like honey: ~ x A, Honey(x). Some of the animals like honey: x A, Honey(x). Not every animal likes honey: ~ x A, Honey(x). All animals like honey: x A, Honey(x). 38
39
Idioms: “None, some, not every, all” (put another way) Let A be the set of animals. Honey(x) means “x likes honey” Bear(x) means “x is a bear” Every animals dislikes honey: x A, ~Honey(x). Not every animal dislikes honey: ~ x A, ~Honey(x). Some animal dislikes honey: x A, ~Honey(x). No animal dislikes honey: ~ x A, ~Honey(x). 39
40
Idioms: “Restricting the Domain of an Existential” Let A be the set of animals. Honey(x) means “x likes honey” Bear(x) means “x is a bear” Some animal likes honey: x A, Honey(x). 40
41
Idioms: “Restricting the Domain of an Existential” Let A be the set of animals. Honey(x) means “x likes honey” Bear(x) means “x is a bear” Some bear likes honey. We’d like to say: x SetOfBears, Honey(x). But we don’t have the set of bears. How do we “restrict” the set of animals to just bears for an existential? You did it on the quiz! 41
42
Idioms: “Restricting the Domain of an Existential” Let A be the set of animals. Honey(x) means “x likes honey” Bear(x) means “x is a bear” Some bear likes honey. AKA “There is a bear that likes honey.” x A, Bear(x) Honey(x). 42 This far, you did on the quiz!
43
Idioms: “Restricting the Domain of an Existential” More generally: Given set D and predicate P(x), you want to say “Some has the property Q.” You say: x D, P(x) Q(x). 43 Some …
44
Idioms: “Restricting the Domain of a Universal” Let A be the set of animals. Honey(x) means “x likes honey” Bear(x) means “x is a bear” “Every bear likes honey.” Does this work: x A, Bear(x) Honey(x). a.Yes. b.No, because then no bear could dislike honey. c.No, because then all wolves would have to like honey. d.No, because then there could be no wolves. e.There’s not enough information to tell. 44
45
Idioms: “Restricting the Domain of a Universal” Let A be the set of animals. Honey(x) means “x likes honey” Bear(x) means “x is a bear” “Every bear likes honey.” So, we must let wolves (and other non-bears) off the hook: “Every animal likes honey (or isn’t a bear so we don’t care).” x A, Honey(x) ~Bear(x). 45 Every animal likes honey… or isn’t a bear (so we don’t care).
46
Idioms: “Restricting the Domain of a Universal” Let’s play with logical equivalences… “Every bear likes honey.” x A, Honey(x) ~Bear(x) x A, ~Bear(x) Honey(x)[by COM] x A, Bear(x) Honey(x)[by IMP] For every animal: if it’s a bear, it must like honey. (If it’s not a bear, we don’t care whether it likes honey.) 46
47
Idioms: “Restricting the Domain of a Universal” 47 More generally: Given set D and predicate P(x), you want to say “All have the property Q.” You say: x D, P(x) Q(x). All …
48
Idioms Questions 48 Let P be the set of Racket Programs. Let Buggy(x) be true iff the program x contains errors. Let Terminate(x) be true iff the program x ever terminates (finishes running). Which of these means: “Every non-terminating program is buggy.” a. p P, ~Terminate(p) Buggy(p). b. p P, ~Terminate(p) Buggy(p). c. p P, ~Terminate(p) Buggy(p). d. p P, ~Terminate(p) Buggy(p). e.None of these.
49
Idioms Questions 49 Let P be the set of Racket Programs. Let Buggy(x) be true iff the program x contains errors. Let Terminate(x) be true iff the program x ever terminates (finishes running). Which of these means: “Some buggy program does not terminate.” a. p P, Buggy(p) ~Terminate(p). b. p P, Buggy(p) ~Terminate(p). c. p P, Buggy(p) ~Terminate(p). d. p P, Buggy(p) ~Terminate(p). e.None of these.
50
Idioms: “At Least One”, “At Least Two” 50 “At least one animal likes honey.” means the same as “Some animal likes honey.” x A, Honey(x). “At least two animals like honey.” means “Some animal and some other animal like honey.” or “Some animal and some animal that is not the same animal like honey.” Just “restricting the domain”!
51
Idioms: “At Least One”, “At Least Two” 51 “At least one animal likes honey.” means the same as “Some animal likes honey.” x A, Honey(x). “At least two animals like honey.” means “Some animal and some other animal like honey.” or “Some animal and some animal that is not the same animal like honey.” x A, Honey(x) y A, x y Honey(y) x A, y A, x y Honey(x) Honey(y). Just “restricting the domain”! (We can move Honey(x) inside the that doesn’t mention x, but beware of negations, (bi)conditionals, and XORs!)
52
Idioms: “At Most One” 52 “No things in A…”~ x A, … “At least one thing in A…” x A, … “At least two things in A…” x A, y A, x y … … “At most one thing in A…” means the same as “It is not the case that at least two things in A…” ~ x A, y A, x y … … The same predicate applied to x and y.
53
Idioms: “Exactly One” 53 “Exactly one thing in A…” means both “at least one thing in A” and “at most one thing in A…” “Exactly one animal likes honey.” “Some animal likes honey and no animal that is not that animal likes honey.” So, either: z A, Honey(z) ~ x A, y A, x y Honey(x) Honey(y). Or just: x A, Honey(x) ~ y A, x y Honey(y).
54
Idiom Summary 54 “None…”/“No x…” ~ x D, … “At least one…”/“Some…”/ “A (particular) x…” x D, … “Every…”/“All…”/“Any x…”/ “A (arbitrary) x…” x D, … “Some P-ish x…” (restricting the domain) x D, P(x) … “Every P-ish x…” (restricting the domain) x D, P(x) … “At least two…” x D, y D, x y … … “At most one…” ~ x D, y D, x y … … “Exactly one…” x D, … ~ y D, x y … And yes, sometimes “An x has property P” means “Some” and sometimes it means “All” In each case with two “…” below, should be the same predicate applied to x and y.
55
Outline Prereqs, Learning Goals, and Quiz Notes Prelude: Scope and Predicate Definition Problems and Discussion –Liszt Etudes Intermezzo on Idioms (DONE) –Sorted Lists –Comparing Algorithms Next Lecture Notes 55
56
List Length Properties Reminder, we want to say: Every list should have a length. No list should have two different lengths. No list should have elements at any index as large or larger than (“beyond”) its length. A list should have exactly one element at every index up to (not including) its length. 56
57
List Length Warmup Elt(a, i, v) a has value v at index i. Let Length(a, n) mean list a is n items long. What does a A, n N, Length(a, n) mean? (Hint: first figure out what n N, Length(mylist, n) means; then, switch from my list to a A,… ) a.Every list is n items long. b.There are many lists that are n items long. c.Every list has a length. d.No list has more than one length. e.None of these. 57 We will assume henceforth that this statement is true.
58
List Element Exercise Elt(a, i, v) a has value v at index i. Let A be the set of all lists. Which means: “a list has at most one element at any index”. a. a A, i N, v Z, Elt(a,i,v). b. a A, i N, ~ v 1 Z, ~ v 2 Z, Elt(a,i,v 1 ) Elt(a,i,v 2 ). c. a A, i N, ~ v 1 Z, v 2 Z, v 1 v 2 Elt(a,i,v 1 ) Elt(a,i,v 2 ). d. a A, i N, v 1 Z, v 2 Z, (Elt(a,i,v 1 ) Elt(a,i,v 2 )) v 1 = v 2. e.All of these We will assume henceforth that a list cannot have more than one element at any index. 58
59
Better “No More Than One”? Use your knowledge of how to break down problems into separate functions! If “ a list has at most one element at any index ” is too hard, write “ list a has at least two elements at index i ”. That’s a new predicate! You can break down complex predicates (or statements) in terms of simpler predicates, just as you break down complex functions in terms of simpler ones. 59
60
Better “No More Than One”? If “a list has at most one element at any index” is too hard, write “the list a has at least two elements at index i”. Let’s make the predicate: MultipleElts(a,i) v 1 Z, v 2 Z, Elt(a,i,v 1 ) Elt(a,i,v 2 ) That’s not quite right. What’s missing? 60
61
Better “No More Than One”? We wrote “the list a has at least two elements at index i”: MultipleElts(a,i) v 1 Z, v 2 Z, v 1 v 2 Elt(a,i,v 1 ) Elt(a,i,v 2 ). Now, how do we write “a list has at most one element at any index”? In other words: “a list does not have multiple elements at any index.” Easy: a A, i N, ~MultipleElts(a,i). 61 Go back and check that this is the same as what we got in the clicker question!
62
Assumptions/Definitions for Lists A list cannot have more than one element at any index a A, i N, ~ v 1 Z, v 2 Z, v 1 v 2 Elt(a,i,v 1 ) Elt(a,i,v 2 ). 62
63
Assumptions/Definitions for Lists A list cannot have more than one element at any index a A, i N, ~ v 1 Z, v 2 Z, v 1 v 2 Elt(a,i,v 1 ) Elt(a,i,v 2 ). Every list has a length. a A, n N, Length(a, n). 63
64
No More than One Length Which of the following means that a list should not have more than one length? a. a A, n 1 Z, n 2 Z, n 1 n 2 (~Length(a,n 1 ) ~Length(a,n 2 )). b. a A, ~ n 1 N, n 2 N, Length(a,n 1 ) Length(a,n 2 ) n 1 n 2. c. a A, ~ n 1 N, n 2 N, n 1 n 2 (Length(a,n 1 ) Length(a,n 2 )). d. a A, n N, Length(a,n 1 ) ~Length(a,n 2 ). e.All of these 64 We will assume henceforth that this statement is true.
65
Defining Length(a, n) Consider: Length(a,n) i N, ( v N, Elt(a, i, v)) (i < n). In English: There’s an element at i if and only if i is a valid index (less than the list’s length). Why does this guarantee that no list has more than one length? a.Because it states that no list has more than one length. b.Because a list with two lengths would now both have and not have a value at some entry. c.Because i now must be less than the length of the list. d.None of these, but it is guaranteed. e.None of these, and it’s not guaranteed. 65 We will assume henceforth that this statement is true.
66
Assumptions/Definitions for Lists A list cannot have more than one element at any index. a A, i N, ~ v 1 Z, v 2 Z, v 1 v 2 Elt(a,i,v 1 ) Elt(a,i,v 2 ). Every list has a length. a A, n N, Length(a, n). A list “has a length n” exactly when there’s an element at i if and only if i is less than n. Length(a,n) i N, ( v Z, Elt(a, i, v)) (i < n). –So: No list has more than one length, and –there’s an elt. at every index up to the list’s length, and –There’s no elt. at any index greater than or equal to the length. 66
67
Outline Prereqs, Learning Goals, and Quiz Notes Prelude: Scope and Predicate Definition Problems and Discussion –Liszt Etudes –Sorted Lists –Comparing Algorithms Next Lecture Notes 67
68
Problem: Sorted Lists Problem: Give a definition for the predicate Sorted(a) in terms of Elt(a, i, v). Assume lists cannot have more than one element at an index, every list has a length, and lists are “filled” to their length. ? 68
69
Problem: Sorted Lists Problem: Give a definition for the predicate Sorted(a) in terms of Elt(a, i, v). Which of the following is a problem with this definition? Sorted(a) i N, Elt(a,i,v 1 ) < Elt(a,i+1,v 2 ). a.a isn’t quantified. b.v 1 and v 2 aren’t quantified. c.We can’t use < on Elt (or any other predicate) d.a and b e.b and c ? 69
70
Problem: Sorted Lists Problem: Give a definition for the predicate Sorted(a) in terms of Elt(a, i, v). What’s wrong with the following definition? Sorted(a) i N, v 1 Z, v 2 Z, (Elt(a,i,v 1 ) Elt(a,i+1,v 2 )) v 1 < v 2. a.It’s missing quantifiers. b.It’s too restrictive (e.g., for equal values). c.It doesn’t handle the “boundary case” when i=length-1. d.a and b e.b and c ? 70
71
Intuition ♥ Formality “...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 (3 rd ed), p. 106-107 We’ll often use predicate logic informally in the future, but the ability to express and reason about ideas formally keeps us honest and helps us discover points we may overlook otherwise. 71
72
Outline Prereqs, Learning Goals, and Quiz Notes Prelude: Scope and Predicate Definition Problems and Discussion –Liszt Etudes –Sorted Lists –Comparing Algorithms Next Lecture Notes 72
73
Efficiency of Algorithms Let’s say each student is in a “MUG” (1st year orientation group). For each of their MUG-mates, each student has a list of all of their classes. Assume each MUG has 13 students and each student is taking 5 classes. I want to determine how many students in my class have a MUG-mate in my class. 73
74
Which algorithm is generally faster? (a) Ask each student for the list of their MUG- mates’ classes, and check for each class whether it is CPSC 121. If the answer is ever yes, include the student in my count. (b) For each student s 1 in the class, ask the student for each other student s 2 in the class whether s 2 is a MUG-mate. If the answer is ever yes, include s 1 in my count. (c) Neither. 74
75
Concrete Examples: 10 students Say checking if a class on a list is CPSC 121 takes 1 second and checking if a classmate is in your MUG takes 1 second. Algorithm (a) takes ~10*12*5 seconds = 10 minutes. Algorithm (b) takes ~10*10 seconds < 2 minutes. 75
76
Concrete Examples: 100 students Say checking if a class on a list is CPSC 121 takes 1 second and checking if a classmate is in your MUG takes 1 second. Algorithm (a) takes ~100*12*5 seconds = 100 minutes. Algorithm (b) takes ~100*100 seconds 167 minutes. 76
77
Concrete Examples: 400 students Say checking if a class on a list is CPSC 121 takes 1 second and checking if a classmate is in your MUG takes 1 second. Algorithm (a) takes ~400*12*5 seconds 7 hours. Algorithm (b) takes ~400*400 seconds 44 hours. 77
78
Which algorithm is generally faster? (a) Ask each student for the list of their MUG- mates’ classes, and check for each class whether it is CPSC 121. If the answer is ever yes, include the student in my count. (b) For each student s 1 in the class, ask the student for each other student s 2 in the class whether s 2 is a MUG-mate. If the answer is ever yes, include s 1 in my count. (c) Neither. 78
79
Comparing at One Input Size Let the predicate Faster(a 1, a 2, n) mean algorithm a 1 is faster than algorithm a 2 on a problem of size n, where n is a positive integer. Alg A Alg B problem size time 79 We’ll assume the Faster predicate is given to us.
80
How Faster Works (1 of 3) Faster(a 1,a 2,n) : a 1 is faster than a 2 at size n. Which of the following means “no algorithm is ever faster than itself”? a. n Z +, a A, ~Faster(a,a,n). b. n Z +, a 1 A, a 2 A, Faster(a 1,a 2,n) ~Faster(a 2,a 1,n). c. n Z +, a 1 A, a 2 A, a 3 A, (Faster(a 1,a 2,n) Faster(a 2,a 3,n)) Faster(a 1,a 3,n). d.None of these. 80 Alg A Alg B problem size time We will assume this statement is true.
81
How Faster Works (2 of 3) Faster(a 1,a 2,n) : a 1 is faster than a 2 at size n. Which of the following means “two algorithms cannot be faster than each other”? a. n Z +, a 1 A, a 2 A, Faster(a 1,a 2,n) ~Faster(a 2,a 1,n). b. n Z +, a 1 A, a 2 A, Faster(a 1,a 2,n) ~Faster(a 2,a 1,n). c. n Z +, a 1 A, a 2 A, Faster(a 1,a 2,n) Faster(a 2,a 1,n). d.b and c. e.All of these. 81 Alg A Alg B problem size time We will assume this statement is true.
82
How Faster Works (3 of 3) Faster(a 1,a 2,n) : a 1 is faster than a 2 at size n. What does the following statement mean? n Z +, a 1 A, a 2 A, a 3 A, (Faster(a 1,a 2,n) Faster(a 2,a 3,n)) Faster(a 1,a 3,n). a.Three algorithms cannot be faster than each other. b.Three algorithms are the same “speed”. c.Of any three algorithms, one is the fastest. d.An algorithm cannot be faster than itself. e.None of these. 82 Alg A Alg B problem size time We will assume this statement is true.
83
How Faster Works (4 of 3, bonus! ) Faster(a 1,a 2,n) : a 1 is faster than a 2 at size n. We assume Faster is: “Anti-reflexive”: No algorithm is faster than itself. “Anti-symmetric”: No two algorithms are faster than each other. “Transitive”: If one algorithm is faster than a second, which is faster than a third, then the first algorithm is faster than the third. (We can “chain” fasters together, analagously to the transitivity rule for propositional logic.) 83 Alg A Alg B problem size time
84
General Efficiency of Algorithms Faster(a 1,a 2,n) : a 1 is faster than a 2 at size n. Problem: Create a definition of GenerallyFaster(a 1, a 2 ) in terms of Faster(a 1, a 2, n) that you can live with. 84 Alg A Alg B problem size time
85
Desirable Properties of Generally Faster Which of these properties should Generally Faster not share with Faster? a.Anti-reflexivity b.Anti-symmetry c.Transitivity d.Should share all of these. e.Should share none of these. 85 Alg A Alg B problem size time
86
English-Language Generally Faster Definitions Which one do you want? [Your definitions here.] a. b. c. d.None of these. 86 Alg A Alg B problem size time
87
Formal Generally Faster Definitions Which one do you want? [Your definitions here.] a. b. c. d.None of these. 87 Alg A Alg B problem size time
88
Which algorithm is generally faster? (a) Ask each student for the list of their MUG- mates’ classes, and check for each class whether it is CPSC 121. If the answer is ever yes, include the student in my count. (b) For each student s 1 in the class, ask the student for each other student s 2 in the class whether s 2 is a MUG-mate. If the answer is ever yes, include s 1 in my count. (c) Neither. 88 Alg A Alg B problem size time
89
Outline Prereqs, Learning Goals, and Quiz Notes Prelude: Scope and Predicate Definition Problems and Discussion –Liszt Etudes –Sorted Lists –Comparing Algorithms Next Lecture Notes 89
90
Learning Goals: In-Class By the start of class, you should be able to: –Build statements about the relationships between properties of various objects—which 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”) using predicate logic. 90
91
Next Lecture Learning Goals: Pre-Class By the start of class, you should be able to: –Determine the negation of a quantified statement in logical notation as well as English. (But, feel free to entirely or partially translate English to logic in the process!) –Given a quantified statement and an equivalence rule, apply the rule to create an equivalent statement (particularly the De Morgan’s and contrapositive rules). –Prove and disprove quantified statements using the “challenge” method (Epp, 3 rd ed 98-99, Epp, 4 th ed 118- 119). –Apply universal instantiation, universal modus ponens, and universal modus tollens to predicate logic statements that correspond to the rules’ premises to infer statements implied by the premises. 91
92
Next Lecture Prerequisites Reread Sections 3.1 and 3.3 (including the negation part that we skipped previously). Read Sections 3.2 and 3.4. (See course website for other texts’ sections.) (You needn’t learn the “diagram” technique, but it may make more sense than other explanations!) Complete the open-book, untimed quiz on Vista that’s due before the next class. 92
93
snick snack More problems to solve... (on your own or if we have time) 93
94
Problem: Java Collections Problem: Translate the following text from the Java 1.6.0 API page for the Collection interface into predicate logic. [T]he specification for the contains(Object o) method says: "returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e))." contains(Object o) c ? a : b acts essentially like a multiplexer. If c is true, it evaluates to a; otherwise, it evaluates to b. 94
95
Problem: Java Collections Problem: The API goes on to say: This specification should not be construed to imply that invoking Collection.contains with a non-null argument o will cause o.equals(e) to be invoked for any element e. Explain whether and how this is consistent with your definition. 95
96
More Quantifier Examples Someone is in charge. Everyone except the person in charge reports to someone else. 96
97
More Quantifier Examples n is a prime number. Note: we use x|y as a predicate meaning x divides y (i.e., x “goes into” y with no remainder). 97
98
More Quantifier Examples n is a prime number. Let’s define a new predicate P(x) in terms of this “clause”. Then, let’s express… There’s some prime number larger than 10. There’s some prime number larger than every natural number. 98
99
Yet More Examples Eating food causes Alice to grow or shrink. Solution: x F, E(x) g s. F = set of all foods E(x) : Alice eats x g : Alice grows s : Alice shrinks 99
100
Yet More Examples Alice shrank when she ate some food. Solution: x F, E(x) s. F = set of all foods E(x) : Alice eats x g : Alice grows s : Alice shrinks 100
101
Yet More Examples All lions are fierce. Solution: x F, L(x) F(x). F(x) : x is a fierce creature L(x) : x is a lion C(x) : x drinks coffee Domain for all is the set of all creatures. 101
102
Yet More Examples Some lions do not drink coffee. F(x) : x is a fierce creature L(x) : x is a lion C(x) : x drinks coffee Domain for all is the set of all creatures. 102
103
Yet More Examples All fierce creatures are not lions. F(x) : x is a fierce creature L(x) : x is a lion C(x) : x drinks coffee Domain for all is the set of all creatures. Is that English sentence ambiguous? 103
104
Yet More Examples Is x, K(x, y) a proposition? Why or why not? F(x) : x is a fierce creature L(x) : x is a lion C(x) : x drinks coffee K(x, y) : x has been in y ’s kitchen Domain for all is the set of all creatures (with kitchens?). 104
105
Yet More Examples Every creature has been in its own kitchen. Some creature has not been in its own kitchen. F(x) : x is a fierce creature L(x) : x is a lion C(x) : x drinks coffee K(x, y) : x has been in y ’s kitchen Domain for all is the set of all creatures (with kitchens?). 105
106
Yet More Examples There is a creature that has been in every creature’s kitchen. Every creature’s kitchen has had some creature in it. F(x) : x is a fierce creature L(x) : x is a lion C(x) : x drinks coffee K(x, y) : x has been in y ’s kitchen Domain for all is the set of all creatures (with kitchens?). Are these the same? 106
107
Yet More Examples Every creature has been in every other creature’s kitchen. Every creature’s kitchen has had every creature in it. F(x) : x is a fierce creature L(x) : x is a lion C(x) : x drinks coffee K(x, y) : x has been in y ’s kitchen Domain for all is the set of all creatures (with kitchens?). Are these the same? What if we removed the word “other”? 107
108
Problem: Voting Database Consider a database that tracks the votes in an election. In the database, the predicate Tally(d, c, n) means that district d reported that candidate c received n votes, where n is an integer 0. Problem: Define a predicate GotVote(c) in terms of Tally whose truth set is the set of all candidates who received at least one vote. 108
109
Problem: Voting Database Problem: Define a predicate whose truth set is the set of all candidates who won at least one district. Why work so hard on defining predicates? This is essentially how we query databases. 109
110
Problem: Voting Database Let’s assume that every candidate has exactly one vote total for every district. That is, there’s no missing and no duplicate data. Problem: Write a logical statement that describes this constraint. Predicates are clumsy for expressing this common idea. Later, we’ll use functions to do a better job. 110
111
Problem: Voting Database Let Winner(c) indicate that candidate c is the winner of the election. Problem: Write a logical statement that means that the winner of the election must have received at least one vote. 111
112
Problem: Voting Database Let D be the set of all districts and C be the set of all candidates. Problem: Determine what the following statement means, whether it is necessarily true (no matter what the actual vote tallies are), and justify your stance: c 1 C, d D, c 2 C, Winner(c 1 ) c 1 c 2 n 1 Z, n 2 Z, Tally(d,n 1,c 1 ) Tally(d,n 2,c 2 ) n 1 >n 2 112
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.