Lecture 4_ Recursive Algorithms
Lecture 4_ Recursive Algorithms
Algorithms
Recursive algorithms
2
Recursive algorithm
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 Design Example
9
Recursive algorithm Properties
A recursive algorithm solves the large problem
by using its solution to a simpler sub-problem
divide and conquer approach
10
Key Components of a Recursive Algorithm
Design
1. What is a smaller identical problem(s)?
Decomposition
11
Recursion
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
14
public static int factorial(int n)
{
Execution Trace int fact;
if (n > 1) // recursive case (decomposition)
fact = factorial(n – 1) * n; (composition)
else // base case
(decomposition) fact = 1;
return fact;
}
factorial(4)
factorial(3) 4
factorial(2) 3
15
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
16
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
17
public static int factorial(int n)
{
Execution Trace int fact;
if (n > 1) // recursive case (decomposition)
fact = factorial(n – 1) * n; (composition)
else // base case
(composition) fact = 1;
return fact;
}
factorial(4)
*
factorial(3) 4
*
factorial(2) ->2 3
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)
*
factorial(3) ->6 4
19
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
20
Improved factorial Method
return fact;
}
21
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
22
How Recursion Works
23
How Recursion Works
One important point in this interaction is that,
unless changed through call-by- reference, all local
data in the calling module must remain unchanged.
Therefore, when a function is called, some
information needs to be saved in order to return
the calling module back to its original state (i.e., the
state it was in before the call).
We need to save information such as the local
variables and the spot in the code to return to after
the called function is finished.
24
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.
25
Limitations of Recursion
The expression:
c n 1
T ( n)
2T c n 1
n
2
is a recurrence.
Recurrence: an equation that describes a
function in terms of its value on smaller functions
27
Recurrence Examples
0 n0 0 n0
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 n c n 1 n
2 aT cn n 1
b
28
Solving Recurrences
Iteration method
Master method
29
The substitution method
Examples:
T(n) = 2T(n/2) + (n) T(n) = (n lg n)
30
The Iteration method
31
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
32
The Iteration method
34
Proving a Recursive Method Correct
35
Recursive Versus Iterative
Methods
36
Recursion Versus Iteration
39
Towers of Hanoi: Description
40
Towers of Hanoi: Solution Strategy
41
Towers of Hanoi: Solution Strategy
42
Towers of Hanoi: Recursion Structure
43
Computing Powers
44
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. 45
A Recursive Squaring Method
46
Analyzing the Recursive Squaring
Method
47
Conclusion
48