CPSC 121: Models of Computation Instructor: Bob Woodham - - PowerPoint PPT Presentation

cpsc 121 models of computation
SMART_READER_LITE
LIVE PREVIEW

CPSC 121: Models of Computation Instructor: Bob Woodham - - PowerPoint PPT Presentation

CPSC 121: Models of Computation Instructor: Bob Woodham woodham@cs.ubc.ca Department of Computer Science University of British Columbia Lecture Notes 2008/2009, Section 203 CPSC 121: Models of Computation Menu January 9, 2009 Topics:


slide-1
SLIDE 1

CPSC 121: Models of Computation

Instructor: Bob Woodham woodham@cs.ubc.ca

Department of Computer Science University of British Columbia

Lecture Notes 2008/2009, Section 203

CPSC 121: Models of Computation

slide-2
SLIDE 2

Menu January 9, 2009

Topics: Conditional Statement — Implication — Biconditional Logical Equivalence Reading: Today: Epp 1.2 January 12: Epp 1.5 Next: Epp 1.3 Reminders: Labs and tutorials begin week of January 12 On-line Quiz 3 deadline 9:00pm January 13 On-line Quiz 2 deadline was 9:00pm January 8 On-line/static Quiz 1 (mark has been waived) WebCT Vista: http://www.vista.ubc.ca www: http://www.ugrad.cs.ubc.ca/~cs121/

CPSC 121: Models of Computation

slide-3
SLIDE 3

Why Truth Tables?

We use truth tables to:

1

Define the meaning of logical connectives

2

Construct (simple) proofs in propositional logic

3

Design (simple) combinational circuits

CPSC 121: Models of Computation

slide-4
SLIDE 4

All 16 Logical Operators (with “Gate” Names)

B ∗ B X N N N U ∗ U N A A X O O N O F ∗ F O N N O T T O p q R p ∗ q R D D R q p R T T T T T T T T T T F F F F F F F F T F T T T T F F F F T T T T F F F F F T T T F F T T F F T T F F T T F F F F T F T F T F T F T F T F T F T F

CPSC 121: Models of Computation

slide-5
SLIDE 5

All 16 Logical Operators (with “Gate” Names)

B ∗ B X N N N U ∗ U N A A X O O N O F ∗ F O N N O T T O p q R p ∗ q R D D R q p R T T T T T T T T T T F F F F F F F F T F T T T T F F F F T T T T F F F F F T T T F F T T F F T T F F T T F F F F T F T F T F T F T F T F T F T F

∗ The operator whose truth values are given in this column is

called implication. Implication is very important in formal logic (and we’ll return to it shortly). Note, however, that implication is not a named gate (in hardware)

CPSC 121: Models of Computation

slide-6
SLIDE 6

All 16 Logical Operators (with “Gate” Names)

B ∗ B X N N N U ∗ U N A A X O O N O F ∗ F O N N O T T O p q R p ∗ q R D D R q p R T T T T T T T T T T F F F F F F F F T F T T T T F F F F T T T T F F F F F T T T F F T T F F T T F F T T F F F F T F T F T F T F T F T F T F T F

∗ The operator whose truth values are given in this column is

called implication. Implication is very important in formal logic (and we’ll return to it shortly). Note, however, that implication is not a named gate (in hardware)

CPSC 121: Models of Computation

slide-7
SLIDE 7

(Harder) Question

How many distinct Boolean operators are there of n Boolean variables? Answer: There are 2(2n) The truth table has 2n rows since there are that number of distinct combinations of truth values for the n variables. For each row, we have an entry that can be either true or false. Thus, there are 2(2n) distinct truth tables for n Boolean variables We use truth tables as a proof method for propositional logic. But, this is feasible only when the number of variables is small. There are other proof methods for propositional logic that we will need to consider

CPSC 121: Models of Computation

slide-8
SLIDE 8

Recall from Last Lecture

Proposition: a statement that is true or false (but not both) Compound Proposition: formed by combining existing propositions with logical connectives (∧ ∨ ¬ ⊕) defined, via a truth table, as follows: p q p ∧ q p ∨ q ¬p p ⊕ q T T T T F F T F F T F T F T F T T T F F F F T F Digital circuit design uses logic gates (NOT AND OR XOR NAND NOR XNOR) to describe a circuit Digital circuits produce the correct binary output for each combination of binary inputs

CPSC 121: Models of Computation

slide-9
SLIDE 9

Question Follow Up

There is no required “correct” ordering to rows in a truth table. For example, the two truth tables p q p ∧ q p ∨ q ¬p p ⊕ q T T T T F F T F F T F T F T F T T T F F F F T F p q AND OR NOT p XOR 1 1 1 1 1 1 1 1 1 1 1 1 are equivalent (when we associate T with 1 and F with 0)

CPSC 121: Models of Computation

slide-10
SLIDE 10

(In-Class) Example 1:

1

Construct the truth table for: (¬p ∨ q) ∧ ¬(¬q ∨ p)

2

Draw the corresponding circuit diagram

3

Find a simpler circuit that is logically equivalent to the circuit in step 2

CPSC 121: Models of Computation

slide-11
SLIDE 11

Implication

Suppose we have 2 propositions: p: You fail the final exam for CPSC 121 q: You fail the course Now, the CPSC 121 course outline tells you that if you fail the final exam then you fail the course Question: How do we write this in propositional logic? Answer: We write p → q (and say “p implies q”) This is called a conditional proposition (or an implication) with p the hypothesis (or antecedent or premise) and q the conclusion (or consequent) Informally, implication corresponds to the English “if p then q”

CPSC 121: Models of Computation

slide-12
SLIDE 12

Implication

Informally, implication also corresponds to the English “p implies q” “q if p” “q whenever p” and to the slightly more mathematical “p is a sufficient condition for q” “q is a necessary condition for p” “p only if q” . . . and even more

CPSC 121: Models of Computation

slide-13
SLIDE 13

Implication

Formally, we define implication via a truth table: p → q p q p → q T T T T F F F T T F F T

CPSC 121: Models of Computation

slide-14
SLIDE 14

Implication

Formally, we define implication via a truth table: p → q p q p → q T T T T F F F T T F F T

CPSC 121: Models of Computation

slide-15
SLIDE 15

Implication

Formally, we define implication via a truth table: p → q p q p → q T T T T F F F T T F F T But, isn’t something strange here? If the hypothesis is false

CPSC 121: Models of Computation

slide-16
SLIDE 16

Implication

Formally, we define implication via a truth table: p → q p q p → q T T T T F F F T T F F T But, isn’t something strange here? If the hypothesis is false then the implication is true

CPSC 121: Models of Computation

slide-17
SLIDE 17

Implication

Formally, we define implication via a truth table: p → q p q p → q T T T T F F F T T F F T But, isn’t something strange here? If the hypothesis is false then the implication is true even though the conclusion may be true or false

CPSC 121: Models of Computation

slide-18
SLIDE 18

Implication

Formally, we define implication via a truth table: p → q p q p → q T T T T F F F T T F F T But, isn’t something strange here? If the hypothesis is false then the implication is true even though the conclusion may be true or false

CPSC 121: Models of Computation

slide-19
SLIDE 19

Implication

Let’s return to our example p: You fail the final exam for CPSC 121 q: You fail the course If p is false then p → q is true. But, this doesn’t say whether or not you pass CPSC 121. Proposition q could still be true or false IMPORTANT POINT: Don’t confuse the truth value of the conclusion with the truth value of the implication!

CPSC 121: Models of Computation

slide-20
SLIDE 20

Another Implication Example

Suppose we have 2 propositions: p: You are > 200 years old q: You are female Question: Is p → q true or false? Answer: The implication p → q is (vacuously) true The implication p → q asserts that q is a necessary condition for p. That is, being female is a necessary condition for being > 200 years old The implication p → q does not assert that p is a necessary condition for q. There are lots of females in the world. It is not necessary to be > 200 years old in order to be female

CPSC 121: Models of Computation

slide-21
SLIDE 21

Yet Another Implication Example

Suppose we have 2 propositions: p: You rob a bank q: You are arrested Consider the implication p → q p is a sufficient condition for q p is not a necessary condition for q. (There are many other ways to get yourself arrested)

CPSC 121: Models of Computation

slide-22
SLIDE 22

Our CPSC 121 Example One More Time

Suppose we take our original 3 propositions: p: You fail the final exam for CPSC 121 q: You fail the course P1: p → q and add 3 more: r: You pass the course s: You pass the final exam for CPSC 121 P2: r only if s P2 says that s is a necessary condition for r P2 also says that r is a sufficient condition for s Finally, s → r is false while P1: p → q is true Question: What is the relationship between P1 and P2? HINT: You can assume that p is the negation of s and that q is the negation of r

CPSC 121: Models of Computation

slide-23
SLIDE 23

A Final Implication Example

Suppose we have 2 propositions: p: The rooster crows at dawn q: The sun rises Implication in propositional logic is independent of any cause–and–effect relationship between hypothesis and conclusion In this example, there may be circumstances in which we conclude that p → q is true. But, that does not mean that we are concluding that it is the rooster’s crowing that causes the sun to rise

CPSC 121: Models of Computation

slide-24
SLIDE 24

Biconditional

Let p and q be propositions. In mathematics, we often write “p if and only if q” Question: How do we write this in propositional logic? Answer: We write p ↔ q This is called a biconditional proposition Informally, the biconditional also corresponds to the English “p is necessary and sufficient for q” “p iff q”

CPSC 121: Models of Computation

slide-25
SLIDE 25

Biconditional

Formally, we define the biconditional via a truth table: p ↔ q p q p ↔ q T T T T F F F T F F F T

CPSC 121: Models of Computation

slide-26
SLIDE 26

Biconditional

Formally, we define the biconditional via a truth table: p ↔ q p q p ↔ q T T T T F F F T F F F T

CPSC 121: Models of Computation

slide-27
SLIDE 27

Biconditional

Formally, we define the biconditional via a truth table: p ↔ q p q p ↔ q T T T T F F F T F F F T NOTE: p ↔ q has the same truth table as (p → q) ∧ (q → p)

CPSC 121: Models of Computation

slide-28
SLIDE 28

Biconditional

Question: Did the biconditional have a “gate name” in our (exhaustive) table of all 16 2-input logical operators?

B B X N N N U U N A A X O O N O F F O N N O T T O p q R p q R D D R q p R T T T T T T T T T T F F F F F F F F T F T T T T F F F F T T T T F F F F F T T T F F T T F F T T F F T T F F F F T F T F T F T F T F T F T F T F

CPSC 121: Models of Computation

slide-29
SLIDE 29

Biconditional

Question: Did the biconditional have a “gate name” in our (exhaustive) table of all 16 2-input logical operators?

B B X N N N U U N A A X O O N O F F O N N O T T O p q R p q R D D R q p R T T T T T T T T T T F F F F F F F F T F T T T T F F F F T T T T F F F F F T T T F F T T F F T T F F T T F F F F T F T F T F T F T F T F T F T F

Answer: Yes, it is XNOR

CPSC 121: Models of Computation

slide-30
SLIDE 30

Biconditional

Question: Did the biconditional have a “gate name” in our (exhaustive) table of all 16 2-input logical operators?

B B X N N N U U N A A X O O N O F F O N N O T T O p q R p q R D D R q p R T T T T T T T T T T F F F F F F F F T F T T T T F F F F T T T T F F F F F T T T F F T T F F T T F F T T F F F F T F T F T F T F T F T F T F T F

Answer: Yes, it is XNOR

CPSC 121: Models of Computation

slide-31
SLIDE 31

(In-Class) Example 2:

Construct the truth table for: (p → q) ↔ (¬p ∨ q) p q T T T F F T F F

CPSC 121: Models of Computation

slide-32
SLIDE 32

(In-Class) Example 2:

Construct the truth table for: (p → q) ↔ (¬p ∨ q) 3 p q p → q T T T T F F F T T F F T Column 3 gives truth values for p → q.

CPSC 121: Models of Computation

slide-33
SLIDE 33

(In-Class) Example 2:

Construct the truth table for: (p → q) ↔ (¬p ∨ q) 3 4 p q p → q ¬p T T T F T F F F F T T T F F T T Column 3 gives truth values for p → q. Add truth values for ¬p.

CPSC 121: Models of Computation

slide-34
SLIDE 34

(In-Class) Example 2:

Construct the truth table for: (p → q) ↔ (¬p ∨ q) 3 4 5 p q p → q ¬p ¬p ∨ q T T T F T T F F F F F T T T T F F T T T Column 3 gives truth values for p → q. Add truth values for ¬p. Column 5 gives truth values for ¬p ∨ q.

CPSC 121: Models of Computation

slide-35
SLIDE 35

(In-Class) Example 2:

Construct the truth table for: (p → q) ↔ (¬p ∨ q) 3 4 5 6 p q p → q ¬p ¬p ∨ q (p → q) ↔ (¬p ∨ q) T T T F T T T F F F F T F T T T T T F F T T T T Column 3 gives truth values for p → q. Add truth values for ¬p. Column 5 gives truth values for ¬p ∨ q. Columns 3 and 5 are identical so that column 6 is always true

CPSC 121: Models of Computation

slide-36
SLIDE 36

(In-Class) Example 2:

Construct the truth table for: (p → q) ↔ (¬p ∨ q) 3 4 5 6 p q p → q ¬p ¬p ∨ q (p → q) ↔ (¬p ∨ q) T T T F T T T F F F F T F T T T T T F F T T T T Column 3 gives truth values for p → q. Add truth values for ¬p. Column 5 gives truth values for ¬p ∨ q. Columns 3 and 5 are identical so that column 6 is always true Since (p → q) ↔ (¬p ∨ q) is always true, we say that p → q and ¬p ∨ q are logically equivalent and we write p → q ≡ ¬p ∨ q

CPSC 121: Models of Computation

slide-37
SLIDE 37

Definitions

A tautology is a compound proposition that is always true Example: p ∨ ¬p A contradiction is compound proposition that is always false Example: p ∧ ¬p Any other proposition is called a contingency Example: p ∨ q The truth value of a contingency depends upon the truth values

  • f its constituent propositions

CPSC 121: Models of Computation

slide-38
SLIDE 38

(In-Class) Example 3:

Prove that p ↔ q ≡ (p → q) ∧ (q → p) Again, we’ll use a truth table: p q T T T F F T F F

CPSC 121: Models of Computation

slide-39
SLIDE 39

(In-Class) Example 3:

Prove that p ↔ q ≡ (p → q) ∧ (q → p) Again, we’ll use a truth table: 3 p q p ↔ q T T T T F F F T F F F T Column 3 gives truth values for p ↔ q.

CPSC 121: Models of Computation

slide-40
SLIDE 40

(In-Class) Example 3:

Prove that p ↔ q ≡ (p → q) ∧ (q → p) Again, we’ll use a truth table: 3 4 p q p ↔ q p → q T T T T T F F F F T F T F F T T Column 3 gives truth values for p ↔ q. Column 4 gives truth values for p → q.

CPSC 121: Models of Computation

slide-41
SLIDE 41

(In-Class) Example 3:

Prove that p ↔ q ≡ (p → q) ∧ (q → p) Again, we’ll use a truth table: 3 4 5 p q p ↔ q p → q q → p T T T T T T F F F T F T F T F F F T T T Column 3 gives truth values for p ↔ q. Column 4 gives truth values for p → q. Column 5 gives truth values for q → p.

CPSC 121: Models of Computation

slide-42
SLIDE 42

(In-Class) Example 3:

Prove that p ↔ q ≡ (p → q) ∧ (q → p) Again, we’ll use a truth table: 3 4 5 6 p q p ↔ q p → q q → p (p → q) ∧ (q → p) T T T T T T T F F F T F F T F T F F F F T T T T Column 3 gives truth values for p ↔ q. Column 4 gives truth values for p → q. Column 5 gives truth values for q → p. Column 6 gives truth values for (p → q) ∧ (q → p).

CPSC 121: Models of Computation

slide-43
SLIDE 43

(In-Class) Example 3:

Prove that p ↔ q ≡ (p → q) ∧ (q → p) Again, we’ll use a truth table: 3 4 5 6 p q p ↔ q p → q q → p (p → q) ∧ (q → p) T T T T T T T F F F T F F T F T F F F F T T T T Column 3 gives truth values for p ↔ q. Column 4 gives truth values for p → q. Column 5 gives truth values for q → p. Column 6 gives truth values for (p → q) ∧ (q → p). Since columns 3 and 6 are identical, their biconditional is a

  • tautology. That is,

p ↔ q ≡ (p → q) ∧ (q → p)

CPSC 121: Models of Computation

slide-44
SLIDE 44

(In-Class) Example 3:

Prove that p ↔ q ≡ (p → q) ∧ (q → p) Again, we’ll use a truth table: 3 4 5 6 p q p ↔ q p → q q → p (p → q) ∧ (q → p) T T T T T T T F F F T F F T F T F F F F T T T T Column 3 gives truth values for p ↔ q. Column 4 gives truth values for p → q. Column 5 gives truth values for q → p. Column 6 gives truth values for (p → q) ∧ (q → p). Since columns 3 and 6 are identical, their biconditional is a

  • tautology. That is,

p ↔ q ≡ (p → q) ∧ (q → p)

CPSC 121: Models of Computation

slide-45
SLIDE 45

(In-Class) Example 4:

Consider the 4 propositions: v: Vulcans are logical s: Spock is a Vulcan P1: v → s (If Vulcans are logical then Spock is a Vulcan) P2: s → v (If Spock is not a Vulcan then Vulcans are not logical) P2 is called the contrapositive of P1

CPSC 121: Models of Computation

slide-46
SLIDE 46

(In-Class) Example 4:

Consider the 4 propositions: v: Vulcans are logical s: Spock is a Vulcan P1: v → s (If Vulcans are logical then Spock is a Vulcan) P2: s → v (If Spock is not a Vulcan then Vulcans are not logical) P2 is called the contrapositive of P1 v s v → s T T T T F F F T T F F T Consider the truth table for P1. P1 is false only if Vulcans are logical and Spock is not a Vulcan.

CPSC 121: Models of Computation

slide-47
SLIDE 47

(In-Class) Example 4:

Consider the 4 propositions: v: Vulcans are logical s: Spock is a Vulcan P1: v → s (If Vulcans are logical then Spock is a Vulcan) P2: s → v (If Spock is not a Vulcan then Vulcans are not logical) P2 is called the contrapositive of P1 v s v → s T T T T F F F T T F F T Consider the truth table for P1. P1 is false only if Vulcans are logical and Spock is not a Vulcan. What is the relationship between P1 and P2? Let’s complete the truth table to see.

CPSC 121: Models of Computation

slide-48
SLIDE 48

(In-Class) Example 4:

Consider the 4 propositions: v: Vulcans are logical s: Spock is a Vulcan P1: v → s (If Vulcans are logical then Spock is a Vulcan) P2: s → v (If Spock is not a Vulcan then Vulcans are not logical) P2 is called the contrapositive of P1 v s v → s v s s → v T T T F F T T F F F T F F T T T F T F F T T T T Consider the truth table for P1. P1 is false only if Vulcans are logical and Spock is not a Vulcan. What is the relationship between P1 and P2? Let’s complete the truth table to see. We

  • bserve: v → s ≡ s → v

CPSC 121: Models of Computation

slide-49
SLIDE 49

Useful Logical Equivalences

Name Rule(s) Identity Laws p ∧ T ≡ p p ∨ F ≡ p Domination Laws p ∧ F ≡ F p ∨ T ≡ T Idempotent Laws p ∧ p ≡ p p ∨ p ≡ p Commutative Laws p ∧ q ≡ q ∧ p p ∨ q ≡ q ∨ p Associative Laws p ∧ (q ∧ r) ≡ (p ∧ q) ∧ r p ∨ (q ∨ r) ≡ (p ∨ q) ∨ r

CPSC 121: Models of Computation

slide-50
SLIDE 50

Useful Logical Equivalences

Name Rule(s) Distributive Laws p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r) p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r) Absorption Laws p ∨ (p ∧ q) ≡ p p ∧ (p ∨ q) ≡ p Negation Laws p ∧ ¬p ≡ F p ∨ ¬p ≡ T Double Negation Law ¬(¬p) ≡ p De Morgan’s Laws ¬(p ∧ q) ≡ ¬p ∨ ¬q ¬(p ∨ q) ≡ ¬p ∧ ¬q

CPSC 121: Models of Computation

slide-51
SLIDE 51

More Useful Logical Equivalences

Name Rule(s) p → q ≡ ¬p ∨ q Contrapositive Law p → q ≡ ¬q → ¬p p ↔ q ≡ (p → q) ∧ (q → p) p ↔ q ≡ ¬(p ⊕ q)

CPSC 121: Models of Computation

slide-52
SLIDE 52

(In-Class) Example 5:

Let P1:p → q be an implication We call p → q the inverse of P1 and q → p the converse of P1 Prove that p → q and q → p are logically equivalent Proof: We start from the inverse and transform it into the converse (using our table of logical equivalences) p → q ≡ q → p Contrapositive Law ≡ q → p Double Negation Law

CPSC 121: Models of Computation

slide-53
SLIDE 53

Let’s Revisit Today’s First In-Class Example

Prove: (p ∨ q) ∧ (q ∨ p) ≡ p ∧ q Proof: Let P1 be (p ∨ q) ∧ (q ∨ p) and P2 be p ∧ q P1 ≡ (p ∨ q) ∧ (q ∧ p) De Morgan ≡ (p ∨ q) ∧ (q ∧ p) Double Negative ≡ (q ∧ p) ∧ (p ∨ q) Commutative ≡ ((q ∧ p) ∧ p) ∨ ((q ∧ p) ∧ q) Distributive ≡ ((q ∧ p) ∧ p) ∨ ((p ∧ q) ∧ q) Commutative ≡ (q ∧ (p ∧ p)) ∨ (p ∧ (q ∧ q)) Asssociative (twice) ≡ (q ∧ p) ∨ (p ∧ q) Idempotent (twice) ≡ (p ∧ q) ∨ (p ∧ q) Commutative ≡ (p ∧ q) Idempotent ≡ P2

CPSC 121: Models of Computation