84
snick snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville and others 1

Snick snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Embed Size (px)

Citation preview

Page 1: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

snick

snack

CPSC 121: Models of Computation2011 Winter Term 1

Describing the World with Predicate Logic

Steve Wolfman, based on notes by Patrice Belleville and others

1

Page 2: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

Page 3: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

6

Page 4: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

7

Page 5: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Outline

• Prereqs, Learning Goals, and Quiz Notes• Prelude: Motivation, Scope & Defining Predicates

• Problems and Discussion– Liszt Etudes– Sorted Lists– Comparing Algorithms

• Next Lecture Notes

8

Page 6: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

9NO predicates allowed, just things like p, q, r, etc.

Page 7: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

10

Page 8: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

What Does Predicate Logic Model?

• Relationships among real-world objects

• Generalizations about patterns

• Infinite domains

11Problems where the properties of different concepts,

ideas, parts, or entities depend on each other.

Page 9: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

12

Page 10: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Quantifier Scope

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

13

Page 11: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

14

Page 12: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

15

Page 13: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

16

Page 14: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

Page 15: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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?

18

Page 16: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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?

19

Page 17: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

Page 18: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

21

Page 19: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

Page 20: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.False, because (for example) no integer

multiplied by itself equals 3.c. It depends on y, but given a value for y, we

could calculate a truth value.d.It depends on y, but we may also need

additional information.e.None of the above.

5

5

23

Page 21: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

24

Page 22: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

PerfectSquare(25). true

PerfectSquare(3). false

y Z, PerfectSquare(y). false

5

5

25

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.

Page 23: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Unbound Variables Check

Which variable(s) does this formula’s truth depend on?

i Z+, (i > n) ~v Z0, 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.26

Page 24: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Outline

• Prereqs, Learning Goals, and Quiz Notes

• Prelude: Scope and Predicate Definition

• Problems and Discussion– Liszt Etudes– Sorted Lists– Comparing Algorithms

• Next Lecture Notes

27

Page 25: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Defining Lists (aka Arrays)

Let Elt(a, i, v) be a predicate indicating that list a has the integer value v at index i, where indexes must be 1.

2 4 5 7 6 10

1 2 3 4 5 6

Elt(mylist, 3, 5) is true.Elt(mylist, 2, 1) and Elt(mylist, 7, 2) are false.

mylist

28

Page 26: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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, 1, 7), Elt(list, 2, 4), Elt(list, 1, 3)

b.Elt(list, 1, 7), Elt(list, 2, 4), Elt(list, 4, 3)

c.Elt(list, -1, 7), Elt(list, 0, 4), Elt(list, 1, 3)

d.Elt(list, 1, 7), Elt(list, 2, 7), Elt(list, 3, 3)

e.Nothing. (i Z+, v Z, ~Elt(list, i, v).)

29

Page 27: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

List Element Exercise

Define a predicate Contains(a, v) meaning “list a contains the value v”.

Contains(a, v) a A, i Z+, v Z, Elt(a,i,v) a A, i Z+, v Z, Elt(a,i,v) i Z+, Elt(a,i,v) i Z+, Elt(a,i,v)• None of these

30This really means the list contains the value “in at least one place”.It might contain the value once or twice or three times or …

Page 28: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

List Element Exercise

Elt(a, i, v) a has value v at index i. Let A be the set of all lists.Which of these means: a list cannot have more than one element at

any index.a. a A, i Z+, v1 Z,

Elt(a,i,v1) ~v2 Z, Elt(a,i,v2).

b. a A, i Z+, v1 Z, v2 Z, Elt(a,i,v1) Elt(a,i,v2).

c. a A, i Z+, v1 Z, v2 Z, (Elt(a,i,v1) Elt(a,i,v2)) v1 = v2.

d. Both a and c.e. All of these We will assume henceforth that a list cannot

have more than one element at any index. 31

Page 29: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Assumptions/Definitions for Lists

• A list cannot have more than one element at any indexa A, i Z+, v1 Z, v2 Z,

(Elt(a,i,v1) Elt(a,i,v2)) v1 = v2.

32

Page 30: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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 Z0, Length(a, n)

mean?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.

33

We will assume henceforth that this statement is true.

Page 31: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Assumptions/Definitions for Lists

• A list cannot have more than one element at any index.a A, i Z+, v1 Z, v2 Z,

(Elt(a,i,v1) Elt(a,i,v2)) v1 = v2.

• Every list has a length.a A, n Z0, Length(a, n).

34

Page 32: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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 more than one length.c.No list should have elements at any index

larger than its length.d.A list should have an element at every index

up to its length.e.All of these should be true.

35

Page 33: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

No More than One Length

Which of the following means that a list should not have more than one length?

a. a A, n1 Z0, Length(a,n1) ~n2 Z0, n1 n2 Length(a,n2).

b. a A, n1 Z0, n2 Z0, n1 n2 (Length(a,n1) Length(a,n2)).

c. a A, n1 Z0, n2 Z0, (Length(a,n1) Length(a,n2)) n1 = n2.

d. Both a and c.e. All of these

36

We will assume henceforth that this statement is true.

Page 34: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Better “No More Than One”?

Let’s make a new predicate “MultiLength(a)” meaning list a has more than one length:

MultiLength(a) n1 Z0, n2 Z0, Length(a,n1) Length(a,n2).

That’s not quite right. What’s missing?

Now, how do we write “No list may have more than one length”?

37

Page 35: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Defining Length(a, n)

Consider: Length(a,n) i Z+, (v Z0, Elt(a, i, v)) (i n).

In English: There’s an element at i if and only if i is a valid index (no greater 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 cannot be greater than the length of the list.d. None of these, but it is guaranteed.e. None of these, and it’s not guaranteed.

38

We will assume henceforth that this statement is true.

Page 36: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Assumptions/Definitions for Lists

• A list cannot have more than one element at any index.a A, i Z+, v1 Z, v2 Z,

(Elt(a,i,v1) Elt(a,i,v2)) v1 = v2.

• Every list has a length.a A, n Z0, Length(a, n).

• A list “has a length n” exactly when there’s an element at i if and only if i is a valid index, less than or equal to n. Length(a,n) i Z+, (v Z0, Elt(a, i, v)) (i n).– So, no list has more than one length.

– And there’s an element at every index up to the list’s length.

– And there’s no element at any index greater than the length.39

Page 37: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Outline

• Prereqs, Learning Goals, and Quiz Notes

• Prelude: Scope and Predicate Definition

• Problems and Discussion– Liszt Etudes– Sorted Lists– Comparing Algorithms

• Next Lecture Notes

40

Page 38: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

?

41

Page 39: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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 Z+, Elt(a,i,v1) < Elt(a,i+1,v2).

a. a isn’t quantified.

b. v1 and v2 aren’t quantified.

c. We can’t use < on Elt (or any other predicate)

d. a and b

e. b and c

?

42

Page 40: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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 Z+, v1 Z, v2 Z,

(Elt(a,i,v1) Elt(a,i+1,v2)) v1 < v2.

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.

d. a and b

e. b and c

?

43

Page 41: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

But... Would You Ever Really Use Pred Logic Like This?• 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...”

44

Page 42: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Predicate Logic Patterns

“There exists” means “there’s at least one”. We often want “there’s exactly one” (e.g., lists have exactly one element at each valid index).

Common problems like this lead to common patterns to solve them.• “At least one” plus “at most one” means “exactly one”.• “At most one” is: if any two items have this property,

then they’re really the same item.• “Two distinct” or “at least two” is: exists one, exists

another, such that the first is not equal to the second

45

Page 43: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

46

Page 44: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Outline

• Prereqs, Learning Goals, and Quiz Notes

• Prelude: Scope and Predicate Definition

• Problems and Discussion– Liszt Etudes– Sorted Lists– Comparing Algorithms

• Next Lecture Notes

47

Page 45: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

48

Page 46: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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 s1 in the class, ask the student for each other student s2 in the class whether s2 is a MUG-mate. If the answer is ever yes, include s1 in my count.

(c) Neither.49

Page 47: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

50

Page 48: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

51

Page 49: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

52

Page 50: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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 s1 in the class, ask the student for each other student s2 in the class whether s2 is a MUG-mate. If the answer is ever yes, include s1 in my count.

(c) Neither.53

Page 51: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

Alg A

Alg Bproblem size

time

54We’ll assume the Faster predicate is given to us.

Page 52: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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”?a.n Z+, a A, ~Faster(a,a,n).b. n Z+, a1 A, a2 A,

Faster(a1,a2,n) ~Faster(a2,a1,n).

c. n Z+, a1 A, a2 A, a3 A, (Faster(a1,a2,n) Faster(a2,a3,n))

Faster(a1,a3,n).

d.None of these.55

Alg A

Alg Bproblem size

time

We will assume this statement is true.

Page 53: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

a. n Z+, a1 A, a2 A, Faster(a1,a2,n) ~Faster(a2,a1,n).

b. n Z+, a1 A, a2 A, Faster(a1,a2,n) ~Faster(a2,a1,n).

c. n Z+, a1 A, a2 A, Faster(a1,a2,n) Faster(a2,a1,n).

d. b and c.e. All of these.

56

Alg A

Alg Bproblem size

time

We will assume this statement is true.

Page 54: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

How Faster Works (3 of 3)

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

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.

57

Alg A

Alg Bproblem size

time

We will assume this statement is true.

Page 55: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

58

Alg A

Alg Bproblem size

time

Page 56: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

59

Alg A

Alg Bproblem size

time

Page 57: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

60

Alg A

Alg Bproblem size

time

Page 58: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

English-Language Generally Faster Definitions

Which one do you want? [Your definitions here.]

a.

b.

c.

d. None of these.

61

Alg A

Alg Bproblem size

time

Page 59: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

FormalGenerally Faster Definitions

Which one do you want? [Your definitions here.]

a.

b.

c.

d. None of these.

62

Alg A

Alg Bproblem size

time

Page 60: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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 s1 in the class, ask the student for each other student s2 in the class whether s2 is a MUG-mate. If the answer is ever yes, include s1 in my count.

(c) Neither.63

Alg A

Alg Bproblem size

time

Page 61: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Outline

• Prereqs, Learning Goals, and Quiz Notes

• Prelude: Scope and Predicate Definition

• Problems and Discussion– Liszt Etudes– Sorted Lists– Comparing Algorithms

• Next Lecture Notes

64

Page 62: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

65

Page 63: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

66

Page 64: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

67

Page 65: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

snick

snack

More problems to solve...

(on your own or if we have time)

68

Page 66: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

c ? a : b acts essentially like a multiplexer.If c is true, it evaluates to a; otherwise, it evaluates to b.

69

Page 67: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

70

Page 68: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

More Quantifier Examples

Someone is in charge.

Everyone except the person in charge reports to someone else.

71

Page 69: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

Page 70: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

73

Page 71: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

74

Page 72: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

75

Page 73: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

76

Page 74: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

77

Page 75: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

Page 76: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Yet More Examples

Is x, K(x, y) a proposition?

Why or why not?

F(x): x is a fierce creatureL(x): x is a lionC(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?).

79

Page 77: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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 creatureL(x): x is a lionC(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?).

80

Page 78: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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 creatureL(x): x is a lionC(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?81

Page 79: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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 creatureL(x): x is a lionC(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”?

82

Page 80: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

83

Page 81: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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

Page 82: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

85

Page 83: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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.

86

Page 84: Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

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:

c1C, dD, c2C, Winner(c1) c1c2 n1Z, n2Z, Tally(d,n1,c1) Tally(d,n2,c2) n1>n2

87