discrete mathematics

23
Discrete Mathematics Chapter 4 Induction and Recursion 大大大大 大大大大大 大大大 (Lingling Huang)

Upload: jordan-david

Post on 03-Jan-2016

28 views

Category:

Documents


1 download

DESCRIPTION

Discrete Mathematics. Chapter 4 Induction and Recursion. 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang). 4.1 Mathematical Induction( 數學歸納法 ). Note : Mathematical induction can be used only to prove results obtained in some other way. It is - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Discrete Mathematics

Discrete Mathematics

Chapter 4 Induction and Recursion

大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Page 2: Discrete Mathematics

Ch4-2

4.1 Mathematical Induction( 數學歸納法 )

Note : Mathematical induction can be used only to

prove results obtained in some other way. It is

not a tool for discovering formulae or theorems.

(p.265)

P(n) : a propositional function (e.g. n 2≦ n) A proof by mathematical induction (MI) that P(n) is true for every nZ+ consists of two steps : 1. Basis step : The proposition P(1) is shown to be true.( 若 n 從 0 開始則證 P(0) 為真 ) 2. Inductive step : the implication P(k) → P(k+1) is shown to be true for every kZ+

Page 3: Discrete Mathematics

Ch4-3

Example 2. Use MI to prove that the sum of the first n odd positive integers is n2.

Note. 不用 MI 就可以得証 :

Pf : Let P(n) denote the proposition that Basis step : P(1) is true , since 1=12

Inductive step : Suppose that P(k) is true for a positive integer k, i.e., 1+3+5+…+(2k1)=k2

Note that 1+3+5+…+(2k1)+(2k+1) = k2+2k+1= (k+1)2

∴ P(k+1) is true By induction, P(n) is true for all nZ+

2

11

)1(2)12( nnnnniin

i

n

i

2

1

)12( nin

i

Page 4: Discrete Mathematics

Ch4-4

Example 5. Use MI to prove the inequality n < 2n for all nZ+

pf : Let P(n) be the proposition “ n < 2n ”. Basis step : P(1) is true since 1 < 21 . Inductive step : Assume that P(k) is true for a positive integer k,

i.e., k < 2k.

Consider P(k+1) : k + 1 < 2k + 1 2k + 2k =2k + 1 ∴ P(k+1) is true. By MI, P(n) is true for all nZ+.

Page 5: Discrete Mathematics

Ch4-5

kH k

1...

3

1

2

11

21

2

nH n

Example 7. The harmonic numbers Hk, k =1,2,3,…, are

defined by . Use MI to show that

Pf : Let P(n) be the proposition that “ ”.

Basis step : P(0) is true, since .

Inductive step : Assume that P(k) is true for some k,

i.e.,

Consider P(k+1) :

whenever n is a nonnegative integer.

2/12

nH n

2/011120 HH

2/12

kH k

Page 6: Discrete Mathematics

Ch4-6

∴P(k+1) is true. By MI, P(n) is true for all nZ+.

12 2

1

22

1

12

1

2

1

3

1

2

111

kkkkkH

12 2

1

22

1

12

1

kkkkH

12

1

22

1

12

1)

21(

kkk

k

kkkkkk

k

22

1

22

1

22

1)

21(

kk

kk

22

2)

21(

2

11

k

Exercise : 7, 13

Page 7: Discrete Mathematics

Ch4-7

4.2 Strong Induction( 強數學歸納法 )

Basis step 相同 Inductive step : Assume all the statements P(1),

P(2), …, P(k) are true. Show that P(k+1) is also true.

Page 8: Discrete Mathematics

Ch4-8

Example 2. Show that if nZ and n >1, then n can be written as the product of primes.Pf : Let P(n) be the proposition that n can be written as the product of primes. Basis : P(2) is true, since 2 is a prime number Inductive : Assume P(2), P(3), …, P(k) are true. Consider P(k + 1) : Case 1 : k + 1 is prime P(k+1) is true Case 2 : k + 1 is composite, i.e., k + 1 = ab where 2 a b < k+1 By the induction hypothesis, both a and b can be written as the product of primes. P(k+1) is true. By Strong MI, P(k) is true if kZ and k >1.

Note: 此題無法僅用 MI 證

Page 9: Discrete Mathematics

Ch4-9

Example 4. Prove that every amount of postage of 12 cents or more can be formed using just 4-cent and 5-cent stamps.Pf : Let P(n) be the statement that the postage of n cents can formed using just 4-cent and 5-cent stamps. Basis : P(12) is true, since 12 = 4 3; P(13) is true, since 13 = 4 2 + 5 1; P(14) is true, since 14 = 4 1 + 5 2; P(15) is true, since 15 = 5 3; Inductive : Assume P(12), P(13), …, P(k) are true. Consider P(k+1) : Suppose k3 = 4 m + 5 n. Then k+1 = 4 (m1) + 5 n. P(k+1) is true. By Strong MI, P(n) is true if nZ and n 12.

Exercise : 7

(k3 12)

Page 10: Discrete Mathematics

Ch4-10

4.3 Recursive Definitions. Def. The process of defining an object in terms of itself

is called recursion( 遞迴 ).

e.g. We specify the terms of a sequence using

(1) an explicit formula:

an=2n, n=0,1,2,…

(2) a recursive form:

a0=1,

an+1=2an , n=0,1,2,…

Example 1. Suppose that f is defined recursively by f(0)=3 , f(n+1)=2f(n)+3 Find f(1), f(2), f(3), f(4).

Page 11: Discrete Mathematics

Ch4-11

Example 2. Give an inductive (recursive) definition of the factorial function F(n) = n!.

Sol :

initial value : F(0) = 1

recursive form : F(n+1) = (n+1)! = n! (n+1)

= F(n) (n+1)

Def1, Example 5. The Fibonacci numbers f0, f1, f2…,are defined by : f0 = 0 , f1 = 1 , fn = fn1 + fn2 , for n = 2,3,4,… what is f4 ?Sol : f4 = f3 + f2 = (f2 + f1) + (f1 + f0) = f2 + 2 = (f1 + f0) + 2 = 3

Page 12: Discrete Mathematics

Ch4-12

Example 6. Show that fn > n2 , where 32

51

n ,

Pf: ( By Strong MI ) Let P(n) be the statement fn >n2 . Basis: f3 = 2 > so that P(3) and P(4) are true. Inductive: Assume that P(3), P(4), …, P(n) are true. We must show that P(n+1) is true. fn+1 = fn + fn1 > n2 + n3 = n3( +1) ∵ +1= 2

∴ fn+1 > n3 2 = n1

We get that P(n+1) is true. By Strong MI , P(n) is true for all n 3

2

533 2

4

f

Page 13: Discrete Mathematics

Ch4-13

※Recursively defined sets.Example 7. Let S be defined recursively by 3S x+yS if xS and yS. Show that S is the of positive integers divisible by 3 (i.e., S = { 3, 6, 9, 12, 15, 18, … }Pf: Let A be the set of all positive integers divisible by 3. We need to prove that A=S. (i) A S : (By MI) Let P(n) be the statement that 3nS … (ii) S A : ( 利用 S 的定義 ) (1) 3 A , (2) if xA,yA, then 3|x and 3|y. 3|(x+y) x+yA ∴S A

S = A

Page 14: Discrete Mathematics

Ch4-14

Definition 2. The set of strings over an alphabet is denoted by *. The empty string is denoted by , , and wx* whenever w* and x.eg. = { a, b, c }

* = { , a , b , c , aa , ab , ac , ba , bb , bc, … abcabccba, …}

Example 9. Give a recursive definition of l(w), the length of the string w*

Sol :

initial value : l()=0

recursive def : l(wx)=l(w)+1 if w*, x.

a b c

Page 15: Discrete Mathematics

Ch4-15

Exercise 3, 7, 13, 48, 49

Exercise 39. When does a string belong to the set A of bit strings defined recursively by

A

0x1A if xA.Sol :

A={, 01 , 0011, 000111, …}

∴ 當 bit string = 000…011…1 時 A n 個 n 個

01

Page 16: Discrete Mathematics

Ch4-16

Ackermann’s function

A(m, n) = 2n if m = 0

0 if m 1 and n = 0

2 if m 1 and n = 1

A(m1, A(m, n1)) if m 1 and n 2

Exercise 49 Show that A(m,2)=4 whenever m 1

Pf :

A(m,2) = A(m1, A(m,1)) = A(m1,2) whenever m 1.

A(m,2) = A(m1,2) = A(m2,2) = … = A(0,2) = 4.

Page 17: Discrete Mathematics

Ch4-17

4.4 Recursive algorithms. ※ Sometimes we can reduce the solution to a problem with a particular set of input to the solution of the same problem with smaller input values.

eg. gcd(a,b) = gcd(b mod a, a) (when a < b)

Def 1. An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input.

Page 18: Discrete Mathematics

Ch4-18

Example 2. Give a recursive algorithm for computing an, where aR \ {0}, nN.

Sol : recursive definition of an : initial value : a0=1 recursive def : an = a an1.

Algorithm 2. Procedure power( a : nonzero real number, n : nonnegative integer ) if n = 0 then power(a, n):=1 else power(a, n):= a * power(a, n1).

Page 19: Discrete Mathematics

Ch4-19

Example 4. Find gcd(a,b) with 0a<bSol :

Algorithm 4. procedure gcd(a,b : nonnegative integers with a<b) if a=0 then gcd(a,b) := b else gcd(a,b) := gcd(b mod a, a).

Example 5. Search x in a1, a2,…,an by Linear SearchSol : Alg. 5

procedure search (i, j, x: integers) if ai = x then location := i else if i = j then location := 0 else search(i+1, j, x)

從 ai,ai+1,…aj 中找 x

callsearch(1, n, x)

Page 20: Discrete Mathematics

Ch4-20

Example 6. Search x from a1,a2,…,an by binary search (recursive version).

Sol : Alg. 5 procedure binary_search (x , i , j: integers) m := (i+j) / 2 if x = am then location := m else if (x < am and i < m) then binary_search(x, i, m1) else if (x > am and j > m) then binary_search(x, m+1, j) else location := 0

call binary_search(x, 1, n)

search x from ai, ai+1, …, aj

表示左半邊 ai, ai+1, …, am1

至少還有一個元素

Page 21: Discrete Mathematics

Ch4-21

Example 1. Give the value of n!, nZ+

Sol :Note : n! = n (n1)!

Alg. 1 (Recursive Procedure) procedure factorial (n: positive integer) if n = 1 then factorial (n) := 1 else factorial (n) := n factorial (n1)

Alg. (Iterative Procedure) procedure iterative_factorial (n : positive integer) x := 1 for i := 1 to n x := i x { x = n! }

Page 22: Discrete Mathematics

Ch4-22

※ iterative alg. 的計算次數通常比 recursive alg.少

※ Find Fibonacci numbers

(Note : f0=0, f1=1, fn=fn1+fn2 for n2)

Alg. 7 (Recursive Fibonacci) procedure Fibonacci (n : nonnegative integer) if n = 0 then Fibonacci (0) := 0 else if n = 1 then Fibonacci (1) := 1 else Fibonacci (n) := Fibonacci (n1)+Fibonacci (n2)

Page 23: Discrete Mathematics

Ch4-23

Alg.8 (Iterative Fibonacci) procedure iterative_fibonacci (n: nonnegative integer) if n = 0 then y := 0 // y = f0

else begin x := 0 y := 1 // y = f1

for i := 1 to n1 begin z := x + y x := y y := z end end {y is fn }

Exercise : 11, 35

i = 1 i = 2 i = 3

z f2 f3 f4

x f1 f2 f3

y f2 f3 f4