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
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)
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
Watch Out
When using recursion, we must be careful not to create an infinite chain of function calls:
Oops! No termination condition
Reference: [6]
Linear Recursion
A Linear Recursion is the simplest form of recursion.
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
11
Binary Recursion
A Binary Recursion is a recursion that calls itself twice
12
Quick Test
Can you think of an example for
13
Exercice 1
Write a recursive function that prints the digits of a non-negative integer number vertically
14
Solution 1
void recursive_write_vertical(int number) { if (number < 10) // stop condition // Write the only digit, and stop recursion System.out.println(number); else // recursive calls { // Print all digits except the last digit recursive_write_vertical(number/10); // Print the last digit System.out.println(number % 10); } }
15
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) { System.out.println(-); // print a negative sign super_write_vertical(abs(number)); // abs computes the absolute value } else if (number < 10) { System.out.println(number); // Write the one digit } else { super_write_vertical(number/10); // Write all but the last digit System.out.println(number % 10); // Write the last digit
17
} }
Exercise 3
Write a recursive function that counts the number of zero digits in an integer Example: zeros(10200) returns 3
18
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
20
if n = 1 if n > 1
21
So we have
T(1) = 1 T(n) = 2 T(n/2) + n as the recurrence relation
22
Reference: [10]
23
Reference: [10]
24
Reference: [10]
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
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
References
[1] Wikipedia Encyclopedia http://en.wikipedia.org/ [2] Dictionary of Algorithms and Data Structures . National Institute of Standards and Technology. http://www.nist.gov/dads/ [3] M.T. Goodrich, R. Tamassia. Data Structures and Algorithms in Java, 4th edition. John Wiley & Sons, 2006. ISBN 0-471-73884-0. http://java4.datastructures.net/ [4] M.T. Goodrich, R. Tamassia, D. Mount. Data Structures and Algorithms in C++. John Wiley & Sons, 2004. ISBN 0-471-20208-8. http://cpp.datastructures.net/ [5] SparkNotes: Examples of Recursion. http://www.sparknotes.com/cs/recursion/examples/ [6] Basic Math and Recursion. Brooks/Cole Publishing Company , 2000. http://www.cs.ust.hk/~huamin/COMP171/recursion.ppt [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. http://www.cs.ucr.edu/~jiang/cs141/recur.pdf [10] Owen L. Astrachan, Big-Oh for Recursive Functions: Recurrence Relations, Duke University, 2003. http://www.cs.duke.edu/~ola/ap/recurrence.html Comp 352: Data Structures and Algorithms
27
28