Cis068 08

398 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
398
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Cis068 08

  1. 1. Welcome to CIS 068 ! Lesson 8: Stacks and Recursion
  2. 2. Overview <ul><li>Subjects: </li></ul><ul><li>Stacks </li></ul><ul><ul><li>Structure </li></ul></ul><ul><ul><li>Methods </li></ul></ul><ul><ul><li>Stacks and Method – Calls </li></ul></ul><ul><li>Recursion </li></ul><ul><ul><li>Principle </li></ul></ul><ul><ul><li>Recursion and Stacks </li></ul></ul><ul><ul><li>Recursion vs. Iteration </li></ul></ul><ul><ul><li>Examples </li></ul></ul>
  3. 3. Stacks: Introduction <ul><li>What do these tools have in common ? </li></ul>Plate Dispenser PEZ ® Dispenser
  4. 4. Stack: Properties <ul><li>Answer: </li></ul><ul><li>They both provide LIFO (last in first out) Structures </li></ul>1 2 3 4 5 6 1 2 3 4 5 6
  5. 5. Stacks: Properties <ul><li>Possible actions: </li></ul><ul><li>PUSH an object (e.g. a plate) onto dispenser </li></ul><ul><li>POP object out of dispenser </li></ul>
  6. 6. Stacks: Definition <ul><li>Stacks are LIFO structures, providing </li></ul><ul><li>Add Item (=PUSH) Methods </li></ul><ul><li>Remove Item (=POP) Methods </li></ul><ul><li>They are a simple way to build a collection </li></ul><ul><li>No indexing necessary </li></ul><ul><li>Size of collection must not be predefined </li></ul><ul><li>But: extremely reduced accessibility </li></ul>
  7. 7. Stacks <ul><li>Application Areas </li></ul><ul><li>LIFO order is desired </li></ul><ul><ul><li>See JVM-example on next slides </li></ul></ul><ul><li>...or simply no order is necessary </li></ul><ul><ul><li>Lab-assignment 5: reading a collection of coordinates to draw </li></ul></ul>
  8. 8. A look into the JVM <ul><li>Sample Code: </li></ul><ul><li>1 public static void main(String args[ ]){ </li></ul><ul><li>2 int a = 3; </li></ul><ul><li>3 int b = timesFive(a); </li></ul><ul><li>4 System.out.println(b+““); </li></ul><ul><li>5 } </li></ul><ul><li>6 Public int timesFive(int a){ </li></ul><ul><li>7 int b = 5; </li></ul><ul><li>8 int c = a * b; </li></ul><ul><li>9 return (c); </li></ul><ul><li>10 } </li></ul>
  9. 9. A look into the JVM <ul><li>Inside the JVM a stack is used to </li></ul><ul><li>create the local variables </li></ul><ul><li>to store the return address from a call </li></ul><ul><li>to pass the method-parameters </li></ul>
  10. 10. A look into the JVM <ul><li>1 public static void main(String args[ ]){ </li></ul><ul><li>2 int a = 3; </li></ul><ul><li>3 int b = timesFive(a); </li></ul><ul><li>4 System.out.println(b+““); </li></ul><ul><li>5 } </li></ul><ul><li>6 Public int timesFive(int a){ </li></ul><ul><li>7 int b = 5; </li></ul><ul><li>8 int c = a * b; </li></ul><ul><li>9 return (c); </li></ul><ul><li>10 } </li></ul>a = 3 a = 3 b Return to LINE 3 b = 5 c = 15
  11. 11. A look into the JVM <ul><li>... </li></ul><ul><li>return (c); </li></ul><ul><li>... </li></ul>a = 3 a = 3 b Return to LINE 3 b = 5 c = 15 15 a = 3 b Return to LINE 3 a = 3 b c = 15 Return to LINE 3 Temporary storage Clear Stack
  12. 12. A look into the JVM A look into the JVM 1 public static void main(String args[ ]){ 2 int a = 3; 3 int b = timesFive(a); 4 System.out.println(b+““); 5 } a = 3 b c = 15 Temporary storage a = 3 b = 15
  13. 13. A look into the JVM A look into the JVM 1 public static void main(String args[ ]){ 2 int a = 3; 3 int b = timesFive(a); 4 System.out.println(b+““); 5 } clear stack from local variables
  14. 14. A look into the JVM A look into the JVM Important: Every call to a method creates a new set of local variables ! These Variables are created on the stack and deleted when the method returns
  15. 15. Recursion Recursion
  16. 16. Recursion Recursion <ul><li>Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first </li></ul><ul><li>Recursion is a technique that solves a problem by solving a smaller problem of the same type </li></ul><ul><li>A procedure that is defined in terms of itself </li></ul>
  17. 17. Recursion Recursion When you turn that into a program, you end up with functions that call themselves: Recursive Functions
  18. 18. Recursion Recursion public int f(int a){ if (a==1) return(1); else return(a * f( a-1)); } It computes f! (factorial) What’s behind this function ?
  19. 19. Factorial Factorial: a! = 1 * 2 * 3 * ... * (a-1) * a Note: a! = a * (a-1)! remember: ...splitting up the problem into a smaller problem of the same type... a! a * (a-1)!
  20. 20. Tracing the example public int factorial(int a){ if (a==0) return(1); else return(a * factorial( a-1)); } RECURSION !
  21. 21. Watching the Stack public int factorial(int a){ if (a==1) return(1); else return(a * factorial( a-1)); } a = 5 a = 5 a = 5 Return to L4 a = 4 Return to L4 a = 4 Return to L4 a = 3 Return to L4 a = 2 Return to L4 a = 1 Initial After 1 recursion After 4 th recursion … Every call to the method creates a new set of local variables !
  22. 22. Watching the Stack public int factorial(int a){ if (a==1) return(1); else return(a * factorial( a-1)); } a = 5 Return to L4 a = 4 Return to L4 a = 3 Return to L4 a = 2 Return to L4 a = 1 After 4 th recursion a = 5 Return to L4 a = 4 Return to L4 a = 3 Return to L4 a = 2*1 = 2 a = 5 Return to L4 a = 4 Return to L4 a = 3*2 = 6 a = 5 Return to L4 a = 4*6 = 24 a = 5*24 = 120 Result
  23. 23. Properties of Recursive Functions <ul><li>Problems that can be solved by recursion have these characteristics: </li></ul><ul><li>One or more stopping cases have a simple, nonrecursive solution </li></ul><ul><li>The other cases of the problem can be reduced (using recursion) to problems that are closer to stopping cases </li></ul><ul><li>Eventually the problem can be reduced to only stopping cases, which are relatively easy to solve </li></ul><ul><li>Follow these steps to solve a recursive problem: </li></ul><ul><li>Try to express the problem as a simpler version of itself </li></ul><ul><li>Determine the stopping cases </li></ul><ul><li>Determine the recursive steps </li></ul>
  24. 24. Solution The recursive algorithms we write generally consist of an if statement: IF the stopping case is reached solve it ELSE split the problem into simpler cases using recursion Solution on stack Solution on stack Solution on stack
  25. 25. Common Programming Error Recursion does not terminate properly: Stack Overflow !
  26. 26. Exercise Define a recursive solution for the following function: f(x) = x n
  27. 27. Recursion vs. Iteration You could have written the power-function iteratively, i.e. using a loop construction Where‘s the difference ?
  28. 28. Recursion vs. Iteration <ul><li>Iteration can be used in place of recursion </li></ul><ul><ul><li>An iterative algorithm uses a looping construct </li></ul></ul><ul><ul><li>A recursive algorithm uses a branching structure </li></ul></ul><ul><li>Recursive solutions are often less efficient, in terms of both time and space , than iterative solutions </li></ul><ul><li>Recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code </li></ul><ul><li>(Nearly) every recursively defined problem can be solved iteratively  iterative optimization can be implemented after recursive design </li></ul>
  29. 29. Deciding whether to use a Recursive Function <ul><li>When the depth of recursive calls is relatively “shallow” </li></ul><ul><li>The recursive version does about the same amount of work as the nonrecursive version </li></ul><ul><li>The recursive version is shorter and simpler than the nonrecursive solution </li></ul>
  30. 30. Examples: Fractal Tree http://id.mind.net/~zona/mmts/geometrySection/fractals/tree/treeFractal.html
  31. 31. Examples: The 8 Queens Problem http://mossie.cs.und.ac.za/~murrellh/javademos/queens/queens.html Eight queens are to be placed on a chess board in such a way that no queen checks against any other queen
  32. 32. Review <ul><li>A stack is a simple LIFO datastructure used e.g. by the JVM to create local variable spaces </li></ul><ul><li>Recursion is a divide and conquer designing technique that often provides a simple algorithmic structure </li></ul><ul><li>Recursion can be replaced by iteration for reasons of efficiency </li></ul><ul><li>There is a connection between recursion and the use of stacks </li></ul><ul><li>There are interesting problems out there that can be solved by recursion </li></ul>

×