



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




1
Lecture 3 CS2110 – Summer 2008
2
Lots of consulting/office-hours available Can set up individual meetings with TAs via email
Assignment #1 must be done individually We treat AI violations seriously The AI hearing process is unpleasant Please help us avoid this process by maintaining Academic Integrity We test all pairs of submitted programming assignments for similarity Similarities are caught even if variables are renamed
4
factorial combinations exponentiation (raising to an integer power)
grammars expressions data structures (lists, trees, ...) 5
E.g., 3! = 3·2·1 = 6
There is just one permutation of one object. 1! = 1 There are two permutations of two objects: 2! = 2 1 2 2 1 There are six permutations of three objects: 3! = 6 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
General Approach to Writing Recursive Functions
8
y Mathematical definition: fib(0) = 0 fib(1) = 1 fib(n) = fib(n − 1) + fib(n − 2), n ≥ 2
y Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, …
static int fib(int n) { if (n == 0) return 0; else if (n == 1) return 1; else return fib(n-1) + fib(n-2); }
Fibonacci (Leonardo Pisano) 1170−1240?
Statue in Pisa, Italy Giovanni Paganucci 1863
9
static int fib(int n) { if (n == 0) return 0; else if (n == 1) return 1; else return fib(n-1) + fib(n-2); }
10
static int fib(int n) { if (n == 0) return 0; else if (n == 1) return 1; else return fib(n-1) + fib(n-2); }
Static int iterative_fib(int n) { if (n == 1 || n == 2) return 1; int last_num = 1, result = 1; for (int i = 2; i < n; i++) { int temp = result; result += last_num; last_num = temp; } return result; }
11
// pre: s != null // post: returns list of all permutations of s static ArrayList
/ Possibly useful functions /
// Deletes character at posn from s. // pre: s != null && 0 <= posn < s.length() // post: returns copy of s missing char at posn static String delete(String s, int posn);
// ArrayList methods void add(Object o); Object get(int posn); // String method char charAt(int posn);
12
// pre: s != null // post: returns list of all permutations of s static ArrayList
13
2-element subsets containing A: {A,B}, {A,C}, {A,D}, {A,E} 2-element subsets not containing A: {B,C},{B,D},{B,E},{C,D},{C,E},{D,E}
n r 5 2
21
static int power(int a, int n) { if (n == 0) return 1; int halfPower = power(a,n/2); if (n%2 == 0) return halfPowerhalfPower; return halfPowerhalfPowera; }*
yThe method has two parameters and a local variable yWhy aren’t these overwritten on recursive calls? (^22)
Use a stack to remember parameters and local variables across recursive calls Each method invocation gets its own stack frame
Local variables of method Parameters of method Return info (return address and return value) Perhaps other bookkeeping info
How the compiler implements recursive methods
23
y Like a stack of plates y You can push data on top or pop data off the top in a LIFO (last-in- first-out) fashion y A queue is similar, except it is FIFO (first-in-first-out)
top-of-stack pointer
stack grows
24
Leaving a return value (if there is one) on top of the stack
a stack frame
return info
local variables
parameters
return info
(a = ) 2
(n = ) 5
(hP = )?
return info
(a = ) 2
(n = ) 5
(hP = )?
return info
(a = ) 2
(n = ) 2
(hP = )?
return info
(a = ) 2
(n = ) 5
(hP = )?
return info
(a = ) 2
(n = ) 2
(hP = )?
return info
(a = ) 2
(n = ) 1
(hP = )?
return info
(a = ) 2
(n = ) 5
(hP = ) 4
return info
(a = ) 2
(n = ) 5
(hP = )?
return info
(a = ) 2
(n = ) 2
(hP = ) 2
return info
(a = ) 2
(n = ) 5
(hP = )?
return info
(a = ) 2
(n = ) 2
(hP = )?
return info
(a = ) 2
(n = ) 1
(hP = ) 1
(retval = ) 1
(retval = ) 2
(retval = ) 4
(retval = ) 32
y At any point in execution, many invocations of power may be in existence Many stack frames (all for power ) may be in Stack Thus there may be several different versions of the variables a and n
y How does processor know which location is relevant at a given point in the computation? Frame Base Register When a method is invoked, a frame is created for that method invocation, and FBR is set to point to that frame When the invocation returns, FBR is restored to what it was before the invocation
y How does machine know what value to restore in the FBR? This is part of the return info in the stack frame
27
y Computational activity takes place only in the topmost (most recently pushed) stack frame
return info
(a = ) 2
(n = ) 5
(hP = )?
return info
(a = ) 2
(n = ) 5
(hP = )?
return info
(a = ) 2
(n = ) 2
(hP = )?
return info
(a = ) 2
(n = ) 5
(hP = )?
return info
(a = ) 2
(n = ) 2
(hP = )?
return info
(a = ) 2
(n = ) 1
(hP = )?
FBR (^) FBR FBR
old FBR
old FBR
old FBR
old FBR old FBR old FBR
28
But many modern languages support both
Which is clearer? Which is more intuitive? (often recursion) Which is faster? Which uses less memory? (often iteration)
Memory overhead: stack frames Execution time overhead: method calls
Reduce a big problem to smaller problems of the same kind, solve the smaller problems Recombine the solutions to smaller problems to form solution for big problem