n7 Recursion F24
n7 Recursion F24
Recursion
recursiveFactorial (0)
if n = 0 then
return 0
else
return
linearSum(A, n - 1) + A[n -
1]
if i < j then
Swap ( A[i], A[ j] );
reverseArray( A, i + 1, j – 1 );
return
For example,
24 = 2(4/2)2 = (24/2)2 = (22)2 = 42 = 16
25 = 21+(4/2)2 = 2(24/2)2 = 2(22)2 = 2(42) = 32
26 = 2(6/ 2)2 = (26/2)2 = (23)2 = 82 = 64
27 = 21+(6/2)2 = 2(26/2)2 = 2(23)2 = 2(82) = 128
© 2014 Goodrich, Tamassia, Goldwasser Recursion 13
Recursive Squaring Method
Algorithm Power(x, n):
Input: A number x and integer n = 0
Output: The value xn
if n = 0 then
return 1
if n is odd then
y = Power( x, (n - 1)/ 2 )
return x · y · y
else
y = Power( x, n/ 2 )
return y · y
© 2014 Goodrich, Tamassia, Goldwasser Recursion 14
Analysis
Algorithm Power(x, n):
Input: A number x and
integer n = 0 Each time we make a
Output: The value xn recursive call we
if n = 0 then halve the value of n;
hence, we make log n
return 1 recursive calls. That
if n is odd then is, this method runs in
y = Power(x, (n - 1)/ O(log n) time.
2)
return x · y · y It is important that we
else use a variable twice
y = Power(x, n/ 2) here rather than
return y · y calling the method
© 2014 Goodrich, Tamassia, Goldwasser Recursion twice. 15
Tail Recursion
Tail recursion occurs when a linearly recursive
method makes its recursive call as its last
step.
The array reversal method is an example.
Such methods can be easily converted to non-
recursive methods (which saves on some
resources).
Example: algorithm reverseArray( )
Algorithm IterativeReverseArray(A, i, j ):
Input: An array A and nonnegative integer indices i
and j
Output: The reversal of the elements in A starting
at index i and ending at j
while i < j do
Swap ( A[i ], A[ j ] );
i = i + 1;
j = j – 1;
© 2014 Goodrich, Tamassia, Goldwasser Recursion 16
Reversing an Array (Recursive)
Algorithm reverseArray(A, i, j):
Input: An array A and nonnegative integer
indices i and j
Output: The reversal of the elements in A
starting at index i and ending at
if i < j then
Swap ( A[i], A[ j] )
reverseArray( A, i + 1, j – 1 )
return
f(1) = f(2) = 1
f(n) = f(n-1) + f(n-2) if n > 2
21
Running Time of Recursive Algorithms
Could be just a hidden “for" or “while” loop.
See “Tail Recursion” slide.
Logarithmic (next)
Examples: binary search, exponentiation, GCD
Solving a recurrence
Example: merge sort, quick sort (later)
22
How to get better at writing recursive
algorithms?
Close the textbook and lecture notes.
23
Multiple Recursion
Multiple recursion:
makes potentially many recursive calls
not just one or two
not covered in this course
25
26
LOGARITHMS
EECS 2101
27
• Binary search
• Compare the search element with the middle element of
the array.
• If not equal, then apply binary search to half of the array
(if not empty) where the search element would be.
29
Exponentiation xn
long exp(long x, int n)
/* A different version of function Power(x, n) */
{
/*1*/ if (n == 0)
/*2*/ return 1;
/*3*/ if (n == 1)
/*4*/ return x;
/*5*/ if (isEven(n))
/*6*/ return exp(x*x, n/2);
else
/*7*/ return exp(x*x, n/2)*x;
}
32
Euclid’s Algorithm
• Homework: trace the following algorithm. What is its running
time? (Hint: see next slide)
• Computing the greatest common divisor (GCD) of two integers