35
CS 126 Lecture P6: Recursion

CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS 126 Lecture P6:Recursion

Page 2: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-1 Randy Wang

Why Learn Recursion?

• Master a powerful programming tool

• Gain insight of how programs (function calls) work

Page 3: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-2 Randy Wang

Outline

• What is recursion?

• How does it work?

• Examples

Page 4: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?
Page 5: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?
Page 6: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

Indentation level denotesstatements belongingto same “invocation”

Page 7: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-6 Randy Wang

Demo convert()

Page 8: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-7 Randy Wang

Outline

• What is recursion?

• How does it work?

• Examples

Page 9: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-8 Randy Wang

Function “Environment”

• When a function executes, it lives in an “environment”

• What’s an “environment”?- Value of local variables (scratch space)- Which statement the computer is executing currently

Page 10: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-9 Randy Wang

Implementing Recursion

Page 11: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-10 Randy Wang

Demo Use of Stacks to ImplementFunction Calls

Page 12: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?
Page 13: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-12 Randy Wang

Removing Recursion{

base case;

some code;

recursion;

more code;

}

{repeat {

some code;push environment;

}base case;repeat {

pop environment;more code;

}}

Page 14: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-13 Randy Wang

Removing Recursion{

base case;

some code;

recursion;

more code;

}

{repeat {

some code;push environment;

}base case;repeat {

pop environment;more code;

}}

Page 15: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-14 Randy Wang

Tail Recursion

• If single recursive call is the last action, don’t need a stack

• Why? - nothing to do after recursion => no need to remember stuff => no need for stack

Page 16: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-15 Randy Wang

Possible Pitfall with Recursion

Page 17: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-16 Randy Wang

Possible Pitfall with Recursion

Page 18: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-17 Randy Wang

Outline

• What is recursion?

• How does it work?

• Examples

Page 19: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-18 Randy Wang

Divide-and-Conquer

L RM L RM L RM

Page 20: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-19 Randy Wang

Finding Root via Bisection

Page 21: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-20 Randy Wang

Bisection for Integer Functions

Page 22: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-21 Randy Wang

Binary Search

• Observations:- An array is a function mapping integer indices to contents- A sorted array is a monotonically increasing function

v

a[k]

a[k]-v

k

xf(x)

Page 23: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-22 Randy Wang

Traveling Salesman Problem

Page 24: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-23 Randy Wang

Traveling Salesman Problemset of cities whose orderhaven’t been decided

set of cities whose orderhave been decided

try all undecided citiesas the Nth stop

Number of nodes whose positionshave not been decided

Visit ith city as the last (Nth) step

Decide the positions of theother undecided cities

Page 25: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-24 Randy Wang

Traveling Salesman Problemvisit(3 ) [1 2 3 ]{}

v is it(2 ) [3 2 ]{1} v isit(2) [1 3 ]{2 } v isit(2) [1 2 ]{3 }

v isit(1 ) [3 ]{2 1} v isit(1) [2 ]{3 1 }v isit(1 ) [1 ]{3 2 } v isit(1) [3 ]{1 2 }

v isit(1) [1 ]{2 3 } v isit(1) [2 ]{1 3 }

nodes w h ose p osition sare no t decided

nodes w hose positionsare a lready decid ed

n um b er o fu ndecid ed nod es

Page 26: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?
Page 27: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?
Page 28: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-27 Randy Wang

Intuition of Algorithm

• AB is a smaller dragon curve by itself

• CB = AB

• Therefore BC is the reverse of AB

• Therefore every turn along BC is the opposite of the corresponding turn on AB

A

B

C

Right turn

Left turn

Left turn

Page 29: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-28 Randy Wang

Recursive Program for Dragon Curve

A

B

C

dragon(n-1)

nogard(n-1)

L()

Page 30: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-29 Randy Wang

Backwards Dragon Curve

Reverse

Page 31: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-30 Randy Wang

dragon Demo

Page 32: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?
Page 33: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

(dup repliates stack top)

replicates top before popping for comparison

pushes two copies of (n-1) for the two recursive calls

all arguments and “scratch variables” are on the stack!

Page 34: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?
Page 35: CS 126 Lecture P6: RecursionCS 126 Lecture P6: Recursion. CS126 7-1 Randy Wang Why Learn Recursion?

CS126 7-34 Randy Wang

What We Have Learned

• How recursion works- A recursive call is no different from a “regular” call- It involves saving the old environment for later return

• Learn to trace the execution of given recursive programs (using pictures)

• Learn to write simple recursion - What’s the base case?- What’s the induction case?