Lecture 4_ Recursive Algorithms
Lecture 4_ Recursive Algorithms
Algorithms
Recursive algorithms
3
General Recursive Design Strategy
4
Requirements for Recursive Solution
5
Recursive Thinking
❖Recursion is:
A problem-solving approach, that can ...
Generate simple solutions to ...
Certain kinds of problems that ...
Would be difficult to solve in other ways
6
Recursive Thinking:
8
Recursive algorithm Properties
9
Key Components of a Recursive Algorithm
Design
1. What is a smaller identical problem(s)?
Decomposition
2. How are the answers to smaller problems
combined to form the answer to the larger
problem?
Composition
3. Which is the smallest problem that can be solved
easily (without further decomposition)?
Base/stopping case
10
Recursion
12
public static int factorial(int n)
Execution Trace {
int fact;
if (n > 1) // recursive case (decomposition)
fact = factorial(n – 1) * n; (composition)
(decomposition) else // base case
fact = 1;
return fact;
}
factorial(4)
factorial(3) 4
13
public static int factorial(int n)
Execution Trace {
int fact;
if (n > 1) // recursive case (decomposition)
fact = factorial(n – 1) * n; (composition)
(decomposition) else // base case
fact = 1;
return fact;
}
factorial(4)
factorial(3) 4
factorial(2) 3
14
public static int factorial(int n)
Execution Trace {
int fact;
if (n > 1) // recursive case (decomposition)
fact = factorial(n – 1) * n; (composition)
(decomposition) else // base case
fact = 1;
return fact;
}
factorial(4)
factorial(3) 4
factorial(2) 3
factorial(1) 2
15
public static int factorial(int n)
Execution Trace {
int fact;
if (n > 1) // recursive case (decomposition)
fact = factorial(n – 1) * n; (composition)
(composition) else // base case
fact = 1;
return fact;
}
factorial(4)
*
factorial(3) 4
*
factorial(2) 3
*
factorial(1) ->1 2
16
public static int factorial(int n)
Execution Trace {
int fact;
if (n > 1) // recursive case (decomposition)
fact = factorial(n – 1) * n; (composition)
factorial(4)
*
factorial(3) 4
*
factorial(2) ->2 3
17
public static int factorial(int n)
Execution Trace {
int fact;
if (n > 1) // recursive case (decomposition)
fact = factorial(n – 1) * n; (composition)
(composition) else // base case
fact = 1;
return fact;
}
factorial(4)
*
factorial(3) ->6 4
18
public static int factorial(int n)
Execution Trace {
int fact;
if (n > 1) // recursive case (decomposition)
fact = factorial(n – 1) * n; (composition)
(composition) else // base case
fact = 1;
return fact;
}
factorial(4) -> 24
19
Improved factorial Method
return fact;
}
20
Example: Palindrome
❖Base case:
An empty string is a palindrome
A string of one character is a palindrome
❖Recursive step
If S is a palindrome, then aSb is also a palindrome if
a=b
21
How Recursion Works
22
How Recursion Works
❖To do this we use a stack.
❖Before a function is called, all relevant data is
stored in a stackframe.
❖This stackframe is then pushed onto the system
stack.
❖After the called function is finished, it simply pops
the system stack to return to the original state.
❖By using a stack, we can have functions call other
functions which can call other functions, etc.
❖Because the stack is a first-in, last-out data
structure, as the stackframes are popped, the
data comes out in the correct order.
23
Limitations of Recursion
❖The expression: c n =1
T ( n) =
2T n + c n 1
2
is a recurrence.
Recurrence: an equation that describes a
26
Recurrence Examples
0 n=0 0 n=0
s ( n) = s ( n) =
c + s(n − 1) n 0 n + s (n − 1) n 0
n =1
c c n =1
T ( n) = T ( n) =
2T + c n 1
n n
2 aT + cn n 1
b
27
Solving Recurrences
Iteration method
Master method
28
The substitution method
❖Examples:
T(n) = 2T(n/2) + (n) → T(n) = (n lg n)
29
The Iteration method
30
The Iteration method
❖ T(n) = c n =1
n
2T(n/2) + c T (n) = 2T
+ c n 1
2(2T(n/2/2) + c) + c 2
22T(n/22) + 2c + c
22(2T(n/22/2) + c) + 3c
23T(n/23) + 4c + 3c
23T(n/23) + 7c
23(2T(n/23/2) + c) + 7c
24T(n/24) + 15c
…
2kT(n/2k) + (2k - 1)c
31
The Iteration method
= n T(1) + (n-1)c
= nc + (n-1)c = (2n - 1)c c n =1
n
T (n) = 2T
+ c n 1
2
32
Proving a Recursive Method
Correct
Recall Proof by Induction:
1. Prove the theorem for the base case(s): n=0
2. Show that:
▪ If the theorem is assumed true for n,
▪ Then it must be true for n+1
33
Proving a Recursive Method Correct
34
Recursive Versus Iterative
Methods
35
Recursion Versus Iteration
39
Towers of Hanoi: Solution Strategy
40
Towers of Hanoi: Solution Strategy
41
Towers of Hanoi: Recursion Structure
42
Computing Powers
43
Recursive Squaring
1 if x = 0
p( x, n) = x p( x, (n − 1) / 2) if x 0 is odd
2
p ( x , n / 2) 2
if x 0 is even
❖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. 44
Analyzing the Recursive Squaring
Method
❖ Memory Usage:
Each call adds a new layer to the call stack, which can increase memory
usage.
Solution: Use iterative solutions where possible or optimize recursion.
❖ Debugging:
Can be difficult to debug when there are multiple recursive calls.
Solution: Add proper base cases and use debugging tools (or print