Upload
lill-pain-f-seiei
View
220
Download
0
Embed Size (px)
Citation preview
7/31/2019 3 - Recursion
1/28
Recursion
Tuesday, September 26, 2006
7/31/2019 3 - Recursion
2/28
Comp 352: Data Structures andAlgorithms2
Recursion
A recursive function is one that makes use ofitself
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 orsmaller parts call the function (recursively) on each part combine the solutions of the parts into a
solution for the problem
7/31/2019 3 - Recursion
3/28
Comp 352: Data Structures andAlgorithms3
Designing a Recursive Algorithm
Consider the following questions:
1. How could you define the problem in terms of asmaller problem of the same type?
2. How would each recursive call affect the size of theproblem? (size should decrease)
3. What instance(s) of the problem can serve as thebase case?
4. As the problem size decreases, will you reach thebase case?
7/31/2019 3 - Recursion
4/28
Comp 352: Data Structures andAlgorithms4
Recursion: Advantages
For some problems, recursive solution may bemore natural and simpler to write than using
non-recursive solutions Example: The factorial function:
n! = 1 (if n = 0)
n! = n * (n-1)! (otherwise)
7/31/2019 3 - Recursion
5/28
Comp 352: Data Structures andAlgorithms5
Recursion : Disadvantages
Calling a recursive function consumes moretime and memory than non-recursive solutionof the same problem
It is easy to write inefficient recursivealgorithms
==> High performance applications hardlyever use recursion
7/31/2019 3 - Recursion
6/28
Comp 352: Data Structures andAlgorithms6
Watch Out
When using recursion, we must be careful not tocreate an infinite chain of function calls:
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
Oops!Oops!
No termination conditionNo termination condition
7/31/2019 3 - Recursion
7/28
Comp 352: Data Structures andAlgorithms7
Fibonacci Sequence Example I
Fibonacci Sequence
7/31/2019 3 - Recursion
8/28
Comp 352: Data Structures andAlgorithms8
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));
}
7/31/2019 3 - Recursion
9/28
Comp 352: Data Structures andAlgorithms9
Fibonacci Sequence Example III
Reference: [6]
7/31/2019 3 - Recursion
10/28
Comp 352: Data Structures andAlgorithms10
Linear Recursion
A Linear Recursion is the simplest form ofrecursion. Has one recursive call
Is used when an algorithm has a simplerepetitive structure
Includes some basic step (usually first or lastelement)
The remaining set has the same structure as theoriginal one
7/31/2019 3 - Recursion
11/28
Comp 352: Data Structures andAlgorithms11
Tail Recursion
A Tail Recursion is a special case of linearrecursion
With the last operation being a recursive call Tail Recursions can very often be easily
transformed into an iteration
7/31/2019 3 - Recursion
12/28
Comp 352: Data Structures andAlgorithms12
Binary Recursion
A Binary Recursion is a recursion that callsitself twice
7/31/2019 3 - Recursion
13/28
Comp 352: Data Structures andAlgorithms13
Quick Test
Can you think of an example for
Linear Recursion
Tail Recursion Binary Recursion
7/31/2019 3 - Recursion
14/28
Comp 352: Data Structures andAlgorithms14
Exercice 1
Write a recursive function that prints thedigits of a non-negative integer number
vertically
7/31/2019 3 - Recursion
15/28
Comp 352: Data Structures andAlgorithms15
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);}
}
7/31/2019 3 - Recursion
16/28
Comp 352: Data Structures andAlgorithms16
Exercise 2
Instead of printing only non-negativeintegers, print any integer number vertically(i.e., including negative numbers)
Hint:
You can have more than one recursivecalls or stopping conditions in yourrecursive function
7/31/2019 3 - Recursion
17/28
Comp 352: Data Structures andAlgorithms17
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}
}
7/31/2019 3 - Recursion
18/28
Comp 352: Data Structures andAlgorithms18
Exercise 3
Write a recursive function that counts thenumber of zero digits in an integer
Example:zeros(10200) returns 3
7/31/2019 3 - Recursion
19/28
Comp 352: Data Structures andAlgorithms19
Solution 3
int zeros(int numb){
if(numb==0)
return 1;
else if(numb < 10 && numb > -10)return 0;
else // > 1 digits: recursionreturn zeros(numb/10) + zeros(numb%10);
}
zeros(10200)zeros(1020) + zeros(0)
zeros(102) + zeros(0) + zeros(0)
zeros(10) + zeros(2) + zeros(0) + zeros(0)
zeros(1) + zeros(0) + zeros(2) + zeros(0) + zeros(0)
7/31/2019 3 - Recursion
20/28
Comp 352: Data Structures andAlgorithms20
Recurrence Relation
A recurrence relation is an equation defininga function f(n) recursively in terms of smaller
values of n Can help to determine the Big-O running time of
recursive function
7/31/2019 3 - Recursion
21/28
Comp 352: Data Structures andAlgorithms21
Example: Recurrence Relation
The running time of Merge-Sort is: (assumingthat n is a power of 2)
T(n) = O(1) if n = 1 T(n) = 2 T(n/2) + O(n) if n > 1
7/31/2019 3 - Recursion
22/28
Comp 352: Data Structures andAlgorithms22
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
Reference: [10]
7/31/2019 3 - Recursion
23/28
Comp 352: Data Structures andAlgorithms23
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 nWe know that T(1) = 1 and this is how we could terminate the
derivation
n/2k = 1 n = 2k k = log2n
Reference: [10]
7/31/2019 3 - Recursion
24/28
Comp 352: Data Structures andAlgorithms24
Solving Recurrence Relation - III
...
= 2k T(n/2k) + k n // k = log2n
= 2log2
n T(1) + (log2
n) n
= 2log2n * 1 + n log2n
= n + n log2n
= O(n log n)
Reference: [10]
7/31/2019 3 - Recursion
25/28
Comp 352: Data Structures andAlgorithms25
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.
7/31/2019 3 - Recursion
26/28
Comp 352: Data Structures andAlgorithms26
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
= T(1) + 1 + 1 + + 1 = 1 + log2N
log2N times
k times
7/31/2019 3 - Recursion
27/28
Comp 352: Data Structures andAlgorithms27
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 UCRCS 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, DukeUniversity, 2003. http://www.cs.duke.edu/~ola/ap/recurrence.html
http://en.wikipedia.org/http://www.nist.gov/dads/http://www.nist.gov/dads/http://java4.datastructures.net/http://java4.datastructures.net/http://cpp.datastructures.net/http://cpp.datastructures.net/http://www.sparknotes.com/cs/recursion/examples/http://www.cs.ust.hk/~huamin/COMP171/recursion.ppthttp://www.cs.ust.hk/~huamin/COMP171/recursion.ppthttp://www.cs.ucr.edu/~jiang/cs141/recur.pdfhttp://www.cs.ucr.edu/~jiang/cs141/recur.pdfhttp://www.cs.duke.edu/~ola/ap/recurrence.htmlhttp://www.cs.duke.edu/~ola/ap/recurrence.htmlhttp://www.cs.duke.edu/~ola/ap/recurrence.htmlhttp://www.cs.ucr.edu/~jiang/cs141/recur.pdfhttp://www.cs.ust.hk/~huamin/COMP171/recursion.ppthttp://www.sparknotes.com/cs/recursion/examples/http://cpp.datastructures.net/http://cpp.datastructures.net/http://java4.datastructures.net/http://java4.datastructures.net/http://www.nist.gov/dads/http://en.wikipedia.org/7/31/2019 3 - Recursion
28/28
Comp 352: Data Structures andAlgorithms28
Thank you !See you next time