461191 discrete mathematics lecture 4: induction and recursion san ratanasanya cs, kmutnb

136
461191 Discrete Mathematics Lecture 4: Induction and Recursion San Ratanasanya CS, KMUTNB

Upload: justin-george

Post on 25-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

461191 Discrete MathematicsLecture 4: Induction and Recursion

San Ratanasanya

CS, KMUTNB

Today’s Topics

Last week review Administrivia Proof Review Mathematical Induction Recursive Definitions Recursive Algorithms Program Correctness

3

Last week review

Algorithms,The Integers, and

Matrices

Algorithm

is a finite set of precise instructions for performing a computation or for solving a problem.

PseudocodeAlgorithm 1 Finding the maximum element in a Finite Sequence.

procedure max (a1, a2,…, an : integers)max := a1for i := 2 to n

if max < ai then max := ai{max is the largest element}

Algorithm 2The Linear Search Algorithm

procedure linear search (x : integer; a1, a2,…, an

:distinct integers)i := 1while (i ≤ n and x ≠ ai)

i := i + 1if i ≤n then location := ielse location := 0{location is the subscript of term that equals x, or is 0 if not found}

Algorithm 3 Binary Search Algorithm

procedure binary search (x : integer; a1, a2,…, an

: increasing integers)i := 1 {i is left endpoint of search interval}j := n {j is right endpoint of search interval}while i < jbegin

m := (i + j)/2 if x > am then i := m + 1

else j := mendif x = ai then location := i

else location := 0{location is the subscript of term equal to x, or 0 if x is not found}

PseudocodesProcedure bubblesort(a1,…,an: real numbers with n 2)

for i := 1 to n-1 for j := 1 to n-1 if aj > aj+1 then interchange aj and aj+1

{a1,…,an is in increasing order}

The growth of functions

DEFINITION 1 : Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f(x) is O(g(x)) if there are constants C and k such that,

|f(x)| ≤ C |g(x)| when x > k

Example 1 : show that f(x) = x2+2x+1 is O(x2)

case 1 0 ≤ x2+2x+1 ≤ x2+2x2+x2 = 4x2, when x >1So f(x) = O(x2) , when C = 4 and k = 1 Ans

case 2 when x >2 , 2x < x2

0 ≤ x2+2x+1 ≤ x2+x2+x2 = 3x2,when x >2so f(x) = O(x2) when C = 3 and k = 2 Ans

In this example, g(x) = O(f(x)) since x2 ≤ x2+2x+1so, we say that f(x) and g(x) are of the same order

THEOREM 1 : Let f(x) = anxn+an-1xn-1+…+a1x+a0 where a0,a1,…an are real numbers.Then f(x) is O(xn)

Proof : |f(x)| = | anxn+an-1xn-1+…+a1x+a0 |

≤ |an|xn+|an-1|xn-1+…+|a1|x+|a0|

= xn (|an|+|an-1|x-1+…+ |a1|x(n-1)+ |a0|x-n-1)

≤ xn (|an|+|an-1|+…+ |a1|+ |a0|)

This shows that |f(x)| ≤ C xn when C = (|an|+|an-1|+…+ |a1|+ |a0| and x > 1.

Example 2 : 1+2+3+…+n = O(?)≤ n +n +…+n = n × n = n2

= O(n2) , C = 1 , k = 1

Example 3 : f(n) = n! = O(?)= 1 * 2 * 3 * …* n≤ n * n * n * …* n= nn ≤ C |g(n)|= O(nn), C = k = 1

Since……n! ≤ nn

log n! ≤ log nn = n log nlog n! = O(n log n)

...n < 2n n = O(2n) , k = C = 1

...log n < n log n = O(n) , k = C = 1

...logbn = log n/log b < n/log b

logbn = O(n) , k = 1 , C = 1/log b

THEOREM 2 : If f1(x) = O(g1(x)) and f2(x) = O(g2(x))then (f1+f2)(x) = O(max(g1(x), g2(x)))

since |f1(x)| ≤ C1 |g1(x)| , when x > k1 and |f2(x)| ≤ C2

|g2(x)| , when x > k2

|(f1+f2)(x)| = |f1(x) + f2(x)| ≤ |f1(x)| + |f2(x)|

|f1(x)| + |f2(x)| < C1|g1(x)| + C2|g2(x)|

≤ C1|g(x)| + C2|g(x)|

= (C1+C2) |g(x)| = C |g(x)|

when C = C1+C2 , and g(x) = max|(g1(x),g2(x)| ,

k = max(k1,k2)

THEOREM 3 : Let f1(x) = O(g1(x)) and f2(x) = O(g2(x))(f1 f2)(x) = O( g1(x) g2(x) )

Example 4 : f(n) = 3n log (n!)+(n2+3) log n = O(?), where n = positive integer≤ O(n) O(n log n) + O(2n2) O(log n)≤ O(n2 log n) + O((2n2) log n) = O(3n2 log n)= O(n2 log n)

Example 5 : f(x) = (x+1) log (x2+1) + 3x2 = O(?)

====================================================Commonly Estimated Functions : 1, log n , n , n log n , n2 , 2n , n!

Complexity Analysis of Algorithms

Computational Complexity:

1) Time Complexity: An analysis of the time required to solve a problem.

Time complexity is described in terms of the number of operations required instead of actual computer time, such as comparison, addition, subtraction, multiplication, division, etc.

There are 3 possible cases in time complexitybest case, average case, and worst case

2) Space Complexity: An analysis of the computer memory required to solve a problem.

space complexity are tied in with the particular data structures used to implement the algorithm.

Example 1

Describe the time complexity of Algorithm 1 of Section 2.1 for finding the maximum element in a set.

Solution: “The number of comparisons will be used as the measurement of the time complexity of the algorithm, since comparisons are the basic operation used.”

when i = n + 1, there are exactly 2(n - 1) + 1 = 2n - 1 comparisons

Example 2

Describe the time complexity of linear search algorithm.

n = 2k or 2k < n < 2k+1

2k elements in the first loop2k-1 elements in the second loop

: :21 elements in the k-1th loop20 elements in the kth loop

At most 2k+2 comparisons= (2 log n ) + 2 = O(log n)

k loops

Example 3Describe the time complexity of the binary search algorithm.

Solution: For simplicity, assume there are n = 2k elements in the list a1, a2,…,an, where k is a non-negative integer. Note that k = log n

(If n, the number of elements in the list, is not a power of 2, the list can be considered as part of a larger list with 2k + 1 elements, where 2k < n <2k + 1. Here 2k + 1 is the smallest power of 2 larger than n).

Example 4Average case performance of Linear Search Algorithm

2i + 1 comparisonsi = 1 : 3i = 2 : 5

:i = n : 2n + 1

Average = (3 + 5 + 7 +n…+ 2n + 1)/2 = (2(1+2+n…+ n) + n)/n

Since 1 + 2 + 3 +…+ n = n(n + 1)/2So, the average = 2[n(n + 1)/2] + n = n + 1 + 1 = n + 2 = O(n)

Polynomial worst-case complexity = tractableO(nb), b is an integer ≥ 1

But when b is large or C is very large problem(takes too long time to solve)

“Intractable” : problem that cannot be solved within Polynomial time.

Big O estimates of time complexity cannot directly tell the actual amount of computer time used.

Knowing the constants C and k makes | f(n)| ≤ C |(g(n))| when n > k

The time each type of operations used is not equivalent. Type of computers can also be different in terms of

specification, and generation.

Asymptotic Notations Big O (Upper bound)

f(n) grow faster than g(n) |f(n)| C|g(n)| when n > k f(n) = O(g(n))

Big (Lower bound) f(n) grow slower than g(n) |f(n)| C|g(n)| when n > k f(n) = (g(n))

Big (Tight bound) f(n) and g(n) have the same rate of growth C1|g(n)| |f(n)| C2|g(n)| when n > k

f(n) = (g(n)) C and k called witness pair.

The Integers & Division

Definition 1: If a and b are integers with a ≠ 0, we say that a divides b if there is an integer c such that b = ac. When a divides b we say that a is a factor of b and that b is multiple of a. The notation a | b denotes that a divides b. We write a | b when a does not divide b.

THEOREM 1: Let a, b and c be integers. Then1. if a | b and a | c, then a | (b + c);2. if a | b, then a | bc for all integers c;3. if a | b and b | c, then a | c.

Definition 2: A positive integer p greater than 1 is called prime if the only positive factors of p are 1 and p. A positive integer that is greater than 1 and is not prime is called composite.

THEOREM 2: THE FUNDAMENTAL THEOREM OF ARITHMETICEvery positive integer greater than 1 can be written uniquely as a primeor as the product of two or more primes where the prime factors arewritten in order of non-decreasing size.

Example 1The prime factorizations of 100, 641, 999, and 1024 are given by

100 = 2255 = 2252,641 = 641,999 = 33337 = 33 37,1024 = 2222222222 = 210

THEOREM 3: If n is a composite integer, then n has a prime divisor less than or equal to n

THE DIVISION ALGORITHM

THEOREM 6: THE DIVISION ALGORITHMLet a be an integer and d a positive integer. Then there areunique integers q and r, with 0 ≤ r < d, such that

a = dq + r

Definition 3: In the equality given in the division algorithm, d is called the divisor, a is called the dividend, q is called the quotient, and r is called the remainder.

Definition 4: Let a and b be integers, not both zero. The largest integer d such that d|a and d|b is called the greatest common divisor of a and b. The greatest common divisor of a and b is denoted by gcd(a, b).

Definition 5: The integer a and b are relatively prime if their greatest common divisor is 1.

Example: gcd (17, 22) = 1 17, 22 = relatively prime

Definition 6: The integer a1, a2,…, an are pairwise relatively prime if gcd(ai, aj) = 1 whenever 1 ≤ i < j ≤ n.

Example: 10, 17, 21 are pairwise relatively prime?10, 19, 24 are pairwise relatively prime?

Definition 7: The least common multiple of the positive integers a and b is the smallest positive that is divisible by both a and b. The least common multiple of a and b is denoted lcm(a, b).

Example: What is the least common multiple of 233572 and 2433?

Solution: We havelcm(233572, 2433) = 2max(3, 4)3max(5, 3)7max(2, 0) = 243572

THEOREM 7: Let a and b be positive integers.Then ab = gcd(a, b) lcm(a, b)

MODULAR ARITHMATICLet a be an integer and m be a positive integer. We denote “a mod m”the remainder when a is divided by m.

Definition 8: If a and b are integers and m is a positive integer, then

a is congruent to b modulo m, if m divides a - b.

The notation a ≡ b (mod m) is used to indicate that a is congruent to bmodulo m. If a and b are not congruent modulo m, a ≡ b (mod m).

Theorem 8: Let a and b be integers, and let m be a positive integer. Then a ≡ b (mod m) if and only if a mod m = b mod m.

Example: Determine whether 17 is congruent to 5 modulo 6 andwhether 24 and 14 are congruent modulo 6.

Solution: Since 6 divides 17 - 5 = 12, we see that 17 ≡ 5 (mod 6).However, since 24 - 14 = 10 is not divisible by 6, we see that24 ≡ 14 (mod 6).

Applications of Number Theory Puzzle Cryptography

Caesar’s Ciphering RSA

Computer Graphics Signal processing Thermodynamics Quantum Physics etc.

Modular Arithmetic

Chinese Remainder Theorem

Matrices Matrix equality

Equals in number of rows and columns, and elements

Matrix Arithmetic Addition, multiplication

Identity matrix AIn = ImA = A

Transpose and Inverse matrix Boolean product

Administrivia

Homework due today 1.6-1.8

Homework 3 is out today Study by yourself

Prolog and Python Programming Assignments will be

coming soon!!

41

Proof Review: Basic Proof Methods

42

Nature & Importance of Proofs In mathematics, a proof is:

A sequence of statements that form an argument. Must be correct (well-reasoned, logically valid) and

complete (clear, detailed) 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.

43

Rules of Inference

Rules of inference are patterns of logically valid deductions from hypotheses to conclusions.

We will review “inference rules” (i.e., correct & fallacious), and “proof methods”.

44

Visualization of Proofs

Various TheoremsVarious Theorems

The AxiomsThe Axiomsof the Theoryof the Theory

A Particular TheoryA Particular Theory

A proofA proof

Rules Rules Of Of InferenceInference

45

Inference Rules - General Form Inference Rule –

Pattern establishing that if we know that a set of hypotheses are all true, then a certain related conclusion statement is true.

Hypothesis 1 Hypothesis 2 … conclusion “” means “therefore”

46

Inference Rules & Implications

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

Hypothesis 1 Inference rule Hypothesis 2 … conclusion

Corresponding tautology: ((Hypoth. 1) (Hypoth. 2) …) conclusion

47

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 hypotheses) to yield a new true statement (the conclusion).

A proof demonstrates that if the premises are true, then the conclusion is true (i.e., valid argument).

48

Common Fallacies A fallacy is an inference rule or other proof

method that is not logically valid. May yield a false conclusion!

Fallacy of affirming the conclusion: “pq is true, and q is true, so p must be true.”

(No, because FT is true.) Fallacy of denying the hypothesis:

“pq is true, and p is false, so q must be false.” (No, again because FT is true.)

49

Common Fallacies - Examples “If you do every problem in this book, then you

will learn discrete mathematics. You learned discrete mathematics.”

p: “You did every problem in this book”q: “You learned discrete mathematics”

Fallacy of affirming the conclusion:pq and q does not imply p

Fallacy of denying the hypothesis:pq and p does not imply q

50

Inference Rules for Quantifiers x P(x)

P(o) (substitute any object o) P(g) (for g a general element of u.d.)

x P(x) x P(x)

P(c) (substitute a new constant c) P(o) (substitute any extant object o)

x P(x)

51

Proof Methods Proving pq

Direct proof: Assume p is true, and prove q. Indirect proof: Assume q, and prove p. Trivial proof: Prove q true. Vacuous proof: Prove p is true.

Proving p Proof by contradiction: Prove p (r r) (r r is a contradiction); therefore p must be false.

Prove (a b) p Proof by cases: prove (a p) and (b p).

More …

52

What you might writeTheorem:

x,y: Rational(x) Irrational(y) → Irrational(x+y)

Proof: Let x, y be any rational and irrational numbers, respectively. … (universal generalization)

Now, just from this, what do we know about x and y? You should think back to the definition of rational:

“… Since x is rational, we know (from the very definition of rational) that there must be some integers i and j such that x = i/j. So, let ix,jx be such integers …”

We give them unique names so we can refer to them later.

53

What next?What do we know about y? Only that y is irrational: ¬ integers i,j:

y = i/j.

But, it’s difficult to see how to use a direct proof in this case. We could try indirect proof also, but in this case, it is a little simpler to just use proof by contradiction (very similar to indirect).

So, what are we trying to show? Just that x+y is irrational. That is, ¬i,j: (x + y) = i/j.

What happens if we hypothesize the negation of this statement?

54

More writing…Suppose that x+y were not irrational. Then x+y would be rational,

so integers i,j: x+y = i/j. So, let is and js be any such integers where x+y = is/ js.

Now, with all these things named, we can start seeing what happens when we put them together.

So, we have that (ix/jx) + y = (is/js).

Observe! We have enough information now that we can conclude something useful about y, by solving this equation for it.

55

Finishing the proof.Solving that equation for y, we have:

y = (is/js) – (ix/jx) = (isjx – ixjs)/(jsjx)

Now, since the numerator and denominator of this expression are both integers, y is (by definition) rational. This contradicts the assumption that y was irrational. Therefore, our hypothesis that x+y is rational must be false, and so the theorem is proved.

56

Proof by CasesTo prove

we need to prove

Example: Show that |xy|=|x| |y|, where x,y are real numbers.

1 2( ... )np p p q

1 2( ) ( ) ... ( )np q p q p q

57

Equivalence of a group of propositions

To prove

we need to prove

1 2[ ... ]np p p

1 2 2 3 1[( ) ( ) ...( )]np p p p p p

58

Proof of Equivalences

( )p q

To proveTo prove

we need to provewe need to prove

ExampleExample: Prove that : Prove that n n is odd iff is odd iff nn2 2 is odd.is odd.

p q

( ) ( )p q q p

59

Example Show that the statements below are

equivalent: p1: n is even

p2: n-1 is odd

p3: n2 is even

60

Counterexamples When we are presented with a statement

of the form xP(x) and we believe that it is false, then we look for a counterexample.

Example Is it true that “every positive integer is the sum

of the squares of three integers?”

61

Proving Existentials A proof of a statement of the form x P(x)

is called an existence proof. If the proof demonstrates how to actually

find or construct a specific element a such that P(a) is true, then it is called a constructive proof.

Otherwise, it is called a non-constructive proof.

62

Constructive Existence Proof Theorem: There exists a positive integer n

that is the sum of two perfect cubes in two different ways: equal to j3 + k3 and l3 + m3 where j, k, l, m are

positive integers, and {j,k} ≠ {l,m} Proof: Consider n = 1729, j = 9, k = 10,

l = 1, m = 12. Now just check that the equalities hold.

63

Another Constructive Existence Proof

Definition: A composite is an integer which is not prime.

Theorem: For any integer n>0, there exists a sequence of n consecutive composite integers.

Same statement in predicate logic:n>0 x i (1in)(x+i is composite)

64

The proof... Given n>0, let x = (n + 1)! + 1. Let i 1 and i n, and consider x+i. Note x+i = (n + 1)! + (i + 1). Note (i+1)|(n+1)!, since 2 i+1 n+1. Also (i+1)|(i+1). So, (i+1)|(x+i). x+i is composite. n x 1in : x+i is composite. Q.E.D.

65

Non-constructive Existence Proof Theorem:

“There are infinitely many prime numbers.” Any finite set of numbers must contain a maximal

element, so we can prove the theorem if we can just show that there is no largest prime number.

i.e., show that for any prime number, there is a larger number that is also prime.

More generally: For any number, a larger prime. Formally: Show n p>n : p is prime.

66

The proof, using proof by cases... Given n>0, prove there is a prime p>n. 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 p mod x = 1. So p>n, and we’re done.

67

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

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

There are true statements of number theory (or any sufficiently powerful system) that can never be proved (or disproved) (Gödel).

68

Circular Reasoning The fallacy of (explicitly or implicitly) assuming the

very statement you are trying to prove in the course of its proof.

Example: Prove that an integer n is even, if n2 is even.Attempted proof: “Assume n2 is even. Then n2=2k for

some integer k. Dividing both sides by n gives n = (2k)/n = 2(k/n). So there is an integer j (namely k/n) such that n=2j. Therefore n is even.”

Begs the question: How doyou show that j=k/n=n/2 is an integer, without first assuming n is even?

69

Mathematical Induction

Mathematical Induction is widely used for Discrete Objects:

Prove results ~ complexity of algorithms Prove correctness of some kinds of computer

programs Prove theory about graph and tree Prove identities and inequalities

70

71

Mathematical Induction A powerful, rigorous technique for proving

that a predicate P(n) is true for every natural number n, no matter how large.

Based on a predicate-logic inference rule:

P(0)n0 (P(n)P(n+1))n0 P(n)

72

Infinite Ladder We know two things:

(1) We can reach the first rung of the ladder

(2) If we can reach a particular rung of the ladder, then we can reach the next rung.

Can we conclude that we can reach every rung?

73

Infinite Ladder

By (1), we can reach the 1st rung.

By (2), we can reach the 2nd rung, because it is next to the 1st rung.

By (2), because it is next to the 2nd rung, we can reach the 3rd rung.

By (2), because we can reach the 100th rung, we can reach the 101st rung.

74

Example

75

Example

76

Mathematical Induction Two parts:

Show that the statement holds for the positive integers 1

Show that if the statement holds for a positive integer then it must also hold for the next larger integer

Based on the rule of inference for positive integers:

)())}1()(()1({ nPkPkPkP

77

Principle of Mathematical Induction

To prove that P(n) is true for all positive integers n, where P(n) is a propositional function, we complete two steps: Basis step: We verify that P(1) is true. Inductive step: We show that the conditional

statement P(k) P(k+1) is true for all positive integers k.

)())]1()(()1([ nPkPkPkP

Proof by Mathematical Induction

P(n) is true for every positive integer n1) Basic step

P(1) = TRUE2) Inductive step

P(k) →P(k+1) , for n

[p(1) k[P(k) →P(k+1)]] →n P(n)if P(k) is assumed to be True, then P(k+1)is also True

78

79

Example: Prove that if n is a positive integer, then

กำ��หนด P(n): sum of the first n positive integers is n(n+1)/2

กำ�รพิสู�จน� P(n) มี� 2 ขั้��นตอน Basis step: ห�ค่��ขั้อง P(1)

ด�งน��น P(1) เป็�นจรง

2

)1(...21

nnn

12

2

2

)11)(1()1(

P

80

Example: Prove that if n is a positive integer, then

Inductive step: สูมีมีต P(k) เป็�นจรง แล้�วห� P(k+1)สู��หร�บ P(k) จะได�

ด�งน��น สู��หร�บ P(k+1) จะต�องแสูดงว�� P(k+1) เป็�นจรง น�#นค่$อต�องแสูดงว��

2

)1(...21

nnn

2

)1(...21

kkk

2

)2)(1(2

)11)(1()1(...21

kk

kkkk

81

Example: Prove that if n is a positive integer, then

สู��หร�บ P(k+1) เร�น��สูมีกำ�รขั้อง P(k) มี�ใช้� โดยกำ�รบวกำ k+1 กำ�บทั้��งสูองขั้��งขั้องสูมีกำ�ร จะได�

ซึ่+#งแสูดงให�เห,นว�� P(k+1) เป็�นจรง

2

)1(...21

nnn

2

)2)(1(2

23

2

22

2

)1(2)1(

)1(2

)1()1(...21

2

2

kk

kk

kkk

kkk

kkk

kk

82

Example: Prove that if n is a positive integer, then

We have completed the basis step and the inductive step, so by mathematical induction we know that P(n) is true for all positive integers n. That is, we have proven that

for all positive integers n.

2

)1(...21

nnn

2

)1(...21

nnn

83

Example2: Prove that the sum of the first n positive odd integers is n2

nth positive odd integer is 2n-1 P(n): Prove

Basis step: P(1) เป็�นจรง เน$#องจ�กำ1 = 12

2)12(...31 nn

84

Example2: Prove that the sum of the first n positive odd integers is n2

Inductive step: P(k)P(k+1) สูมีมีต P(k) เป็�นจรง น�#นค่$อ 1+3+…+(2k-1) = k2

ห�ค่��ขั้อง P(k+1) เน$#องจ�กำเล้ขั้ค่�#ต�วทั้�# k+1 ค่$อ 2(k+1)-1 = 2k+1 ด�งน��น

1+3+…+(2k-1)+(2k+1) = k2+2k+1 = (k+1)2

แสูดงว�� P(k+1) เป็�นจรง ด�งน��นจ+งสูร-ป็ได�ว�� P(n) เป็�นจรง

Example3Prove that n3-n is divisible by 3, whenever n is a positive integer.Let P(k) : k3-k is divisible by 3.Basic step : P(1) : 13-1 = 0 is divisible by 3Inductive step :

if we assume P(k) = True,P(k) = k3-k -------(1)then P(k + 1) must be True,P(k + 1) = (k + 1)3 - (k + 1) -------(2)Proof : from (2),(k + 1)3 - (k + 1) = (k3 + 3k2 + 3k + 1) - (k+1) = (k3-k) + 3(k2 + k)

85

Example 4 : Prove that 1+2+22+…+2n = 2n+1-1 Step1 : P(0) = 1 = 21-1 = 1 is true Step2 :

Example 5 : Prove (sum of Geometric Progressions)

86

Example 6: An Inequality for Harmonic Numbers

Show that

Example 7: Prove that 2n < n! For n = positive integer and n ≥ 4

87

88

Another Induction Example Prove that , n<2n. Let P(n)=(n<2n)

Base case: P(0)=(0<20)=(0<1)=T. Inductive step: For prove P(n)P(n+1).

Assuming n<2n, prove n+1 < 2n+1. Note n + 1 < 2n + 1 (by inductive hypothesis)

< 2n + 2n (because 1<2=22022n-1= 2n) = 2n+1

So n + 1 < 2n+1, and we’re done.

0n

0n

89

Outline of an Inductive Proof Want to prove n P(n) … Base case (or basis step): Prove P(0). Inductive step: Prove n P(n)P(n+1).

e.g. use a direct proof: Let nN, assume P(n). (inductive hypothesis) Under this assumption, prove P(n+1).

Inductive inference rule then gives n P(n).

90

Validity of Induction Prove: if n0 (P(n)P(n+1)), then k0 P(k)

(a) Given any k0, n0 (P(n)P(n+1)) implies (P(0)P(1)) (P(1)P(2)) … (P(k1)P(k))

(b) Using hypothetical syllogism k-1 times we have P(0)P(k)

(c) P(0) and modus ponens gives P(k).

Thus k0 P(k).

91

The Well-Ordering Property The validity of the inductive inference rule

can also be proved using the well-ordering property, which says: Every non-empty set of non-negative integers

has a minimum (smallest) element. SN : mS : nS : mn

92

Use Well-Ordering Property and Proof by Contradiction

Suppose P(0) is true and that P(k)P(k+1) is true for all positive k.

Assume P(n) is false for some positive n. Implies S={n|P(n)} is non-empty and has a

minimum element m (i.e., P(m)=false) But then, P(m-1)P((m-1)+1)=P(m) which is

a contradiction!

93

Generalizing Induction Can also be used to prove nc P(n) for a

given constant cZ, where maybe c0, then: Base case: prove P(c) rather than P(0) The inductive step is to prove: nc (P(n)P(n+1)).

94

Induction Example Prove that the sum of the first n odd positive

integers is n2. That is, prove:

Proof by induction. Base case: Let n=1. The sum of the first 1 odd positive

integer is 1 which equals 12.(Cont…)

2

1

)12(:1 ninn

i

P(n)

95

Example cont. Inductive step: Prove n 1: P(n)P(n+1).

Let n 1, assume P(n), and prove P(n+1).

2

2

1

1

1

)1(

12

)1)1(2()12()12(

n

nn

niin

i

n

i

By inductivehypothesis P(n)

96

Strong Induction Characterized by another inference rule:

P(0)n0: (0kn P(k)) P(n+1)n0: P(n)

Difference with previous version is that the inductive step uses the fact that P(k) is true for all smaller , not just for k=n.

P is true in all previous cases

k n

P(1) P(2) P(3) ... P( ) P( 1)n n

97

Example 1

Show that every n>1 can be written as a product p1p2…ps of some series of s prime numbers. Let P(n)=“n has that property”

Base case: n=2, let s=1, p1=2.Inductive step: Let n2. Assume 2kn: P(k).

Consider n+1. If prime, let s=1, p1=n+1.Else n+1=ab, where 1<an and 1<bn.Then a=p1p2…pt and b=q1q2…qu. Then n+1= p1p2…pt q1q2…qu, a product of s=t+u primes.

98

Example 2

Prove that every amount of postage of 12 cents or more can be formed using just 4-cent and 5-cent stamps.

Base case: 12=3(4), 13=2(4)+1(5), 14=1(4)+2(5), 15=3(5), so 12n15, P(n).

Inductive step: Let n15, assume 12kn P(k). Note 12n3n, so P(n3), so add a 4-cent stamp to get postage for n+1.

99

Example 3

Suppose a0, a1, a2, … is defined as follows: a0=1, a1=2, a2=3, ak = ak-1+ak-2+ak-3 for all integers k≥3.

Then an ≤ 2n for all integers n≥0. P(n)

Proof (by strong induction): 1) Basis step: The statement is true for n=0: a0=1 ≤1=20 P(0)

for n=1: a1=2 ≤2=21 P(1) for n=2: a2=3 ≤4=22 P(2)

100

Example 3 (cont’d)2) Inductive hypothesis: For any k>2,

Assume P(i) is true for all i with 0≤i<k: ai ≤ 2i for all 0≤i<k .

3) Inductive step: Show that P(k) is true: ak ≤ 2k

ak= ak-1+ak-2+ak-3

≤ 2k-1+2k-2+2k-3 (using inductive hypothesis) ≤ 20+21+…+2k-3+2k-2+2k-1

= 2k-1 (as a sum of geometric sequence)

≤ 2k

Thus, P(n) is true by strong induction.

101

Recursive

Recursive definition

When it is difficult to define an object explicitly. It may be easier to define the object in terms of itself. This process is call “Recursive Process”

Ex : an = 3n for n = 0,1,2,…

can be defined as a0 = 1 , and an+1 = 3an

102

Discrete Mathematics for Computer Science 103

Example

104

Recursively Defined Function Define the function in terms of itself. Two steps

Basis step: Specify the value of the function at zero

Recursive step: Give a rule for finding its value at an integer from its values at smaller integers.

Also called recursive/inductive definition

Recursively defined functions

To define a function f(n) where n = 0, 1, 2,…Basic Step:

Specify the value of the function f(0)Recursive Step:

Give the rule for finding f(n+1) in terms of f(n)

105

106

Example

107

Example

Suppose that f is defined recursively by f(0) = 3 f(n+1) = 2f(n) + 3

Find f(1), f(2), f(3) and f(4) f(1) = 2f(0) + 3 = 2(3) + 3 = 6 + 3 = 9 f(2) = 2f(1) + 3 = 2(9) + 3 = 18 + 3 = 21 f(3) = 2f(2) + 3 = 2(21) + 3 = 42 + 3 = 45 f(4) = 2f(3) + 3 = 2(45) + 3 = 90 + 3 = 93

108

Example: Factorial Factorial function F(n) = n! where

F(0) = 1 F(n+1) = (n+1)F(n)

Find F(5)F(5) = 5 · F(4) = 5 · 4 · F(3) = 5 · 4 · 3 · F(2)

= 5 · 4 · 3 · 2 · F(1) = 5 · 4 · 3 · 2 · 1 · F(0) = 5 · 4 · 3 · 2 · 1 · 1 = 120

109

Example: Fibonacci Number

Definition: The Fibonacci numbers, ,

are defined by the equation

and for n = 2,3,4,… Find

,...,, 210 fff

1,0 10 ff

21 nnn fff

65432 ,,,, fffff

110

Example: Fibonacci Numbers (cont.)

835

523

312

211

101

456

345

234

123

012

fff

fff

fff

fff

fff

111

Recursively Defined Sets Two steps

Basis step: an initial collection of elements is specified

Recursive step: rules for forming new elements in the set from those already known to be in the set are provided

112

Example The subset S of the set of integers defined

by Basis step: 3 Є S Recursive step:

If x Є S and y Є S, then x + y Є S.

S = { 3, 6, 9, 12, … }

113

Recursive Algorithms

Recursive Algorithms An algorithm is said to be recursive if it

solves a problem by reducing it to an instance of the same problem with smaller input

Examples See example 1-6 of Section 4.4 on pages

311-314procedure factorial (n: nonnegative integer)

if n = 0 then factorial (n) := 1else factorial (n) := nfactorial (n-1)

procedure gcd (a, b: nonnegative integers with a<b)if a = 0 then gcd (a, b) := belse gcd (a, b) := gcd (b mod a, a)

Proving Recursive Algorithms

Applies proof by induction either mathematical or strong

induction Two steps

Basis step: prove that P(1) is true Inductive step: assume inductive

hypothesis P(k) is ture and prove that P(k+1) is also true

ExampleProve that Algorithm 2, which computes powers of realnumbers, is correct

Basis Step: if n = 0, power(a, 0) = 1. This is correct.Inductive Step: the inductive hypothesis is power(a,k) = ak for a 0for nonnegative integer k. We have to show that power(a,k+1) = ak+1.

Since power (a, k+1) = apower (a, k) = a ak a= ak+1. This complete the inductive step.

Algorithm 2procedure power (a: nonzero real numbers, n: nonnegative integers with a<b) if n = 0 then power (a, n) := 1 else power (a, n) := apower (a, n-1)

Iterative Approach

Iterative approach is an approach to evaluate a recursively defined sequence that often requires much less computation than recursive.

By setting the value at the base case of the function and successively apply the recursive definition

Exampleprocedure fibonacci (n: nonnegative integer)

if n = 0 then fibonacci (0) := 0else if n = 1 then factorial (1) := 1else fibonacci (n) := fibonacci (n-1) + fibonacci (n-2)

procedure iterative fibonacci (n: nonnegative integer)if n = 0 then y := 0elsebegin

x := 0y := 0for i := to n-1begin

z := x+yx := yy := z

endend

{y is the nth fibonacci number}

Merge Sortprocedure mergesort (L: a1, a2, …, an)if n > 1 then begin m := n/2 L1 := a1, a2, …, am

L2 := am+1, am+2, …, an

L := merge ( mergesort (L1), mergesort (L2) )end{L is now sorted into elements in nondecreasing order}

121

Program Correctness

Program Correctness How to ensure that a program is correct?

Test with some data Program verification

Tests with data are subjective to data What about other untested data?

Program verification = proof of correctness of programs

Give strong assurance Independent to data

Program Correctness A program is correct if it produces the corre

ct output for every required input

program

Initial State Final State

• x is greater than 0• x > 0

• y is the square root of x• y = x

Program Correctness Program correctness =

Program specification + Proof methods (verification)

Program specification Describe program effects Describe Input and output Use mathematical precise notation

Program VerificationDefinition 1:A program, or program segment, S is said to be partially correct withrespect to the initial assertion p and the final assertion q if whenever p is true for the input values of S and S terminates, then q is true for the output values of S. The notation p{S}q indicates that the program, or program segment, S is partially correct with respect to the initial assertion p and final assertion q.

Initial and Final Assertions Initial Assertion

The properties that the input values must have

Final Assertion The properties that the output values

of the program should have Hoare triple notation

p{S}q

Program Specification Description about input and output = assertion

Logical formula – two propositions Initial assertion and final assertion

Program specification focuses on properties Not describe procedures to compute output from input

Program specification is more precise than informal specification eg. English language

Flow charts, Diagrams

Proof Methods Proof methods = inference rules about input an

d output of each program statement

To prove that a program will produce desired output by reading some specified input

Program Correctness Logic We focus on imperative programming languages

allows destructive assignment, eg. x := y Like C, C++, Java, etc.

Program specification is due to Hoare triples Proof method is due to Floyd-Hoare logic We normally split program into series of subprograms

and prove that each section is true then we show that the program is correct using rules of inference.

Example

Show that this program segment is correct with respect to the initial assertion p: x = 1 andthe final assertion q: z = 3

SolutionSuppose that p is true then z equals to 3. Hence, p{S}q is true which means this program is correct.

y := 2z := x +y

Rules of Inference Composition Rule

we write S = S1;S2 to show that a program S is made up of subprogram S1 followed by S2.

Suppose that p{S1}q is ture and q{S2}r is true if p is true and S = {S1;S2} is executed and terminates,

then r is true. p{S1}q q{S2}rp{S1;S2

}r

Rules of inference Conditional Statements

(p condition){S}q (p condition) qp{if condition then S}q

(p condition){S1}q (p condition){S2}q p{if condition then S1 else S2}q

If condition then S

If condition then S1

else S2

Rules of inference

Loop Invariants An assertion that remains true each time

S is executed must be chosen and such assertion is called loop invariant.

(p condition){S}pp{while condition S}( condition p)

while condition S

ExampleVerify that the program segment is correct with respect to the initial assertion T and the final assertion abs = |x|

Solution We have to show two things- If the initial assertion is true and x < 0, then abs = |x|- If the initial assertion is true and x >0, then abs = xThese are true, thus this program segment is correct

If x < 0 then abs := -xelse abs := x

Excercise

Prove that this program is correct.

procedure multiply (m, n: integers)if n < 0 then a := -nelse a := nk := 0x := 0

while k < abegin x := x + m k := k + 1end

if n < 0 then product := -xelse product := x

S1

S2

S3

S4

Homework 4 Section 4.1

3, 6, 10, 16, 23, 32, 48, 49, 51, 78 Section 4.2

4, 9, 16, 29, 39 Section 4.3

4, 6, 8, 24, 25, 58 Section 4.4

4, 19, 34, 42, 44 Section 4.5

1, 4, 7 Supplementary

4, 14, 27, 37, 47, 51

136