×

### Let's log you in.

or

Don't have a StudySoup account? Create one here!

×

or

by: Gaoqi Zheng

28

0

23

# Computer Science II Chapter 8 CSE 214

Marketplace > Stony Brook University > ComputerScienence > CSE 214 > Computer Science II Chapter 8
Gaoqi Zheng
Stony Brook U

Get a free preview of these Notes, just enter your email below.

×
Unlock Preview

Chapter 8 - Recursion
COURSE
Computer Science II
PROF.
TYPE
Class Notes
PAGES
23
WORDS
CONCEPTS
Computer Science II
KARMA
25 ?

## Popular in ComputerScienence

This 23 page Class Notes was uploaded by Gaoqi Zheng on Monday March 28, 2016. The Class Notes belongs to CSE 214 at Stony Brook University taught by Ahmad Esmaili in Spring 2016. Since its upload, it has received 28 views. For similar materials see Computer Science II in ComputerScienence at Stony Brook University.

×

## Reviews for Computer Science II Chapter 8

×

×

### What is Karma?

#### You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 03/28/16
Recursion Revisited Chapter 8 Fundamentals • A method is recursive if it calls itself. • A recursive method should contain a stopping case or base case that is not recursive. (Without it, the method would be infinitely recursive, never ending.) • The recursive call(s) should be for simpler versions of the same problem. Activation Records • When a method calls another method (even itself), an activation record is stored on the system stack (of the O.S.). • An activation record contains: -where to return when the called method ends - parameter(s) passed to the called method - values of the method’s local variables • When a method returns, it uses the top activation record on the system stack to restore the conditions before the method call. Factorial public int factorial(int n) { if (n == 0) return 1; int x = factorial(n-1);  return location B return n * x; }  return location A result = factorial(4); System.out.println(result); Factorial Activation Record x n Return location Trace (Activation Record) ? 0 if (n=0) return 1; B x = factorial(n-1); ? 1 1 1 return n * x; B B 2 2 2 B B B ? ? ? 2 3 3 3 3 B B B B ? ? ? ? 6 4 4 4 4 4 A A A A A Trace (factorial) public int factorial(int n) { if (n == 0) return 1; int x = factorial(n-1); return n * x; } factorial 4 return 4 * 6 = 24 factorial 3 return 3 * 2 = 6 factorial 2 return 2 * 1 = 2 factorial 1 return 1 * 1 = 1 factorial 0 return 1 Activation Records(summary) • Hold return location. • Temporary storage for local variables including parameters, if any. • Basis for re-entrant code. Fibonacci Numbers public int fib(int n) { if (n == 0 || n == 1) return n; int x = fib(n-1);  return location B int y = fib(n-2);  return location C return (x + y); } result = fib(4);  return location A System.out.println(result); Fibonacci Numbers Activation Record y x n Return location f(6) f(5) f(4) f(4) f(3) f(3) f(2) f(3) f(2) f(2) f(1) f(2) f(1) f(1) f(0) f(2) f(1) f(1) f(0) f(1) f(0) f(1) f(0) f(1) f(0) Backtracking • An exhaustive search is a technique of generating a solution from all combinations of partial solutions. • If any step leads to an invalid solution or infeasible solution, we backtrack to the most recent partial solution and try a different path to a full solution until we find the best solution. Example: Searching a maze X X X X X X X X O X O X X O O X O X O O O O X O O O O X O X O O O X O X O X X O O X X O O O O O T X X X X X O X X O O O O O O o findPath(x,y): The trick findPath(x,y-1) findPath(x-1,y) findPath(x+1,y) X findPath(x,y+1) Initial Algorithm • Start from position x,y such that Maze[x][y] = o • if findPath(x,y) output TARGET FOUND else output TARGET NOT FOUND findPath(x,y) if Maze[x][y] = T output x,y return true This is a simplification in the algorithm. What’s else if Maze[x][y] = X missing? return false else (must be an O) Maze[x][y] = X if findPath(x-1,y) OR findPath(x,y-1) OR findPath(x+1,y) OR findPath(x,y+1) output x,y return true else return false Dynamic Programming • Reduce the number of recursive calls by saving the return values of recursive calls as they are determined. • Use the saved value in place of an identical recursive call later in the execution. • Example: let d[ ] be a global array that holds the previously determined Fibonacci values. Give these initial values of –1. Example : Matrix Chain Multiplication LetA be a 100 x 10 matrix 1 LetA b2 a 10 x 20 matrix LetA b3 a 20 x 30 matrix How many operations are required to multiply A 1 A x 2 ? 3 Fibonacci Numbers Revisited Using Dynamic Programming public int fib(int n) { if (n == 0 || n == 1) { return n; } if (d[n-1] == -1) d[n-1] = fib(n-1); if (d[n-2] == -1) d[n-2] = fib(n-2); return (d[n-1]+d[n-2]); } Tail Recursion • If a method is defined such that it has one recursive call as the last computational statement, then the method is called tail recursive. • Every tail recursive method can be re- written as an equivalent method without recursion using a loop. • Example: factorial is tail recursive. Factorial Revisited public int factorial(int n) { if (n == 0) return 1; return n * factorial(n-1);  tail } recursion public int factorial(int n) { int product = 1; int i; Why should for (i = n; i >= 1; i--) we try to product = i * product; eliminate return product; tail recursion? } Reverse Print public static void reversePrint(int n) { if (n > 0) { System.out.println(n); reversePrint(n-1);  tail } recursion } public static void reversePrint(int n) { L: if (n > 0) { while (n > 0) { System.out.println(n); System.out.println(n); Set up new parameters n--; Jump to L // no code necessary } } } T owers of Hanoi public static void move(int n, int src, int dest, int aux) { if (n > 0) { move(n-1, src, aux, dest); System.out.println(src+" "+dest); move(n-1, aux, dest, src); }  tail } recursion public static void move(int n, int src, int dest, int aux) { while (n > 0) { move(n-1, src, aux, dest); System.out.println(src+" "+dest); n = n-1; exchange(aux, src); } }

×

×

### BOOM! Enjoy Your Free Notes!

×

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

## Why people love StudySoup

Steve Martinelli UC Los Angeles

#### "There's no way I would have passed my Organic Chemistry class this semester without the notes and study guides I got from StudySoup."

Kyle Maynard Purdue

#### "When you're taking detailed notes and trying to help everyone else out in the class, it really helps you learn and understand the material...plus I made \$280 on my first study guide!"

Jim McGreen Ohio University

Forbes

#### "Their 'Elite Notetakers' are making over \$1,200/month in sales by creating high quality content that helps their classmates in a time of need."

Become an Elite Notetaker and start selling your notes online!
×

### Refund Policy

#### STUDYSOUP CANCELLATION POLICY

All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email support@studysoup.com

#### STUDYSOUP REFUND POLICY

StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here: support@studysoup.com

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to support@studysoup.com