Quick Review (?) of Sets A set is a collection of elements: the set - - PDF document

quick review of sets
SMART_READER_LITE
LIVE PREVIEW

Quick Review (?) of Sets A set is a collection of elements: the set - - PDF document

Quick Review (?) of Sets A set is a collection of elements: the set of students in this class CPSC 121: Models of Computation the set of lowercase letters in English 2017S the set of natural numbers ( N ) the set of all


slide-1
SLIDE 1

1

CPSC 121: Models of Computation 2017S

Describing the World with Predicate Logic Meghan Allen based on notes by Steve Wolfman, Patrice Belleville and others

1

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

2

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.

3

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)

4

Describing Sets (2/4)

Some sets… A = {1, 5, 25, 125, …} B = {…, -2, -1, 0, 1, 2, …} C = {1, 2, 3, …, 98, 99, 100} (The set of powers of 5, the set of integers, and the set of integers between 1 and 100.)

5

“…” is an ellipsis

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}

6

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”

slide-2
SLIDE 2

2 Describing Sets (4/4)

Graphical depiction of sets: Venn diagrams. Draw the set of all five-letter things. All red things?

7

fire truck snows happiness Texas heart books seven  U U is the universal set of everything.

Outline

  • Prereqs, Learning Goals, and Quiz Notes
  • Prelude: Scope and Predicate Definition
  • Problems and Discussion

– Lists – Sorted Lists – Comparing Algorithms

  • Next Lecture Notes

8

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

9

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.

10

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!

11

Outline

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

– Lists – Sorted Lists – Comparing Algorithms

  • Next Lecture Notes

15

slide-3
SLIDE 3

3

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

16

What Does Predicate Logic Model?

  • Relationships among real-world objects
  • Generalizations about patterns
  • Infinite (or even just big) domains

17

Problems where the properties of different concepts, ideas, parts, or entities depend on each other.

But... Would You Ever Really Use Predicate Logic?

  • Data Structures Example: “...every key is less than
  • r 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...”

18

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

19

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.

20

Quantifier Scope

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

21

slide-4
SLIDE 4

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

22

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

23

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

24

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

25

another smaller subexpression

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

26

Reminder: Z is the set of integers.

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?

27

slide-5
SLIDE 5

5

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?

28

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

29

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.

30

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

31

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

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

32

5 5

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

33

5 5

slide-6
SLIDE 6

6

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

34

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.

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.

35

(DO NOT worry about what it means!)

Outline

  • Prereqs, Learning Goals, and Quiz Notes
  • Prelude: Scope and Predicate Definition
  • Problems and Discussion

– Lists

  • Intermezzo on Idioms

– Sorted Lists – Comparing Algorithms

  • Next Lecture Notes

36

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

37

2 4 5 7 6 10

1 2 3 4 5

(length mylist) is 6. (list-ref mylist 3) is 7. mylist

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? a. Yes. b. No, because they don’t start with capital letters. c. No, because they have parentheses around them instead of after. d. No, because they don’t produce booleans.

38

ListOfInteger

;; ListOfInteger is one of: ;; - empty ;; - (cons Integer ListOfInteger)

Also:

(where index 0 gets us the first element)

39

slide-7
SLIDE 7

7 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  A, i  N, v  Z, (list-ref a i) = v.
  • b. a  A, i  N, v  Z, (list-ref a i) = v.
  • c. i  N, (list-ref a i) = v.
  • d. i  N, (list-ref a i) = v.

e. None of these

40

Hint: Try some examples, like mylist! We can also think of this as a contains v “at least once”.

Outline

  • Prereqs, Learning Goals, and Quiz Notes
  • Prelude: Scope and Predicate Definition
  • Problems and Discussion

– Lists

  • Intermezzo on Idioms

– Sorted Lists – Comparing Algorithms

  • Next Lecture Notes

41

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.

42

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

43

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

44

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

45

slide-8
SLIDE 8

8

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!

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

47

This far, you did on the quiz!

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

48

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

49

Some <thing in D for which P is true>…

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

50

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

51

Every animal likes honey… or isn’t a bear (so we don’t care).

slide-9
SLIDE 9

9

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

52

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

53

All <things in D for which P is true>…

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

54

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

55

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

56

Just “restricting the domain”!

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

57

Just “restricting the domain”!

(We can move Honey(x) inside the  that doesn’t mention x, but beware of negations, (bi)conditionals, and XORs!)

slide-10
SLIDE 10

10 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  …  …

58

The same predicate applied to x and y. The same predicate applied to x and y.

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

59

Idiom Summary

60

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

Outline

  • Prereqs, Learning Goals, and Quiz Notes
  • Prelude: Scope and Predicate Definition
  • Problems and Discussion

– Lists – Sorted Lists – Comparing Algorithms

  • Next Lecture Notes

61

Problem: Sorted Lists

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

62

?

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

a. There is no quantifier for a. b. There is no quantifier for i. c. We can’t use  on (list-ref a i) or (list-ref a i+1). d. We can’t use < on (list-ref a i) or (list-ref a i+1).

63

?

Note: There’s another problem with this. We’ll fix it in a moment!

slide-11
SLIDE 11

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

a. It is too restrictive (because of equal values in the list). b. It does not limit the ranges of i and j. c. It is missing quantifiers. d. Both a and b e. Both b and c

64

?

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

65

?

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

  • ur formal manipulations.”
  • Epp (3rd ed), p. 106-107

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.

66

Outline

  • Prereqs, Learning Goals, and Quiz Notes
  • Prelude: Scope and Predicate Definition
  • Problems and Discussion

– Lists – Sorted Lists – Comparing Algorithms

  • Next Lecture Notes

67

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.

68

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 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 groupmate. If the answer is ever yes, include s1 in my count. (c) Neither.

69

slide-12
SLIDE 12

12

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.

70

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.

71

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.

72

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 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 groupmate. If the answer is ever yes, include s1 in my count. (c) Neither.

73

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.

74

Alg A Alg B problem size time We’ll assume the Faster predicate is given to us.

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.

75

Alg A Alg B problem size time

We will assume this statement is true.

slide-13
SLIDE 13

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

76

Alg A Alg B problem size time

We will assume this statement is true.

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.

77

Alg A Alg B problem size time

We will assume this statement is true.

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

78

Alg A Alg B problem size time

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.

79

Alg A Alg B problem size time

Desirable Properties of Generally Faster

Which of these properties should Generally Faster share with Faster?

  • a. Anti-reflexivity
  • b. Anti-symmetry
  • c. Transitivity
  • d. Should share all of these.
  • e. Should share none of these.

80

Alg A Alg B problem size time

English-Language Generally Faster Definitions

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

  • d. None of these.

81

Alg A Alg B problem size time

slide-14
SLIDE 14

14

Formal Generally Faster Definitions

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

  • d. None of these.

82

Alg A Alg B problem size time

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

  • 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

  • groupmate. If the answer is ever yes, include s1 in

my count. (c) Neither.

83

Alg A Alg B problem size time

Outline

  • Prereqs, Learning Goals, and Quiz Notes
  • Prelude: Scope and Predicate Definition
  • Problems and Discussion

– Lists – Sorted Lists – Comparing Algorithms

  • Next Lecture Notes

84

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.

85

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.

86

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.

87

slide-15
SLIDE 15

15 More problems to solve...

(on your own or if we have time)

88

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

89

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

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

  • .equals(e) to be invoked for any element

e.

Explain whether and how this is consistent with your definition.

90

More Quantifier Examples

Someone is in charge. Everyone except the person in charge reports to someone else.

91

More Quantifier Examples

n is a prime number.

92

Note: we use x|y as a predicate meaning x divides y (i.e., x “goes into” y with no remainder).

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.

93

slide-16
SLIDE 16

16 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

94

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

95

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.

96

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.

97

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.

98

Is that English sentence ambiguous?

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

99

slide-17
SLIDE 17

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

100

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

101

Are these the same?

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

102

Are these the same? What if we removed the word “other”?

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

  • f Tally whose truth set is the set of all

candidates who received at least one vote.

103

Problem: Voting Database

Problem: Define a predicate whose truth set is the set of all candidates who won at least one district.

104

Why work so hard on defining predicates? This is essentially how we query databases.

Problem: Voting Database

Let’s assume that every candidate has exactly

  • ne vote total for every district. That is,

there’s no missing and no duplicate data. Problem: Write a logical statement that describes this constraint.

105

Predicates are clumsy for expressing this common idea. Later, we’ll use functions to do a better job.

slide-18
SLIDE 18

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

106

Problem: Voting Database

Let D be the set of all districts and C be the set

  • f 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

107