CS100 : Discrete Structures Chapter 1 Logic Theory Dr.Saad Alabbad Department of Computer Science E-mail: salabbad@gmail.com Office # SR 068 Tel # 2581888 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 1 Propositional Logic Proposition: A proposition is a declarative statement ( a statement that declares a fact) that is either TRUE or FALSE, but not both. The area of logic that deals with propositions is called propositional logic. Propositions Not Propositions Riyadh is the capital of Saudi Arabia How many students in this class? Every cow has 4 legs. Bring me coffee! 3 + 2 = 32 4+3=7 X+2=3 Y+Z=X "x +2=3", where x is a variable representing a number, is not a proposition , because unless a specific value is given to x we can not say whether it is true or false, nor do we know what x represents . 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 2 Propositional Logic - Negation We use letters to denote propositional variables Truth value of true proposition is denoted by T Truth value of false proposition is denoted by F Negation: Suppose p is a proposition. The negation of p is written p and has meaning: “It is not the case that p.” The proposition p is read “NOT p” Example: p: “Today is Friday” p: “Today is NOT Friday” Truth table for negation: p p Notice that p is a proposition! 3/16/2016 T F Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed F T 3 Propositional Logic - Conjunction Conjunction corresponds to English “and.” Conjunction: Let p and q be two propositions. The conjunction of p and q, denoted by p q, is the proposition “p and q”. The p q is true when both p and q are true, otherwise false. Example: p: “Today is Friday” q: “It is raining today” p q: “Today is Friday and it is raining today” Truth table for conjunction: P q pq 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed T T F F T F T F T F F F 4 Propositional Logic - Disjunction Disjunction corresponds to English “or.” Disjunction: Let p and q be two propositions. The disjunction of p and q, denoted by p q, is the proposition “p or q”. The p q is false when both p and q are false, otherwise true. Example: p: “Today is Friday” q: “It is raining today” p q: “Today is Friday or it is raining today” Truth table for disjunction: p q pq 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed T T F F T F T F T T T F 5 Propositional Logic – Exclusive Or Exclusive Or: Let p and q be two propositions. The exclusive or of p and q, denoted by p ⊕ q, is the proposition that is true when exactly one of p and q is true, otherwise false. Example: p: “Today is Friday” q: “It is raining today” p ⊕ q: “Today is Friday exclusive or it is raining today” p q p⊕q Truth table for Exclusive Or: T T F F 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed T F T F F T T F 6 Propositional Logic – Conditional Statement It corresponds to English “if p then q” or “p implies q.” Conditional: Let p and q be two propositions. The conditional statement (implication) p q is the proposition “if p, then q”. The conditional statement p q is false when p is true and q is false, otherwise true. (p is called the hypothesis ,q is the conclusion) Example: If it is raining, then it is cloudy. If I am elected, then I will lower taxes. If you get 100% in the final, then you will get Grade A+. p q pq Truth table for implication: T T T 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed T F F F T F F T T 7 Propositional Logic - Special Definitions Converse: q p is converse of p q. Ex. p q: “If it is noon, then I am hungry.” q p: “If I am hungry, then it is noon.” Contrapositive: q p is contapositive of p q. Ex. p q: “If it is noon, then I am hungry.” q p: “If I am not hungry, then it is not noon.” Inverse: p q is inverse of p q. Ex. p q: “If it is noon, then I am hungry.” p q: “If it is not noon, then I am not hungry.” p q has same truth values as q p 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 8 Propositional Logic - Questions Q1: Give some examples of p q, then tell what is q p q p p q Q2: State the converse, contrapositive, and inverse of each of them If it rains today, then I will stay at home I come to class whenever there is going to be a examination A positive integer is prime only if it has no divisors other than 1 and itself. 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 9 Propositional Logic – Biconditional Statement It corresponds to English “p if and only if q”. Biconditional: Let p and q be two propositions. The biconditional statement (bi-implication) p ↔ q is the proposition “if p and only if q”. The biconditional statement p ↔ q is true when p and q have the same truth values, otherwise false. p ↔ q has same truth value as (p q) (q p). Example: p: “You can take the flight” p q p↔q q : “You buy a ticket” T T T p ↔ q : “You can take the T F F flight if and only if F T F you buy a ticket” 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed F F T 10 Propositional Logic – Precedence Precedence of Logical Operators: P Λ ¬ q νr →s ↔w νm (((P Λ (¬ q)) ν r) →s) ↔(w νm) 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed Operator Precedence ¬ Λ ν → ↔ 1 2 3 4 5 11 Propositional Logic – Precedence A compound proposition that is: (1) always true is called a tautology (2) always false is called a contradiction (3) neither a tautology nor a contradiction is called contingency or satisfiable. p p p p p p T F T F F T T F 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 12 Propositional Logic – Compound Propositions Construct truth table for (p q) → (p q). p q q p q p q (p q) → (p q) T T F F T F T F F T F T T T F T T F F F T F T F Q3: Construct truth table for (p q) ↔ (p q). (p ⊕ q) → (p ⊕ q). 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 13 Propositional Logic – Translating English Sentences The sentence “The automated reply cannot be sent when the file system is full” can be translated as q → p where p: “The automated reply can be sent ” q: “The file system is full” The sentence “You cannot drive a car if you are under 4 feet tall unless you are older than 16 years old” can be translated as (q r) → p where p: “You can drive a car ” q: “You are under 4 feet tall ” r: “You are older than 16 years old” 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 14 Propositional Logic – Translating English Sentences The sentence “You can access the Internet from campus only if you are a computer science major or you are not a freshman” can be translated as p → (q r) where p: “You can access the Internet from campus” q: “You are a computer science major” r: “You are a freshman” Q4: Translate the following sentences into logical expressions: “Access is granted whenever the user has paid the subscription fee and enters a valid password” “If the user has not entered a valid password but has paid the subscription fee, then access is granted” 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 15 Propositional Equivalences – Logic and Bit Operations Bit: A bit is a symbol with two possible values, namely, 0 (zero) and 1 (one). A bit can be used to represent a truth value as 1 for T and 0 for F Bit string: A bit string is a sequence of bits. The length of the string is number of bits in the string. Example: 10101001 is a bit string of length eight We define the bitwise OR, AND, and XOR of two strings of same length to be the strings that have as their bits the OR, AND, and XOR of the corresponding bits in the two strings, respectively. We use the symbols , , and ⊕ to represent bitwise OR, AND, and XOR, respectively. 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 16 Propositional Equivalences – Logic and Bit Operations Truth table for bitwise OR, AND, and XOR: x y xy xy x⊕y 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0 1 0 1 1 0 Q5: Find bitwise OR, AND, and XOR of the bit strings 01 0011 0100 and 10 1001 0101 00 1011 1010 and 11 1010 0101 11 0101 0110 and 10 1101 0010 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 17 Propositional Logic - Applications We are using propositional logic as a foundation for formal proofs. Propositional logic is also the key to writing good code…you can’t do any kind of conditional (if) statement without understanding the condition you’re testing. All the logical connectives we have discussed are also found in hardware and are called “gates.” 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 18 Propositional Equivalences – Logical Equivalences Equivalence: The compound propositions p and q are logically equivalent if p↔q is a tautology. In other words, p and q are logically equivalent if their truth tables are the same. We write p q. Example: (p q) p q. Truth tables for (p q) and p q : 3/16/2016 p q T T F F T F T F p q (p q) T T T F F F F T p q p q F F T T F T F T F F F T Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 19 Propositional Equivalences – Logical Equivalences Example: p q p q. Truth tables for p q and p q: p q pq p p q T T F F T F T F T F T T F F T T T F T T Q5: Show that p (q r) (p q) (p r) Note: if we have n logical variable then the size of the truth table is 2n 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 20 Propositional Equivalence – Prove the following Logical Equivalences Equivalence Name p Λ T p and p ν F p Identity laws p ν T T and p Λ F F Domination laws p ν p p and p Λ p p Idempotent laws ¬( ¬ p) p Double negation law p ν q q ν p and p Λ q q Λ p Commutative laws (p ν q) ν r p ν (q ν r) and (p Λ q) Λ r p Λ (q Λ r) Associative laws p (q r) (p q) (p r) p (q r) (p q) (p r) Distributive laws ¬(p ν q) ¬p Λ ¬q and ¬(p Λ q) ¬p ν ¬q De Morgan’s laws p (p q) p and p (p q) p Absorption laws p ν ¬p T and p Λ ¬p F Negation laws 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 21 Propositional Equivalence – Prove the following Logical Equivalences 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 22 Propositional Equivalences – Another way of proving Logical Equivalences In addition to truth table we can use logical identities that we already know to establish new logical equivalences Examples: ¬(p q) p ¬q (see p.26) Show that ¬(p (¬ p q)) and ¬ p ¬ q are equivalent. Show that p q p q is a tautology .(see p.27 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 23 Predicate Logic Introduction: x>20 (We must know x to tell if it is true or false) All students passed. (We have to write a proposition for every student asserting that he passed and take the conjunction of all these propositions) Some students didn’t pass (Can you make such an assertion without referring to a particular student?) X is father of y The above assertions are not propositions. However, they appear quite often in mathematics and we want to do deal with such assertions. Predicate logic is a more powerful logic than propositional logic and it addresses some of the limitations of propositional logic 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 24 Predicate Logic Predicate: refers to a property that the subject of the statement can have or relationships among other subjects Examples X>20. This statements has two parts. The variable x and is called the subject The property “is greater than 20” and is called the predicate We can also write P(x) where P is the predicate ‘’is greater than 20” and x is the subject. P(x) is called the propositional function P at x Note: once a value has been assigned to the variable x the statement P(x) becomes a proposition that has a truth value. For example P(8) is the statement that 8>20 which is false 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 25 Predicate Logic Examples x is father of y. This statements has two parts. The variables x and y and are called the subjects The relationship “is father of ” and is called the predicate We can also write P(x,y) where P is the predicate ‘is father of ” and x,y are the subjects. P(x,y) is called the propositional function P at x and y. As we have seen predicate logic can describe both relationships and properties of subjects 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 26 Predicate Logic: Quantifiers Quantification expresses the extent to which a predicate is true over a range of elements. In English: all,some,none anf few are used. All birds have wings Some birds do not fly. The area of logic that deals with predicates and quantifiers is called predicate calculus or first order logic. 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 27 Predicate Logic: Universal Quantifier Universal Quantifier: Asserts that a property is true for all values of a variable in a particular domain. Let P(x) be a propostional function.The universal quantifier of P(x) is the statement: P(x) is true for all x in the domain. The domain is called the universe of discourse We write x P ( x) We read it as “for all x P(x)” An element for which P(x) is false is called counterexample of x P ( x) The statement is either true of false, sox P ( x) is a proposition. 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 28 Predicate Logic: Universal Quantifier Examples: Let P(x) be “x2 > 0” then Statement x P ( x) is not true when the universe of discourse consists of all real numbers P(0) is false ( counterexample) Statement x P ( x) is true when the universe of discourse consists of all real numbers which are greater than 0. What is the truth value of x( x 2 x) consists of all real numbers? 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed if the domain 29 Predicate Logic: Existential Quantifier Existential Quantifier: Asserts that there is an element with a certain property in a particular domain. Let P(x) be a propostional function.The Existential quantifier of P(x) is the statement: There exists an element x in the domain such that P(x) is true. The domain is called the universe of discourse We write x P ( x ) We read it as “There is an x such that P(x)” or “For some x P(x)” The statement x P ( x ) is false iff there is no element x in the domain for which P(x) is true The statement is either true of false, so is a proposition. 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 30 Predicate Logic: Existential Quantifier Examples: Let P(x) be “x2 > 0” then Statement x P ( x) is true since for example P(2) is true Statement x P ( x) is false iff there is no element in the domain for which P(x) is true. Finding at least one element for which P(x) is true is sufficient to asserts that x P ( x) is true. What is the truth value of x ( x 3) if the domain consists of all real numbers? What is the truth value of x( x 1 x) if the domain consists of all real numbers? 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 31 Predicate Logic: Quantifiers When all the elements in domain can be listed as:x1,x2,…,xn then: is the same as P(x1) P(x2)… P(xn) xP(x ) is the same as P(x1) P(x2)… P(xn) Quantifiers with restricted domain x 0( x 2 0) (note that it is the same as x( x 0 x 2 0) y 0( y 3 0) z R( z 2 1) z R( z 2 1) 3/16/2016 The two quantifiers have precedence over all logical operators from propositional logic. x P( x) Q( x) means (x P( x)) Q( x) Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 32 Predicate Logic: Negation of Quantified Expression Examples Every Students in this class has taken calculus course Can be represented as: x P ( x) where P(x) is”x has taken calculus course” To negate it we write “there is a student in this class who has not taken calculus course” which can be translated to x P ( x ) There is a man who can fly Can be represented as: x F ( x) where F(x) is”x can fly” To negate it we write “There is no man who can fly” Which can be stated as ”every man can not fly” x F ( x) 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 33 Predicate Logic: From English to Logic(1) Every student in the class has taken calculus course 1- For every student x in the class, x has taken calculus 2x T ( x) where T(x) is “x has taken calculus” The domain of x is students of this class For every Man, if the man has a job then he has a car 1- For every man x, if x has a job then x has a car x ( J ( x) C ( x)) where 2 J(x) is “x has a job” C(x) is “x has a car” The domain of x is all the men Some student in this class has visited Abha or Dammam For every Saudi, if he is a student then he has visitd Abha 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed x( A( x) D( x)) x( S (x) A( x)) 34 Predicate Logic: Nested Quantifiers (1) The statement xy ( x y 0) says that For every number x, there is a number y such that x+y=0 (additive inverse) (2) The statement negative integers) xy ( y x ) says that (the domain is all non- There exists x, such that for every number y, y is greater than or equal to x (0 is the smallest non-negative number) (3) The statement xy( x y y x) says that X+y=y+x for all numbers x and y (commutative law of addition). (4) The statement xy( x y 17 ) says that There exist two numbers x and y such that their product equals 17 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 35 Predicate Logic: From English to Logic and vice versa(2) The sum of two positive integers is always positive xy ( ( x 0) ( y 0) x y 0) Every real number except zero has a multiplicative inverse x( x 0 y ( xy 1)) or x 0 y ( xy 1) Translate(p.55) x(C ( x) y (C ( y ) F ( x, y ))) xy ( xy y ) Translate If a person is a male and is a parent then this person is someone’s father. x(Male(x)Parent(x)y Father(x,y)) 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 36 Predicate Logic: From English to Logic(3) Example: Let 1) S(x) be “x is s student” 2)F(x) be “x is a faculty member” 3) E(x,y) be “x has sent an email to y” and the domain is every person in our college. Translate the following sentences into logical Expressions: Every person in the college has sent an email to every person including himself xy(E(x,y)) Every person in the college has sent an email to every other person not including himself xy(xy E(x,y)) At least one email has been sent from one person to another xy(xyE(x,y)) There is a person who sent an email to every other person xy(xyE(x,y)) Every person has been sent at least one email xy(xyE(y,x)) 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 37 Predicate Logic: From English to Logic(4) Example: Let 1) S(x) be “x is s student” 2)F(x) be “x is a faculty member” 3) E(x,y) be “x has sent an email to y” and the domain is every person in our college. Translate the following sentences into logical Expressions: Ayman has sent an email to Dr.Saad: E(Ayman,Dr.Saad) Every student sent an email to Dr.Zakir x(S(x)E(x,Dr.Zakir)) Every Faculty member has either sent an email to Dr.Ahmad or has been sent an email by Dr.Ahmad. x(F(x)E(x,Dr.Ahmad) E(Dr.Ahmad,x)) Some student has not sent an email to any faculty member x(S(x) y(F(y)E(x,y)) There is a faculty member who has never been sent an email by a student. x(F(x) y(S(y)E(y,x)) Some student has sent an email to every faculty member x(S(x) y(F(y)E(x,y)) 3/16/2016 Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 38 Predicate Logic: Negating Nested Quantifiers Negating Nested Quantifiers Negate x y( xy 1) 3/16/2016 1- x y( xy 1) 2- x y( xy 1) 3- x y( xy 1) 4- x y( xy 1) Prepared by Dr.Saad Alabbad & Dr. Zakir Ahmed 39