DR Nazir A. Zafar Advanced Algorithms Analysis and Design

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

Advanced Algorithms Analysis

and Design

By

Dr. Nazir Ahmad Zafar

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Lecture No 3

Logic and Proving Techniques

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Today Covered

Tools used for proving algorithms


• Propositional Logic
• Predicate Logic
• Proofs using
• Truth Tables
• Logical Equivalences
• Counter Example
• Contradiction
• Rule of Inference
• Probability as Analysis Tool
• Series and Summation etc.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Propositional and Predicate Logic

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Logical Connectives

• Proposition: Simplest statements also called atomic formula


• Propositions may be connected based on atomic formula.
• Logical connectives, in descending order of operator precedence

Symbol Name Pronunciation

 negation not
 conjunction and
 disjunction or
 implication implies
 equivalence if and only if

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Negation, Conjunction and Disjunction

Negation
Conjunction
• The conjunction p  q is true only if p and q both are
true otherwise false
• The conjunction follows the commutative property,
i.e. p  q = q  p

Disjunction
• The disjunction p  q is false if both p and q are false
otherwise true
• The disjunction follows the commutative property as
well, i.e. p  q = q  p
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Implication

• The p is antecedent and q is consequent


• The antecedent is stronger than consequent.
• Commutative property does not hold, i.e.
(p  q)  (q  p)
p q pq qp pq
t t t t t
t f f t f
f t t f t
f f t t t
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Bi-implication

The equivalence p  q means p  q & q  p


Commutative property does hold, i.e.
(p  q) = (q  p)

p q pq qp pq&qp


t t t t t
t f f t f
f t t f f
f f t t t

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Predicates and Quantifiers

Predicate: P(x)  x < 5


Example:  x : N | x2 = x  x < 2
For all quantifier
  x, P(x) is true  P(x) is true for all x.
Existential Quantifier
  x, P(x) is true  P(x) is true for some value of x.
Logical Equivalences
  x, P(x) is logically equivalent to  ( x, P(x))
  x, P(x) is logically equivalent to ( x, P(x))
  x, (P(x)  Q(x)) means x, P(x)  Q(x)
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Proving Techniques

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Proof using Truth Table: (p  q  r)  (p  (q  r))

p q r (p  q  r)  (p  (q  r))
t t t t t t t tt
t t f t f t f ft
t f t f t t t tt
t f f f t t t tt
f t t f t t t tt
f t f f t t t ft
f f t f t t t tt
f f f f t t t tt
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
De Morgan’s Laws

1. (p  q) =  p   q

p q pq (p  q) p q p  q

t t t f f f f
t f f t f t t
f t f t t f t
f f f t t t t

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


De Morgan’s Laws

2. (p  q) =  p   q

p q pq (p  q) p q p  q

t t t f f f f
t f t f f t f
f t t f t f f
f f f t t t t

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Proof using Counter Example, Contraposition

Counter Example
To prove  x (A(x)  B(x)) is false, we show some
object x for which A(x) is true and B(x) is false.
Proof
 ( x (A(x)  B(x))) 
 x, (A(x)  B(x))) 
 x, (A(x)  B(x)) 
 x, A(x)  B(x))

Contraposition
To prove A  B, we show ( B)  ( A)
• x is divisible by 4  x is divisible by 2 
x is not divisible by 2  x is not divisible by 4 
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Proof by Contradiction

Contradiction
To prove A  B,
Steps in Proof
• We assume A and to prove that B
• On contrary suppose that  B and
• Then prove B, it will be contradiction
Further analysis
• A  B  (A  B)  B Contradiction
• A  B  (A  B) is false
• Assuming (A  B) is true,
and discover a contradiction (such as A  A),
then conclude (A  B) is false, and so A  B.

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Problem: Proof by Contradiction

Prove:
[B  (B  C)]  C, by contradiction
Proof:
Suppose [B  (B  C)], to prove C
On contrary, assume C
C  [B  (B  C)] must be true
 C  [B  ( B  C)]
 C  [(B   B)  (B  C)]
 C  [f  (B  C)]
 C  B  C = C  C  B = f  B = f
 False, Contradiction  C
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Rules of Inference

• A rule of inference is a general pattern that allows us


to draw some new conclusion from a set of given
statements. If we know P then we can conclude Q.

Modus ponens
If {B  (B  C)} then {C}, example in last slide
Proof:
Suppose B  (B  C) then
B
BC

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Rules of Inference

Syllogism
If {A  B  B  C} then {A  C}
Proof
• Suppose A  B  B  C, To prove A  C
• B
• C

Rule of cases
If {B  C  B  C} then {C}
B, true, implies C true
 B, true, implies C true
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Two Valued Boolean Logic

1. Boolean values = B = {0, 1}, there are two binary


operations:
• + = or = 
• · = and = 
2. Closure properties:
  x, y  B, x + y  B
  x, y  B, x . y  B
3. Identity element:
• x+0=0+x=x
• x·1=1.x= x
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Two Valued Boolean Logic

4. Commutative:
• x+y=y+x
• x·y=y·x
5. Distributive:
• x · (y + z) = (x · y) + (x · z)
• x + (y · z) = (x + y) · (x + z)
6. Complement:
  x  B,  x’  B such that
x + x’ = 1, x · x’ = 0

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Tautologies and Truth Table
Tautology:
• Any statement which is always true is called a tautology
Example
• Show [B  (B  C)]  C is a tautology:
Proof
B C (B  C) (B  (B  C)) (B  (B  C))  C
0 0 1 0 1
0 1 1 0 1
1 0 0 0 1
1 1 1 1 1
• For every assignment for B and C, the statement is True,
hence the above statement is a tautology.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Probability as Analysis Tool
Elementary events
• Suppose that in a given situation an event, or an
experiment, may have any one, and only one, of k
outcomes, s1, s2, …, sk. Assume that all these
outcomes are mutually exclusive.
Universe
The set of all elementary events is called the
universe of discourse and is denoted
U = {s1, s2, …, sk}.
Probability of an outcome si
• Associate a real number Pr(si), such that
0  Pr(si)  1 for 1  i  k;
Pr(s1) + Pr(s2) + … + Pr(sk) = 1
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Event

Event
• Let S  U. Then S is called an event, and
Pr( S )   Pr( si )
si S

Sure event
• U = {s1, s2, …, sk}, if S = U
Pr( S )   Pr( si )  1
si S

Impossible event
• S = , Pr() = 0

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Arithmetic and Geometric Series
n
n(n  1)

i 1
i
2
n
n ( n  1)( 2 n  1) 2 n 3
 3n 2
n

i 1
i 
2

6

6
n
i (i n
 1)

k 1
i  i  i  ...  i 
k 2 n

i 1
k
 
n

2 i
 2 1
n
 i
i 1
2 i
 k  1 2 k 1
2
i 1

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design


Conclusion

• Propositional Logic
• Predicate Logic
• We have discussed various techniques of proving
• Truth Tables
• Logical Equivalence
• Counter Example
• Contraposition
• Contradiction
• Rule of Inference
• Probability can be used for average cost analysis
• Series and summation are very helpful in simplification

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

You might also like