Discrete
Mathematics
Chapter 3
Mathematical Reasoning, Induction,
and Recursion
大葉大學 資訊工程系 黃鈴玲
3.2 Sequences and Summations
※Sequence (數列)
Def 1. A sequence is a function f from A  Z+
(or A  N) to a set S. We use an to denote f(n),
and call an a term (項) of the sequence.
Example 1. {an} , where an = 1/n , n  Z+
 a1 =1, a2 =1/2 , a3 =1/3, …
Example 2. {bn} , where bn= (-1)n, n  N
 b0 = 1, b1 = -1 , b2 = 1, …
3.2.1
Special Integer Sequence
Example 6. What is a rule that can produce the term of
a sequence if the first 10 terms are
1, 2, 2, 3, 3, 3, 4, 4, 4, 4?
Sol :
規則:數字 i 出現 i 次
數字 i 出現之前共有 1+2+3…….+(i -1) = i(i -1)/2 項
ai(i-1)/2 +1= ai(i-1)/2 +2 =… = ai(i+1)/2 = i
3.2.2
A common problem in discrete mathematics is
finding a formula for constructing the term of a sequence.
方法:找出 ai  ai+1的變化;加減某數或者乘除某數?
Example 7. How can we produce the terms of a
sequence if the first 10 terms are
5, 11, 17, 23, 29, 35,41, 47, 53, 59?
Sol :
a1 = 5
a2 =11 = 5 + 6
a3 =17 = 11 + 6 = 5 + 6  2
:
:
 an= 5 + 6  (n-1) = 6n-1
3.2.3
Example 8. Conjecture a simple formula for an if
the first 10 terms of the sequence {an} are
1, 7, 25, 79, 241, 727, 2185, 6559, 19681,59047?
Sol:
顯然非等差數列
後項除以前項的值接近3
 猜測數列為 3n  …
比較:
{3n} : 3, 9, 27, 81, 243, 729, 2187,…
{an} : 1, 7, 25, 79, 241, 727, 2185,…
 an = 3n - 2 , n  1
3.2.4
 Summations
Here, the variable j is call the index of summation,
m is the lower limit, and n is the upper limit.
3.2.5
n
m
m
n
m
j
j a
a
a
a 


 

 
1
Example 10.
Example 13. (Double summation)
55
25
16
9
4
1
5
1
2








j
j
60
)
4
3
2
1
(
6
6
)
3
2
(
4
1
4
1
4
1
3
1








 

 

  i
i
i j
i
i
i
i
ij
Example 14.
Table 2. Some useful summation formulae
3.2.6
6
4
2
0
}
4
,
2
,
0
{






S
S
1
,
1
)
1
(
)
1
(
1
0

-
-



 r
r
r
a
ar
n
n
k
k
2
)
1
(
)
2
(
1




n
n
k
n
k
6
)
1
2
)(
1
(
)
3
(
1
2 




n
n
n
k
n
k
Cardinality
Def 4. The sets A and B have the same cardinality
(size) if and only if there is a one-to-one
correspondence (1-1,onto 的function) from A to B.
Def 5. A set that is either finite or has the same
cardinality as Z+ (or N) is called countable (可數).
A set that is not countable is called uncountable.
3.2.7
3.2.8
Example 18. Show that the set of odd positive
integers is a countable set.
Pf: (Figure 1)
Z+ : 1 2 3 4 5 6 7 8 …
……
{ 正奇數 } : 1 3 5 7 9 11 13 15 …
f : Z+  {正奇數}
f (n) = 2n – 1 is 1-1 & onto.
Example 19. Show that the set of positive rational number (Q+)
is countable.
...
,
,
,
,
,
,
,
,
1
4
2
3
3
2
4
1
3
1
1
3
1
2
2
1
1
1
∴ Z+ : 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 …
Q+ :
(注意,因 等於 ,故 不算)
※Note. R is uncountable. (Example 20)
Exercise :
9,13,17,38
3.2.9
Pf: Q+ = { a / b | a, b Z+ }
5
1

2
2
1
1
2
2
(Figure 2)
1
2
1
3
1
4
1
5
1
1

2
1
2
2
2
3
2
4
2
5

3
2
3
1
3
3
3
4
3
5

4
2
4
1


3.3 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.239)
P(n) : a propositional function (e.g. n ≦ 2n)
A proof by mathematical induction (MI) that P(n) is
true for every nZ+ 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 kZ+
3.3.1
Example 1. 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+…+(2k-1)=k2
Note that 1+3+5+…+(2k-1)+(2k+1) = k2+2k+1= (k+1)2
∴ P(k+1) is true
By induction, P(n) is true for all nZ+
2
1
1
)
1
(
2
)
1
2
( n
n
n
n
n
i
i
n
i
n
i

 


-


-

-
2
1
)
1
2
( n
i
n
i



-
3.3.2
Example 2. Use MI to prove the inequality
n<2n for all nZ+
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 nZ+.
3.3.3
k
Hk
1
...
3
1
2
1
1 




2
1
2
n
H n 

Example 6. 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) :
3.3.4
whenever n is a nonnegative integer.
2
/
1
2
n
H n 

2
/
0
1
1
1
20 


 H
H
2
/
1
2
k
H k 

∴P(k+1) is true.
By MI, P(n) is true for all nZ+.
3.3.5
1
2
2
1
2
2
1
1
2
1
2
1
3
1
2
1
1
1













k
k
k
k
k
H 

1
2
2
1
2
2
1
1
2
1







 k
k
k
k
H 
1
2
1
2
2
1
1
2
1
)
2
1
( 







 k
k
k
k

k
k
k
k
k
k
k
2
2
1
2
2
1
2
2
1
)
2
1
(








 
k
k
k
k
2
2
2
)
2
1
(




2
1
1



k
※The 2nd principle of mathematical induction:
( 又稱為強數學歸納法 strong induction)
 Basis step 相同
 Inductive step : Assume P(k) is true for all k  n
Show that P(n+1) is also true.
3.3.6
Example 14. Show that if nZ 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(k) is true for all k  n.
Consider P(n+1) :
Case 1 : n+1 is prime  P(n+1) is true
Case 2 : n+1 is composite,
i.e., n+1=ab where 2  a  b < n+1
By the induction hypothesis, both a and b can be
written as the product of primes.
 P(n+1) is true.
By 2nd MI, P(n) is true if nZ and n >1.
Note: 此題無法用 1st MI 證 Exercise : 3,11,17
3.3.7
3.4 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).
3.4.1
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)
Example 5. The Fibonacci numbers f0, f1, f2…,are
defined by : f0 = 0 ,
f1 = 1 ,
fn = fn-1 + fn-2 , for n = 2,3,4,…
what is f4 ?
Sol :
f4 = f3 + f2 = (f2 + f1) + (f1 + f0) = f2 + 2
= (f1 + f0) + 2 = 3
3.4.2
Example 6. Show that fn > a n-2 , where 3
2
5
1


 n
,
a
Pf: ( By 2nd MI )
Let P(n) be the statement fn >a n-2 .
Basis: f3 = 2 > a
so that P(3) and P(4) are true.
Inductive: Assume that P(k) is true, 3 k  n, n  4.
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 2nd MI , P(n) is true for all n  3
2
5
3
3 2
4



 a
f
3.4.3
※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 3nS
…
(ii) S  A : (利用S的定義)
(1) 3  A ,
(2) if xA,yA, then 3|x and 3|y.
 3|(x+y)  x+yA
∴S  A
S = A
3.4.4
Example 8. The set of strings over an alphabet 
is denoted by *. The empty string is denoted
by l, and wx* whenever w* and x.
eg.  = { a, b, c }
* = { 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.
la lb lc
3.4.5
Exercise 3,13, 25, 49
Exercise 39. When does a string belong to the
set A of bit strings defined recursively by
lA
0x1A if xA.
Sol :
A={l, 01 , 0011, 000111, …}
∴當bit string a = 000…011…1 時
aA n個 n個
0l1
3.4.6
 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(m-1, A(m, 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.
3.4.7
3.5 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.
3.5.1
Example 1. Give a recursive algorithm for
computing an, where aR  {0}, nN.
Sol :
recursive definition of an :
initial value : a0=1
recursive def : an = a  an-1.
Algorithm 1.
Procedure power( a : nonzero real number,
n : nonnegative integer )
if n = 0 then power(a, n):=1
else power(a, n):= a * power(a, n-1).
∴
3.5.2
Example 4. Find gcd(a,b) with 0a<b
Sol :
Algorithm 3.
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 Search
Sol : Alg. 4
procedure search (i, j, x)
if ai = x then location := i
else if i = j then location := 0
else search(i+1, j, x)
從ai,ai+1,…aj 中找 x
call
search(1, n, x)
3.5.3
Example 6. Search x from a1,a2,…,an by binary
search.
Sol : Alg. 5
procedure binary_search (x , i , j)
m := (i+j) / 2
if x = am then location := m
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)
search x from ai, ai+1, …, aj
3.5.4
表示左半邊
ai, ai+1, …, am-1
至少還有一個元素
Example 7. Give the value of n!, nZ+
Sol :
Note : n! = n  (n-1)!
Alg. 6 (Recursive Procedure)
procedure factorial (n: positive integer)
if n = 1 then factorial (n) := 1
else factorial (n) := n  factorial (n-1)
Alg. 7 (Iterative Procedure)
procedure iterative_factorial (n : positive integer)
x := 1
for i := 1 to n
x := i  x
{ x = n! }
3.5.5
※ iterative alg. 的計算次數通常比 recursive alg.少
※ Find Fibonacci numbers
(Note : f0=0, f1=1, fn=fn-1+fn-2 for n2)
Alg. 8 (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)
3.5.6
Alg.9 (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 n-1
begin
z := x + y
x := y
y := z
end
end
{y is fn }
Exercise : 5 , 27
i = 1 i = 2 i = 3
z f2 f3 f4
x f1 f2 f3
y f2 f3 f4
3.5.7

ch3.ppt

  • 1.
    Discrete Mathematics Chapter 3 Mathematical Reasoning,Induction, and Recursion 大葉大學 資訊工程系 黃鈴玲
  • 2.
    3.2 Sequences andSummations ※Sequence (數列) Def 1. A sequence is a function f from A  Z+ (or A  N) to a set S. We use an to denote f(n), and call an a term (項) of the sequence. Example 1. {an} , where an = 1/n , n  Z+  a1 =1, a2 =1/2 , a3 =1/3, … Example 2. {bn} , where bn= (-1)n, n  N  b0 = 1, b1 = -1 , b2 = 1, … 3.2.1
  • 3.
    Special Integer Sequence Example6. What is a rule that can produce the term of a sequence if the first 10 terms are 1, 2, 2, 3, 3, 3, 4, 4, 4, 4? Sol : 規則:數字 i 出現 i 次 數字 i 出現之前共有 1+2+3…….+(i -1) = i(i -1)/2 項 ai(i-1)/2 +1= ai(i-1)/2 +2 =… = ai(i+1)/2 = i 3.2.2 A common problem in discrete mathematics is finding a formula for constructing the term of a sequence. 方法:找出 ai  ai+1的變化;加減某數或者乘除某數?
  • 4.
    Example 7. Howcan we produce the terms of a sequence if the first 10 terms are 5, 11, 17, 23, 29, 35,41, 47, 53, 59? Sol : a1 = 5 a2 =11 = 5 + 6 a3 =17 = 11 + 6 = 5 + 6  2 : :  an= 5 + 6  (n-1) = 6n-1 3.2.3
  • 5.
    Example 8. Conjecturea simple formula for an if the first 10 terms of the sequence {an} are 1, 7, 25, 79, 241, 727, 2185, 6559, 19681,59047? Sol: 顯然非等差數列 後項除以前項的值接近3  猜測數列為 3n  … 比較: {3n} : 3, 9, 27, 81, 243, 729, 2187,… {an} : 1, 7, 25, 79, 241, 727, 2185,…  an = 3n - 2 , n  1 3.2.4
  • 6.
     Summations Here, thevariable j is call the index of summation, m is the lower limit, and n is the upper limit. 3.2.5 n m m n m j j a a a a         1 Example 10. Example 13. (Double summation) 55 25 16 9 4 1 5 1 2         j j 60 ) 4 3 2 1 ( 6 6 ) 3 2 ( 4 1 4 1 4 1 3 1                 i i i j i i i i ij
  • 7.
    Example 14. Table 2.Some useful summation formulae 3.2.6 6 4 2 0 } 4 , 2 , 0 {       S S 1 , 1 ) 1 ( ) 1 ( 1 0  - -     r r r a ar n n k k 2 ) 1 ( ) 2 ( 1     n n k n k 6 ) 1 2 )( 1 ( ) 3 ( 1 2      n n n k n k
  • 8.
    Cardinality Def 4. Thesets A and B have the same cardinality (size) if and only if there is a one-to-one correspondence (1-1,onto 的function) from A to B. Def 5. A set that is either finite or has the same cardinality as Z+ (or N) is called countable (可數). A set that is not countable is called uncountable. 3.2.7
  • 9.
    3.2.8 Example 18. Showthat the set of odd positive integers is a countable set. Pf: (Figure 1) Z+ : 1 2 3 4 5 6 7 8 … …… { 正奇數 } : 1 3 5 7 9 11 13 15 … f : Z+  {正奇數} f (n) = 2n – 1 is 1-1 & onto.
  • 10.
    Example 19. Showthat the set of positive rational number (Q+) is countable. ... , , , , , , , , 1 4 2 3 3 2 4 1 3 1 1 3 1 2 2 1 1 1 ∴ Z+ : 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 … Q+ : (注意,因 等於 ,故 不算) ※Note. R is uncountable. (Example 20) Exercise : 9,13,17,38 3.2.9 Pf: Q+ = { a / b | a, b Z+ } 5 1  2 2 1 1 2 2 (Figure 2) 1 2 1 3 1 4 1 5 1 1  2 1 2 2 2 3 2 4 2 5  3 2 3 1 3 3 3 4 3 5  4 2 4 1  
  • 11.
    3.3 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.239) P(n) : a propositional function (e.g. n ≦ 2n) A proof by mathematical induction (MI) that P(n) is true for every nZ+ 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 kZ+ 3.3.1
  • 12.
    Example 1. UseMI 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+…+(2k-1)=k2 Note that 1+3+5+…+(2k-1)+(2k+1) = k2+2k+1= (k+1)2 ∴ P(k+1) is true By induction, P(n) is true for all nZ+ 2 1 1 ) 1 ( 2 ) 1 2 ( n n n n n i i n i n i      -   -  - 2 1 ) 1 2 ( n i n i    - 3.3.2
  • 13.
    Example 2. UseMI to prove the inequality n<2n for all nZ+ 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 nZ+. 3.3.3
  • 14.
    k Hk 1 ... 3 1 2 1 1      2 1 2 n H n  Example 6. 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) : 3.3.4 whenever n is a nonnegative integer. 2 / 1 2 n H n   2 / 0 1 1 1 20     H H 2 / 1 2 k H k  
  • 15.
    ∴P(k+1) is true. ByMI, P(n) is true for all nZ+. 3.3.5 1 2 2 1 2 2 1 1 2 1 2 1 3 1 2 1 1 1              k k k k k H   1 2 2 1 2 2 1 1 2 1         k k k k H  1 2 1 2 2 1 1 2 1 ) 2 1 (          k k k k  k k k k k k k 2 2 1 2 2 1 2 2 1 ) 2 1 (           k k k k 2 2 2 ) 2 1 (     2 1 1    k
  • 16.
    ※The 2nd principleof mathematical induction: ( 又稱為強數學歸納法 strong induction)  Basis step 相同  Inductive step : Assume P(k) is true for all k  n Show that P(n+1) is also true. 3.3.6
  • 17.
    Example 14. Showthat if nZ 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(k) is true for all k  n. Consider P(n+1) : Case 1 : n+1 is prime  P(n+1) is true Case 2 : n+1 is composite, i.e., n+1=ab where 2  a  b < n+1 By the induction hypothesis, both a and b can be written as the product of primes.  P(n+1) is true. By 2nd MI, P(n) is true if nZ and n >1. Note: 此題無法用 1st MI 證 Exercise : 3,11,17 3.3.7
  • 18.
    3.4 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). 3.4.1
  • 19.
    Example 2. Givean 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) Example 5. The Fibonacci numbers f0, f1, f2…,are defined by : f0 = 0 , f1 = 1 , fn = fn-1 + fn-2 , for n = 2,3,4,… what is f4 ? Sol : f4 = f3 + f2 = (f2 + f1) + (f1 + f0) = f2 + 2 = (f1 + f0) + 2 = 3 3.4.2
  • 20.
    Example 6. Showthat fn > a n-2 , where 3 2 5 1    n , a Pf: ( By 2nd MI ) Let P(n) be the statement fn >a n-2 . Basis: f3 = 2 > a so that P(3) and P(4) are true. Inductive: Assume that P(k) is true, 3 k  n, n  4. 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 2nd MI , P(n) is true for all n  3 2 5 3 3 2 4     a f 3.4.3
  • 21.
    ※Recursively defined sets. Example7. 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 3nS … (ii) S  A : (利用S的定義) (1) 3  A , (2) if xA,yA, then 3|x and 3|y.  3|(x+y)  x+yA ∴S  A S = A 3.4.4
  • 22.
    Example 8. Theset of strings over an alphabet  is denoted by *. The empty string is denoted by l, and wx* whenever w* and x. eg.  = { a, b, c } * = { 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. la lb lc 3.4.5
  • 23.
    Exercise 3,13, 25,49 Exercise 39. When does a string belong to the set A of bit strings defined recursively by lA 0x1A if xA. Sol : A={l, 01 , 0011, 000111, …} ∴當bit string a = 000…011…1 時 aA n個 n個 0l1 3.4.6
  • 24.
     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(m-1, A(m, 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. 3.4.7
  • 25.
    3.5 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. 3.5.1
  • 26.
    Example 1. Givea recursive algorithm for computing an, where aR {0}, nN. Sol : recursive definition of an : initial value : a0=1 recursive def : an = a  an-1. Algorithm 1. Procedure power( a : nonzero real number, n : nonnegative integer ) if n = 0 then power(a, n):=1 else power(a, n):= a * power(a, n-1). ∴ 3.5.2
  • 27.
    Example 4. Findgcd(a,b) with 0a<b Sol : Algorithm 3. 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 Search Sol : Alg. 4 procedure search (i, j, x) if ai = x then location := i else if i = j then location := 0 else search(i+1, j, x) 從ai,ai+1,…aj 中找 x call search(1, n, x) 3.5.3
  • 28.
    Example 6. Searchx from a1,a2,…,an by binary search. Sol : Alg. 5 procedure binary_search (x , i , j) m := (i+j) / 2 if x = am then location := m 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) search x from ai, ai+1, …, aj 3.5.4 表示左半邊 ai, ai+1, …, am-1 至少還有一個元素
  • 29.
    Example 7. Givethe value of n!, nZ+ Sol : Note : n! = n  (n-1)! Alg. 6 (Recursive Procedure) procedure factorial (n: positive integer) if n = 1 then factorial (n) := 1 else factorial (n) := n  factorial (n-1) Alg. 7 (Iterative Procedure) procedure iterative_factorial (n : positive integer) x := 1 for i := 1 to n x := i  x { x = n! } 3.5.5
  • 30.
    ※ iterative alg.的計算次數通常比 recursive alg.少 ※ Find Fibonacci numbers (Note : f0=0, f1=1, fn=fn-1+fn-2 for n2) Alg. 8 (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) 3.5.6
  • 31.
    Alg.9 (Iterative Fibonacci) procedureiterative_fibonacci (n: nonnegative integer) if n = 0 then y := 0 // y = f0 else begin x := 0 y := 1 // y = f1 for i := 1 to n-1 begin z := x + y x := y y := z end end {y is fn } Exercise : 5 , 27 i = 1 i = 2 i = 3 z f2 f3 f4 x f1 f2 f3 y f2 f3 f4 3.5.7