- Prof. amr Goneid, AUC
1
CSCE 110 PROGRAMMING FUNDAMENTALS
WITH C++
- Prof. Amr Goneid
WITH C++ Prof. Amr Goneid AUC Part 12. Recursion Prof. amr - - PowerPoint PPT Presentation
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 12. Recursion Prof. amr Goneid, AUC 1 Recursion Prof. amr Goneid, AUC 2 Recursion Definition Examples from Math Functions Why Recursion Rules for
1
2
3
Definition Examples from Math Functions Why Recursion Rules for Recursion General Recursive Structures Famous Methods The Role of the Stack Recursive Array processing using Exclude &
Recursive Array processing using Divide &
More Examples Iterative VS Recursive Algorithms
4
5
Has the the ability to call itself. A finite number of recursive calls with different input
Desired result is obtained from current action and the
Terminal action is predefined (does not need
The termination condition (Base Case) is obviously
6
7
Sum of the first n integers
8
1 1 1
n n i i
− = =
9
10
11
12
13
Factorial of n = n! = 1*2*3…*n =
1 n i
i
=
14
An Iterative Factorial Function:
15
16
17
A numeric value x raised to an integer power:
1
n n
−
18
19
A Recursive function to print elements of an array A from
index s through index e. Main call may be printlist(A,0,n-1):
20
21
22
Each call to a module pushes a stack frame on the
Where the jump to the module came from. The input parameters The result (or output parameters).
3 Results 2 Input params 1 Where from
23
24
1 n = 0
Addr3
? n = 1
Addr2
? n = 2
Addr1
? n = 2
Addr1
? n = 1
Addr2
n = 2
Addr1
1 n = 1
Addr2
? n = 2
Addr1
2 n = 2
Addr1
25
Recursive sum of array elements from location s
26
Number of zero elements in an array from
27
Reversing an Array
28
Recursive Sequential Search of x in array A from
29
Recursive Selectsort of array A from location s through
location e. Invoke e.g. as SelectSort(A , 1 , n)
30
Maximum value in an array from location s
Assume we have a function that returns the
31
int maximum (int a[ ], int s, int e) { // Base Case if (s == e) return a[s]; else // General Case { int m = (s + e)/2; // Divide in the middle int maxL = maximum (a , s , m); // Conquer left half int maxR = maximum (a , m+1 , e); // Conquer right return max2 ( maxL , maxR); // Combine } }
32
33
int Bsearch (int A[ ], int x, int s, int e) { int mid; // Base case: No elements left, search failed if (s > e) return -1; else { // General case mid = (s+e) / 2; // Divide in the middle if (x == A[mid]) return mid; // Success at mid else (if x > A[mid]) // Conquer right return Bsearch(A,x,mid+1,e); else // Conquer left return Bsearch(A,x,s,mid-1); } }
34
35
For example: To move one disk from A to C: Move disk1 from A to C To move two disks (top is 1, bottom is 2): Move 1 from A to B Move 2 from A to C Move 1 from B to C To move N disks from A to C and we already know how to move N-1 disks from any one peg to another: Move the top N-1 disks by a series of legal moves from A to B using C Move Disk N from A to C directly Move N-1 disks from B to C using A
36
Obviously, this is a recursive problem that can be solved by the following recursive algorithm: Towers (N, A , C , B) { if N = 1 move disk 1 from A to C directly else { Towers ( N-1 , A , B , C) Move disk N from A to C directly Towers ( N-1 , B , C , A) } }
37
38
//Computes gcd(m, n) by Euclid’s algorithm //Input: Two nonnegative, not-both-zero integers m and n //Output: Greatest common divisor of m and n
function gcd(m, n) if n = 0 return m else return gcd (n, m mod n)
"The Euclidean algorithm is the granddaddy of all algorithms, because it is the oldest nontrivial algorithm that has survived to the present day”. Donald Knuth, The Art of Computer Programming, Vol. 2
39
ALGORITHM Euclid (m, n) //Computes gcd(m, n) by Euclid’s algorithm //Input: Two nonnegative, not-both-zero integers m and n //Output: Greatest common divisor of m and n
while n != 0 do r ←m mod n m←n n←r return m
40
2 /2 2 /2
n n n
41
42
A Binary Tree is a special data structure where a node has a maximum
The nodes in the BST can be implemented as a linked structure:
element right
16 45 32 40
32 16 45 40
43
Algorithm: Pre-order Traversal (Visit parent before children) void PreOrder ( tree T) { if ( T != NULL) { Visit (T); PreOrder (T->left); PreOrder (T->right); } }
The resulting visit order = {a} {b , d , e} {c , f }
b c a d e f
1 2 3 5 6 4
44
The following function draws recursive squares (called a fractal star). The drawing primitive is Box (x , y , n) which draws a square of side (n) pixels centered at (x,y) : void STAR( int x, int y, int n) { if (n > 1){ STAR(x-n , y+n , n/2); STAR(x+n , y+n , n/2); STAR(x-n , y-n , n/2); STAR(x+n , y-n , n/2); Box(x , y , n); } }
45
Recursive Algorithms can be more elegant in
They will cost about 5% more time than an
46
Fibonacci numbers represent the sequence:
Introduced by Leonardo Fibonacci (1202) Can be computed by the recurrence
47
n n n
48
49
50
51
52
53
54
55
Analysis
Tracing for n = 5
5 3 4 1 2 2 3 1 2 1 1 1 n 1 2 3 4 5 Calls 1 3 5 9 15
56
This solution is an example of Overlapping Subproblems. It means that any recursive algorithm solving the problem
should solve the same subproblems over and over, rather than generating new subproblems.
The number of calls is ~ ϕn The cost is very high because we keep computing the same
instance over and over again.
For example:
It will take about 6369 Years to compute Fib(100) using a computer with 4 Gega Operations/sec. !!
57