Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Recursion with Mazes, Bunnies, and Towers - Lecture Slides | CS 111, Study notes of Computer Science

Material Type: Notes; Class: LAB:Comp Program & Prob Solv; Subject: Computer Science; University: Wellesley College; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/16/2009

koofers-user-1qy
koofers-user-1qy 🇺🇸

10 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS111 Computer Programming
Department of Computer Science
Wellesley College
Recursion with Mazes,
Bunnies, and Towers
Classic recursion examples
Friday, October 27, 2006
Classic recursion 13-2
Today:
1. An example of mutual recursion in BuggleWorld:
PathFinder.
2. Review of class (static) methods.
3. Some classic recursion examples that every computer
scientist must know: factorial, fibonacci, and Towers of
Hanoi.
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Recursion with Mazes, Bunnies, and Towers - Lecture Slides | CS 111 and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science

Wellesley College

Recursion with Mazes,

Bunnies, and Towers

Classic recursion examples

Friday, October 27, 2006 Classic recursion 13- 2

Today:

  1. An example of mutual recursion in BuggleWorld: PathFinder.
  2. Review of class (static) methods.
  3. Some classic recursion examples that every computer scientist must know: factorial, fibonacci, and Towers of Hanoi.

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

(1,4), NORTH

(1,5), NORTH

(1,6), NORTH (2,3), WEST (4,3), EAST

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

public boolean findFront() {

if (isFacingWall()) {

return false;

} else {

forward();

boolean result = findBagel();

backward();

return result;

public boolean findLeft() {

left();

boolean result = findFront();

right();

return result;

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

o Class (static) variables reside in the class itself. There is exactly one

occurrence of a class variable, regardless of the number of instances.

o Instance variables live in each instance of a class. There can be many

distinct occurrences of an instance variable, one in each instance.

o Local variables reside

in execution frames

public class Color {

// class variables

public static Color

red, cyan, …;

// instance variables

private int r, g, b;

… rest of Color class …}

red cyan CLASS Color class LAND Buggle class r Color instance 255 g^0 b^0 r Color instance 0 g^255 b^255

OBJECT

LAND

c

EXECU-

TION

LAND

c2 bob position Buggle instance heading color brush^ true

Color c1 = Color.red;

Color c2 = Color.cyan;

Buggle bob = new Buggle();

bob.setColor(c2);

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

public static int fact (int n)

if (n == 0 ) {

return 1 ;

} else {

return n * fact (n - 1 );

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?

public static int factTail (int num, int ans)

// factorial 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

public int fibonacci(int n)

if (n < 2 ) // Assume n >= 0

return n;

else

return fibonacci(n- 1 ) + fibonacci(n- 2 );

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("

  • n + ", A, B, C):”); Hanoi(n, "A”, "B”, "C”); } public void hanoi (int n,String source,String dest,String spare) { if (n > 0) { hanoi(n - 1, source, spare, dest); // n-1 disks->spare moveDisk(n, source, dest); // largest disk->goal hanoi(n - 1, spare, dest, source); // n-1 disks->goal } }**