Download presentation
Presentation is loading. Please wait.
Published byMelanie Shepherd Modified over 9 years ago
1
Chapter 6 Recursion
2
Solving simple problems Iteration can be replaced by a recursive function Recursion is the process of a function calling itself
3
Computing 2^5th without recursion #include int twoRaisedTo0() { return 1; } int twoRaisedTo1() { return 2 * twoRaisedTo0(); } int twoRaisedTo2() { return 2 * twoRaisedTo1(); } int twoRaisedTo3() { return 2 * twoRaisedTo2(); } int twoRaisedTo4() { return 2 * twoRaisedTo3(); } int twoRaisedTo5() { return 2 * twoRaisedTo4(); }
4
Main program int main() { cout << "2 to the 5th is " << twoRaisedTo5(); cout << endl; return 0; }
5
Recursively computing powers of 2 #include int twoRaisedTo(int n) { if (n == 0) // special case return 1; else return 2 * twoRaisedTo(n-1); }
6
Main program int main() { cout << "2 to the 5th is " << twoRaisedTo(5); cout << endl; return 0; }
7
The Nature of Recursion Solving a problem by first solving a smaller version of it Recursive solutions have two parts –A recursive call –A recursive stop The function calls itself until the stop condition is hit, then it returns back through the recursive calls.
8
Powers of 2 equation When n >= 1 When n = 0
9
Recursive substitution It is easy to turn a recursive definition into a recursive program Keep substituting values until you get an expression that can be evaluated without recursion
10
Recursion back to the stop case
11
Back substitution Once the recursive stop has been located you can use the value obtained and return to the previous definition. You then repeatedly work your way back to the place where you started This is called ‘back substitution’
12
Substitution details
13
Factorial problem This is a classic problem demonstrating the use of recursion The only problem comes when you try to implement it with ints or long integers (a small integer may generate a factorial that exceeds the representational capacity of the machine.
14
Equation 6-5 Equation 6-6 When n >= 1 When n = 0
15
Factorial function int factorial(int n) { // precondition: n is not negative if (n == 0) return 1; else return (n*factorial(n-1); // postcondition: n is not changed // returns: n! // limitations: INT_MAX }
16
Combinatorial problem How many different one hour shows can be produced from 40 records if you can only play 10 songs in an hour? How many combinations of 40 can be arrived at choosing 10 at a time? “n choose k” problem
17
Factorial solution n choose k = n!/(k!(n-k)!) This problem has a recursive solution that employs our recursive factorial function (nested recursion) This function has two recursive stops
18
Another solution We can also come up with a recursive version of the ‘n choose k’ problem on our own, without factorial.
19
Equation 6-11 when k = 1 when n = k when n > k and k > 1 Equation 6-12
20
n choose k function int choose(int n, int k) { if (k == 1) return n; else if (n == k) return 1; else // recursive case: n>k and k>1 return choose(n - 1, k - 1) + choose(n - 1, k); }
21
Recursion in searching and sorting Any iterative process (a loop) that terminates can be redefined recursively Iterative processes are used in both searching and sorting routines
22
A recursive linear search Given an array ‘a’, indexed from 0 to n-1 Function search is called like this: search(a,n-1,target) where the value n-1 becomes the value for n in the function Here is it’s recursive algorithm –If a[n-1] == target return n-1 –else – return the result of search(a,n-1,target)
23
a[0] through a[n-2] a[n-1] use recursive call linearSearch(a, n-1, target) for this part Visualization of recursive Linear Search
24
Recursive Linear Search int linearSearch(int a[], int n, int target) { // Recursive version of linear search // Precondition: a is an indexed from 0 to n-1 if (n < 0) // an empty list is specified return -1; else {
25
Recursive linear search (con’t) if (a[n-1] == target) // test final position return n-1; else // search the rest of the list recursively return linearSearch(a, n-1, target); } // Postcondition: If a value between 0 and n-1 is returned, // a[returnValue] == target; // Otherwise, if -1 is returned, target is not in a }
26
Recursive binary search Preconditions: a is an array sorted in ascending order, first is the index of the first element to search, last is the index of the last element to search, target is the item to search for.
27
Recursive binary search The repetitive process involves dividing the search domain in half The recursive stop happens when the target value equals the middle value of the current list, or when you run out of places to look
28
Algorithm for binary search If first > last return -1 (target not found) If a[mid] == target return mid else if target < a[mid] bsearch(a,first,mid-1, target) else bsearch(a, mid+1, last, target)
29
Binary search (con’t) If first > last return failure mid = (first+last)/2 if a[mid] is equal to target return mid else if target < a[mid] return result of recursive search of a from first up to mid-1 else return result of recursive search of a from mid+1 up to last
30
Recursive binary search code int binarySearch(int a[], int first, int last, int target) { // Preconditions: a is an array sorted in ascending order, // first is the index of the first element to search, // last is the index of the last element to search, // target is the item to search for.
31
Binary search code if (first > last) return -1; // -1 indicates failure of search int mid = (first+last)/2; if (a[mid] == target) return mid; else if (target < a[mid]) return binarySearch(a, first, mid-1, target); else // target must be > a[mid] return binarySearch(a, mid+1, last, target); // Postcondition: Value returned is position of target in a, // otherwise -1 is returned }
32
Correctness Proof of correctness was done with loop invariants for non-recursive loops Recursive algorithms are proved by induction –Does the program work for the base case? –Does the program work for each case smaller than the current n? If both are true we assume it has been proved correct.
33
Recursive quicksort Quicksort was published in 1962 by British mathematician C.A.R. Hoare Basic idea: –Pick one item from an array (the pivot) –Reorganize the array so that smaller things are on one side and larger things on the other (partitioning). –Recursively select pivots for each half of the list, partition, select pivots, partition, etc.
34
Quicksort: The array after one partition partition 1: all items <= pivotpartition 2: all items > pivotpivot
35
Quicksort code int partition(int a[], int first, int last); void quicksort(int a[], int first, int last) { // precondition: a is an array; // The portion to be sorted runs from // index first to index last inclusive. if (first >= last) // Base Case -- nothing to sort, so return return;
36
Code Example // Otherwise, we’re in the recursive case. // The partition function uses the item in a[first] as the pivot // and returns the position of the pivot -- split -- after the partition. int split(partition(a, first, last)); // Recursively, sort the two partitions. quicksort(a, first, split-1); quicksort(a, split+1, last); // postcondition: a is sorted in ascending order // between first and last inclusive. }
37
First attempt at a loop invariant for partition partition 1: all items <= pivotpartition 2: all items > pivot firstsplitlast items yet to be processed 149 22 11 4 82741 56 31 33 101 6614 53 99 11 2 24 87 33 4722 The initial value ‘27’ is moving right as array elements are processed. This is unnecessarily complicated. A better way would be to keep it in position 0 until all elements have been moved, then swap it with the last small one.
38
Better loop invariant for partition partition 1: all items <= pivotpartition 2: all items > pivot firstlastSmalli items yet to be processed 2714 9 22 11 4841 56 31 33 101 6653 99 11 2 24 87 33 47 2214 14 is less than 27, so lastSmall increments by one to point to 41 and then 14 and 41 are switched. Then i moves on to the value 53. 2714 9 22 11 4 856 31 33 101 66 4199 11 2 24 87 33 47 225314
39
Getting the pivot into its proper location firstlastSmall Exchange a[first] with a[lastSmall]
40
Partition function void swapElements(int a[], int first, int last); int partition(int a[], int first, int last) { int lastSmall(first), i; for (i=first+1; i <= last; i++) // loop invariant: a[first+1]...a[lastSmall] <= a[first] && // a[lastSmall+1]...a[i-1] > a[first] if (a[i] <= a[first]) { // key comparison ++lastSmall; swapElements(a, lastSmall, i); }
41
Partition continued // put pivot into correct position swapElements(a, first, lastSmall); // postcondition: a[first]...a[lastSmall-1] <= a[lastSmall] && // a[lastSmall+1]...a[last] > a[lastSmall] return lastSmall; // this is the final position of the pivot -- the split index }
42
Example recursion tree for Quicksort [67, 58, 38, 81, 90, 57, 54] [54, 58, 38, 57, 67, 81, 90] [54, 58, 38, 57][81, 90] [38, 54, 58, 57][81, 90] [58, 57][38][][90] [57, 58] [][58]
43
A worst case recursion tree for Quicksort [1,2,3,4,5,6,7] [] [2,3,4,5,6,7] [] [3,4,5,6,7] [] [4,5,6,7] [5,6,7] [][6,7] [] [7]
44
Efficiency of quicksort’s worst case
45
Quicksort versus bubble sort n(as power of 2) n Quicksort Bubble Sort Comparisions a Comparisons
46
A best case recursion tree for Quicksort [4, 1, 3, 2, 6, 5, 7] [2, 1, 3, 4, 6, 5, 7] [2, 1, 3][6, 5, 7] [1, 2, 3][5, 6, 7] [3][1][7][5]
47
the number of items to sort at each level The number of levels (I) is no more than log(base 2)n
48
A uniformly distributed pivot splits near the middle half the time about half the time, the pivot falls in the shaded area around the middle n 3n 4 n 2 n 4 0
49
How is recursion implemented? When a function calls itself –The original stops executing and it’s values are stored on the ‘data stack’ –Eventually, the recursive call will return and at that time the values in storage are popped off the stack and the function picks up processing
50
Example Given a function b(int x) that contains a call to another function a(int z) within it... Function b must be temporarily suspended until a finishes. This means that b, and the values of its variables must be stored on the stack temporarily.
51
Example, (continued) When b goes on the stack, then the execution of a can begin with any parameters from b bound to it. When a is finished it returns a value to b At the point of return, b and its variables are popped off of the stack and execution of b continues.
52
Example continued The next slide is an example of a function call of this nature.
53
Nested function calls int a(int x) { return x+x; } int b(int x) { int z(x + 3); int y(a(z)); // *** z = x * y; return z; }
54
Recursion? Recursive calls are like any other function call. They require the same binding of data values and the same suspension of the parent function through storage of its parameters on the stack Recursion therefore comes at a cost.
55
Recursive function calls int f(int x) { int y; if (x == 0) return 1; else { y = 2 * f(x-1); return y + 1; }
56
Execution of Code Example for f(3) x = 3 y = ? call f(2) y = 2 * 7 = 14 return y + 1 = 15 x = 2 y = ? call f(1) y = 2 * 3 = 6 return y + 1 = 7 x = 1 y = ? call f(0) y = 2 * 1 = 2 return y + 1 = 3 x = 0 y = ? return 1 copy of f value returned by call is 15
57
Recursion and RAM Recursive functions may require more space in memory than non- recursive calls. With some deeply recursive algorithms this can be a problem. It can also be a problem with poorly written recursive functions with bad base case definitions (result is like an infinite loop only on the stack)
58
Advantages of recursion Recursion’s big advantage is the elegance with which it embodies the solution to many problems. It closely follows the mathematical definition of many functions This makes it easier to prove correctness.
59
Advantages of recursion (con’t) Some problems are much easier to solve recursively than iteratively (Towers of Hanoi)
60
Disadvantages of recursion It’s use of system resources slows the process down compared to iterative solutions –This may not be a big factor on modern machines however (author’s note: 2% difference in speed) Difficult to ‘think recursively’
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.