Beruflich Dokumente
Kultur Dokumente
5)
Recursion: when a method calls itself
Classic example
- - the factorial function:
Using Recursion n! = 1· 2· 3· ··· · (n-1)· n
Recursive definition:
1 if n = 0
f ( n) =
n ⋅ f (n − 1) else
As a Java method:
// recursive factorial function
public static int recursiveFactorial(int n) {
if (n == 0) return 1; // basis case
else return n * recursiveFactorial(n- 1); // recursive case
}
© 2004 Goodrich, Tamassia Using Recursion 1 © 2004 Goodrich, Tamassia Using Recursion 2
Every possible chain of recursive calls must n = 1, such that A has at least LinearSum(A,5)
eventually reach a base case, and the handling of n elements call return 13 + A[3] = 13 + 2 = 15
each base case should not use recursion. Output: LinearSum (A,4)
The sum of the first n integers
Recur once. in A
call
LinearSum (A,3)
return 7 + A[2] = 7 + 6 = 13
Perform a single recursive call. (This recursive step if n = 1 then call return 4 + A[1] = 4 + 3 = 7
may involve a test that decides which of several return A[0] LinearSum (A,2)
possible recursive calls to make, but it should else call return A[0] = 4
ultimately choose to make just one of these calls return LinearSum(A, n - 1) + LinearSum (A,1)
each time we perform this step.) A[n - 1]
Define each possible recursive call so that it makes
progress towards a base case.
© 2004 Goodrich, Tamassia Using Recursion 3 © 2004 Goodrich, Tamassia Using Recursion 4
Reversing an Array Defining Arguments for Recursion
Algorithm ReverseArray(A, i, j): In creating recursive methods, it is important
Input: An array A and nonnegative integer to define the methods in ways that facilitate
indices i and j recursion.
Output: The reversal of the elements in A This sometimes requires we define additional
starting at index i and ending at j paramaters that are passed to the method.
if i < j then For example, we defined the array reversal
Swap A[i] and A[ j] method as ReverseArray(A, i, j), not
ReverseArray(A, i + 1, j - 1) ReverseArray(A).
return
© 2004 Goodrich, Tamassia Using Recursion 5 © 2004 Goodrich, Tamassia Using Recursion 6
© 2004 Goodrich, Tamassia Using Recursion 7 © 2004 Goodrich, Tamassia Using Recursion 8
Analyzing the Recursive Squaring
A Recursive Squaring Method Method
Algorithm Power(x, n): Algorithm Power(x, n):
Input: A number x and integer n = 0 Input: A number x and
integer n = 0
Output: The value xn
Output: The value xn Each time we make a
if n = 0 then if n = 0 then recursive call we halve the
return 1 return 1 value of n; hence, we make
log n recursive calls. That
if n is odd then if n is odd then is, this method runs in
y = Power(x, (n - 1)/ 2) y = Power(x, (n - 1)/ 2) O(log n) time.
return x · y ·y return x · y · y
else It is important that we
else used a variable twice here
y = Power(x, n/ 2) y = Power(x, n/ 2) rather than calling the
return y · y return y · y method twice.
© 2004 Goodrich, Tamassia Using Recursion 9 © 2004 Goodrich, Tamassia Using Recursion 10
© 2004 Goodrich, Tamassia Using Recursion 13 © 2004 Goodrich, Tamassia Using Recursion 14
© 2004 Goodrich, Tamassia Using Recursion 15 © 2004 Goodrich, Tamassia Using Recursion 16
A Better Fibonacci Algorithm Multiple Recursion (§ 4.1.3)
Use linear recursion instead:
Algorithm LinearFibonacci(k): Motivating example: summation puzzles
Input: A nonnegative integer k pot + pan = bib
Output: Pair of Fibonacci numbers (Fk, Fk-1) dog + cat = pig
if k = 1 then
return (k, 0) boy + girl = baby
else
(i, j) = LinearFibonacci(k - 1)
return (i +j, i) Multiple recursion: makes potentially
many recursive calls (not just one or
Runs in O(k) time. two).
© 2004 Goodrich, Tamassia Using Recursion 17 © 2004 Goodrich, Tamassia Using Recursion 18
© 2004 Goodrich, Tamassia Using Recursion 19 © 2004 Goodrich, Tamassia Using Recursion 20