1 / 29

Recursion

Recursion. Circular Definition (not useful). E.g., Projenitor : one who produces an offspring Offspring: one who is produced by a projenitor E.g., Oak: a tree that produces acorn Acorn: a nut produced by oak tree. Recursive Definition. Recursive Definition

Download Presentation

Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recursion

  2. Circular Definition (not useful) • E.g., • Projenitor: one who produces an offspring • Offspring: one who is produced by a projenitor • E.g., • Oak: a tree that produces acorn • Acorn: a nut produced by oak tree

  3. Recursive Definition • Recursive Definition • Uses the term being defined as part of the definition • Is not a circular definition • Must have base case(s) • Factorial of n: n!n! = 1, when n = 0 // base casen! = n (n – 1)!, when n > 0 // recursive case

  4. Recursive Function • Recursive function • Invokes itself within the function • Is an example of divide-and-conquor technique • Recursive factorial functionint fact(int n){ if (n == 0) // base case return 1; else // recursive case return n * fact(n – 1);} • Invocationcout << fact(4);

  5. fact(4) return 4 * fact(3) Recursive Factorial Function fact(3) return 3 * fact(2) fact(2) return 2 * fact(1) fact(1) return 1 * fact(0) fact(0) return 1 fact(1) return 1 * 1 fact(2) return 2 * 1 fact(3) return 3 * 2 fact(4) return 4 * 3

  6. Recursive Functions Consider a function for solving the count-down problem from some number num down to 0: • Base case: when num == 0: the problem is solved and we “blast off!” • Otherwise, num > 0: we count off num and then recursively count down from num-1

  7. Recursive Functions A recursive function for counting down to 0: void countDown(intnum) { if (num == 0) // base case cout << "Blastoff!"; else // recursive {// call cout << num << ". . ."; countDown(num-1); } }

  8. What Happens When Called? first call to countDown num is 2 OUTPUT: 2... countDown(1); second call to countDown num is 1 1... countDown(0); third call to countDown num is 0 Blastoff! // no // recursive // call

  9. Fibonnaci Number • fib(n) = 1, when n = 0fib(n) = 1, when n = 1fib(n) = fib(n -1) + fib(n – 2), when n > 1 • Fibonacci Functionint fib(int n){ if (n == 0 || n == 1) return 1; else return fib(n – 1) + fib(n – 2);}

  10. Your Turn • Write a recursive function sumOf(int n), which returns the sum of consecutive integers between 1 and n. • E.g., cout << sumOf(10) prints 55.

  11. Solution int sumOf(int n) { if (n == 1) { return 1; } else { return n + sumOf(n - 1); } }

  12. Your Turn • Write a recursive function power(double b, int x), which returns b^x(b raised to the power of x). • E.g., cout << power(2, 8) prints 256

  13. Solution int power(double b, int x){ double result; if (x == 0){ result = 1; } else if (x == 1){ result = b; } else { result = b * power(b, x – 1); } return result; }

  14. Your Turn • Write a recursive function which reads a series of characters from the console and prints them backwords. (A sentinel value of ‘z’ signals the end of input.) • E.g., main() can contain the following:int main() { cout << “Enter a series of chars.\n”; printBackwards(); …}

  15. Solution void printBackward() { char ch; cin >> ch; if (ch != 'z') { printBackward(); cout << ch; } }

  16. Your Turn • Write a recursive function named minimum(int a[], int count) which returns the minimum value of an int array. • E.g., main() can contain the following:int main() { int a[]; int count; intializeArray(a, count); cout << “Min: “ << minimum(a, count); …}

  17. Solution int minimum(int a[], int count) { int min = a[count - 1]; if (count == 1) { return min; } else { if (min < minimum(a, count - 1)) return min; else return minimum(a, count - 1); } }

  18. Your Turn • Write a recursive function named greatestCommonFactor(int a, int b)which returns the greatest common facterbetween positive integers a and b. • GCF can be found from the following rules:if b = 0, then GCF is a; otherwise, it is the GCF of b and (a mod b).

  19. Solution int greatestCommonFactor(int a, int b){ if (b == 0) return a; else return lowestCommonFactor(b, a % b); }

  20. Recursive Binary Search Function • Assume an array a that is sorted in ascending order, and an item X • We want to write a function that searches for Xwithin the array a, returning the index of X if it is found, and returning -1 if X is not in the array

  21. hi lo m Recursive Binary Search A recursive strategy for searching a portion of the array from index lo to index hi is to set m to index of the middle portion of array:

  22. hi lo m Recursive Binary Search If a[m] == X, we found X, so return m If a[m] > X, recursively search a[lo..m-1] If a[m] < X, recursively search a[m+1..hi]

  23. Recursive Binary Search intbSearch(int a[],intlo,inthi,int X) { int m = (lo + hi) /2; if(lo > hi) return -1; // base if(a[m] == X) return m; // base if(a[m] > X) return bsearch(a,lo,m-1,X); else return bsearch(a,m+1,hi,X); }

  24. Tower of Hanoi • Stack of 64 golden disks, with the largest at the bottom and progressively smaller ones above. • When the stack is completely moved, the world will come to an end. • It would take 264−1 (18,446,744,073,709,551,615) moves to finish. At a rate of one move per sec, it would take them roughly 585 billion years to finish.

  25. Tower of Hanoi • Objective • Move the stack of disks from one tower to another with the following rules: • Only one disk may be moved at a time. • Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod. • No disk may be placed on top of a smaller disk.

  26. Solution N disks Move N – 1 disks from 1 to 2 Move 1 disk from 1 to 3 Tower 3 Tower 1 Tower 2 Demo

  27. Algorithm (n = 3)

  28. Solution N disks Move N – 1 disks from 1 to 2 Move 1 disk from 1 to 3 Tower 3 Tower 1 Tower 2 Demo

  29. Algorithm • Move (int n, int from, int to, int aux) If (n = 1) Then cout << “Move disk from “ << from << “ to “ << to << endl Else Move (n – 1, from, aux, to) cout << “Move disk from “ << from << “ to << to << endl; Move (n – 1, aux, to, from) End If

More Related