Discrete Mathematics Chapter 4 InductionRecursion Ch 4 3

• Slides: 35

Discrete Mathematics Chapter 4 歸納(Induction)與遞迴(Recursion) 大葉大學 資訊 程系 黃鈴玲

Ch 4 -3

(跳過) Example 7. The harmonic numbers Hk, k =1, 2, 3, …, are defined by . Use MI to show that whenever n is a nonnegative integer. 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) : Ch 4 -9

(跳過) ∴P(k+1) is true. By MI, P(n) is true for all n Z+. Exercise : 7, 13 Ch 4 -10

(跳過) 4. 2 Strong Induction(強數學歸納法) n n Basis step 相同 Inductive step : Assume all the statements P(1), P(2), …, P(k) are true. Show that P(k+1) is also true. Ch 4 -11

(跳過) 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 k-3 = 4 m + 5 n. (k-3 12) Then k+1 = 4 (m+1) + 5 n. P(k+1) is true. By Strong MI, P(n) is true if n Z and n 12. Exercise : 7 Ch 4 -13

(跳過) Example 6. Show that fn > a n-2 , where Pf: ( By Strong MI ) Let P(n) be the statement fn >a n-2. Basis: f 3 = 2 > a 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 + fn-1 > a n-2 + a n-3 = a n-3(a +1) ∵ a +1= a 2 ∴ fn+1 > a n-3 a 2 = a n-1 We get that P(n+1) is true. By Strong MI , P(n) is true for all n 3 Ch 4 -18

(跳過) ※Recursively defined sets. Example 7. Let S be defined recursively by 3 S x+y S if x S and y S. 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 3 n S … (ii) S A : (利用S的定義) S=A (1) 3 A , (2) if x A, y A, then 3|x and 3|y. 3|(x+y) x+y A ∴S A Ch 4 -19

(跳過) Definition 2. The set of strings over an alphabet is denoted by *. The empty string is denoted by l, l , and wx * whenever w * and x . eg. = { a, b, c } la lb lc * = { l, 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(l)=0 recursive def : l(wx)=l(w)+1 if w *, x . Ch 4 -20

(跳過) Exercise 13, 48, 49 Exercise 39. When does a string belong to the set A of bit strings defined recursively by l A 0 x 1 A if x A. Sol : A={l, 01 , 0011, 000111, …} 0 l 1 ∴當bit string a = 000… 011… 1 時 a A n個 n個 Ch 4 -21

(跳過) n Ackermann’s function A(m, n) = 2 n 0 2 A(m-1, A(m, n-1)) if m = 0 if m 1 and n = 1 if m 1 and n 2 Exercise 49 Show that A(m, 2)=4 whenever m 1 Pf : A(m, 2) = A(m-1, A(m, 1)) = A(m-1, 2) whenever m 1. A(m, 2) = A(m-1, 2) = A(m-2, 2) = … = A(0, 2) = 4. Ch 4 -22

Example 1. 求出計算 n!的遞迴演算法, 其中 n為 非負整數. Sol : n! 的遞迴定義: 初始值: 0! =1 遞迴式: n! = n (n-1)!. 要求出 4! 時 3! = 3 2!, 2! = 2 1!, 1! = 1 0!, : 4! 0! == 4 3!, 1, 代回得到 1! = 1, 2! = 2, 3! = 6, 4! = 24 ∴ Algorithm 1. Procedure factorial(n : nonnegative integer) if n = 0 then factorial(n): =1 else factorial(n): = n factorial(n-1) Ch 4 -24

Algorithm 1. Procedure factorial(n : nonnegative integer) if n = 0 then factorial(n): =1 else factorial(n): = n factorial(n-1) 遞迴呼叫自己 也可改寫為: Algorithm 1. Procedure factorial(n : nonnegative integer) if n = 0 then return(1) else return(n factorial(n-1)) Ch 4 -25

Example 2. 求出計算 an, 的遞迴演算法, 其中 a為實數，n為非負整數。 Sol : an的遞迴定義 : 初始值: a 0=1 遞迴式: an = a an-1. ∴ Algorithm 2. Procedure power(a : number, n : nonnegative integer) if n = 0 then power(a, n): =1 else power(a, n): = a power(a, n-1). Ch 4 -27

Exercise 4. 4 補充: 根據演算法 2的步驟，以a=2, n=4為輸入， 詳細寫出求出 24的值之過程。 Algorithm 2. Procedure power(a : number, n : nonnegative integer) if n = 0 then power(a, n): =1 else power(a, n): = a power(a, n-1). Ch 4 -28

Example 4. 寫出求 gcd(a, b) 的遞迴演算法，其中 a<b. Sol : 遞迴式: gcd(a, b) : = gcd(b mod a, a) 初始值: gcd(0, b) : = b 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). Ch 4 -29

Exercise 4. 4 6. 根據演算法 4的步驟，求出gcd(8, 13)的值。 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). 29. 設計一個遞迴演算法來找出數列的第n項，其中 a 0=1，a 1=2，而an= an-1 an-2，n=2, 3, …。 Ch 4 -30

(跳過) Example 5. Search x in a 1, a 2, …, an by Linear Search Sol : 從ai, ai+1, …aj 中找 x Alg. 5 procedure search (i, j, x: integers) if ai = x then location : = i else if i = j then location : = 0 call else search(i+1, j, x) search(1, n, x) Ch 4 -31

(跳過) Example 6. Search x from a 1, a 2, …, an by binary search (recursive version). search x from ai, ai+1, …, aj Sol : Alg. 5 procedure binary_search (x , i , j: integers) m : = (i+j) / 2 表示左半邊 if x = am then location : = m ai, ai+1, …, am-1 else if (x < am and i < m) then 至少還有一個元素 binary_search(x, i, m-1) else if (x > am and j > m) then binary_search(x, m+1, j) else location : = 0 call binary_search(x, 1, n) Ch 4 -32

(跳過) Example 1. Give the value of n!, n Z+ Sol : Note : n! = n (n-1)! Alg. 1 (Recursive Procedure) procedure factorial (n: positive integer) if n = 1 then factorial (n) : = 1 else factorial (n) : = n factorial (n-1) Alg. (Iterative Procedure) procedure iterative_factorial (n : positive integer) x : = 1 for i : = 1 to n x : = i x { x = n! } Ch 4 -33

(跳過) ※ iterative alg. 的計算次數通常比 recursive alg. 少 ※ Find Fibonacci numbers (Note : f 0=0, f 1=1, fn=fn-1+fn-2 for n 2) 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 (n-1)+Fibonacci (n-2) Ch 4 -34

(跳過) Alg. 8 (Iterative Fibonacci) procedure iterative_fibonacci (n: nonnegative integer) if n = 0 then y : = 0 // y = f 0 else begin x : = 0 y : = 1 // y = f 1 for i : = 1 to n-1 begin i=1 i=2 i=3 z : = x + y z f 2 f 3 f 4 x : = y y : = z x f 1 f 2 f 3 end y f 2 f 3 f 4 end {y is fn } Exercise : 11, 35 Ch 4 -35