cs203 discrete mathematical structures logic (2)

39
CS203 Discrete Mathematical Structures Logic (2)

Upload: clare-black

Post on 05-Jan-2016

235 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS203 Discrete Mathematical Structures Logic (2)

CS203Discrete Mathematical Structures

Logic (2)

Page 2: CS203 Discrete Mathematical Structures Logic (2)

Predicate Logic - everybody loves somebody

Proposition, YES or NO?3 + 2 = 5X + 2 = 5X + 2 = 5 for any choice of X in {1, 2, 3}X + 2 = 5 for some X in {1, 2, 3}

YES

NO

YES

YES

Page 3: CS203 Discrete Mathematical Structures Logic (2)

PredicatesAhmed eats pizza at least once a week.

Define:EP(x) = “x eats pizza at least once a week.”Universe of Discourse - x is a student in CS203

A predicate is a function that takes some variable(s) as arguments and returns True or False.

Note that EP(x) is not a proposition, EP(Hassan) is.

Page 4: CS203 Discrete Mathematical Structures Logic (2)

PredicatesA predicate is a property or description of

subjects in the universe of discourse. The following predicates are all italicized :– Johnny is tall.– The bridge is structurally sound.– 17 is a prime number.

Java: predicates are boolean-valued method calls-– someLinkedList.isEmpty()– isPrime(17)

Page 5: CS203 Discrete Mathematical Structures Logic (2)

QuantifiersThere are two quantifiers• Existential Quantifier

“” reads “there exists”• Universal Quantifier

“” reads “for all”Each is placed in front of a propositional

function and binds it to obtain a proposition with semantic value.

Page 6: CS203 Discrete Mathematical Structures Logic (2)

Predicates - the universal quantifier

Suppose P(x) is a predicate on some universe of discourse.Ex. B(x) = “x is carrying a backpack,” x is set of CS203

students.

The universal quantifier of P(x) is the proposition:“P(x) is true for all x in the universe of discourse.”

We write it x P(x), and say “for all x, P(x)”

x P(x) is TRUE if P(x) is true for every single x.x P(x) is FALSE if there is an x for which P(x) is

false.x

B(x)?

Page 7: CS203 Discrete Mathematical Structures Logic (2)

Predicates - the existential quantifier

Suppose P(x) is a predicate on some universe of discourse.Ex. C(x) = “x has a cat,” x is set of CS203 students.

The existential quantifier of P(x) is the proposition:“P(x) is true for some x in the universe of discourse.”

We write it x P(x), and say “for some x, P(x)”

x P(x) is TRUE if there is an x for which P(x) is true. x P(x) is FALSE if P(x) is false for every single x.

x C(x)?

Page 8: CS203 Discrete Mathematical Structures Logic (2)

Predicates - the existential quantifier

B(x) = “x is wearing sneakers.”L(x) = “x is at least 16 years old.”Y(x)= “x is less than 24 years old.”

Are either of these propositions true?

a) x B(x)b) x (Y(x) L(x))

A: only a is true

B: only b is true

C: both are true

D: neither is true

Universe of discourse is people in this room.

Page 9: CS203 Discrete Mathematical Structures Logic (2)

Predicates - more examples

Universe of discourse is all creatures.

L(x) = “x is a lion.”F(x) = “x is fierce.”C(x) = “x drinks coffee.”

All lions are fierce.

Some lions don’t drink coffee.

Some fierce creatures don’t drink coffee.

x (L(x) F(x))x (L(x) C(x))

x (F(x) C(x))

Page 10: CS203 Discrete Mathematical Structures Logic (2)

Predicates - more examples

Universe of discourse is all creatures.

B(x) = “x is a hummingbird.”L(x) = “x is a large bird.”H(x) = “x lives on honey.”R(x) = “x is richly colored.”

All hummingbirds are richly colored.

No large birds live on honey.

Birds that do not live on honey are dully colored.

x (B(x) R(x))

x (L(x) H(x))

x (H(x) R(x))

Page 11: CS203 Discrete Mathematical Structures Logic (2)

Fundamentals of Logic

The Use of Quantifiers

Ex

p x x

q x x

r x x x

s x x

( ):

( ):

( ):

( ):

0

0

3 4 0

3 0

2

2

2

x p x r x TRUE

x p x q x TRUE

x p x q x TRUE

x q x s x FALSE

x r x s x FALSE

x r x p x FALSE

[ ( ) ( )]:

[ ( ) ( )]:

[ ( ) ( )]:

[ ( ) ( )]:

[ ( ) ( )]:

[ ( ) ( )]:

x=4

x=1

x=5,6,...

x=-1

universe: real numbers

Page 12: CS203 Discrete Mathematical Structures Logic (2)

Predicates - the universal quantifier

B(x) = “x is wearing sneakers.”L(x) = “x is at least 18 years old.”Y(x)= “x is less than 24 years old.”

Are either of these propositions true?

a) x (Y(x) B(x))b) x (Y(x) L(x))

A: only a is true

B: only b is true

C: both are true

D: neither is true

Universe of discourse is people in this Hall.

Page 13: CS203 Discrete Mathematical Structures Logic (2)

Predicates - quantifier negation

Not all large birds live on honey.

x P(x) means “P(x) is true for every x.”What about x P(x) ?

Not [“P(x) is true for every x.”]“There is an x for which P(x) is not true.”

x P(x)

So, x P(x) is the same as x P(x).

x (L(x) H(x))

x (L(x) H(x))

Page 14: CS203 Discrete Mathematical Structures Logic (2)

Predicates - quantifier negation

No large birds live on honey.

x P(x) means “P(x) is true for some x.”What about x P(x) ?

Not [“P(x) is true for some x.”]“P(x) is not true for all x.”

x P(x)

So, x P(x) is the same as x P(x).

x (L(x) H(x))

x (L(x) H(x))

Page 15: CS203 Discrete Mathematical Structures Logic (2)

Predicates - quantifier negation

So, x P(x) is the same as x P(x).So, x P(x) is the same as x P(x).

General rule: to negate a quantifier, move negation to the right, changing quantifiers as you go.

Page 16: CS203 Discrete Mathematical Structures Logic (2)

Fundamentals of Logic

Ex. p(x): x is odd.q(x): x2-1 is even.

Negate

x p x q x[ ( ) ( )] (If x is odd, then x2-1 is even.)

[ ( ( ) ( )] [ ( ( ) ( ))]

[ ( ( ) ( ))] [ ( ) ( )]

x p x q x x p x q x

x p x q x x p x q x

There exists an integer x such that x is odd and x2-1 is odd.(a false statement, the original is true)

Page 17: CS203 Discrete Mathematical Structures Logic (2)

Predicates - quantifier negation

No large birds live on honey.

x (L(x) H(x)) x (L(x) H(x)) Negation rule

x (L(x) H(x)) DeMorgan’s

x (L(x) H(x)) Subst for

What’s wrong with this proof?

Page 18: CS203 Discrete Mathematical Structures Logic (2)

Fundamentals of Logicmultiple variables

x yp x y y xp x y

x yp x y y xp x y

( , ) ( , )

( , ) ( , )

Page 19: CS203 Discrete Mathematical Structures Logic (2)

Fundamentals of LogicBUT

Ex. p(x,y): x+y=17.

x yp x y( , ) : For every integer x, there exists an integer y such that x+y=17. (TRUE)

y xp x y( , ) : There exists an integer y so that for all integer x, x+y=17. (FALSE)

),(),( yxxpyyxypx Therefore,

Page 20: CS203 Discrete Mathematical Structures Logic (2)

20

Order matters

Set the universe of discourse to be all natural numbers {0, 1, 2, 3, … }.

Let R (x,y ) = “x < y”.

Q1: What does x y R (x,y ) mean?

Q2: What does y x R (x,y ) mean?

Page 21: CS203 Discrete Mathematical Structures Logic (2)

21

Order mattersR (x,y ) = “x < y”A1: x y R (x,y ): “All numbers x admit a bigger number y ”A2: y x R (x,y ): “Some number y is bigger than all x”Q: What’s the true value of each

expression?

Page 22: CS203 Discrete Mathematical Structures Logic (2)

More Practice for Predicate Logic

•Nermin likes all movies that Rehab likes (and possibly more).x [Movie(x) Likes(Rehab, x) Likes(Nermin, x)]

•There is exactly one AU professor who won a Nobel prizex[AU_Prof(x) Wins(x, NobelPrize)] y,z[y z AU_Prof(y) AU_Prof(z) Wins(y, NobelPrize) Wins(z, NobelPrize)]

Page 23: CS203 Discrete Mathematical Structures Logic (2)

23

Review of Boolean algebra• Not is a horizontal bar above the number

– 0 = 1– 1 = 0

• Or is a plus– 0+0 = 0– 0+1 = 1– 1+0 = 1– 1+1 = 1

• And is multiplication– 0*0 = 0– 0*1 = 0– 1*0 = 0– 1*1 = 1

Page 24: CS203 Discrete Mathematical Structures Logic (2)

NOTA

A’ or -A

Logic Gate:(also called an inverter)

Single-throwDouble-poleSwitch:

A A -A

0 1

1 0

Truth Table:A’ or -A

• Y = ~X Y = !X Y = not X Y = X’ not(Y,X)

Page 25: CS203 Discrete Mathematical Structures Logic (2)

ANDA

B

A*B

Logic Gate:

Series Circuit:A B

AA BB A*BA*B

00 00 00

00 11 00

11 00 00

11 11 11

Truth Table:A*B

Page 26: CS203 Discrete Mathematical Structures Logic (2)

• X & Y (Verilog and ABEL)• X and Y (VHDL)• X Y• X Y• X * Y• XY (textbook)• and(Z,X,Y) (Verilog)

AND

U

V

Page 27: CS203 Discrete Mathematical Structures Logic (2)

A

B

A+B

Logic Gate:

Parallel Circuit:

A

B

AA BB A+BA+B

00 00 00

00 11 11

11 00 11

11 11 11

Truth Table:A+B

OR

Page 28: CS203 Discrete Mathematical Structures Logic (2)

OR

• X | Y (Verilog)• X # Y (ABEL)• X or Y (VHDL)• X + Y (textbook)• X V Y• X U Y• or(Z,X,Y) (Verilog)

Page 29: CS203 Discrete Mathematical Structures Logic (2)

NAND GateNAND

X

Y

Z

X Y Z0 0 10 1 11 0 11 1 0

Z = ~(X & Y)nand(Z,X,Y)

Page 30: CS203 Discrete Mathematical Structures Logic (2)

NOR Gate

NOR

X

YZ

X Y Z0 0 10 1 01 0 01 1 0

Z = ~(X | Y)nor(Z,X,Y)

Page 31: CS203 Discrete Mathematical Structures Logic (2)

Exclusive-OR Gate

X Y ZXOR

X

YZ 0 0 0

0 1 11 0 11 1 0

Z = X ^ Yxor(Z,X,Y)

Page 32: CS203 Discrete Mathematical Structures Logic (2)

XOR

• X ^ Y (Verilog)• X $ Y (ABEL)• X @ Y

• xor(Z,X,Y) (Verilog)

X Y (textbook)

Page 33: CS203 Discrete Mathematical Structures Logic (2)

Logic Circuits ≡ Boolean Expressions

• All logic circuits are equivalent to Boolean expressions and any boolean expression can be rendered as a logic circuit.

• AND-OR logic circuits are equivalent to sum-of-products form.• Consider the following circuits:

A

CB abc

aBc

Ab

y=abc+aBc+Ab

y

A

B

C

Y

y=aB+Bc

Page 34: CS203 Discrete Mathematical Structures Logic (2)

34

question • Find the output of the following circuit

• Answer: (x+y)y– Or (xy)y

xy

x+y

y

(x+y)y

____

xy

y

Page 35: CS203 Discrete Mathematical Structures Logic (2)

xyxyxy

question

• Write the circuits for the following Boolean algebraic expressions

• (x+y)x______________

xy

x+yx+y (x+y)x

Page 36: CS203 Discrete Mathematical Structures Logic (2)

36

Draw a circuit diagram for = (xy' + x'y)z.

Page 37: CS203 Discrete Mathematical Structures Logic (2)

37

Page 38: CS203 Discrete Mathematical Structures Logic (2)

38

Page 39: CS203 Discrete Mathematical Structures Logic (2)

Let’s compare the resulting circuits• Here are two

different but equivalent circuits.

• In general the one with fewer gates is “better”:

– It costs less to build

– It requires less power

– But we had to do some work to find the second form