Download presentation
Presentation is loading. Please wait.
Published byArabella Harris Modified over 9 years ago
1
Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion Design a recursive algorithm Determine when an recursion is an appropriate solution Write simple recursive functions Recursion
2
Data Structures: A Pseudocode Approach with C2 How to writing repetitive algorithms Iteration Recursion Is a repetitive process in which an algorithm calls itself. 請同學區分這三個字 repetitive 、 iterative 、 recursive , 特別是看中文書的同學。
3
Data Structures: A Pseudocode Approach with C3 2-1 Factorial - A Case Study We begin the discussion of recursion with a case study and use it to define the concept. This section also presents an iterative and a recursive solution to the factorial algorithm. Recursive Defined Recursive Solution
4
Data Structures: A Pseudocode Approach with C4 Iterative The definition involves only the algorithm parameter(s) and not the algorithm itself.
5
Data Structures: A Pseudocode Approach with C5 Recursive A repetitive algorithm use recursion whenever the algorithm appears within the definition itself.
6
Data Structures: A Pseudocode Approach with C6
7
7 Note that Recursion is a repetitive process in which an algorithm called itself.
8
Data Structures: A Pseudocode Approach with C8
9
9
10
10 Which code is simpler? Which one has not a loop?
11
Data Structures: A Pseudocode Approach with C11 Calling a recursive algorithm
12
Data Structures: A Pseudocode Approach with C12 2-2 Designing Recursive Algorithms In this section we present an analytical approach to designing recursive algorithms. We also discuss algorithm designs that are not well suited to recursion. The Design Methodology Limitation of Recusion Design Implemenation
13
Data Structures: A Pseudocode Approach with C13 The Design Methodology Every recursive call either solves a part of the problem or it reduce the size of the problem.
14
Data Structures: A Pseudocode Approach with C14 The Design Methodology Base case The statement that “solves” the problem. Every recursive algorithm must have a base case. General case The rest of the algorithm Contains the logic needed to reduce the size of the problem.
15
Data Structures: A Pseudocode Approach with C15 Rules for designing a recursive algorithm 1.Determine the base case. 2.Determine the general case. 3.Combine the base case and the general cases into an algorithm.
16
Data Structures: A Pseudocode Approach with C16 Combine the base case and the general cases into an algorithm Each call must reduce the size of the problem and move it toward the base case. The base case, when reached, must terminate without a call to the recursive algorithms; that is, it must execute a return.
17
Data Structures: A Pseudocode Approach with C17 Limitations of recursion You should not use recursion if the answer to any of the following questions is no: 1.Is the algorithm or data structure naturally suited to recursion? 2.Is the recursive solution shorter and more understandable? 3.Does the recursive solution run within acceptable time and space limits?
18
Data Structures: A Pseudocode Approach with C18 Design implementation – reverse keyboard input
19
Data Structures: A Pseudocode Approach with C19 data=6 data=20 data=14 data=5 ※ 請注意 print data 在什麼時候執行
20
Data Structures: A Pseudocode Approach with C20 2-3 Recursive Examples Slide 05 Four recursive programs are developed and analyzed. Only one, the Towers of Hanoi, turns out to be a good application for recursion. Greatest Common Divisor Fiboncci Numbers Prefix to Postfix Conversion The Towers of Honoi
21
Data Structures: A Pseudocode Approach with C21 GCD design Greatest Common Divisor Recursive Definition
22
Data Structures: A Pseudocode Approach with C22 Pseudocode 歐基里德 輾轉相除法
23
Data Structures: A Pseudocode Approach with C23 GCD C implementation
24
Data Structures: A Pseudocode Approach with C24 gcd(10, 25) gcd(25, 10) gcd(10, 5) gcd(5, 0)
25
Data Structures: A Pseudocode Approach with C25 Another G.C.D. Recursive Definition
26
Data Structures: A Pseudocode Approach with C26 Fibonacci numbers 在費波那西的〈算經〉中提到一個問題: 假如一對兔子,一個月後能生產一對兔子, 而新生兔子在一個月後便具備生育能力, 也能生下一對兔子。 那麼若從一對兔子開始,一年之後將會有 多少對兔子?
27
Data Structures: A Pseudocode Approach with C27 Fibonacci numbers
28
Data Structures: A Pseudocode Approach with C28 Fibonacci numbers -- 費式數 Each number is the sum of the previous two numbers. The first few numbers in the Fibonacci series are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
29
Data Structures: A Pseudocode Approach with C29 Fibonacci numbers -- 費式數
30
Data Structures: A Pseudocode Approach with C30 Fibonacci numbers
31
Data Structures: A Pseudocode Approach with C31 Fibonacci numbers – an example
32
Data Structures: A Pseudocode Approach with C32 0 represents.T.
33
Data Structures: A Pseudocode Approach with C33 (Continued)
34
Data Structures: A Pseudocode Approach with C34 Analysis
35
Data Structures: A Pseudocode Approach with C35 We omit Prefix to Postfix Conversion The Towers of Honoi
36
Data Structures: A Pseudocode Approach with C36 HW2 Write a recursive algorithm to calculate the combination of n objects taken k at a time. Due date : ( 甲: 941012 、乙: 941012)
37
Data Structures: A Pseudocode Approach with C37 2-3 Recursive Examples Slide 05 Four recursive programs are developed and analyzed. Only one, the Towers of Hanoi, turns out to be a good application for recursion. The Towers of Honoi
38
Data Structures: A Pseudocode Approach with C38 Towers of Hanoi Problem: Invented by French mathematician Lucas in 1880s. Original problem set in India in a holy place called Benares. There are 3 diamond needles fixed on a brass plate. One needle contains 64 pure gold disks. Largest resting on the brass plate, other disks of decreasing diameters. Called tower of Brahma.
39
Data Structures: A Pseudocode Approach with C39 Towers of Hanoi Problem: Priests are supposed to transfer the disks from one needle to the other such that at no time a disk of larger diameter should sit on a disk of smaller diameter. Only one disk can be moved at a time. Later setting shifted to Honoi, but the puzzle and legend remain the same. How much time would it take? Estimate…..
40
Data Structures: A Pseudocode Approach with C40
41
Data Structures: A Pseudocode Approach with C41 Towers of Hanoi Problem: Today we know that we need to have 2 64 -1
42
Data Structures: A Pseudocode Approach with C42 Recursive Towers of Hanoi Design Find a pattern of moves. Case 1: move one disk from source to destination needle.
43
Data Structures: A Pseudocode Approach with C43 Move two disks Case 2: Move one disk to auxiliary needle. Move one disk to destination needle. Move one disk to from auxiliary to destination needle.
44
Data Structures: A Pseudocode Approach with C44
45
Data Structures: A Pseudocode Approach with C45 Move three disks Case 3: Move two disks from source to auxiliary needle. Move one disk from source to destination needle. Move two disks from auxiliary to destination needle.
46
Data Structures: A Pseudocode Approach with C46 (Continued) Move two disks from source to auxiliary needle. Move two disks from auxiliary to destination needle.
47
Data Structures: A Pseudocode Approach with C47 Algorithm Tower of Hanoi Towers (numDisks, source, dest, auxiliary) numDisks is number of disks to be moved source is the source tower dest is the destination tower auxiliary is the auxiliary tower
48
Data Structures: A Pseudocode Approach with C48
49
Data Structures: A Pseudocode Approach with C49 Generalize Tower of Hanoi General case Move n-1 disks from source to auxiliary. Base case Move one disk from source to destination. General case Move n-1 disks from auxiliary to destination.
50
Data Structures: A Pseudocode Approach with C50
51
Data Structures: A Pseudocode Approach with C51
52
Data Structures: A Pseudocode Approach with C52
53
Data Structures: A Pseudocode Approach with C53
54
Data Structures: A Pseudocode Approach with C54
55
Data Structures: A Pseudocode Approach with C55
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.