Week 3-4 Recursion REVISED
Week 3-4 Recursion REVISED
1
COPYRIGHT © 2005, MICHAEL T. GOODRICH AND ROBERTO TAMASSIA.
3.5 Recursion
2
Base case(s)
Values of the input variables for which we perform no recursive
calls are called base cases (there should be at least one base case).
Every possible chain of recursive calls must eventually reach a base
case.
Recursive calls
Calls to the current method.
Each recursive call should be defined so that it makes progress
towards a base case.
4
Visualizing Recursion
If the value looked for is larger than the value in the middle of
the array or array segment
Then the first half of the array or array segment can be ignored
This strategy is then applied to the second half of the array or array
segment
If the value looked for is at the middle of the array or array
segment, then it has been found
If the entire array (or array segment) has been searched in this
way without finding the value, then it is not in the array
LinearSum (A,2)
if ( n = 1) return A[0]; call return A[0] = 4
else LinearSum (A,1)
return LinearSum(A, n - 1) + A[n];
}
15
Output:
20
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 j
if (i < j )
{
swap A[i] and A[j];
ReverseArray(A, i + 1, j - 1);
}
}
18
Output:
54321
2 Dec 2023 Computer Science Department
Tail Recursion
19
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 )
{ swap A[i] and A[j];
i = i + 1;
j = j – 1;
}
}
Computing Powers
21
1 if n 0
p ( x, n )
x p ( x, n 1) else
This leads to an power function that runs in O(n) time (for
we make n recursive calls).
We can do better than this, however.
22
Recursive Squaring
Algorithm Power(x, n)
{
Input : A number x and integer n = 0
Output : The value xn
if (n = 0) return 1;
if (n is odd )
{ y = Power(x, (n - 1)/ 2);
return x*y*y; }
else
{ y = Power(x, n/ 2);
return y*y; }
}
Analyzing the Recursive Squaring Method
24
Algorithm Power(x, n)
{
Input : A number x and integer n = 0 Each time we make a
Output : The value xn recursive call we halve the
value of n; hence, we make
if (n = 0) return 1; log n recursive calls. That
if (n is odd ) is, this method runs in
{ y = Power(x, (n - 1)/ 2); O(log n) time.
return x*y*y; }
It is important that we
else used a variable twice here
{ y = Power(x, n/ 2); rather than calling the
method twice.
return y*y; }
}
25
Output:
power of 3^4 =81
power of 5^5 =3125
26
3.5.2 Binary Recursion
0, 4 4, 4
0, 2 2, 2 4, 2 6, 2
0, 1 1, 1 2, 1 3, 1 4, 1 5, 1 6, 1 7, 1
28
Output:
14
2 Dec 2023 Computer Science Department
29
Computing Fibanacci Numbers
Algorithm BinaryFib(k)
{ Input : Nonnegative integer k
Output : The kth Fibonacci number Fk
if ( k = 1) return k;
else
return BinaryFib(k - 1) + BinaryFib(k - 2);
}
30
Output:
fibonacci of 7 =13
fibonacci of 2 =1
2 Dec 2023 Computer Science Department
Analyzing the Binary Recursion Fibonacci
Algorithm
31
Initial call
PuzzleSolve (3,(),{a,b,c})