







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Class: LAB:Comp Program & Prob Solv; Subject: Computer Science; University: Wellesley College; Term: Unknown 1989;
Typology: Study notes
1 / 13
This page cannot be seen from the preview
Don't miss anything!
Friday, October 27, 2006 Classic recursion 13- 2
Classic recursion 13- 3 PathFinder Patty buggle wants to find a single bagel in an acyclic maze and draw a path from the bagel back to the origin. Classic recursion 13- 4 An Acyclic Maze Looks Like a Tree (1,1), EAST L = left(); forward(); F = forward(); R = right(); forward(); (1,2), NORTH (1,3), NORTH (^) (2,2), EAST (3,2), EAST (3,3), NORTH (^) (4,2), EAST (4,1), SOUTH
L F L L F F R F F F R X X X F R R L
Classic recursion 13- 7 findBagel() implementation public boolean findBagel() { // Base case: if there is a bagel, put down brush and return true if (isOverBagel()) { brushDown(); return true; } else { // Recursive case: explore left, front, and right. // If a bagel is found in any of these directions, // return true immediately without further exploration. // Return false if no bagel found in any of the 3 directions. return findLeft() || findFront() || findRight(); // Note: “or” operator (||) is a “short-ciruit” operator // that returns true as soon as one operand is true, // without evaluating subsequence operands. } } Classic recursion 13- 8 findFront() and Friends
findFront() is like findBagel() except that it only finds a bagel in the submaze rooted at the cell in front of the buggle. findLeft() is like findBagel() except that it only finds a bagel in the submaze rooted at the cell to the left of the buggle. (findRight() is similar.)
Classic recursion 13- 9 PathFinder Illustrates Mutual Recursion findBagel(): true findLeft(): true findFront() findRight() findBagel(): true findLeft(): false findFront(): false findRight(): true findBagel(): false findBagel(): true findLeft(): false findFront(): false findFront(): false findFront(): true findLeft(): false findRight() (Red nodes are never created) Classic recursion 13- 10 Class (static) vs. Instance vs. Local Variables
red cyan CLASS Color class LAND Buggle class r Color instance 255 g^0 b^0 r Color instance 0 g^255 b^255
c
c2 bob position Buggle instance … heading color … brush^ true
Classic recursion 13- 13 Classic Recursion: Factorial 4! = 4 x (3 x 2 x 1) = 4 x 3! 2! = 2 x (1) = 2 x 1! 1! = 1 X 0! 3! = 3 x (2 x 1) = 3 x 2! Likewise 0! = 1 base case recursive cases Classic recursion 13- 14 Factorial in Java
So, if n==4, fact(4) 4fact(3) 3fact(2) 2fact(1) 1fact(0) 1**
Classic recursion 13- 15 Invoking fact() public class Factorial { public static int fact (int n) { if (n == 0 ) { return 1; } else { return n * fact (n - 1 ); } } public static void main(String [] args) { int n = 4 ; System.out.println("The value of " + n + "! is " + fact(n)); } } Classic recursion 13- 16 Can we write fact() using tail recursion?
serves as a place to pass answer along
Classic recursion 13- 19 Leonardo Pisano Fibonacci Time # Pairs (^1 ) 2 1 3 2 (^4 ) (^5 ) (^6 ) Classic recursion 13- 20 fibonacci() method
fib(4) fib(3) fib(2) fib(2) fib(1) fib(1) fib(0) 1 0 fib(1) fib(0) 1 1 0
Classic recursion 13- 21 It gets worse fib(4) fib(3) fib(2) fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) 1
fib(3) fib(2) fib(1) fib(1) fib(0) 1
fib(5) Classic recursion 13- 22 public int fibTail(int n, int i, int fibIMinus1, int fibI) { if (i == n) { return fibI; } else { return fibTail(n, i+ 1 , fibI, fibIMinus1 + fibI); } } fibonacciFast(n) invokes fibTail(n, 1 , 0 , 1 ); fibonacciFast(5) fibTail(5,3,1,2) fibTail(5,4,2,3) fibTail(5,5,3,5) 5 fibTail(5,2,1,1) fibTail(5,1,0,1)
Classic recursion 13- 25 Java code **public void hanoi (int n) { System.out.println("------------------------------------"); System.out.println("Instructions for solving Hanoi("