Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
45 slides
-
Memory Upload • Recursive factorial algorithm • Box trace for
recursive factorial • Iterative and recursive solutions for power function • Iterative and recursive solutions for Fibonacci series -
Factorial Example • For the Big-O lecture, we used loops
to calculate the factorial of a number • 2! = 2 * 1 = 2 • 3! = 3 * 2 * 1 = 6 • 4! = 4 * 3 * 2 * 1 = 24 • 5! = 5 * 4 * 3 * 2 * 1 = 120 • 6! = 6 * 5 * 4 * 3 * 2 * 1 = 720 • n! = n * n-1 * n-2 * n-3 * …* 3 * 2 * 1 -
Factorial Algorithm • Here is a recursive algorithm for calculating
a factorial Base case #1: if n is negative, return -1, because factorial is undefined Base case #2: if n is 0 or 1, return 1 Recursive case: return n * (method call for n-1) -
A Few Pointers • We can have more than one
base or recursive case • We must have at least one base case to stop the recursion • The recursive case must make the problem smaller in some way, or we will get infinite recursion -
Example Code • See the Factorials.java program for methods iterative()
and recursive() • Although both methods accomplish the same task, they use either a loop or recursion • More than one way to skin a cat! -
Big-O Break • Iterative factorial algorithm of looping from n
to 1 and multiplying the numbers • O(n), because we must loop though each number • Recursive factorial algorithm of having a base case (if n is 1) and recursive case to multiply numbers • O(n), because have a method call for each number -
Box Trace • Let’s do a box trace for factorial
4! • The box trace shows the runtime stack for the recursive() method • You should open jGRASP and make the line numbers visible, so you can trace though the program with me -
Box Trace • Program code • On line #41, the
method call pushes return address B and argument 4 to the runtime stack • Program control jumps to line #82 • Runtime stack return address=B number=4 -
Box Trace • Program code • The if statements on
lines #84 and #88 are false, so program control goes to the else statement on line #91 • This is the recursive case • Runtime stack return address=B number=4 -
• Program code • On line #94, the return statement
multiplies number by the method call with argument (number-1), which pushes the return address C and argument 3 • Runtime stack return address=C number=3 Box Trace return address=B number=4 return value=4*m(3) -
Box Trace • Program code • Program control jumps to
line #82 • The two if statements are false, so the program goes to the recursive case again • Runtime stack return address=C number=3 return address=B number=4 return value=4*m(3) -
Box Trace • Program code • On line #94, the
return statement is 3 * method(3-1), so the method call pushes return address C and argument 2 to the runtime stack • Runtime stack return address=B number=4 return value=4*m(3) return address=C number=3 return value=3*m(2) return address=C number=2 -
Box Trace • Program code • Program control jumps back
to the beginning of the method • The two if statements are false, so next is recursive case • Runtime stack return address=B number=4 return value=4*m(3) return address=C number=3 return value=3*m(2) return address=C number=2 -
Box Trace • Program code • The return statement multiplies
2 by method(2-1) • Return address C and argument 1 are pushed to the runtime stack return address=C number=1 return value=1 return address=B number=4 return value=4*m(3) return address=C number=3 return value=3*m(2) return address=C number=2 Return value=2*m(1) -
Box Trace • Program code • Program control jumps to
the beginning of the method, line #82 • On line #88, the if statement is true, so the base case returns 1 return address=C number=1 return value=1 return address=B number=4 return value=4*m(3) return address=C number=3 return value=3*m(2) return address=C number=2 Return value=2*m(1) -
• Program code • On line #89, the return statement
pops off the bottom most ARI on the stack, and returns the return value 1 to return address C on line #94 return address=B number=4 return value=4*m(3) return address=C number=3 return value=3*m(2) return address=C number=2 return value=2*1=2 • Runtime stack Box Trace -
Box Trace • Program code • On line #94, the
return statement pops off the bottom most ARI on the stack, and returns the return value 2 to return address C on line #94 return address=B number=4 return value=4*m(3) return address=C number=3 return value=3*2=6 • Runtime stack -
Box Trace • Program code • On line #94, the
return statement pops off the bottom most ARI on the stack, and returns the return value 6 to return address C on line #94 return address=B number=4 return value=4*6=24 • Runtime stack -
Box Trace • Program code • On line #94, the
return statement pops off the bottom most ARI on the stack, and returns the return value 24 to return address B on line #41 • Runtime stack • The main() method has an ARI as well args[0]="4" number=4 result=24 result2=24 -
jGRASP Debugger Box Trace • Make sure you mark the
code with a red dot breakpoint, press ladybug button, and press the “Step in for selected thread” button (top left blue arrow curving down to the right) • Also check out the call stack window to see the ARI for each method -
Power Function • To show another example, let’s write iterative
and recursive Java methods for x to the power of y (xy) 72 = 7 * 7 = 49 43 = 4 * 4 * 4 = 64 25 = 2 * 2 * 2 * 2 * 2 = 32 xy = x1 * x2 * x3 * … * xy-2 * xy-1 * xy -
Restrictions • In order to keep the code simple, we
will place a few restrictions on our factorial function Variables x and y have to be integers Variables x and y must be positive -
Iterative Algorithm 1. If x or y is negative, return
-1 (for error condition) 2. If y is 0, return 1 (special condition) 3. Initialize local variable product = 1 4. Loop from 1 to y 5. Within loop, multiply x by product, and store result in product 6. Return product -
Big-O Break 1. If x or y is negative, return
-1: O(1) 2. If y is 0, return 1: O(1) 3. Initialize local variable product = 1: O(1) 4. Loop from 1 to y: O(n) 5. Within loop, multiply x by product, and store result in product: O(1) 6. Return product: O(1) -
Big-O Break • Big-O calculations • O(1)+O(1)+O(1)+O(n)*O(1)+O(1) Multiply O(n)*O(1)=O(n)
• O(1)+O(1)+O(1)+O(n)+O(1) Ignore low-order terms • O(n) -
Example Code • See method iterative() in the Powers.java program
Don’t forget that we need two integers in the commandline input -
Recursion Pointers • When creating a recursive algorithm, think about
how to stop the recursion for the base case • The base case will have an if statement and a return statement to return a value -
Recursion Pointers • For the recursive case, you need to
make the problem smaller • This usually involves subtracting a number or letter from a variable • The return statement has the method call for the same method, but the argument in the method call should be a smaller value -
Recursive Algorithm 1. Base case #1: if y or x
is negative, return -1 (error) 2. Base case #2: if y equals 0, return 1 (mathematical definition) 3. Recursive case: return x * (method call with argument y-1) -
Big-O Break • Analyzing recursive algorithms for Big-O are not
so straightforward • We need to think about how many steps must be taken to reach base case from the recursive case for n • Equivalent to the number of ARIs (boxes with local environment data) pushed to the runtime stack -
Big-O Break • In the recursive power algorithm, the value
of argument x does not change, but the value of argument y decreases by 1 for each recursion • So we have y + 1 steps, as the base case is true when y is 0 • O(n+1) = O(n) -
Example Code • See method recursive() in the Powers.java program
Don’t forget that we need two integers for the commandline input -
Fibonacci Series • Add previous two numbers 0, 1,
1, 2, 3, 5, 8, 13, 21, 34, 55, ... • At least 2000 years ago, this series of numbers was known in India • Named after Leonardo Fibonacci who used the series to predict idealized breeding rates of rabbits in 1202 -
Fibonacci Series • Fibonacci series is often seen in nature
Scales of certain pinecones Sunflower floret spirals Pineapple leaves -
Fibonacci Series Details • Fibonacci numbers in terms of n
f0 =0, f1 =1, f2 =1, f3 =2, f4 =3, f5 =5, f6 =8, f7 =13, f8 =21, f9 =34, f10 =55, … • Recursive definition fib(0) = 0 if n = 0 fib(1) = 1 if n = 1 fib(n) = fib(n-1) + fib(n-2) if n > 0 fib(-n) = undefined if n < 0 -
Fibonacci Code • Recursive and iterative Java methods See
program Fibonacci.java Compare the two methods by number of method calls which are displayed as program output The recursive method has many more method calls than the iterative method! -
Big-O Break • Iterative Fibonacci method of looping from 3
to n and adding numbers • O(n-2)=O(n), because we must loop roughly n times -
Big-O Break • Recursive Fibonacci method of calling two methods
for each recursion • O(2n), because we have to double the number of method calls each time n increases by 1 (one) • This is an example of exponential Big- O, which has a very long runtime -
Method Calls for fib(5) fib(5) | fib(4) + fib(3) |
| fib(3) + fib(2) fib(2) + fib(1) | | | fib(2) + fib(1) fib(1) + fib(0) fib(1) + fib(0) | fib(1) + fib(0) -
Tradeoffs • Advantages to recursion Code itself is simple
and short Easy to read, understand and maintain the code • Disadvantages to recursion Because of method call overhead, an equivalent iterative solution is faster to execute on today’s computers -
Memory Defragmenter • Recursive algorithm for factorial • Factorial code
box trace • Power function • Fibonacci series -
Task Manager • Before the next class, you need to:
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture 4.Take a break by going on a nice hike!