Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
CS 126 Lecture P6:Recursion
CS126 7-1 Randy Wang
Why Learn Recursion?
• Master a powerful programming tool
• Gain insight of how programs (function calls) work
CS126 7-2 Randy Wang
Outline
• What is recursion?
• How does it work?
• Examples
Indentation level denotesstatements belongingto same “invocation”
CS126 7-6 Randy Wang
Demo convert()
CS126 7-7 Randy Wang
Outline
• What is recursion?
• How does it work?
• Examples
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
CS126 7-9 Randy Wang
Implementing Recursion
CS126 7-10 Randy Wang
Demo Use of Stacks to ImplementFunction Calls
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;
}}
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;
}}
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
CS126 7-15 Randy Wang
Possible Pitfall with Recursion
CS126 7-16 Randy Wang
Possible Pitfall with Recursion
CS126 7-17 Randy Wang
Outline
• What is recursion?
• How does it work?
• Examples
CS126 7-18 Randy Wang
Divide-and-Conquer
L RM L RM L RM
CS126 7-19 Randy Wang
Finding Root via Bisection
CS126 7-20 Randy Wang
Bisection for Integer Functions
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)
CS126 7-22 Randy Wang
Traveling Salesman Problem
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
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
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
CS126 7-28 Randy Wang
Recursive Program for Dragon Curve
A
B
C
dragon(n-1)
nogard(n-1)
L()
CS126 7-29 Randy Wang
Backwards Dragon Curve
Reverse
CS126 7-30 Randy Wang
dragon Demo
(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!
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?