### Create a StudySoup account

#### Be part of our community, it's free to join!

Already have a StudySoup account? Login here

# Computer Science II Chapter 8 CSE 214

Stony Brook U

### View Full Document

## About this Document

## 28

## 0

## Popular in Computer Science II

## 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.

## Popular in ComputerScienence

## Reviews for Computer Science II Chapter 8

### What is Karma?

#### Karma is the currency of StudySoup.

#### 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!

We've added these Notes to your profile, click here to view them now.

### You're already Subscribed!

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

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

#### "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!"

#### "Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

#### "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."

### 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

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.