Discrete Mathematics For Computer Science
Discrete Mathematics For Computer Science
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.
yes
Paradox
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. Example I am lying, this sentence is wrong etc
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.
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.
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 T T q T F p q T F
F
F
T
F
F
F
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.
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 T T F F q T F T F pVq T T T F
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 exclusive 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.
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.
Example 5:
Make a truth table for the statement (p q) V (~ p)
Truth Table:
p T q T p q T ~p F V T
T
F F
F
T F
F
F F (1)
F
T T (2)
F
T T (3)
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
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.
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 T T q T F p => q T F
F
F
T
F
T
T
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 England 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.
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 T T F F q T F T F pq T F F T
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.
Example 4.
Compute the truth table of the statement (p=>q) (~q => ~p) Truth Table :
p T T F F q T F T F p=>q T F T T ~q F T F T ~p F F T T ~q=>~p T F T T (p=>q) (~q => ~p) T T T T
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)
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 pq
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 T T F F q T F T F pVq T T T F qVp T T T F p V q q V p T T T T
Two Structures with similar properties (Theorem 1) Structure 1 (logic operations): (propositions, , V, ~)
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)
p p p
Properties of Negation ~(~p) p ~(p q) (~p) (~q) ~(p q) (~p) (~q) De Morgans laws
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))
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
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)
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)
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
(p1 p2 pn) q
The pis 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
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
p q (p q) (q p)
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.
Modus Ponens
p is true, and p=>q is true, so q is true
p p=>q q
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.
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
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.
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)
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)
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) P(k) P(k+1) is a tautology P(n0) P(n1) P(k) P(k+1) is a tautology
Induction Step
Example 7: Prove that every positive integer n>1 can be written uniquely as p1a1p2a2psas, 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= q1b1q2b2qsbs (unique form)
Prove that 3 n > n 2 for n = 1, n = 2 and use the mathematical induction to prove that 3 n > n 2 for n a positive integer greater than 2. Statement P (n) is defined by 3 n > n 2 STEP 1: We first show that p (1) is true. Let n = 1 and calculate 3 1 and 1 2 and compare them 3 1 = 3, 1 2 = 1. 3 is greater than 1 and hence p (1) is true. Let us also show that P(2) is true. 3 2 = 9 , 2 2 = 4. Hence P(2) is also true. STEP 2: We now assume that p (k) is true i,e,3 k > k 2 Multiply both sides of the above inequality by 3 3 * 3k > 3 * k2 The left side is equal to 3 k + 1. For k >, 2, we can write k 2 > 2 k and k 2 > 1 We now combine the above inequalities by adding the left hand sides and the right hand sides of the two inequalities 2 k2 > 2 k + 1 We now add k 2 to both sides of the above inequality to obtain the inequality 3 k2 > k2 + 2 k + 1 Factor the right side we can write 3 * k 2 > (k + 1) 2 If 3 * 3 k > 3 * k 2 and 3 * k 2 > (k + 1) 2 then 3 * 3 k > (k + 1) 2 Rewrite the left side as 3 k + 1 3 k + 1 > (k + 1) 2 Which proves tha P(k + 1) is true
Prove that n ! > 2 n for n a positive integer greater than or equal to 4. (Note: n! is n factorial and is given by 1 * 2 * ...* (n-1)*n.) Statement P (n) is defined by n! > 2 n STEP 1: We first show that p (4) is true. Let n = 4 and calculate 4 ! and 2 n and compare them 4! = 24 , 2 4 = 16 , 24 is greater than 16 and hence p (4) is true. STEP 2: We now assume that p (k) is true k! > 2 k Multiply both sides of the above inequality by k + 1 k! (k + 1)> 2 k (k + 1) The left side is equal to (k + 1)!. For k >, 4, we can write k + 1 > 2 Multiply both sides of the above inequality by 2 k to obtain 2 k (k + 1) > 2 * 2 k The above inequality may be written 2 k (k + 1) > 2 k + 1 We have proved that (k + 1)! > 2 k (k + 1) and 2 k (k + 1) > 2 k + 1 we can now write (k + 1)! > 2 k + 1 We have assumed that statement P(k) is true and proved that statment P(k+1) is also true.
Example 1
Problem: does this function work?
int square(int val) { int result = 0; int counter = 0; while (counter < val) { result += val; counter++; } return result; }
the postcondition we will use the loop invariant to show that this is true
Basis
S(0) is when the loop has not yet been executed.
result0 = counter0 = 0
So:
result0 = val * counter0 which means that S(0) is true.
Induction
We assume that S(k) is true for some k >= 0, which means that:
resultk = val * counterk (1)
(3)
continued
Substitute the RHS of (1) for the 1st operand of the rhs of (2):
resultk+1 = (val * counterk) + val = val * (counterk + 1) = val * counterk+1 (by using
(3))
Termination
The loop does actually terminate, since counter is increasing, and will reach val. At loop termination (and function return):
countern = val
So in S(n):
resultn = val * val = val2
The function works!!
Example 2
Problem: does this function work?
int exp(int b, int m) // return bm { int res = 1; while (m > 0) { res = res * b; m = m - 1; } return res; }
always returns bm
the postcondition we will use the loop invariant to show that this is true
Basis
S(0) is when the loop has not yet been executed.
res0 = 1; m0 = m
So:
1 * bm = bm which means that S(0) is true.
Induction
We assume that S(k) is true for some k >= 0, which means that:
resk * bmk = bm (1)
(3)
continued
(3)
Substitute the right hand sides of (2) and (3) into (1):
( resk+1 / b ) * b(mk+1 + 1 ) = bm which is resk+1 * b(mk+1 + 1 - 1 ) = bm which is resk+1 * bmk+1 = bm
continued
S(k+1) is:
resk+1 * bmk+1 = bm
(4)
continued
Termination
The loop does actually terminate, since m is decreasing, and will reach 0. At loop termination (and function return):
mn = 0
So in S(n):
resn * b0 = bm so resn = bm
The function works!!
Example 3
Problem: does this function work?
int factorial(int num) { int i = 2; int fact = 1; while (i <= num) { fact = fact * i; i++; } return fact; }
the postcondition we will use the loop invariant to show that this is true
Basis
S(0) is when the loop has not yet been executed.
i0 = 2; fact0 = 1
So:
Induction
We assume that S(k) is true for some k >= 0, which means that:
factk = (ik-1)! (1)
continued
Substitute the rhs of (1) for the 1st operand of the rhs of (2):
factk+1 = (ik - 1)! * ik = (ik)! = (ik+1 - 1)!
continued
Termination
The loop does actually terminate, since i is increasing, and will reach num+1. At loop termination (and function return):
in = num+1
So in S(n):
factn = (in - 1)! = (num + 1 - 1)! = num!
The function works!!
Convert the following statements into a formal proposition: a) If the unicorn is mythical, then it is immortal, but if it is not mythical, then it is a mortal mammal. If the unicorn is either immortal or a mammal, then it is horned. The unicorn is magical if it is horned. Consider the following three propositions: b) The unicorn is mythical. c) The unicorn is magical. d) The unicorn is horned. Construct a truth table. From it, determine whether each of b, c or d follows necessarily from a. Solution. M: unicorn is mythical O: unicorn is mortal A: unicorn is mammal H: unicorn is horned G: unicorn is magical M ~O If the unicorn is mythical, then it is immortal, ~M O ^ A but if it is not mythical, then it is a mortal mammal. ~O v A H If the unicorn is either immortal or a mammal, then it is horned. H G The unicorn is magical if it is horned The truth table below is sufficient to answer the queries (one only need to find the shaded rows, since they are the only ones which are all true for the above)
M T T T
O T T T
A T T T
H T T F
G T F T
M~O F F F
~M O^A T T T
~O v A H T T F
HG T F T
T T T
T T T T T T T T T
T T T
T T F F F F F F F
T F F
F F T T T T F F F
F T T
F F T T F F T T F
F T F
T F T F T F T F T
F F F
F F T T T T T T T
T T T
T T T T T T T T T
F T T
T T T T F F T T F
T T F
T T T F T T T F T
M~O
~M O^A
~O v A H
HG
T F F F F
F F F F F F F F F F F F
F T T T T
T T T T F F F F F F F F
F T T T T
F F F F T T T T F F F F
F T T F F
T T F F T T F F T T F F
F T F T F
T F T F T F T F T F T F
T T T T T
T T T T T T T T T T T T
T T T T T
F F F F F F F F F F F F
F T T F F
T T T T T T F F T T F F
T T F T T
T F T T T F T T T F T T
The results: e) The unicorn is mythical. a. M: not T in all shaded rows, hence does not follow from the sentences f) The unicorn is magical. a. G: T in all shaded rows, hence follows necessarily g) The unicorn is horned. a. H: T in all shaded rows, hence follows necessarily
Problem 1 : Find the truth table of the compound proposition (p q)(p r).
p T T T T F F F F
q T T F F T T F F
r T F T F T F T F
(p q)(p r). F T F T F F T T
Problem 2: Give the converse, the contrapositive, and the inverse of the statement If it rains today, then I will drive to work. The converse is If I drive to work today, then it will rain. (q => p) The contrapositive is If I do not drive to work today, then it will not rain. (~ q => ~p ) The inverse is If it does not rain today, then I will not drive to work. (~ p => ~q )
Show that p(q r) and q (p r) are logically equivalent using the laws of logical equivalences. Be sure to cite each law whenever used.
p(q r) p ( q r) ( p) (( q r) p ( q r) (p q) r ( q p) r q (p r) q (p r)
[p q p q]
(a) F; (b) T
Using induction prove that 103n+13n+1 is divisible by 7 for all positive integer value of n Let P(n)= 103n+13n+1 P(0)=1+13=14 which is divisible by 7 P(1)=1000+169=1169 which is divisible by 7 Let us assume P(K)= 103k+13k+1 is divisible by 7 Therefore P(K)= 103k+13k+1 =7p where p is a positive integer P(k+1)= 103(k+1)+13(k+1)+1 = 103k+3+13k+2 = 103103k+13k+2 = 103(7p - 13k+1) +13k+2 = 7000p 1000.13k+1+13 .13k+1 = 7000p 987.13k+1 = 7(1000p 141.13k+1) which is divisible by 7 as the quantity inside the parentheses is an integer