Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 121: Models of Computation 2016W2

Similar presentations


Presentation on theme: "CPSC 121: Models of Computation 2016W2"— Presentation transcript:

1 CPSC 121: Models of Computation 2016W2
Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville and others TODO (future terms): update year/term

2 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).

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

4 Describing Sets (1/4) Some sets… A = {1, 3, 9} B = {1, 3, 9, 27, snow}
C = {1, 1, 3, 3, 9, 9} (the same as A!) D = {A, B} D = { {1, 3, 9}, {1, 3, 9, 27, snow} } E = { } (the empty set, with nothing in it)

5 Describing Sets (2/4) Some sets… A = {1, 5, 25, 125, …}
(The set of powers of 5, the set of integers, and the set of integers between 1 and 100.) “…” is an ellipsis

6 Describing Sets (3/4) Some sets, using set builder notation:
A = {x  N | y  N, x = 5y} B = {2i - 1 | i is a prime} C = {n  Z | 0 < n  100} To read, start with “the set of all”. Read “|” as “such that”. A: “the set of all natural numbers x such that x is a power of 5” B: “the set of all numbers of the form 2i-1 such that i is a prime” C: “the set of all integers n such that 0 < n  100”

7 Describing Sets (4/4) Graphical depiction of sets: Venn diagrams.
Draw the set of all five-letter things. All red things? All red, five-letter things? Mention the universal set U Texas U is the universal set of everything. snows seven happiness books fire truck heart

8 Outline Prereqs, Learning Goals, and Quiz Notes
Prelude: Scope and Predicate Definition Problems and Discussion Liszt Etudes Sorted Lists Comparing Algorithms Next Lecture Notes

9 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). Discuss point of learning goals.

10 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. Discuss point of learning goals.

11 Where We Are in The Big Stories
Theory Hardware 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! How do we build devices to compute? Now: Still taking a bit of a vacation in lecture!

12 Quiz 5 Notes If x and y are drawn from the 8-bit signed ints, x = 127, and y = 1, what is x+y? -128 -127 128 None of these. Is “for all positive 8-bit signed x and y, x + y is positive” true?

13 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?

14 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?

15 Outline Prereqs, Learning Goals, and Quiz Notes
Prelude: Motivation, Scope & Defining Predicates Problems and Discussion Liszt Etudes Sorted Lists Comparing Algorithms Next Lecture Notes

16 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. NO predicates allowed, just things like p, q, r, etc.

17 Limitations of Propositional Logic as a Model
Which of the following can propositional logic model effectively? Relationships among factory production lines like “wheel assembly and frame welding both connect to the undercarriage line”. Defining what it means for a number to be prime. Generalizing from examples to abstract patterns like “everyone takes off their shoes at airport security”. Prop logic can model all of these effectively. Prop logic cannot model any of these effectively. 2009W1: Didn’t really like this question, but it does make an important point. 2010W2: Going to try this one this way. A tells us that prop logic doesn’t let us break up propositions into fine enough detail. We want to be able to notice that the ul in wa-ul and in fw-ul are the same ul, but as is, they’re no more similar than identifiers in a programming language named result and restore (which is to say, unrelated). REASON ABOUT THE REL’nSHIP BTW OBJECTS. B tells us that we want to be able to DEFINE NEW PROPERTIES OF OBJECTS. In other words, speak generally about an object without naming it specifically. C tells us we might want some ways to build propositions out of common generalizations like “everyone”, “not everyone”, “someone”, and “no one”.

18 What Does Predicate Logic Model?
Relationships among real-world objects Generalizations about patterns Infinite (or even just big) domains array doubling pre/post conditions Problems where the properties of different concepts, ideas, parts, or entities depend on each other.

19 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 c1 and c2, if all voters prefer c1 to c2, then society must rank c1 above c2...” Stole the first from (min-heaps) Stole the second from an AI textbook. (Admissible A* heuristics.) Made up the third (based on old 121 assignments), but it’s certainly realistic for a PL class or a garbage collector. Last one is Pareto Efficiency in voting.

20 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).

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

22 Quantifier Scope A quantifier applies to everything to its right until a closing parenthesis stops it.

23 one subexpression (all under the universal)
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). one subexpression (all under the universal)

24 a smaller subexpression
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). a smaller subexpression

25 another smaller subexpression (all under the existential)
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). another smaller subexpression (all under the existential)

26 another smaller subexpression
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). another smaller subexpression

27 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). ()x  Z, y  Z, x < y  Even(y). (x)  Z, y  Z, x < y  Even(y). (x  Z), y  Z, x < y  Even(y). (x  Z, y  Z, x < y)  Even(y). (x  Z, y  Z, x < y  Even(y)). The answer is e. EMPHASIZE how meaningless most of these answers are! Reminder: Z is the set of integers.

28 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? [2011W1: added these asides because of TERRIBLE results in 2010W2 on the upcoming clicker Q on negation. Specifically, only 1/3 of the class got the right answers, and that got LOWER after discussion. See if things get better!]

29 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?

30 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).

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

32 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). (~)x  Z+, y  Z+, x < y  Even(y). (~(x))  Z+, y  Z+, x < y  Even(y). (~(x  Z+)), y  Z+, x < y  Even(y). (~(x  Z+, y  Z+, x < y))  Even(y). (~(x  Z+, y  Z+, x < y  Even(y))). The answer is e, for the same reasons as on the last question. EMPHASIZE how meaningless most of these answers are! REALLY need to do a better job of this. The point is that a quantifier “puts parentheses around” everything it modifies. So, a negation CANNOT apply to just the quantifier (because that’s not a separate sub-expression) and DOES apply across the whole quantified expression. Students NEVER get this confidently, however. Makes me wish we used a notation that makes quantifier scope explicit. Are we deviating from Epp here?

33 A Bit of Semantics: Unbound Variables
What is the truth value of the following formula? x  Z, x*x = y. (Warning: a bit of a trick question.) True, because (for example) 5*5=25. True, because every y=(sqrt y)*(sqrt y) False, because of counterexamples like no integer multiplied by itself equals 3. It depends on y, but given a value for y, we could calculate a truth value. None of the above. [2011W1] 2010W2 results settled on c and d [MUST have changed the letters since then.. Discussion below doesn’t make sense!]. D’s not that big a deal as a misconception. Just correct w/lecture. The answer is c... In this case, I think. Does this invoke an undecidability problem (assuming we have functions)? In that case, is the answer technically d? 2010W2: In answer to the Q above: NO, clearly not. Given a VALUE for y, we know whether there’s an integer x s.t. X*x = y. 5 5

34 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 = true x  Z, x*x = false x  Z, x*x = robson. false (malformed?) y  Z, x  Z, x*x = y. false 5 5

35 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 PerfectSquare(robson). malformed y  Z, PerfectSquare(y). false 5 5 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.

36 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). i and v a and n n and v i and n None of these is correct. (DO NOT worry about what it means!) [2011W1] Remind students (before question? After having it posted for a while?) that this is the same as having parentheses around everything “inside” the universal quantifier. A and n are unbound.

37 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

38 Modeling ListOfInteger
Assume that L represents a list of integers. The length of L is denoted by (length L). The ith element of L is denoted by (list-ref L i), where 0 ≤ i < (length L). 1 2 3 4 5 2 4 5 7 6 10 mylist (length mylist) is 6. (list-ref mylist 3) is 7.

39 Modeling ListOfInteger
Assume that L represents a list of integers. The length of L is denoted by (length L). The ith element of L is denoted by (list-ref L i), where 0 ≤ i < (length L). Here are the signatures of these functions: ;; length: ListOfInteger -> natural ;; list-ref: ListOfInteger natural -> integer Are these functions predicates? Yes. No, because they don’t start with capital letters. No, because they have parentheses around them instead of after. No, because they don’t produce booleans. Answer: d. These are functions, but they’re not predicates. So, we cannot use our AND, OR, and other logical operators with them!

40 ListOfInteger Also: ;; ListOfInteger is one of: ;; - empty
;; - (cons Integer ListOfInteger) Also: (where index 0 gets us the first element)

41 List Element Exercise Reminder: the length of L is denoted by (length L); the ith element of L is denoted by (list-ref L i), where 0 ≤ i < (length L). Define a predicate Contains(a, v) meaning “list a contains the value v”. Contains(a, v): a  A, i  N, v  Z, (list-ref a i) = v. a  A, i  N, v  Z, (list-ref a i) = v. i  N, (list-ref a i) = v. i  N, (list-ref a i) = v. None of these NOTE THAT: we use a instead of l because l looks like I and 1. 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 … Students had some trouble before class with what it meant to define a predicate. Let’s see if they still do. Hint: Try some examples, like mylist! We can also think of this as a contains v “at least once”.

42 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

43 Idioms: “a form of expression natural to a language”
We may want 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.

44 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 likes 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).

45 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).

46 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).

47 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!

48 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… Some animal is a bear and also likes honey… There is an animal that is a bear and likes honey. x  A, Bear(x)  Honey(x). This far, you did on the quiz!

49 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” But wait.. by the commutative law: x  A, Bear(x)  Honey(x)  x  A, Honey(x)  Bear(x) So that also means “Some honey-liker is a bear.” Yes, “some bear likes honey” is the same as “some honey-liker is a bear.”

50 Idioms: “Restricting the Domain of an Existential”
More generally: Given set D and predicate P(x), you want to say “Some <thing in D for which P is true> has the property Q.” You say: x  D, P(x)  Q(x). Some <thing in D for which P is true>…

51 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” Does x  A, Bear(x)  Honey(x) mean “Every bear likes honey”? (Try this test case: Steve is a wolf who dislikes honey; George is a bear who likes honey. Try to pick the best answer.) Yes. No, because then no bear could dislike honey. No, because then all wolves would have to like honey. No, because then there could be no wolves. There’s not enough information to tell.

52 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). Every animal likes honey… or isn’t a bear (so we don’t care).

53 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.)

54 Idioms: “Restricting the Domain of a Universal”
More generally: Given set D and predicate P(x), you want to say “All <things in D for which P is true> have the property Q.” You say: x  D, P(x)  Q(x). All <things in D for which P is true>…

55 Idioms Questions 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.” p  P, ~Terminate(p)  Buggy(p). p  P, ~Terminate(p)  Buggy(p). p  P, ~Terminate(p)  Buggy(p). p  P, ~Terminate(p)  Buggy(p). None of these.

56 Idioms Questions 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.” p  P, Buggy(p)  ~Terminate(p). p  P, Buggy(p)  ~Terminate(p). p  P, Buggy(p)  ~Terminate(p). p  P, Buggy(p)  ~Terminate(p). None of these.

57 Idioms: “At Least One”, “At Least Two”
“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”!

58 Idioms: “At Least One”, “At Least Two”
“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!)

59 Idioms: “At Most One” “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. The same predicate applied to x and y.

60 Idioms: “Exactly One” “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).

61 Idiom Summary “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  … In each case with two “…” below, should be the same predicate applied to x and y. And yes, sometimes “An x has property P” means “Some” and sometimes it means “All” 

62 Outline Prereqs, Learning Goals, and Quiz Notes
Prelude: Scope and Predicate Definition Problems and Discussion Liszt Etudes Sorted Lists Comparing Algorithms Next Lecture Notes

63 Problem: Sorted Lists Problem: Give a definition for the predicate Sorted(a) in terms of length and list-ref. ?

64 ? Problem: Sorted Lists Problem: Give a definition for the predicate Sorted(a) in terms of length and list-ref. Which of the following is a problem with this definition? Sorted(a)  i  N, (list-ref a i)  (list-ref a i+1)  ((list-ref a i) < (list-ref a i+1)). There is no quantifier for a. There is no quantifier for i. We can’t use  on (list-ref a i) or (list-ref a i+1). We can’t use < on (list-ref a i) or (list-ref a i+1). The answer is c. It’s GOOD that a isn’t quantified (since this is defining a predicate dependent on a). This is similar to a definition that students gave me in online quizzes. EMPHASIZE that a function returning a number is not a predicate. Similarly, we cannot use < or > on the result of a predicate. Note: There’s another problem with this. We’ll fix it in a moment!

65 ? Problem: Sorted Lists Problem: Give a definition for the predicate Sorted(a) in terms of length and list-ref. What’s wrong with the following definition? Sorted(a)  i  N, j  N, i < j  ((list-ref a i) < (list-ref a j)). It is too restrictive (because of equal values in the list). It does not limit the ranges of i and j. It is missing quantifiers. Both a and b Both b and c This patches the problems from the previous slide but is still two restrictive. It’s worth explaining why this DOES work when i = length. It hammers home the “paradox of material implication” (and how USEFUL it is here).

66 ? Problem: Sorted Lists Problem: Give a definition for the predicate Sorted(a) in terms of length and list-ref. Finish this definition to fix the problem on the previous slide: Sorted(a)  i  N, j  N, (i < j )  ((list-ref a i) ≤ (list-ref a j)). We need to and in conditions on i and j such that i < (length a) and j < (length a).

67 Intuition ♥ Formality Epp (3rd ed), p. 106-107
“...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 (3rd ed), p Soon, we’ll mostly use predicate logic informally and mixed with English, but the ability to express and reason about ideas formally keeps us honest and helps us discover points we may overlook otherwise.

68 Outline Prereqs, Learning Goals, and Quiz Notes
Prelude: Scope and Predicate Definition Problems and Discussion Liszt Etudes Sorted Lists Comparing Algorithms Next Lecture Notes

69 Efficiency of Algorithms
Let’s say each student is in a 1st year orientation group. For each of their groupmates, each student has a list of all of their classes. Assume each group has 13 students and each student is taking 5 classes. I want to determine how many students in my class have a groupmate in my class. I fudged the numbers here to make things easy to compute. 2016W2 (from Patrice’s notes): students sometimes mention the scaling discussion in labs, which is a great thing to connect to! A “tree” of gates has a shorter “longest path” through it than a “line” of gates.

70 Which algorithm is generally faster?
(a) Ask each student for the list of their groupmates’ classes, and check for each class whether it is CPSC If the answer is ever yes, include the student in my count. (b) For each student s1 in the class, ask the student for each other student s2 in the class whether s2 is a groupmate. If the answer is ever yes, include s1 in my count. (c) Neither.

71 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 group takes 1 second. Algorithm (a) takes ~10*12*5 seconds = 10 minutes. Algorithm (b) takes ~10*10 seconds < 2 minutes. 10*10 is not really right. Should be 10*9/2 or some such… asymptotically makes no difference. If students ask, just point out that we’ll get big enough so any differences will still apply. BTW, we’re going to try these on what REALLY ARE reasonable class sizes. A little seminar, a midsized lecture, and a BIG lecture.

72 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 group takes 1 second. Algorithm (a) takes ~100*12*5 seconds = 100 minutes. Algorithm (b) takes ~100*100 seconds  167 minutes. Again, shouldn’t really be 100*100.

73 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 group takes 1 second. Algorithm (a) takes ~400*12*5 seconds  7 hours. Algorithm (b) takes ~400*400 seconds  44 hours. Now, even if we divide 400*400 by 2 (and lop a bit more off to get to 400*399), it’s still much more than 7 hours.

74 Which algorithm is generally faster?
(a) Ask each student for the list of their groupmates’ classes, and check for each class whether it is CPSC If the answer is ever yes, include the student in my count. (b) For each student s1 in the class, ask the student for each other student s2 in the class whether s2 is a groupmate. If the answer is ever yes, include s1 in my count. (c) Neither.

75 Comparing at One Input Size
Let the predicate Faster(a1, a2, n) mean algorithm a1 is faster than algorithm a2 on a problem of size n, where n is a positive integer. time Alg A 2009W1: Many students cue into the fact that we can’t talk about just being “faster” on a given problem size. They talk about “averages” or “majorities” for a problem size. So, I need to spend at least a few words on this. Mention worst case? Mention adversary? Alg B problem size We’ll assume the Faster predicate is given to us.

76 How Faster Works (1 of 3) Faster(a1,a2,n): a1 is faster than a2 at size n. Which of the following means “no algorithm is ever faster than itself”? n  Z+, a  A, ~Faster(a,a,n). n  Z+, a1  A, a2  A, Faster(a1,a2,n)  ~Faster(a2,a1,n). n  Z+, a1  A, a2  A, a3  A, (Faster(a1,a2,n)  Faster(a2,a3,n))  Faster(a1,a3,n). None of these. Worth mentioning the word “reflexive”/”anti-reflexive”? A is correct. Alg A Alg B problem size time We will assume this statement is true.

77 How Faster Works (2 of 3) Faster(a1,a2,n): a1 is faster than a2 at size n. Which of the following means “two algorithms cannot be faster than each other”? n  Z+, a1  A, a2  A, Faster(a1,a2,n)  ~Faster(a2,a1,n). n  Z+, a1  A, a2  A, Faster(a1,a2,n)  ~Faster(a2,a1,n). n  Z+, a1  A, a2  A, Faster(a1,a2,n)  Faster(a2,a1,n). b and c. All of these. Talk about “symmetry” here? (This isn’t quite anti-symmetry, however.) Technically, ONLY a is correct. The others mean in addition that at least one algorithm is faster than the other for a particular input size. This may be a good time to talk about students’ issues in the quiz wrt averages, etc. That is, it may be that neither is faster than the other at some problem sizes (or even all!). Alg A Alg B problem size time We will assume this statement is true.

78 How Faster Works (3 of 3) What does the following statement mean?
Faster(a1,a2,n): a1 is faster than a2 at size n. What does the following statement mean? n  Z+, a1  A, a2  A, a3  A, (Faster(a1,a2,n)  Faster(a2,a3,n))  Faster(a1,a3,n). Three algorithms cannot be faster than each other. Three algorithms are the same “speed”. Of any three algorithms, one is the fastest. An algorithm cannot be faster than itself. None of these. Talk about transitivity here? None of these. State transitivity in English for them. Alg A Alg B problem size time We will assume this statement is true.

79 How Faster Works (4 of 3, bonus!)
Faster(a1,a2,n): a1 is faster than a2 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.) ANTI-SYMMETRIC IS NOT QUITE RIGHT HERE. OK abuse of terminology? Maybe not  Alg A Alg B problem size time

80 General Efficiency of Algorithms
Faster(a1,a2,n): a1 is faster than a2 at size n. Problem: Create a definition of GenerallyFaster(a1, a2) in terms of Faster(a1, a2, n) that you can live with. Alg A Alg B problem size time

81 Desirable Properties of Generally Faster
Which of these properties should Generally Faster share with Faster? Anti-reflexivity Anti-symmetry Transitivity Should share all of these. Should share none of these. Should really share ALL of these. Alg A Alg B problem size time

82 English-Language Generally Faster Definitions
Which one do you want? [Your definitions here.] None of these. Here’s the HARD but fun part: solicit students’ contributions! (Translate to formal pred logic on the next slide. YOU are going to need to take the burden on that one ) This worked GREAT in 2008W2 when students regularly did group problem-solving and infrequently used clickers. (They came up with “the right” solution, a radically different formulation that turned out to be logically equivalent to “the right” solution, and another solution that failed anti-symmetry (an algorithm that was faster on every odd vs. Another faster on every even were generally faster than each other). That was AWESOME.) It worked not so great in 2009W1 when they did less open-ended problem-solving on their own. Still, with time and the right encouragement/management, I think it CAN work. 2011W1: worked wonderfully in a big class with lots of students thinking it over. Many of them came up with def’ns that resolve to the “classic” def’n we’re working on; others came up with alternate, interesting def’ns. As previously, the big problem was expressing what we wanted in the available language (e.g., can’t just do limits!). Alg A Alg B problem size time

83 Formal Generally Faster Definitions
Which one do you want? [Your definitions here.] None of these. Alg A Alg B problem size time

84 Which algorithm is generally faster?
(a) Ask each student for the list of their groupmates’ classes, and check for each class whether it is CPSC If the answer is ever yes, include the student in my count. (b) For each student s1 in the class, ask the student for each other student s2 in the class whether s2 is a groupmate. If the answer is ever yes, include s1 in my count. (c) Neither. If you settled on the “Standard” definition, the answer is (a). Alg A Alg B problem size time

85 Outline Prereqs, Learning Goals, and Quiz Notes
Prelude: Scope and Predicate Definition Problems and Discussion Liszt Etudes Sorted Lists Comparing Algorithms Next Lecture Notes

86 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. Discuss point of learning goals.

87 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, 3rd ed 98-99, Epp, 4th ed ). 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.

88 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 that’s due before the next class. Previously (Epp 3rd ed): Solve problems like Exercise Set 2.1 #25-28 and 30-31; Set 2.2 #1-25 and 27-36; Set 2.3 #13-20, 23-29, 40-42, and 45-52; and Set 2.4 #2-19, and

89 (on your own or if we have time)
More problems to solve... (on your own or if we have time)

90 Problem: Java Collections
Problem: Translate the following text from the Java 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))." c ? a : b acts essentially like a multiplexer. If c is true, it evaluates to a; otherwise, it evaluates to b.

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

92 More Quantifier Examples
Someone is in charge. Everyone except the person in charge reports to someone else.

93 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).

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

95 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 Let’s practice turning English statements into logic. In the process, we’ll discover how valuable it is to have a precise language for expressing statements. Things that appear to mean the same thing in English will often be clearly different in logic, and we’ll be able to understand WHY they’re different!

96 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

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

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

99 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. EMPHASIZE: logic is NOT ambiguous. Is that English sentence ambiguous?

100 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?).

101 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?).

102 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?

103 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”?

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

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

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

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

108 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: c1C, dD, c2C, Winner(c1)  c1c2  n1Z, n2Z, Tally(d,n1,c1)  Tally(d,n2,c2)  n1>n2


Download ppt "CPSC 121: Models of Computation 2016W2"

Similar presentations


Ads by Google