Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Data Structures 資料結構 Chapter 2 Recursion What’s Recursion? Definition An algorithmic technique where a function, in order to accomplish a task, calls itself with some part of the task. Every recursive solution involves two major parts or cases, the second part having three components. Base case(s): in which the problem is simple enough to be solved directly. 2 What’s Recursion? (cont.) Recursive case(s): A recursive case has three components. Divide: the problem into one or more simpler or smaller parts of the problem. Call: the function (recursively) on each part. Combine: the solutions of the parts into a solution for the problem. 3 「遞迴」(Recursive)是程式設計的一 個重要觀念。 「遞迴函數」(Recursive Functions) 可以讓函數的程式碼變的很簡潔,但是 設計這類函數需要很小心,不然很容易 就掉入類似無窮迴圈的陷阱。 遞迴的觀念主要是在建立遞迴函數,其 基本定義,如下所示: 一個問題的內涵是由本身所定義的話, 稱之為遞迴。 遞迴函數是由上而下分析方法的一種特殊 的情況,因為子問題本身和原來問題擁有 相同的特性,只是範圍改變,範圍逐漸縮 小到一個終止條件 遞迴函數的特性,如下所示: 遞迴函數在每次呼叫時,都可以使問題範圍逐 漸縮小。 函數需要擁有一個終止條件,以便結束遞迴函 數的執行,否則遞迴函數並不會結束,而是持 續的呼叫自已。 2-1 Factorial - A Case Study 0! 1 N! 1 2 3 ... N 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 factor(n) prod ← 1 for i ← 1 to n prod ← prod * I return prod Iterative algorithm 6 Factorial Function (cont.) 0! 1 N! 1 2 3 ... N 0! = 1 1! = 1 * 0! 2! = 2 * 1! 3! = 3 * 2! 4! = 4 * 3! Recursiv e algorithm factor(n) IF (n = 0) THEN return 1 ELSE return n * factor(n-1) 7 Factorial Function (cont.) 1 2 3 4 5 6 Before we can evaluate n!, we must first evaluate (n-1)!. 5!=5*4! 4!=4*3! 3!=3*2! 2!=2*1! 1!=1*0! 0!=1 8 2-1 Factorial - A Case Study 請參考課本 9 2-2 Designing Recursive Algorithms The Design Methodology Limitation of Recusion Design Implemenation 16 Properties of Recursive Definitions 補充 or Algorithms One important requirement for a recursive algorithm is that it can not generate an infinite sequence of calls on itself. There must be a “way out” of the sequence of recursive calls. 說明 17 Properties of Recursive Definitions 補充 or Algorithms (cont.) Without such a nonrecursive exit, no recursive function can ever be computed. A recursive definition must eventually reduce to some manipulation of one or more simple, nonrecursive cases. 說明 18 2-3 Recursive Examples Multiplication of Natural Numbers Greatest Common Divisor Fiboncci Numbers The Towers of Honoi 19 Multiplication of Natural Numbers 補充 說明 a*b=a+a+a+a+a+a+a+a………..+a b times multi(a, b) prod ← 0 for i ← 1 to b prod ← prod + a return prod Recursive multi(a, b) IF (b = 1) THEN return a ELSE Iterative return multi(a,(b-1))+a 20 Fibonacci Sequence Fibonacci sequence is the sequence of integers: 0,1,1,2,3,5,8,13,21,34,… Each number is the sum of the preceding two numbers. 0+1=1, 1+1=2, 1+2=3, 2+3=5,… 21 Fibonacci Sequence (cont.) This famous sequence was published in 1202 by Leonardo Pisano, who is sometimes called Leonardo Fibonacci. His book contains the following exercise: How many pairs of rabbits can be produced from a single pair in a year’s time? 22 Fibonacci Sequence (cont.) To solve this problem, we are told to assume that each pair produces a new pair of offspring every month, ant that each new pair becomes fertile at the age of one month. Furthermore, the rabbits never die. 23 Fibonacci Sequence (cont.) fib(n) IF (n=0 or n=1) THEN return n ELSE return fib(n-1) + fib(n-2) 24 Fibonacci Sequence (cont.) fib(4) = fib(3) + fib(2) = fib(2) + fib(1) + fib(1) + fib(0) = fib(1) + fib(0) + fib(1) + fib(1) + fib(0) =1+0+1+1+0 =3 25 (Continued) 求最大公因數GCD 補充 說明 1. 判斷兩數中哪一個比較大,大的當被除數,小的當除數 2. 把被除數除以除數,得到商及餘數 3. 若餘數等於零則 GCD = 小數 否則 令小數成為新的被除數,餘數成為新的除數, 重複步驟 2 31 常見的遞迴應用 求最大公因數GCD // 求GCD之遞迴函數 int gcd(int dividend, int divisor) { int remainder; if (dividend < divisor){ // 找出兩數之大者當被除數 swap(dividend,divisor); // 小者當除數 } if (divisor !=0) { remainder = dividend % divisor; return gcd(divisor,remainder); } else return dividend; } 補充 說明 // 以參考值傳遞引數, 將x,y兩數對調 void swap(int &x, int &y) { int temp = x; x = y; y = temp; } 32 常見的遞迴應用 求最大公因數GCD 3 2 gcd(123,36) gcd(36,15) gcd(15,6) gcd(6,3) 123 108 15 12 3 被除數 = 除數 123 = 36 36 = 15 15 6 6 3 36 30 6 6 0 * * * * * 商 3 2 2 2 2 補充 說明 2 + 餘數 + 15 + 6 + 3 + 0 33 (Continued) Recursive Solution Hanoi(numDisk, src, dest, mid) IF (numDisk = 1) THEN move a disk from src to dest ELSE Hanoi(numDisk – 1, src, mid, dest) Hanoi(1, src, dest, mid) Hanoi(numDisk – 1, mid, dest, src) 48 To move n disks from A to C, using B as auxiliary: 1. If n == 1, then move the single disk from A to C and stop. 2. Move the top n - 1 disks from A to B, using C as auxiliary. 3. Move the remaining disk from A to C. 4. Move the n - 1 disks from B to C, using A as auxiliary. 49 50 move disk 1 from peg A to peg B move disk 2 from peg A to peg C move disk 1 from peg B to peg C move disk 3 from peg A to peg B move disk 1 from peg C to peg A move disk 2 from peg C to peg B move disk 1 from peg A to peg B move disk 4 from peg A to peg C move disk 1 from peg B to peg C move disk 2 from peg B to peg A move disk 1 from peg C to peg A move disk 3 from peg B to peg C move disk 1 from peg A to peg B move disk 2 from peg A to peg C move disk 1 from peg B to peg C 51 Towers of Hanoi 4 3 2 1 A B C 64 gold disks to be moved from tower A to tower C 52 each tower operates as a stack Towers of Hanoi 3 2 1 A B C 3-disk Towers Of Hanoi 53 Towers of Hanoi 2 1 A 3 B C 3-disk Towers Of Hanoi 54 Towers of Hanoi 1 2 3 A B C 3-disk Towers Of Hanoi 55 Towers of Hanoi 1 3 2 A B C 3-disk Towers Of Hanoi 56 Towers of Hanoi A 3 2 1 B C 3-disk Towers Of Hanoi 57 Towers of Hanoi 3 2 1 A B C 3-disk Towers Of Hanoi 58 Towers of Hanoi 2 1 3 A B C 3-disk Towers Of Hanoi 59 Towers of Hanoi 3 2 1 A B 3-disk Towers Of Hanoi • 7 disk moves C 60 Recursive Solution 1 A B C n > 0 gold disks to be moved from A to C using B 61 move top n-1 disks from A to B using C Recursive Solution 1 A B C move top disk from A to C 62 Recursive Solution 1 A B C move top n-1 disks from B to C using A 63 Recursive Solution 1 A B C moves(n) = 0 when n = 0 64 moves(n) = 2*moves(n-1) + 1 = 2n-1 when n > 0 第二章 重點回顧 遞迴的基本原理 遞迴在程式設計上有何優缺點? 設計遞迴程式的原則 常見的遞迴問題的解法 河內塔 費式數列 Ackerman 函數 65