Chap. 2 - Sun Yat

advertisement
Chapter 2. Logic
Weiqi Luo (骆伟祺)
School of Software
Sun Yat-Sen University
Email:weiqi.luo@yahoo.com Office:A309
Chapter two: Logic






2.1. Propositions and Logical Operation
2.2. Conditional Statements
2.3. Methods of Proof
2.4. Mathematical Induction
2.5. Mathematical Statements
2.6. Logic and Problem Solving
2
School of Software
2.1. Propositions and Logical Operation
 Statement (Proposition)
A statement or proposition is a declarative sentence that is either
true or false but not both
Example 1:
Yes.
(a) The earth is round.
Yes.
(b) 2+3=5
No. This is a question.
(c) Do you speak English?
No. May true or false
(d) 3-x=5
No. This is a command.
(e) Take two aspirins.
(f) The temperature on the surface of the planet Venus is 800 F
Yes.
(g) The sun will come out tomorrow
3
yes
School of Software
2.1. Propositions and Logical Operation
 Paradox
A male barber shaves all and only those men who do not shave
themselves
Paradox: A paradox is a seemingly true statement or group of
statements that lead to a contradiction or a situation which seems
to defy logic or intuition . Paradox is not a statement.
Refer to
http://en.wikipedia.org/wiki/List_of_paradoxes
Others
“I am lying”, “this sentence is wrong”, so on and so forth
4
School of Software
2.1. Propositions and Logical Operation
 Propositional variables
In logic, the letters p, q, r … denote propositional variables,
which are replaced by statements
p: 1+2 = 5
q: It is raining.
 Compound statements
Propositional variables can be combined by logical connectives to
obtain compound statements. E.g.
p and q : 1+2 =5 and it is raining.
5
School of Software
2.1. Propositions and Logical Operation
 Negation (a unary operation)
If p is a statement, the negation of p is the statement not p,
denoted by ~ p, meaning “it is not the case that p”.
if p is true, then ~p is false, and if p is false, then ~p is true.
Truth Table : List the truth value of a compound statement in
terms of its component parts.
p
~q
T
F
F
T
6
School of Software
2.1. Propositions and Logical Operation
 Example:
Give the negation of the following statements
(a) p: 2+3 >1
(b) q: It is snowing.
Solution:
(a) ~p: 2+3 is not greater than 1, namely, 2+3 <=1
(b) ~q: It is not the case that it is snowing. More simply, ~q: It is
not snowing.
7
School of Software
2.1. Propositions and Logical Operation
 Conjunction
If p and q are statements, the conjunction of p and q is the
compound statement “p and q” denoted by p∧ q .
Truth Table :
p
q
p∧ q
T
T
T
T
F
F
F
T
F
F
F
F
Note: p∧ q is T if and only if p is T and q is T.
8
School of Software
2.1. Propositions and Logical Operation
 Example
Form the conjunction of p and q
p: 1>3
q: It is raining.
Solution:
p∧ q : 1 > 3 and It is raining.
Note:
In logic, unlike in everyday English, we may join two totally
unrelated statements by logical connectives.
9
School of Software
2.1. Propositions and Logical Operation
 Disjunction
If p and q are statements, the disjunction of p and q is the
compound statement “p or q”, denoted by p V q
Truth Table :
p
q
pVq
T
T
T
T
F
T
F
T
T
F
F
F
Note: p V q is F is and only if q is F and q is F.
10
School of Software
2.1. Propositions and Logical Operation
 The connective “or”
(a) I left for Spain on Monday or I left for Spain on Friday.
(b) I passed math or I failed French”
Note:
Case (a): Both could not have occurred. “or” is an excusive
sense in this case.
Case (b): Both could have occurred. “or” is an inclusive sense in
this case.
In mathematics and computer science. We agree to use the
connective “or” in inclusive manner.
11
School of Software
2.1. Propositions and Logical Operation
 Example:
Form the disjunction of p and q
p: 2 is a positive integer
q : sqrt(2) is a rational number
Solution:
p V q: 2 is a positive integer or sqrt(2) is a rational number.
Since p is true, p V q is true, even through q is false.
12
School of Software
2.1. Propositions and Logical Operation
Algorithm for making Truth Table:
 Step 1: The first n columns of the table are labeled by the
component propositional variables. Further columns are included
for all intermediate combinations of the variables, culminating in
a column for the full statement.
 Step 2: Under each of the first n headings, we list the 2n possible
n-tuples of truth values for the n component statements.
 Step 3: For each of the remaining columns, we compute, in
sequence, the remaining truth values.
13
School of Software
2.1. Propositions and Logical Operation
 Example 5:
Make a truth table for the statement (p ∧ q) V (~ p)
Truth Table:
p
q
p∧ q
~p
V
T
T
T
F
T
T
F
F
F
F
F
T
F
T
T
F
F
F
T
T
(1)
(2)
(3)
14
School of Software
2.1. Propositions and Logical Operation
 Propositional function (predicate)
An element of a set {x | P(x)} is an object t for which the
statement P(t) is true. P(x) is called a propositional function (or
predicate) , because each choice of x produces a proposition
P(x) that is either true or false (well-defined)
E.g. Let A={ x | x is an integer less than 8}.
Here P(x) is the sentence “x is an integer less than 8”
P(1) denotes the statement “1 is an integer less than 8” (true)
P(8) denotes the statement “8 is an integer less than 8” (false)
15
School of Software
2.1. Propositions and Logical Operation
 Universal Quantifiers (∀)
The Universal Quantifiers of a predicate P(x) is the statement
“for all values of x, P(x) is true” , denoted by ∀ x P(x)
Example 8
(a) P(x) : -(-x) = x is a predicate that makes sense for all real
number x.
then ∀ x P(x) is true statement. Since ∀ x ∈ R, -(-x) = x
(b) Q(x): x+1<4.
then ∀ x Q(x) is a false statement, since Q(5) is false
16
School of Software
2.1. Propositions and Logical Operation
 Existential Quantifiers (∃)
The Existential Quantifiers of a predicate P(x) is the statement
“there exists a value of x for which P(x) is true”, denoted by
∃ x P(x)
Example 9
(a) Let Q(x): x+1<4. then the existential quantification of Q(x),
∃ x Q(x), is a true statement, since Q(1) is a true statement
(b) The statement ∃ y y+2=y is false since there is no value of y
for which the propositional function y+2=y produces a true
statement.
17
School of Software
2.1. Propositions and Logical Operation
 The order of the Quantifiers ∀ & ∃
The order does not affect the output for the same quantifiers,
while it may produce different results for different quantifiers.
E.g.
P(x, y) : x + y =1
∀ x ∃ y P(x) is true, ∃ y ∀ x is false.
P(x, y): x *y = 0
∀ x ∃ y P(x) is true, ∃ y ∀ x is true too.
18
School of Software
2.1. Propositions and Logical Operation
 The negation of Quantifiers ∀ & ∃
(a) let p: ∀x P(x),
then ~p: there must be at least one value of x for which P(x)
is false, namely,
~ ∀x P(x) = ∃x ~P(x)
(b) let p: ∃x P(x),
then ~p: for all x, P(x) is false, namely,
~ ∃x P(x)= ∀x ~P(x)
19
School of Software
2.1. Propositions and Logical Operation
 Homework
Ex. 2, Ex. 4, Ex. 16, Ex. 28
20
School of Software
2.2. Conditional Statements
 Conditional statement
If p and q are statements, the compound statement “if p then q”,
denoted p=>q, is called a conditional statement or implication.
p : antecedent or hypothesis q : consequent or conclusion
if … then : =>
Truth Table :
p
q
p => q
T
T
T
T
F
F
F
T
T
F
F
T
Note: when p is F, then p=>q is T.
21
School of Software
2.2. Conditional Statements
 Example
Form the implication p=>q for each the following
(a) p: I am hungry. q: I will eat.
(b) p: 2+2=5
q: I am the king of England.
Solution
(a) If I am hungry, then I will eat
(b) If 2+2=5, then I am the king of English
Note: There is no cause-and effect relationship between p and q in
case (b). And (b) is true, since 2+2=5 is false.
22
School of Software
2.2. Conditional Statements
 Converse and Contrapositive
If p=>q is an implication, then its converse is the
implication q => p
and its contrapositive is the implication ~ q => ~p
E.g. Give the converse and the contrapositive of the
implication “If it is raining, then I get wet”
Converse:
If I get wet, then It is raining.
Contrapositive: If I do not get wet, then It is not raining.
23
School of Software
2.2. Conditional Statements
 Equivalence (biconditional)
If p and q are statements, the compound statement p if and only if
q, denoted by p  q, is called an equivalence or biconditional.
Truth Table :
p
q
pq
T
T
T
T
F
F
F
T
T
F
F
T
Note: p <=> q is T when p and q are both T or both F.
24
School of Software
2.2. Conditional Statements
 Example 3:
Is the following equivalence a true statement?
3>2 if and only if 0< 3 – 2
Solution:
Let p: 3>2 and q : 0< 3 – 2,
since p and q are both true, we then conclude that
p  q is true statement.
25
School of Software
2.2. Conditional Statements
 Example 4.
Compute the truth table of the statement
(p=>q)  (~q => ~p)
Truth Table :
p
q
p=>q
~q
~p
~q=>~p
(p=>q) 
(~q => ~p)
T
T
T
F
F
T
T
T
F
F
T
F
F
T
F
T
T
F
T
T
T
F
F
T
T
T
T
T
26
School of Software
2.2. Conditional Statements
 Tautology
A statement that is true for all possible values of its propositional
variables called Tautology. (e.g. Example 4)
 Contradiction (Absurdity)
A statement that is false for all possible values of its propositional
variables called Contradiction or Absurdity. (e.g. p ∧ ~p)
 Contingency
A statement that can be either true or false, depending on the truth
values of its propositional variables, is called a contingency.
(e.g. p => q)
27
School of Software
2.2. Conditional Statements
 Logically Equivalent
If two statements p and q are always either both true or
both false, for any values of the propositional variables,
namely
p  q is a tautology
Then we call p and q are logically equivalent.
Denoted by
p≡q
28
School of Software
2.2. Conditional Statements
 Example 6
Show that p V q and q V p are logically equivalent
The truth table of (p V q )  (q V p ) are shown as
follows
Truth Table :
p
q
pVq
qVp
p V q q V p
T
T
T
T
T
T
F
T
T
T
F
T
T
T
T
F
F
F
F
T
29
School of Software
2.2. Conditional Statements
 Two Structures with similar properties
(Theorem 1)
Structure 1 (logic operations):
(propositions, ∧, V, ~)
Structure 2 (sets operations)
(sets, U, ∩ , - )
30
School of Software
2.2. Conditional Statements
 Theorem 1
Commutative properties
p ∨ q ≡ q ∨p, p∧ q ≡ q ∧ p
Associative Properties
p ∨ (q ∨ r) ≡ (p ∨ q) ∨r , p∧ (q ∧ r) ≡ (p ∧ q) ∧ r
Distributive Properties
p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r)
p ∧ (q ∨ r) ≡ (p ∧ q) ∨ (p ∧ r)
31
School of Software
2.2. Conditional Statements
Idempotent Properties
p∨p ≡p ,
p∧ p ≡ p
Properties of Negation
~(~p) ≡ p
~(p ∨ q) ≡ (~p) ∧ (~q)
~(p ∧ q) ≡ (~p) ∨ (~q) De Morgan’s laws
32
School of Software
2.2. Conditional Statements
 Theorem 2
(p ⇒ q) ≡ ((~p) ∨ q)
(p ⇒ q) ≡ (~q ⇒ ~p)
(p  q) ≡ ((p ⇒ q) ∧ (q ⇒ p))
~(p ⇒ q) ≡ (p ∧ ~q)
~(p  q) ≡ ((p ∧ ~q) ∨ (q ∧ ~p))
33
School of Software
2.2. Conditional Statements
 Theorem 3
~(∀xP(x)) ≡ ∃x~P(x)
~(∃xP(x)) ≡ ∀x(~P(x))
∃x(P(x) ⇒ Q(x)) ≡ ∀xP(x) ⇒ ∃xQ(x)
∃x(P(x) ∨ Q(x)) ≡ ∃xP(x) ∨ ∃xQ(x)
∀x(P(x) ∧ Q(x)) ≡ ∀xP(x) ∧ ∀xQ(x)
∀xP(x) ∨ ∀xQ(x) ⇒ ∀x(P(x) ∨ Q(x)) is a tautology
∃x(P(x) ∧ Q(x)) ⇒ ∃xP(x) ∧ ∃xQ(x) is a tautology
34
School of Software
2.2. Conditional Statements
 Theorem 4 : Each of the following is a tautology
(p ∧ q) ⇒ p , (p ∧ q) ⇒ q
p ⇒ (p ∨ q) , q ⇒ (p ∨ q)
~p ⇒ (p ⇒ q) , ~(p ⇒ q) ⇒ p
(p ∧ (p ⇒ q)) ⇒ q , (~p ∧ (p ∨ q)) ⇒ q
(~q ∧ (p ⇒ q)) ⇒ ~p
((p ⇒ q) ∧ (q ⇒ r)) ⇒ (p ⇒ r)
35
School of Software
2.2. Conditional Statements
 Properties of Quantifiers ∃ and ∀
(a) ∃ x (P(x) V Q(x)) ≡ ∃x P(x) V ∃x (Q(x))
(b) ∀ x (P(x) ∧ Q(x)) ≡ ∀x P(x) ∀x Q(x)
36
School of Software
2.2. Conditional Statements
 Homework
ex.2, ex.7, ex.12, ex.15, ex.21 ex.34
37
School of Software
2.3. Method of Proof
 Logically Follow
If an implication p ⇒ q is a tautology, where p and q may be
compound statements involving any number of proposition
variables, we say that q logically follows from p.
Suppose that an implication of the form (p1 ∧ p2 ∧… ∧ pn)
⇒ q is a tautology. We say that q logically follows from p1, p2,
…, pn, denoted by
38
School of Software
2.3. Method of Proof
(p1 ∧ p2 ∧… ∧ pn) ⇒ q
The pi’s are called the hypotheses or premises, and q is called the
conclusion.
Note: we are not trying to show that q is true, but only that q will
be true if all the pi are true.
∴ denotes “therefore”
39
School of Software
2.3. Method of Proof
 Rules of inference
Arguments based on tautologies represent universally correct
methods of reasoning. Their validity depends only on the form of
the statements involved and not on the truth values of the
variables they contain.
Example 1
((p ⇒ q) ∧ (q ⇒ r)) ⇒ (p ⇒ r) is tautology, then the argument
is universally valid, and so is a rule of inference.
40
School of Software
2.3. Method of Proof
 Example 2: Is the following argument valid?
If you invest in the stock market, then you will get rich.
If you get rich, then you will be happy.
∴ If you invest in the stock market, then you will be happy.
let p: you invest in the stock market, q: you will get rich
r: you will be happy
The above argument is of the form given in Example 1, hence the
argument is valid!
41
School of Software
2.3. Method of Proof
 Example 3
The tautology
(p  q) ((p ⇒ q) ∧ (q ⇒ p))
means that the following two arguments are valid
p q
∴ (p ⇒ q) ∧ (q ⇒ p)
∴
42
p⇒q
q⇒p
p q
School of Software
2.3. Method of Proof
 Equivalence (p  q)
They are usually stated “p if and only if q”. We need to
prove both p=>q and q=>p by the tautology mentioned
in example 3.
Algorithm :
Step one: Assuming p is true, show q must be true.
Step two: Assuming q is true, show p must be true.
43
School of Software
2.3. Method of Proof
 Modus Ponens
p is true, and p=>q is true, so q is true
(Theorem 4(g) in Section 2.2.)
∴
p
p=>q
q
44
School of Software
2.3. Method of Proof
 Example 4: Is the following argument valid?
Smoking is healthy.
If smoking is healthy, then cigarettes are prescribed by physicians.
∴ Cigarettes are prescribed by physicians
p: Smoking is healthy.
q: cigarettes are prescribed by physicians
The argument is valid since it is of form modus ponens.
45
School of Software
2.3. Method of Proof
 Example 5 Is the following argument valid?
If taxes are lowered, then income rises
Income rises
∴
taxes are lowered
Solution:
Let p: taxes are lowed q: income rise
p=>q
(or show the truth table of
(p=>q) ∧ q => p , and determine
q
whether or not it is a tautology)
∴ p
Then argument is not valid , since p=>q and q can be both true
with p being false.
46
School of Software
2.3. Method of Proof
 Indirect Method of Proof
The tautology
(p=>q )  (~q) => (~p )
(An implication is equivalent to its contrapositive)
Note:
To proof p=>q indirectly, we assume q is false (~q) and
show that p is then false (~p)
47
School of Software
2.3. Method of Proof
 Example 6
Let n be an integer. Prove that if n2 is odd, then n is odd.
Solution:
Let p: n2 is odd , q: n is odd.
To prove that (p=>q)
We try to prove its contrapositive ~q=>~p
Assuming that n is even (~q), let n=2k, k is an integer,
then we have n2 = (2k) 2 =4k2 is even (~p).
Hence, the given statement has been proved.
48
School of Software
2.3. Method of Proof
 The tautology
((p ⇒ q) ∧ (~q)) ⇒ ~p
If a statement p implies a false statement q, then p must
be false.
Proof by contradiction
To prove (p1 ∧ p2 ∧… ∧ pn) ⇒ q ,
We add (~q) into hypothesis p1 ∧ p2 ∧… ∧ pn, if the
enlarged hypothesis p1 ∧ p2 ∧… ∧ pn ∧ (~q) implies a
contradiction, then we can conclude that q follows from
p1 ,p2 , … and pn .
49
School of Software
2.3. Method of Proof
 Example 7
Prove there is no rational number a/b whose square is 2,
namely, sqrt(2) is irrational.
Solution:
Let p: a, b are integers and no common factors, and b is
not 0
q: (a/b)2 is not 2
In order to prove p => q ,
We try to find the contradiction from p ∧ ~q
Refer to Example 7 for more details.
50
School of Software
2.3. Method of Proof
 Example 9:
Prove or disprove the statement that if x and y are real numbers,
(x2=y2)  (x=y)
Solution:
Since (1)2=(-1)2, but -1 ≠ 1, the result is false. Our example is
called counterexample, and any other counterexample would do
just as well.
Note:
If a statement claims that a property holds for all objects of a
certain type, then to prove it, we must use steps that are valid for
all objects of that type and that do not make references to any
particular object. To disprove such a statement, we need only
show one counterexample.
51
School of Software
2.3. Method of Proof
 Homework
Ex. 6, Ex. 8, Ex. 9
Ex. 19, Ex. 20
Ex. 31, Ex. 34
52
School of Software
2.4 Mathematical Induction
 To Prove
∀n>n0 P(n)
where n0 is some fixed integer
Two Steps:
1) Basis Step
Prove that P(n0) is true
2) Induction Step
Prove that P(k) => P(k+1) is a tautology
(if P(k) is true, then P(k+1) must be true)
53
School of Software
2.4 Mathematical Induction
 Example 1: Prove that for all n>0
1+2+3…+n = n(n+1)/2
Solution: let P(n)=n(n+1)/2
Basis step: P(1) = 1 = 1*2/2=1 is true.
Induction Step: assuming P(k) is true, then
P(k+1) = 1+2 + 3 … + k +(k+1)
= P(k) +(k+1)
= k (k+1)/2 +(k+1)
P(k) is true
= (k+1)(k+2)/2
= P(k+1)
54
School of Software
2.4 Mathematical Induction
 Example 3: Prove any finite, nonempty set is
countable (the elements can be arranged in a list)
Solution: Let P(n) be the predicate that if A is any set
with |A|=n>0, then A is countable
Basis Step: P(1) is true ( A ={x} )
Induction Step: assuming P(k) is true
Let B denotes any finite, nonempty set with k+1
elements. We first pick any element x in B, then B-{x}
is a set with k elements, and it is countable (P(k) is
true), and listed by x1,x2,…xk . Then we can also list the
elements of B as x1,x2,…xk , x (P(k+1) is true)
55
School of Software
2.4 Mathematical Induction
 Example 5: Consider the following function
given in pseudocode
Function SQ(A)
1. C <- 0
2. D <- 0
3. WHILE (D is not A)
a. C <- C+A
b. D <- D+1
4. RETURN (C)
End of Function SQ
56
School of Software
2.4 Mathematical Induction
 Strong Induction
Weak Induction
Strong Induction
Basis Step
P(no) is true
P(no) is true
(or the first several (or the first several statements are
statements are true) true)
Induction Step
P(k)  P(k+1)
is a tautology
P(n0) ∧P(n1) ∧… P(k)  P(k+1)
is a tautology
57
School of Software
2.4 Mathematical Induction
 Example 7: Prove that every positive integer n>1
can be written uniquely as p1a1p2a2…psas, where
pi are primes and p1<p2<..Ps
Basis Step : P(2) is true, since 2 is prime and 2 = 21 (unique )
Induction Step: Assuming P(2), P(3), … P(k) are true
if k+1 is prime, then k+1= (k+1)1
if k+1 is not prime, then we let k+1=Lm, where L, m are
positive integers less than k+1. Using P(L) and P(m) are true,
we have k+1=Lm= q1b1q2b2…qsbs (unique form)
Why? Proof by contradiction
58
School of Software
2.4 Mathematical Induction
 Homework
Ex. 2, Ex. 14, Ex. 17, Ex. 22, Ex. 34
59
School of Software
Download