Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion. Circular Definition (not useful) E.g., E.g., Projenitor: one who produces an offspring Projenitor: one who produces an offspring Offspring:

Similar presentations


Presentation on theme: "Recursion. Circular Definition (not useful) E.g., E.g., Projenitor: one who produces an offspring Projenitor: one who produces an offspring Offspring:"— Presentation transcript:

1 Recursion

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

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

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

5 Recursive Factorial Function fact(4) return 4 * fact(3) 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 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!” 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 Otherwise, num > 0 : we count off num and then recursively count down from num-1 14-6

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

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

9 Fibonnaci Number fib(n) = 1, when n = 0 fib(n) = 1, when n = 1 fib(n) = fib(n -1) + fib(n – 2), when n > 1 fib(n) = 1, when n = 0 fib(n) = 1, when n = 1 fib(n) = fib(n -1) + fib(n – 2), when n > 1 Fibonacci Function int fib(int n){ if (n == 0 || n == 1) return 1; else return fib(n – 1) + fib(n – 2); } Fibonacci Function int 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. 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. 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). 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 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.) 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(); … } 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. 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); … } 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 facter between positive integers a and b. Write a recursive function named greatestCommonFactor(int a, int b) which returns the greatest common facter between 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). 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 Assume an array a that is sorted in ascending order, and an item X We want to write a function that searches for X within the array a, returning the index of X if it is found, and returning -1 if X is not in the array We want to write a function that searches for X within the array a, returning the index of X if it is found, and returning -1 if X is not in the array 14-20

21 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: set m to index of the middle portion of array: 14-21 mlo hi

22 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] 14-22 mlo hi

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

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 2 64 −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.billion

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 Tower 2Tower 1 Tower 3 Move N – 1 disks from 1 to 2 Move 1 disk from 1 to 3 Demo

27 Algorithm (n = 3)

28 Solution N disks Tower 2Tower 1 Tower 3 Move N – 1 disks from 1 to 2 Move 1 disk from 1 to 3 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 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


Download ppt "Recursion. Circular Definition (not useful) E.g., E.g., Projenitor: one who produces an offspring Projenitor: one who produces an offspring Offspring:"

Similar presentations


Ads by Google