Recursion
Tuesday, September 26, 2006
Recursion
A recursive function is one that makes use of itself Every recursive function involves
base case(s), simple instance of the problem that can be solved directly crucial for termination recursive case(s). divide the problem into one or more simpler or smaller parts call the function (recursively) on each part combine the solutions of the parts into a solution for the problem
Comp 352: Data Structures and Algorithms
Designing a Recursive Algorithm
Consider the following questions: 1. How could you define the problem in terms of a smaller problem of the same type? 2. How would each recursive call affect the size of the problem? (size should decrease) 3. What instance(s) of the problem can serve as the base case? 4. As the problem size decreases, will you reach the base case?
Comp 352: Data Structures and Algorithms
Recursion: Advantages
For some problems, recursive solution may be more natural and simpler to write than using non-recursive solutions Example: The factorial function: n! = 1 (if n = 0) n! = n * (n-1)! (otherwise)
Comp 352: Data Structures and Algorithms
Recursion : Disadvantages
Calling a recursive function consumes more time and memory than non-recursive solution of the same problem It is easy to write inefficient recursive algorithms ==> High performance applications hardly ever use recursion
Comp 352: Data Structures and Algorithms
Watch Out
When using recursion, we must be careful not to create an infinite chain of function calls:
Oops! No termination condition
int factorial(int numb){ return numb * factorial(numb-1); } A recursive function must
Contain at least one non-recursive branch Eventually lead to a non-recursive branch
Comp 352: Data Structures and Algorithms
Fibonacci Sequence Example I
Fibonacci Sequence
Comp 352: Data Structures and Algorithms
Fibonacci Sequence Example II
int fib(int number) { if (number == 0) return 0; if (number == 1) return 1; return (fib(number-1) + fib(number-2)); } 8
Comp 352: Data Structures and Algorithms
Fibonacci Sequence Example III
Reference: [6]
Comp 352: Data Structures and Algorithms
Linear Recursion
A Linear Recursion is the simplest form of recursion.
Has one recursive call
Is used when an algorithm has a simple repetitive structure
Includes some basic step (usually first or last element) The remaining set has the same structure as the original one
Comp 352: Data Structures and Algorithms
10
Tail Recursion
A Tail Recursion is a special case of linear recursion
With the last operation being a recursive call
Tail Recursions can very often be easily transformed into an iteration
11
Comp 352: Data Structures and Algorithms
Binary Recursion
A Binary Recursion is a recursion that calls itself twice
12
Comp 352: Data Structures and Algorithms
Quick Test
Can you think of an example for
Linear Recursion Tail Recursion Binary Recursion
13
Comp 352: Data Structures and Algorithms
Exercice 1
Write a recursive function that prints the digits of a non-negative integer number vertically
14
Comp 352: Data Structures and Algorithms
Solution 1
void recursive_write_vertical(int number) { if (number < 10) // stop condition // Write the only digit, and stop recursion [Link](number); else // recursive calls { // Print all digits except the last digit recursive_write_vertical(number/10); // Print the last digit [Link](number % 10); } }
15
Comp 352: Data Structures and Algorithms
Exercise 2
Instead of printing only non-negative integers, print any integer number vertically (i.e., including negative numbers)
Hint:
You can have more than one recursive calls or stopping conditions in your recursive function
Comp 352: Data Structures and Algorithms
16
Solution 2
void super_write_vertical(int number) // If number is negative, then a negative sign appears on the top. { if (number < 0) { [Link](-); // print a negative sign super_write_vertical(abs(number)); // abs computes the absolute value } else if (number < 10) { [Link](number); // Write the one digit } else { super_write_vertical(number/10); // Write all but the last digit [Link](number % 10); // Write the last digit
17
} }
Comp 352: Data Structures and Algorithms
Exercise 3
Write a recursive function that counts the number of zero digits in an integer Example: zeros(10200) returns 3
18
Comp 352: Data Structures and Algorithms
Solution 3
int zeros(int numb) { if(numb==0) return 1; else if(numb < 10 && numb > -10) return 0; else // > 1 digits: recursion return zeros(numb/10) + zeros(numb%10); }
19
zeros(10200) zeros(1020) + zeros(0) zeros(102) + zeros(0) + zeros(0) zeros(10) + zeros(2) + zeros(0) + zeros(0) Comp 352: Data Structures and zeros(1) + zeros(0) + zeros(2) + zeros(0)Algorithms + zeros(0)
Recurrence Relation
A recurrence relation is an equation defining a function f(n) recursively in terms of smaller values of n
Can help to determine the Big-O running time of recursive function
20
Comp 352: Data Structures and Algorithms
Example: Recurrence Relation
The running time of Merge-Sort is: (assuming that n is a power of 2)
T(n) = O(1) T(n) = 2 T(n/2) + O(n)
if n = 1 if n > 1
21
Comp 352: Data Structures and Algorithms
Solving Recurrence Relation - I
To simplify the algebra, we write
n instead of O(n) 1 instead of O(1)
So we have
T(1) = 1 T(n) = 2 T(n/2) + n as the recurrence relation
22
Reference: [10]
Comp 352: Data Structures and Algorithms
Solving Recurrence Relation - II
T(n) = 2 T(n/2) + n = 2 [2 T(n/4) + n/2] + n = 4 T(n/4) + 2n = 4 [2 T(n/8) + n/4] + 2n = 8 T(n/8) + 3n ... = 2k T(n/2k) + k n We know that T(1) = 1 and this is how we could terminate the derivation n/2k = 1 n = 2k k = log2n
23
Reference: [10]
Comp 352: Data Structures and Algorithms
Solving Recurrence Relation - III
... = 2k T(n/2k) + k n = 2log2n T(1) + (log2n) n = 2log2n * 1 + n log2n = n + n log2n = O(n log n) // k = log2n
24
Reference: [10]
Comp 352: Data Structures and Algorithms
Exercise 4
Solve the following recurrence relation: T(N) = T(N/2) + 1 T(1) = 1 You may assume that N is a power of 2.
25
Comp 352: Data Structures and Algorithms
Solution 4
Recurrence Relation: T(N) = T(N/2) + 1; T(1) = 1 T(N) = T(N/2) + 1 = T(N/4) + 1 + 1 = T(N/8) + 1 + 1 + 1 = T(N/2k) + 1 + 1 ++ 1 k times = T(1) + 1 + 1 + + 1 = 1 + log2N
26
log2N times
Comp 352: Data Structures and Algorithms
References
[1] Wikipedia Encyclopedia [Link] [2] Dictionary of Algorithms and Data Structures . National Institute of Standards and Technology. [Link] [3] M.T. Goodrich, R. Tamassia. Data Structures and Algorithms in Java, 4th edition. John Wiley & Sons, 2006. ISBN 0-471-73884-0. [Link] [4] M.T. Goodrich, R. Tamassia, D. Mount. Data Structures and Algorithms in C++. John Wiley & Sons, 2004. ISBN 0-471-20208-8. [Link] [5] SparkNotes: Examples of Recursion. [Link] [6] Basic Math and Recursion. Brooks/Cole Publishing Company , 2000. [Link] [7] Walter Savitch, Problem Solving with C++ - The Object of Programming, Chapter 13 Recursion, 4th Edition, Pearson Education Inc., 2003 [8] Andrew Horner, Comp104 notes on Recursion, CS Dept, HKUST. [9] Tao Jiang, Recurrence relations and analysis of recursive algorithms, Lecture notes for UCR CS 141: Intermediate Data Structures and Algorithms. [Link] [10] Owen L. Astrachan, Big-Oh for Recursive Functions: Recurrence Relations, Duke University, 2003. [Link] Comp 352: Data Structures and Algorithms
27
Thank you ! See you next time
28
Comp 352: Data Structures and Algorithms