recursion - top steptopstep.weebly.com/uploads/2/4/9/4/24947581/chapter02.pdf · 2-2 designing...
TRANSCRIPT
1
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
Data Structures: A Pseudocode Approach with C , Second Edition
2
How to writing repetitive algorithms
Iteration
Recursion
Is a repetitive process in which an algorithm calls itself.
Data Structures: A Pseudocode Approach with C , Second Edition
3
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
Data Structures: A Pseudocode Approach with C , Second Edition
4
Iterative
The definition involves only the algorithm parameter(s) and not the algorithm itself.
Data Structures: A Pseudocode Approach with C , Second Edition
5
Recursive
A repetitive algorithm use recursion whenever the algorithm appears within the definition itself.
Data Structures: A Pseudocode Approach with C , Second Edition
6Data Structures: A Pseudocode Approach with C , Second Edition
7
Note that
Recursion is a repetitive process in which an algorithm called itself.
Data Structures: A Pseudocode Approach with C , Second Edition
8Data Structures: A Pseudocode Approach with C , Second Edition
9Data Structures: A Pseudocode Approach with C , Second Edition
10
Which code is simpler?
Which one has not a loop?
Data Structures: A Pseudocode Approach with C , Second Edition
11
Calling a recursive algorithm
Data Structures: A Pseudocode Approach with C , Second Edition
12
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
Data Structures: A Pseudocode Approach with C , Second Edition
13
The Design Methodology
Every recursive call either solves a part of the problem or it reduce the size of the problem.
Data Structures: A Pseudocode Approach with C , Second Edition
14
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.
Data Structures: A Pseudocode Approach with C , Second Edition
15
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.
Data Structures: A Pseudocode Approach with C , Second Edition
16
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.
Data Structures: A Pseudocode Approach with C , Second Edition
17
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?
Data Structures: A Pseudocode Approach with C , Second Edition
18
Design implementation – reverse keyboard input
Data Structures: A Pseudocode Approach with C , Second Edition
19
data=6
data=20
data=14
data=5
※ 請注意 print data
在什麼時候執行
Data Structures: A Pseudocode Approach with C , Second Edition
20
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
Data Structures: A Pseudocode Approach with C , Second Edition
21
GCD design
otherwise
aif
bif
bab
b
a
ba 0
0
)mod,gcd(
),gcd(
Greatest Common Divisor Recursive Definition
Data Structures: A Pseudocode Approach with C , Second Edition
22
Pseudocode
Data Structures: A Pseudocode Approach with C , Second Edition
23
GCD C implementation
Data Structures: A Pseudocode Approach with C , Second Edition
24
gcd(10, 25)
gcd(25, 10)
gcd(10, 5)
gcd(5, 0)
Data Structures: A Pseudocode Approach with C , Second Edition
25
Another G.C.D. Recursive Definition
Data Structures: A Pseudocode Approach with C , Second Edition
26
Fibonacci numbers
Mentioned a problem FIBONACCI:A pair of rabbits, a month later able to produce a pair of rabbits, and newborn rabbit have in a month after fertility, but also gave birth to rabbits.Start from a pair of rabbits, a year later there will be a number of rabbits?
Data Structures: A Pseudocode Approach with C , Second Edition
27
Fibonacci numbers
Data Structures: A Pseudocode Approach with C , Second Edition
28
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, …
Data Structures: A Pseudocode Approach with C , Second Edition
29
Fibonacci numbers
Data Structures: A Pseudocode Approach with C , Second Edition
30
Fibonacci numbers
Data Structures: A Pseudocode Approach with C , Second Edition
31
Fibonacci numbers – an example
Data Structures: A Pseudocode Approach with C , Second Edition
32
0 represents .T.
Data Structures: A Pseudocode Approach with C , Second Edition
33
(Continued)
Data Structures: A Pseudocode Approach with C , Second Edition
34
Analysis
Data Structures: A Pseudocode Approach with C , Second Edition
35
We omit
Prefix to Postfix Conversion
The Towers of Honoi
Data Structures: A Pseudocode Approach with C , Second Edition
36
HW2
Write a recursive algorithm to calculate the combination of n objects taken k at a time.
Due date : (甲:941012、乙:941012)
Data Structures: A Pseudocode Approach with C , Second Edition
37
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
Data Structures: A Pseudocode Approach with C , Second Edition
38
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.
Data Structures: A Pseudocode Approach with C , Second Edition
39
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…..
Data Structures: A Pseudocode Approach with C , Second Edition
40Data Structures: A Pseudocode Approach with C , Second Edition
41
Towers of Hanoi Problem:
Today we know that we need to have
264-1
Data Structures: A Pseudocode Approach with C , Second Edition
42
Recursive Towers of Hanoi Design
Find a pattern of moves.
Case 1:
move one disk from source to destination needle.
Data Structures: A Pseudocode Approach with C , Second Edition
43
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.
Data Structures: A Pseudocode Approach with C , Second Edition
44Data Structures: A Pseudocode Approach with C , Second Edition
45
Move three disks
Case 3:
Move two disks from source to auxiliaryneedle.
Move one disk from source to destinationneedle.
Move two disks from auxiliary to destination needle.
Data Structures: A Pseudocode Approach with C , Second Edition
46
(Continued)
Move two disks from source to auxiliary needle.
Move two disks from auxiliary to destination needle.
Data Structures: A Pseudocode Approach with C , Second Edition
47
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
Data Structures: A Pseudocode Approach with C , Second Edition
48Data Structures: A Pseudocode Approach with C , Second Edition
Data Structures: A Pseudocode Approach with C 49
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.
50Data Structures: A Pseudocode Approach with C , Second Edition
51Data Structures: A Pseudocode Approach with C , Second Edition
52Data Structures: A Pseudocode Approach with C , Second Edition
53Data Structures: A Pseudocode Approach with C , Second Edition
54Data Structures: A Pseudocode Approach with C , Second Edition
55Data Structures: A Pseudocode Approach with C , Second Edition