Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 132 Spring 2008 Chapter 6 Recursion Read p 367-378 Skip example 6-3 (Fibonacci), 6-4 (Hanoi) Read example 6-5 (p. 387)

Similar presentations


Presentation on theme: "1 CS 132 Spring 2008 Chapter 6 Recursion Read p 367-378 Skip example 6-3 (Fibonacci), 6-4 (Hanoi) Read example 6-5 (p. 387)"— Presentation transcript:

1 1 CS 132 Spring 2008 Chapter 6 Recursion Read p 367-378 Skip example 6-3 (Fibonacci), 6-4 (Hanoi) Read example 6-5 (p. 387)

2 2 Definitions Recursion solving a problem by reducing it to smaller versions of itself Recursive definition a problem is expressed in terms of a smaller version of itself it has one or more base cases e.g. N!= 1 if N =0, N!=N*(N-1)! if N > 0 Recursive algorithm an algorithm that finds the solution to a given problem by reducing the problem to smaller versions of itself implemented using recursive functions e.g. To Find N!, find (N-1)! and multiply it by N

3 3 Definitions General solution –breaks problem into smaller versions of itself Base case –case in recursive definition in which the solution is obtained directly, e.g. 0! = 1 –stops the recursion General case –case in recursive definition in which a smaller version of itself is called. E.g. N!= N*(N-1)! –must eventually reduce to a base case

4 4 Tracing a Recursive Function Every recursive call has –its own code –own set of parameters –own set of local variables After completing recursive call: –control goes back to calling environment –recursive call must execute completely before control goes back to previous call –execution in previous call begins from point immediately following recursive call

5 5 Recursive Factorial Trace int fact(int num) { if(num == 0) return 1; else return num*fact(num–1); }

6 6 //use recursion to print a list void printList(nodeType* theList) { if (theList!=NULL) { cout info<< " "; printList(theList->link); } } //use recursion to print a list backwards void revprintList(nodeType* theList) { if (theList!=NULL) { revprintList(theList->link); cout info<< " "; } }

7 7 Find the Largest Value in an Array int largest(const int list[], int lowerIndex, int upperIndex) { int max; if(lowerIndex == upperIndex) //the size of the sublist is 1 return list[lowerIndex]; else { //find the largest value in [lowerIndex+1,upperIndex] max = largest(list, lowerIndex + 1, upperIndex); if(list[lowerIndex] >= max) return list[lowerIndex]; else return max; } }

8 8 Execution of largest(list, 0, 3)

9 9 Recursive Fibonacci int rFibNum(int a, int b, int n) { if(n == 1) return a; else if(n == 2) return b; else return rFibNum(a, b, n - 1) + rFibNum(a, b, n - 2); } Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13... a n = a n-1 + a n-2 More general: let the sequence start with a and b

10 10 Execution of rFibonacci(2,3,5)

11 11 Towers of Hanoi: Three Disk Solution

12 12 Towers of Hanoi: Recursive Algorithm void moveDisks(int count, int needle1, int needle3, int needle2) { if(count > 0) { moveDisks(count - 1, needle1, needle2, needle3); cout << "Move disk "<< count << “ from " <<needle1 << “ to " << needle3 << "." <<endl; moveDisks(count - 1, needle2, needle3, needle1); }

13 13 Converting from Decimal to Binary 13 = 1101 2 13 = 1x8 + 1x4 + 1x1 = 1(2 3 ) + 1(2 2 ) + 0(2 1 ) + 1(2 0 ) How to calculate this? –first digit is num%2 13%2 = 1 –second digit: (num/2) %2(13/2) % 2 = 0 –third digit: (num/2)/2 %2(13/2)/2 % 2 = 0 Algorithm –binary(num) = 0 if num = 0 –binary(num) = binary(num/2) followed by num%2 Recursive!

14 14 Execution of decToBin(13,2) void decToBin(int num, int base) { if(num > 0) { decToBin(num/base, base); cout<<num % base; }

15 15 Recursion or Iteration? Two ways to solve particular problem –iteration –recursion Iteration: uses looping to repeat a set of statements Tradeoffs between two options –sometimes recursive solution is easier –sometimes recursive solution is slower

16 16 Backtracking Algorithm Attempts to solve a problem by constructing partial solutions If a partial solution leads to a dead end, algorithm backs up by –removing the most recently added part and –then trying other possibilities Famous example: N-Queens problem: place n Queens on an nxn board so that each is safe –what is safe? –a queen can take another piece diagonally, horizontally, or vertically

17 17 Solution to 8-Queens Puzzle

18 18 4-Queens Puzzle


Download ppt "1 CS 132 Spring 2008 Chapter 6 Recursion Read p 367-378 Skip example 6-3 (Fibonacci), 6-4 (Hanoi) Read example 6-5 (p. 387)"

Similar presentations


Ads by Google