Upgrade to Pro — share decks privately, control downloads, hide ads and more …

lecture07.pdf

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for William Albritton William Albritton
August 27, 2014
87

 lecture07.pdf

Avatar for William Albritton

William Albritton

August 27, 2014

Transcript

  1. Memory Upload • Recursive factorial algorithm • Box trace for

    recursive factorial • Iterative and recursive solutions for power function • Iterative and recursive solutions for Fibonacci series
  2. 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
  3. 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)
  4. 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
  5. 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!
  6. 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
  7. 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
  8. 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
  9. 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
  10. • 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)
  11. 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)
  12. 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
  13. 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
  14. 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)
  15. 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)
  16. • 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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)
  25. 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)
  26. Example Code • See method iterative() in the Powers.java program

     Don’t forget that we need two integers in the commandline input
  27. 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
  28. 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
  29. 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)
  30. 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
  31. 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)
  32. Example Code • See method recursive() in the Powers.java program

     Don’t forget that we need two integers for the commandline input
  33. 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
  34. Fibonacci Series • Fibonacci series is often seen in nature

     Scales of certain pinecones  Sunflower floret spirals  Pineapple leaves
  35. 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
  36. 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!
  37. 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
  38. 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
  39. 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)
  40. 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
  41. Memory Defragmenter • Recursive algorithm for factorial • Factorial code

    box trace • Power function • Fibonacci series
  42. 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!