discrete mathematics ? transparency no. 2-0 chapter 3 mathematical reasoning transparency no. 3-1...

91
Discrete Mathematics ? Transparency No. 2-0 Chapter 3 Mathematical Reasoning Transparency No. 3-1 formal logic mathematical preliminaries

Post on 18-Dec-2015

237 views

Category:

Documents


4 download

TRANSCRIPT

Discrete Mathematics

? Transparency No. 2-0

Chapter 3

Mathematical Reasoning

Transparency No. 3-1

formal logicmathematical preliminaries

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-2

Contents

1.5 and 3.1 Proofs ad proof methods

3.2 Mathematical Inductions

3.3 Recursive defined sets

Recursive definitions

Structural Induction

3.4 Recursive algorithms

3.5 Program correctness

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-3

Proofs and proof methods

Nature and importance of proofs Applications of proofs Proof terminology Inference rules Some inference rules Soundness of inference rules Formal proofs Formal proof examples Inference rules for quantifiers common fallacies proof methods

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-4

Nature & Importance of Proofs

In mathematics, a proof is: a correct (well-reasoned, logically valid) and complete

(clear, detailed) argument that rigorously & undeniably establishes the truth of a mathematical statement.

Why must the argument be correct & complete? Correctness prevents us from fooling ourselves. Completeness allows anyone to verify the result.

[Discrete] Mathematics requires a very high standard for correctness and completeness of proofs.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-5

Overview of §§1.5 & 3.1

Methods of mathematical argument (i.e., proof methods) can be formalized in terms of rules of logical inference.

Mathematical proofs can themselves be represented formally as discrete structures.

Review both correct & fallacious inference rules, & several proof methods.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-6

Applications of Proofs

Clear communication of logical arguments in any area of study.

Discovery and elucidation, through proofs, of interesting new mathematical theorems.

Theorem-proving has applications in program verification, computer security, automated reasoning systems, etc.

Proving a theorem allows us to rely upon on its correctness even in the most critical scenarios.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-7

Proof Terminology

Theorem [定理 ] A statement that has been proven to be true.

Axioms[公設 ], postulates, hypotheses, premises Assumptions (often unproven) defining the structures

about which we are reasoning.

Rules of inference [推論法則 ] Patterns of logically valid deductions from hypotheses

to conclusions.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-8

More Proof Terminology

Lemma[輔助定理 ] - A minor theorem used as a stepping-stone to proving a major theorem.

Corollary - A minor theorem proved as an easy consequence of a major theorem.

Conjecture - A statement whose truth value has not been proven. (A conjecture may be widely believed to be true, regardless.)

Theory – The set of all theorems that can be proven from a given set of axioms.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-9

Graphical Visualization

A Particular TheoryA Particular Theory

Various TheoremsVarious TheoremsThe AxiomsThe Axiomsof the Theoryof the Theory

A proofA proof

inference ruleinference rule

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-10

Inference Rules - General Form

Inference Rule – Pattern establishing that if we know that a set

of antecedent statements of certain forms are all true, then a certain related consequent statement is true.

antecedent 1 antecedent 2 … consequent “” means “therefore”

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-11

Inference Rules & Implications

Each logical inference rule corresponds to an implication that is a tautology.

antecedent 1 Inference rule antecedent 2 … consequent

Corresponding tautology:

((ante. 1) (ante. 2) …) consequent

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-12

Some Inference Rules

p Rule of Addition pq

pq Rule of Simplification p

p Rule of Conjunction q pq

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-13

Modus Ponens & Tollens

p Rule of modus ponenspq (a.k.a. law of detachment)q

qpq Rule of modus tollens p

“the mode of affirming”

“the mode of denying”

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-14

Syllogism Inference Rules

pq Rule of hypothetical qr syllogismpr

p q Rule of disjunctive p syllogism q

Aristotle(ca. 384-322 B.C.)

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-15

Formal Proofs

A formal proof of a conclusion C, given premises p1, p2,…,pn consists of a sequence of steps, each of which applies some inference rule to premises or to previously-proven statements (as antecedents) to yield a new true statement (the consequent).

A proof demonstrates that if the premises are true, then the conclusion is true.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-16

Formal Proof Example

Suppose we have the following premises:“It is not sunny and it is cold.”“We will swim only if it is sunny.”“If we do not swim, then we will dance.”“If we dance, then we will be home early.”

Given these premises, prove the theorem“We will be home early” using inference rules.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-17

Proof Example cont.

Let us adopt the following abbreviations: sunny = “It is sunny”; cold = “It is cold”;

swim = “We will swim”; dance = “We will dance”; early = “We will be home early”.

Then, the premises can be written as:(1) sunny cold

(2) swim sunny(3) swim dance

(4) dance early

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-18

Proof Example cont.

Step Proved by1. sunny cold Premise #1.2. sunny Simplification of 1.3. swimsunny Premise #2.4. swim Modus tollens on 2,3.5. swimdance Premise #3.6. dance Modus ponens on 4,5.7. danceearly Premise #4.8. early Modus ponens on 6,7.

Discrete Mathematics

? Transparency No. 2-0

The formalization of proofs and inference rules

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-20

What is a proof

what is a proof ? ==> a sequence of formulas (I.e., statements or

logical expressions)

A1, ... An [=A]

generated according to some ( valid inference) rules

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-21

Inference rules

An inference rule is a pattern of formulas of the form: P1,P2,...,Pm (m 0) ------------------------------ rule-name C

Meaning that if P1,..,Pm have been produced (proved, generated, etc) before then we can add C to the proof sequence (now).

P1,..,Pm : premises of the rule; C: Conclusion of the rule.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-22

Formal definition of proofs

Ax: a set of axioms ; R: a set of inference rules Let Th = Ax U R ;

Th is called an axiom system (or proof system)

A: a formula ; B = {B1,…,Bm } is a set of formulas. A proof of A (according to axiom system Th) with B

as premises is a nonempty sequence of formulas

A1,A2,...,An s.t., 1. An = A. 2. For each Ai, i = 1,.., n

Either Ai is an axiom (i.e., a member of Ax) or a premise Bj, or

there is an inference rule r: P1,..,Pm / C in R s.t.

1. C = Ai

2. {p1,..,Pm}{A1,...,Ai-1}

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-23

More about proofs

Notes: 1. We use B |-Th A (or B1,…,Bm |-Th A)

to mean the existence of a proof of A with B1,…,Bm as premises. (Th can be omitted if there is no worry of confusion)

2. If {} |-Th A (or simply |-Th A) , we say A is a theorem and each Ai (i <n) is called a lemma.

3. If B can be inferred from A directly, it is called a corollary of theorem A; both lemmas and corollaries are theorems.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-24

Soundness of inference rules

An inference rule:

P1,..,Pm // C is said to be sound(可靠 ) (or correct[正確 ], valid

[有效 ]) iff It is impossible that all premises are true w

hile the conclusion is false. I.e., If all premise are true then the conclusion m

ust be true. I.e., (P1/\P2/\…/\Pn ) C is a tautology. I.e., C is a logical consequence of P1,P2,… and Pm. I.e., P1,P2,…Pn |= C

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-25

Example sound rules

A, A->B -------------- ;Modus Ponens (->e) B

It is impossible that A and A->B are ture while B is false. [A /\ (A->B)] -> B is a tautology

A/\ B // B ; /\e It is impossible that A/\B is true while B is false.

A // A \/ B ; \/i It is impossible that A is true but A\/B is false.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-26

Fallacies

A fallacy is an inference rule which is not sound.

(I.e., It is possible that all premises are true but the conclusion is false).

Ex:

1. RainWet // ~Rain ~Wet.

2. A->B, ~A // ~B

3. A->B, B // A ; B true, A->B true, A false is possible.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-27

Fact1 : If P1,..,Pm // C is sound, and all premises Pis hold for an interpretation I then so is the conclusion C.

pf: By def. of soundness,

P1,..,Pm // C implies P1,..,Pm |= C

i.e., M M |= P1/\.../\Pm M |= C.

Since now I |= P1/\.../\Pm, we have I |= C.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-28

Soundness of a proof system

Soundness theorem: If

1. there is a proof of C from a set of premises P= {P1,… Pn } (I.e., P1,…Pn |- C ), and

2. all inference rules used are sound, then

the conclusion C must be a consequence of {P1,… Pn}. (i.e., P1,P2,…Pn |= C )

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-29

skippedpf: The proof is by ind. on the minimal length k of th

e proofs witnessing P1,…Pn |- C. case 1: k = 1: The = C is either an axioms or some

premise Pj and we have P |= C.

case 2: k = t + 1> 1: The = t C for some shorter proof t.

2.1 If C is either a premise Pj or an axioms, the analysis is the same as case 1.

2.2 If C is obtained via rule Q1,…,Qn // C where assume Q1,…,Qn occurs at the last line of j1,…jn , respectively.

By Ind.hyp., for all Qr , P |= Qr, ( M M |= P M |= Q1/\…/\Qn and M M |= Q1/\…/\Qn

M|= C) M M |= P M |= C (i.e., P |= C.)

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-30

Corollary : If |-Th C, then |= C. I.e., Every theorem is valid).

pf: |-Th C means {} |-Th C.

Hence {} |= C.

But all interpretations are model of {},

C thus is valid.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-31

Example inference rules

1. Modus Ponus(MP) : AB, A // B

2. abduction (ABD) : AB, B // A

3. denying premise : AB, ~A // ~B

4. Math. ind.: (let P be any formula ) P(0)

x P(x) P(x +1)

--------------------------

x P(x)

Notes:

1. rule 1 is correct.

2. rule 2,3 are in general not correct.

3. Rule 4 is correct for natural number(NT) theory, but not correct for integer theory(ZT) and real number theory(RT).

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-32

Some commonly used inference rules

Rules of inferences

Tautologies Name

p // p\/q

p->(p\/q) Addition

p/\q // p p/\q->p Simplication

p, p->q//q p/\(p->q) -> q Modus ponens

~q, p->q //~p

~q /\ (p->q) ->~p Modus Tollens

p->q,q->r//p->r ((p->q) /\ (q->r)) -> (p->r)

Hypothetical syllogism

p\/q,~p //q ~p /\ (p \/q) -> q Disjunctive syllogism

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-33

Some commonly used fallacies

Affirming the conclusion [abduction]: From p->q, q infer p Ex: Do all exercises => learn discrete math.

Since have learned D.M., hence have done all exercises. note: p is a possible reason (explanation) of q, instead of a

(necessary) consequence of q.

Denying the hypothesis: from ~p and p->q infer ~q. Ex: rain => wet, since not rain, hence not wet.

Circular reasoning Assume n2 is even. n2 = 2k for some k. Hence n2 is even

Assume A…B

-------------- IAB

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-34

Inference Rules for Quantifiers

x P(x) universal instantiation (e) P(t) (substitute any term t for x in P(-))

Ex: from x x2 > 0 infer (y+2)2 > 0.

P(t) existential generalization (I)x P(x) (substitute a new var x for term t in P(-) )

ex: from (y + 2 )2 > (y+2) infer x x2 > (y+2) or x x2 > x.

Note: Term t is either a constant or a variable or function expression f(t1,…,tn)

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-35

Example

Definitions:s :≡ Socrates (ancient Greek philosopher);H(x) :≡ “x is human”;M(x) :≡ “x is mortal”.

Premises:H(s) Socrates is human.

x H(x)M(x) All humans are mortal. Conclusion:

M(s) Socrates is mortal.

Topic #3 – Predicate Logic

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-36

The proof

Some valid conclusions you can draw:H(s)M(s) [Instantiate universal.] If Socrates is human

then he is mortal.

H(s) M(s) Socrates is inhuman or mortal.

H(s) (H(s) M(s)) Socrates is human, and also either inhuman or mortal.

(H(s) H(s)) (H(s) M(s)) [Apply distributive law.]

F (H(s) M(s)) [Trivial contradiction.]

H(s) M(s) [Use identity law.]

M(s) Socrates is mortal.

Topic #3 – Predicate Logic

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-37

Another Example [skipped]

Definitions:

H(x) :≡ “x is human”; M(x) :≡ “x is mortal”;

G(x) :≡ “x is a god” Premises:

x H(x) M(x) (“Humans are mortal”) and x G(x) M(x) (“Gods are immortal”).

Show that x (H(x) G(x))(“No human is a god.”)

Topic #3 – Predicate Logic

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-38

The Derivation[skipped]

x H(x)M(x) and x G(x)M(x). x M(x)H(x) [Contrapositive.] x [G(x)M(x)] [M(x)H(x)] x G(x)H(x) [Transitivity of .] x G(x) H(x) [Definition of .] x (G(x) H(x)) [DeMorgan’s law.] x G(x) H(x) [An equivalence law.]

Rules used: where Q is either or , QX A, A B // QX B. QX ~A , // ~QX A. X A, X B // X (A/\B).

Topic #3 – Predicate Logic

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-39

More Inference Rules for Quantifiers

universal generalization(I) P(g) x P(x)

Existential instantiation (e) x P(x)

P(c) (substitute a new (Skolem)

constant c ) c is an object restricted to property P)

(where g is a general (or universal) element of u.d. I.e., g cannot be restricted to any property except being an element of u.d.)

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-40

Example1 (universal genealization (I ) The square of all odd numbers are odd.

x Odd(x) Odd(x2)

pf: Let g be any number. // note: cannot assume additional property for g except for its being an integer; g is a universal constants.

[ Assume g is odd, then,

g = 2k + 1 for some k,

g2 = 4kk + 4k + 1 = 2(2kk + 2k) + 1,

g2 is odd. ] // [ … ] is a subproof for

=> Odd(g) Odd(g2).

=> x Odd(x) Odd(x2).

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-41

Example2

xy x < y. // no largest numberpf: Let a be any number. =>a < a + 1 ( number property; true of all numbers) (but not a2 < a + 10 ; not a universal number prop ) => y a < y (I) => xy x < y (I)

xy x y //least numberpf: Let a be any natural number. => 0 a ( property of natural numbers) => y 0 y (I) => xy x y (e)

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-42

a fallacy

Prove that x P(x) |- x P(x) What’s wrong with the proof: ?

1. x P(x) --- premise 2. P(c) --- e, where c is a restricted object. 3. x P(x) --- I (x) why?

Ans: c is not a general element! It requires to satisfy property P and thus is not a general element.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-43

Techniques for proving implication theorems : pq

Different ways of proving a theorem: p implies q. Vacuous proof: Prove that ~p. used rule : [ ~p //p->q ]

Trivial proof: Prove that q. [q // p->q ]

Direct proof: Prove that if p then q. [p |- q implies |- p->q]suppose p, ..., conclude q --------------------- (I)hence pq

Indirect proof: (proof by contraposition) Prove that "~q implies ~P" [~q->~p // p->q]

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-44

Proof by contradiction and proof by cases

Proof by contradiction: To prove P, it suffices to show that ~P -> F (false)

rules used: [~p ->F // p]

Proof by cases: To prove that "p \/ q implies r " it suffices to show that p->r and q -> r.

rules used: [p->r, q->r // (p\/ q) ->r.]

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-45

Direct Proof Example

Definition: An integer n is called odd iff n=2k+1 for some integer k; n is even iff n=2k for some k. Axiom: Every integer is either odd or even.

Theorem: (For all numbers n) If n is an odd integer, then n2 is an odd integer.

Proof: Let n be any integer. [Assume n is odd, then n = 2k+1 for some integer k. Thus, n2 = (2k+1)2 = 4k2 + 4k + 1 = 2(2k2 + 2k) + 1. Therefore n2 is of the form 2j + 1 (with j the integer 2

k2 + 2k), thus n2 is odd. ]

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-46

Indirect Proof Example

Theorem: (For all integers n) If 3n+2 is odd, then n is odd.

Proof: Suppose that the conclusion is false, i.e., that n is even.

Then n=2k for some integer k. Then 3n+2 = 3(2k)+2 = 6k+2 = 2(3k+1). Thus 3n+2 is even, because it equals 2j for

integer j = 3k+1. So 3n+2 is not odd. We have shown that ¬(n is odd)→¬(3n+2 is odd),

thus its contra-positive (3n+2 is odd) → (n is odd) is also true. □

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-47

Vacuous Proof Example

Theorem: (For all n) If n is both odd and even, then n2 = n + n.

Proof: The statement “n is both odd and even” is necessarily false, since no number can be both odd and even.

So, the theorem is vacuously true. □

lemma : n could not be both even and odd.

pf: Suppose n is both odd and even, then n = 2k = 2m + 1 for some integer m and k. Then 2(k-m) = 1.

Then k != m and hence 2 | 1. But this is impossible.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-48

Trivial Proof Example

Theorem: (For integers n) If n is the sum of two prime numbers, then either n is odd or n is even.

Proof: Any integer n is either odd or even.

So the conclusion of the implication is true regardless of the truth of the antecedent.

Thus the implication is true trivially. □

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-49

Proving existence theorem

Methods for proving x p(x): Constructive proof: find an object (or term) a, s.t.

P(a). rules used : [p(a) // x p(x) ]

Nonconstructive proof: a proof of x P(x) w/o knowing what object satisfies p. ex:proof by contradiction: Show that ~x p(x) ->F.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-50

Example of existence proofs

Ex 20: [constructive proof] Show that there are n consecutive composite integers for every integer n >0. (I.e. for all n x (x+1,x+2,...x+n) are all composites.

Sol: Let x = (n+1)! +1.

=> x+i = (n+1)! + (i+1) = (i+1)( (n+1)!/(i+1) +1) is composite for i = 1,..,n. QED.

Ex 21: [nonconstructive proof] For all n >0 prime number > n.

Sol: by contradiction. Assume n s.t. all prime number < n.

Let m = n! +1. ==> gcd(k, m) = 1 for all k ≤ n.

=> all primes cannot divide m

=> m is a prime > n

=> a contradiction. QED.

Note: We cannot know a prime > n from the proof.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-51

Example of proof by cases...

Given n>0, prove there is a prime p>n.

pf: Consider x = n!+1. Since x>1, we know (x is prime)(x is composite).

Case 1: x is prime. Obviously x>n, so let p=x and we’re done.

Case 2: x has a prime factor p. But if pn, then

x – (n!/p) x p = 1 => p 1 (mod x).

Then p does not divide x, a contradiction.

So p>n, and we’re done.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-52

Limits on Proofs

Some very simple statements of number theory haven’t been proved or disproved! E.g. Goldbach’s conjecture: Every integer n≥2 is exactl

y the average of some two primes. n≥2 primes p,q: n=(p+q)/2.

For every formal proof system, there are true (or false )statements of number theory which can never be proved (or disproved) (Gödel incompleteness theorem).

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-53

3.2 Mathematical Induction

To show that a property p hold for all nonnegative integer n, it suffices to show that

1. Basis step: P(0) is true

2. Ind. step: P(n) P(n+1) is true for all nonnegative integer.

P(n) in 2. is called the inductive hypothesis. Note: Math. Ind. is exactly the inference rule:

P(0), n P(n)P(n+1) // n P(n) for any property P

The second form of MI (complete MI) Basis: P(0) holds Ind. step: P(0) /\ P(1) /\ ...,/\p(n-1) P(n) holds for all n. P(0) /\ P(1) /\ ...,/\p(n-1) (or k k<n P(k)) is the ind. hyp.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-54

Correctness of Math. Ind.

Correctness of MI.Pf: Assume MI is incorrect. i.e.

the set NP = {k | P(k) is false} is not empty.

Let m be the least number of NP

---existence by well-order theorem

Since p(0), 0 NP and m >0.

=> m-1 exists and P(0),P(1),…,P(m-1) hold

=> P(m) holds [by MI I or II]

=> m NP => a contradiction.

Q.E.D.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-55

Examples :

2: i=1,n 2i-1 = n2

3. n < 2n

4. 3 | n3 - n if n > 0

i=1,n 2i = 2(n+1) -1

6. j=1,n arj = arn+1 - a / (r -1)

7. Let Hk = 1 + 1/2 +...+ 1/k => H2n 1 + n/2

8. |S| = n => |2S| = 2n.

9. 1 + 2+...+ n = n(n+1)/2

10. If n > 3 => 2n < n!

11. ~(S1 ...Sn) = ~S1 U ... U ~Sn.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-56

More examples:

13: n >1 => n can be written as a product of primes. [hint: use 2nd form of MI; proved in chapter 2. ]

14. for every k 12, there are m,n 0 s.t. k = 4m + 5n.pf: By induction on k’ where k’ = k-12. Basis: k’= 0 (i.e., k = 12). Then k = 12 = 4 x 3 + 5 x 0. Inductive step: k’ = t’ + 1 > 0 (i.e., k = t +1 > 12 ) By Ind. Hyp., t = 4m + 5n. Then k = t + 1 = 4m + 5n + 1. if m > 0 => k = 4(m-1) + 5 (n+1) if m = 0 => t = 5n > 11 => n 3. hence t+1 = 5(n-3) + 15 + 1 = 4 x 4 + 5 (n-3).Q.E.D.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-57

3.3 Recursive definition of functions

Different ways of defining a functions Explicit listing

Suitable for finite functions only.

Define by giving an explicit expressionEx: F(n) = 2n

recursive (or inductive ) definitionDefine value of objects (sequences, functions,

sets, ...) in terms of values of smaller similar ones.Ex: the sequence 1,2,4,... (an = 2n) can be defined

recursively as follows:

1. a0 = 1;

2. an+1 = 2 x an for n > 0.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-58

Recursively defined functions

To define a function over natural numbers: specify the value of f at 0 (i.e., f(0)) Given a rule for finding f(n) from f(n-1),..., f(0).

i.e., f(n) = some expression in terms of n, f(n), ..., f(0).

Ex1: f(n) = 3 if n = 0 = 2f(n-1) +3 if n >0 => f(0) = 3, f(1) = 2f(0) +3 = 9 f(2) = 2f(1)+3 = 21,... This guarantees f be defined for all numbers.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-59

More examples functions

Ex2: The factorial function f(n) = n! f(0) = 1 f(n) = n f(n-1) for all n > 0.

Recursively defined functions (over N) are well defined

Pf: Let P(n) = "there is at least one value assigned to f(n)".

Q(n) = "there are at most one value assigned to f(n)".

We show P(n) hold for all n by MI..

basis: P(0) holds.

Ind. : assume p(k) holds for all k ≤ n

=> since f(n+1) can be assigned a value by evaluating the expr(n,f(0),..,f(n)), where by ind. hyp. all f(i)s (i<n) have been assigned a value.

The fact that Q(n) holds for all n is trivial, since each f(k) appear at the left hand side of the definition exactly once. QED

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-60

More examples:

Ex5: The Fibonacci number: f(0) = 0; f(1) = 1; f(n) = f(n-1) + f(n-2) for n > 1. ==> 0,1,1,2,3,5,8,...

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-61

Ex6: Show that f(n) > n-2 whenever n ≥ 3,

where = (1+ sqrt(5))/2 = 1.618 is the golden ratio Properties of : 2 = (1 + ).

Pf: (by MI). Let P(n) = "f(n) > n-2 ".

Basis: P(3) holds since f(3) = 2 >3-2 .

Ind.step: (for n ≥ 4)

If n = 4 =>f(4) = 3 > 4-2 = 1.6182.

If n > 4 => by ind. hyp., f(n-1) >n-3, f(n-2) >n-4

Hence f(n) = f(n)+f(n-1) > n-3 + n-4

=(1+ n-4 = n-2. QED

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-62

Lame's theorem a,b: positive integer with a b.=> #divisions used by the Euclidean algorithm to find gcd(a,b) 5 x

#decimal digits in b.Pf: seq of equations used for finding gcd(a,b) where r0 = a, r1 = b. r2 = ro mod r1 0, r3 = r1 mod r2 0 ... … rn = rn-2 mod rn-1 0, rn+1 = rn-1 mod rn = 0 i.e., until rn | rn-1 . Then gcd(a,b) = rn. and #division used = n. Note: rn 1 = f2 ; rn-1 2rn 2f2 = f3; rn-2 rn+rn-1 = f2 + f3 = f4 ... r2 r3 + r4 fn-1+fn-2=fn; b = r1 r2r3fn+fn-1 = fn+1.> n-1.

logb > (n-1) log ~ 0.208 (n-1) > (n-1)/5 n -1 < 5 log b < 5 #digit(b). => n 5#digit(b).

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-63

Recursively defined sets

Given a universal set U, a subset V of U and a set of operations OP on U, we often define a subset D of U as follows: 1. Init: Every element of V is an element of D. 2. Closure: For each operation f in OP, if f:Un->U and

t1,..,tn are objects already known to be in the set D, then f(t1,..,tn) is also an object of D.

Example: The set S = {3n | n >0} N can be defined recursively as follows: 1. Init: 3 S (i.e., V = { 3 } )∈ 2. closure: S is closed under +. i.e., If a,b S then so are a+b . (OP = {+})∈

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-64

Notes about recursively defined sets

1. The definition of D is not complete (in the sense that there are multiple subsets of U satisfying both conditions.

Ex: the universe U satisfies (1) and (2), but it is not Our intended D.2. In fact the intended defined set 3': D is the least of all subsets of U satisfying 1 & 2, or 3'': D is the intersection of all subsets of U satisfying 1 & 2or 3''': Only objects obtained by a finite number of applications

of rule 1 & 2 are elements of D.3. It can be proven that 3',3'',and 3''' are equivalent.4. Hence, to be complete, one of 3',3'' or 3''' should be

appended to condition 1 & 2, though it can always be omitted(or replaced by the adv. inductively, recursively) with such understanding in mind.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-65

Proof of the equivalence of 3',3'' and 3'''

D1: the set obtained by 1,2,3' D1 satisfies 1&2 and any S satisfies 1&2 is a superset of

D1.

D2: the set obtained by 1,2,3''. D2 = the intersection of all subsets Sk of U satisfying 1&2.

D3: the set obtained by 1,2,3'''. For any x U, x D∈ ∈ 3 iff there is a (proof) sequence

x1,...,xm = x, such that for each xi (i = 1..m) either

(init: ) xi V or∈ (closure:) there are f in OP and t1,...tn in {x1,..,xi-1} s.t.

xi = f(t1,..,tn).

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-66

notes for the proof

1. D2 satisfies 1&2 and is the least of all sets satisfying 1&2 , He

nce D1 exists and equals to D2.

2 (2.1) D3 satisfies 1 & 2. (2.2) D3 is contained in all sets satisf

ying 1 & 2. Hence D3 = D2.

pf: 1.1: Let C = { T1,…,Tm,…} be the collection of all sets satisfying 1&2, and D2, by definition, is ∩C.

Hence V T∈ k for all Tk C and as a result V D∈ ∈ 2.--- (1)

Suppose t1,…,tn D∈ 2, then t1,…,tn T∈ k for each Tk in C,

Hence f(t1,…,tn) T∈ k and as a result f(t1,..,tn) D∈ 2. ---(2).

1.2: Since D2 = ∩C, D2 is a subset of all Tk’s which satisfies 1&2, D2 is the least among these sets.

Hence D1 exists and equals to D2.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-67

2.1 D3 satisfies 1 & 2.[ by ind.]

2.2 D3 is contained in all sets satisfying 1 & 2 [by ind.]

Hence D3 = D2.pf: 2.1: two propositions need to be proved: V D3 ---(1) and ⊆ {t1,..,tn} D3 => f(t1,…,tn) D3 ---(2).⊆ ∈ (1) is easy to show, since for each x in V, the singleton sequen

ce x is a proof. Hence x D3.∈ As to (2), since {t1,..,tn} D3, by definition, there exist proof se⊆

quences S1,S2,…,Sn for t1,…,tn, respectively. We can thus join them together to form a new sequence S = S

1,S2,…,Sn. We can then safely append f(t1,…,tn) to the end of S to form a

new sequence for f(t1,…,tn), since all t1,…,tn have appeared in S.

As a result f(t1,…,tn) D3. (2) thus is proved.∈

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-68

2.2 D3 is contained in all sets satisfying 1 & 2 [by ind.]

pf: Let D be any set satisfying 1&2.

We need to show that for all x, x D∈ 3 =>x D. ∈ The proof is by ind. on the length of the m of the proof seq

uence : x1,…,xm = x of x.

If m = 1 then x=x1 V, and hence x D.∈ ∈ If m = k+1 > 1, then either xm V (and x∈ m D) or∈ ∃ j1,j2,…jn < m and xm = f(xj1,…,xjn) for some f OP.∈ For the latter case, by ind. hyp., xj1,…xjn D.∈ Since D satisfies closure rule, f(xj1,…,xjn) = xm D. ∈Q.E.D

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-69

Example:

Ex 7': The set of natural numbers can be defined inductively as follows: Init: 0 in N. closure: If x in N, then x' in N.

=> 0, 0',0'',0''',... are natural numbers (unary representation of natural numbers)

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-70

Induction principles III (structural induction)

D: a recursively defined set P; a property about objects of D. To show that P(t) holds for all t in D, it suffices to sh

ow that 1. basis step: P(t) holds for all t in V. 2. Ind. step: For each f in OP and t1,..,tn in D, if P(t

1),...,P(tn) holds, then P(f(t1,..,tn)) holds, too.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-71

Correctness of SI

Show the correctness of structural induction.Pf: Assume not correct.

=> NP = {t D | P(t) does not hold} is not empty. ∈=> x NP s.t. a derivation x∃ ∈ ∃ 1,..xn of x and all xi (i<

n) ∉ NP.

=> If n =1, then x1 = x V (impossible)∈ Else either n > 1 and x V (impossible, like n=1)∈ or n > 1, and x=f(t1,.,tn) for some {t1,..,tn} in {x

1,..xn-1} and P holds for all tk’s

=> P(x) holds too

=> x ∉ NP, a contradiction. QED.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-72

MI is a specialization of SI

Rephrase the SI to the domain N, we have: To show P(t) holds for all t N, it suffices to show that∈ Init: P(0) holds Ind. step: [OP={ ‘ }] for any x in N, If P(x) holds than P(x') holds.

Notes: 1. The above is just MI. 2. MI is only suitable for proving properties of natural

numbers; whereas SI is suitable for proving properties of all recursively defined sets.

3. The common variant of MI starting from a value c ≠ 0 ,1 is also a special case of SI with the domain

D = {c, c+1, c + 2, … }

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-73

well-formed arithmetic expressions

Ex: (2 +x), (x + (y/3)),... (ok)

x2+, xy*/3 ... (no)

Let Vr = {x,y,..,} be the set of variables,

M = numerals = finite representations of numbers

OP = {+,-,x,/,^}

U = the set of all finite strings over Vr U M U OP U {(,)}.

The set of all well-formed arithmetic expressions (wfe) can be defined inductively as follows:

1. Init: every variable x in Vr and every numeral n in M is a wfe.

2. closure: If A, B are wfe, then so are (x+y), (x-y), (x * y),

(x / y) and (x ^ y).

Note: "1 + x " is not a wfe. Why ?

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-74

More examples:

Ex9: Wff (well-formed propositional formulas) PV: {p1,p2,.. } a set of propositional symbols. OP = {/\, \/, ~, -> } U = the set of all finite strings over PV U OP U {(,)} Init: every pi in PV is a wff closure: If A and B are wffs, then so are (A/\B), (A \/B), (A->B), ~A.

Ex10: [strings] : an alphabet *: the set of finite strings over is defined inductively a

s follows:1. Init: is a string.2. closure: If x is a string and a a symbol in , then a·x is a

string.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-75

Ex11: Recursively define two functions on *.

len : * -> N s.t. len(x) = the length of the string x. basis: len() = 0 Ind. step: for any x in and a in , len(ax) = len(x) + 1.

· : * x * * s.t. x · y = the concatenation of x and y. Basis: · y = y for all string y. recursive step: (a · z) · y = a · (z · y) for all symbols a and strin

gs z,y.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-76

Prove properties of len(-) on *:

Ex12: show that len(x · y) = len(x) +len(y) for any x,y ∈ *. By SI on x. Let P(x) = "len(xy) = len(x) +len(y)". Basis: x = . => x · y = y => len(x · y) = len(y) = len() + len(y). Ind. step: x = az len(x · y) = len((a · z) · y) = len((a · (z · y)) = 1 + len(zy) = 1+ len(z) + len(y) =len(x) +len(y).

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-77

Where we use Recursion

Define a domain numbers, lists, trees, formulas, strings,...

Define functions on recursively defined domains Prove properties of functions or domains by stru

ctural induction. compute recursive functions

--> recursive algorithm

Ex:

len(x){ // x : a string

if x = then return(0)

else return(1+ l(tl(x))) }

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-78

3.4 Recursive algorithm

Definition: an algorithm is recursive if it solve a problem by reducing it to an instance of the same problem with smaller inputs.

Ex1: compute an where a R and n N.∈ ∈ Ex2: gcd(a,b) a, b N, a > b∈ gcd(a,b) =def if b = 0 then a else gcd(b, a mod b). Ex: show that gcd(a,b) will always terminate. Comparison b/t recursion and iteration

Recursion: easy to read, understand and devise. Iteration: use much less computation time. Result: programmer --> recursive program --> compiler --> iterative program --> machine.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-79

3.5 Program correctness

After designing a program to solve a problem, how can we assure that the program always produce correct output?

Types of errors in a program: syntax error --> easy to detect by the help of compiler semantic error --> test or verify

Program testing can only increase our confidence about the correctness of a program; it can never guarantee that the program passing test always produce correct output.

A program is said to be correct if it produces the correct output for every possible input.

Correctness proof generally consists of two steps: Termination proof : Partial correctness: whenever the program terminates,

it will produce the correct output.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-80

Program verification

Problem: what does it mean that a program produce the

correct output (or results)?By specifying assertions (or descriptions) about the

expected outcome of the program.

Input to program verifications: Pr : the program to be verified. Q : final assertions (postconditions), giving th

e properties that the output of the program should have

P : initial assertions(preconditions) , giving the properties that the initial input values are required to have.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-81

Hoare triple:

P,Q; assertions S: a program or program segment. P {S} Q is called a Hoare triple, meaning that

S is partially correct (p.c.) w.r.t P,Q,i.e., whenever P is true for I/P value of S and terminates, then Q is true for the O/P values of S.

Ex1: x=1 {y := 2; z := x+ y} z = 3 is true. Why ?

Ex 2: x = 1 { while x > 0 x++ } x = 0 is true. why?

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-82

Typical program constructs:

1. assignment: x := expr x := x+y-3

2. composition: S1;S2 Execute S1 first, after termination, then execute S2.

3. Conditional: 3.1 If <cond> then S 3.2 If <cond> then S1 else S2.

4. Loop: 4.1 while <cond> do S 4.2 repeat S until <cond> // 4.3 do S while <cond> …

Other constructs possible, But it can be shown that any program can be converted into an equivalent one using only 1,2,3.1 and 4.1

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-83

Assignment rule

P[x/expr] {x := expr } P P[x/expr] is the result of replacing every x in P by the expres

sion expr. ex: P = "y < x /\ x + z = 5" => P[x/3] = “y < 3 /\ 3+z = 5". Why correct? consider the variable spaces (...,x,...) == x := expr ==> (..., expr,...) |= P Hence if P[x/expr] holds before execution, P will hold after e

xecution. Example: Q {y := x+y} x > 2y + 1 => Q = ? (xb,yb) ==>{ya := xb+yb} ==>(xb,xb+yb) = (xa,ya) |= P(xa,ya) =def ‘’x

a > 2ya +1’’

=> (xb,yb) |= Q = P(xa,ya)[xa/xb;ya/xb+yb]

= P(xb,xb+yb) “xb > 2(xb+yb) +1”

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-84

Composition rules:

Splitting programs into subprograms and then show that each subprogram is correct.

The composition rule: P {S1} Q x = 0 { x:= x+2} ?

Q {S2} R ? { x := x-1} x > 0

------------------- ---------------------------------------

P {S1;S2} R x=0 {x:= x+2; x:= x -1} x > 0 Meaning:

Forward reading: Backward reading: to prove P{S1;S2}Q, it suffices to find

an assertion Q s.t. P{S1}Q and Q {S2}R.

Problem: How to find Q ?

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-85

Example:

Show that x =1 {y := 2; z := x +y} z = 3

x = 1 {y := 2; z := x+y} z = 3 -------------------------------------------------------- x=1 {y := 2} ? ? {z := x+y} z = 3

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-86

Classical rules

Classical rules:

P => P1 P {S} Q1 P => P1

P1 {S} Q Q1 => Q P1 {S} Q1

---------------------- ----------------------- Q1 => Q

P {S} Q P{S} Q -------------------------

P {S} Q

Examples:

x = 1 => x+1>1 x+1>0 {x := x + 1} x > 0

x+1>1 { x := x + 1 } x > 1 x > 0 => x ≠ 0

----------------------------------- -----------------------------------

x = 1 { x := x + 1} x > 1 x+1 > 0 {x := x+1 } x ≠ 0

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-87

Conditional rules

P /\ <cond> {S1} Q

P /\~ <cond> {S2} Q

------------------------------------------------

P {if <cond> then S1 else S2 } Q

T /\ x > y => x x x x {y:=x} y x

------------------------------------------------

P /\ <cond> {S} Q T /\ x>y {y := x} y x

P /\~<cond> => Q ~ x > y => y x

--------------------------- --------------------------------------

P {if <cond> then S} Q T {if x > y then y := x} y x

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-88

While-loop rules

Loop invariant: A statement P is said to be a loop invariant of a while progr

am: While <cond> do S, if it remains true after each iteration of the loop body S.

I.e., P /\ <cond> {S} P is true.

While rule: P /\ <cond> {S} P ----------------------------------------------------- P {while <cond> do S} P /\ ~<cond>

Issues: How to find loop invariant P? Most difficulty of program verification lies in the finding of a

ppropriate loop invariants.

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-89

While loop example

Show that

n>0 { i:= 1; f := 1;

while i < n do (i := i+1 ; f := f x i ) } f = n!

To prove the program terminates with f = n!, a loop invariant is needed.

Let p = "i ≤ n /\ f = i!"

First show that p is a loop invariant of the while program

i.e., i n /\ f = i! /\ i < n { i:= i+1; f:= f x i} i n /\ f=i!

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-90

while loop example(cont'd)

n > 0 --- i:= 1; ------ i ≤ n

f := 1; ------ p = "i ≤ n /\ f = i! “

while i < n do (i := i+1 ; f := f x i )

------ p /\ ~ i < n ==> i=n /\ f = i!

==> f = n!

Discrete Mathematics Ch 3 Mathematical reasoning

Transparency No. 3-91

Another example:

Ex5:Show that the following program is correct: Procedure prod(m,n: integer) : integer1. If n < 0 then a := -n else a := n ; ------ a = |n|2. k := 0 ; x := 0 3. while k < a do --- p = "x = mk /\ k ≤ a" is a loop x := x + m; invariant. k := k+1 enddo --- x = mk /\ k ≤ a /\ ~k<a => k=a /\ x=ma => x = m |n| 4. If n < 0 then prod := -x => prod = - m |n| = mn else prod := x => Prod = m |n| = mn---- prod = mn. Hence the program is [partially] correct !Note: to be really correct, we need to show that the program will

eventually terminates.