cis068 08
TRANSCRIPT
![Page 1: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/1.jpg)
CIS 068
Welcome to CIS 068 !Lesson 8:
Stacks and Recursion
![Page 2: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/2.jpg)
CIS 068
Overview
Subjects:
• Stacks– Structure
– Methods
– Stacks and Method – Calls
• Recursion– Principle
– Recursion and Stacks
– Recursion vs. Iteration
– Examples
![Page 3: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/3.jpg)
CIS 068
Stacks: IntroductionWhat do these tools have in common ?
Plate Dispenser
PEZ ® Dispenser
![Page 4: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/4.jpg)
CIS 068
Stack: PropertiesAnswer:
They both provide LIFO (last in first out) Structures
12345
6
123456
![Page 5: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/5.jpg)
CIS 068
Stacks: PropertiesPossible actions:
• PUSH an object (e.g. a plate) onto dispenser
• POP object out of dispenser
![Page 6: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/6.jpg)
CIS 068
Stacks: DefinitionStacks are LIFO structures, providing
• Add Item (=PUSH) Methods
• Remove Item (=POP) Methods
They are a simple way to build a collection
• No indexing necessary
• Size of collection must not be predefined
• But: extremely reduced accessibility
![Page 7: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/7.jpg)
CIS 068
StacksApplication Areas
• LIFO order is desired
– See JVM-example on next slides
• ...or simply no order is necessary
– Lab-assignment 5: reading a collection of coordinates to draw
![Page 8: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/8.jpg)
CIS 068
A look into the JVMSample Code:1 public static void main(String args[ ]){
2 int a = 3;
3 int b = timesFive(a);
4 System.out.println(b+““);
5 }
6 Public int timesFive(int a){
7 int b = 5;
8 int c = a * b;
9 return (c);
10 }
![Page 9: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/9.jpg)
CIS 068
A look into the JVM
Inside the JVM a stack is used to
• create the local variables
• to store the return address from a call
• to pass the method-parameters
![Page 10: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/10.jpg)
CIS 068
A look into the JVM1 public static void main(String args[ ]){
2 int a = 3;
3 int b = timesFive(a);
4 System.out.println(b+““);
5 }
6 Public int timesFive(int a){
7 int b = 5;
8 int c = a * b;
9 return (c);
10 }a = 3
a = 3
b
Return to LINE 3
b = 5
c = 15
![Page 11: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/11.jpg)
CIS 068
A look into the JVM...
9 return (c);
10 ...
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
![Page 12: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/12.jpg)
CIS 068
A look into the JVMA look into the JVM1 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
![Page 13: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/13.jpg)
CIS 068
A look into the JVMA look into the JVM1 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
![Page 14: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/14.jpg)
CIS 068
A look into the JVMA look into the JVMImportant:
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
![Page 15: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/15.jpg)
CIS 068
RecursionRecursion
![Page 16: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/16.jpg)
CIS 068
RecursionRecursion
• Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first
• Recursion is a technique that solves a problem by solving a smaller problem of the same type
• A procedure that is defined in terms of itself
![Page 17: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/17.jpg)
CIS 068
RecursionRecursion
When you turn that into a program, you end up with functions that call themselves:
Recursive Functions
![Page 18: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/18.jpg)
CIS 068
RecursionRecursion
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 ?
![Page 19: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/19.jpg)
CIS 068
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)!
Factorial
![Page 20: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/20.jpg)
CIS 068
public int factorial(int a){if (a==0) return(1);else return(a * factorial( a-1));
}
Tracing the example
RECURSION !
![Page 21: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/21.jpg)
CIS 068
public int factorial(int a){if (a==1) return(1);else return(a * factorial( a-1));
}
Watching the Stack
a = 5 a = 5 a = 5Return to L4
a = 4Return to L4
a = 4Return to L4
a = 3Return to L4
a = 2Return to L4
a = 1
Initial After 1 recursion After 4th recursion
…
Every call to the method creates a new set of local variables !
![Page 22: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/22.jpg)
CIS 068
public int factorial(int a){if (a==1) return(1);else return(a * factorial( a-1));
}
Watching the Stack
a = 5Return to L4
a = 4Return to L4
a = 3Return to L4
a = 2Return to L4
a = 1
After 4th recursion
a = 5Return to L4
a = 4Return to L4
a = 3Return to L4a = 2*1 = 2
a = 5Return to L4
a = 4Return to L4a = 3*2 = 6
a = 5Return to L4a = 4*6 = 24
a = 5*24 = 120
Result
![Page 23: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/23.jpg)
CIS 068
Problems that can be solved by recursion have these characteristics:
• One or more stopping cases have a simple, nonrecursive solution
• The other cases of the problem can be reduced (using recursion) to problems that are closer to stopping cases
• Eventually the problem can be reduced to only stopping cases, which are relatively easy to solve
Follow these steps to solve a recursive problem:
• Try to express the problem as a simpler version of itself
• Determine the stopping cases
• Determine the recursive steps
Properties of Recursive Functions
![Page 24: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/24.jpg)
CIS 068
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
Solution on stackSolution on stack
Solution on stack
![Page 25: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/25.jpg)
CIS 068
Recursion does not terminate properly: Stack Overflow !
Common Programming Error
![Page 26: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/26.jpg)
CIS 068
Define a recursive solution for the following function:
f(x) = x
Exercise
n
![Page 27: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/27.jpg)
CIS 068
Recursion vs. Iteration
You could have written the power-function iteratively, i.e. using a loop construction
Where‘s the difference ?
![Page 28: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/28.jpg)
CIS 068
• Iteration can be used in place of recursion– An iterative algorithm uses a looping
construct– A recursive algorithm uses a branching
structure
• Recursive solutions are often less efficient, in terms of both time and space, than iterative solutions
• Recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code
• (Nearly) every recursively defined problem can be solved iteratively iterative optimization can be implemented after recursive design
Recursion vs. Iteration
![Page 29: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/29.jpg)
CIS 068
Deciding whether to use a Recursive Function
• When the depth of recursive calls is relatively “shallow”
• The recursive version does about the same amount of work as the nonrecursive version
• The recursive version is shorter and simpler than the nonrecursive solution
![Page 30: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/30.jpg)
CIS 068
Examples: Fractal Tree
http://id.mind.net/~zona/mmts/geometrySection/fractals/tree/treeFractal.html
![Page 31: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/31.jpg)
CIS 068
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
![Page 32: Cis068 08](https://reader033.vdocuments.mx/reader033/viewer/2022060109/55586937d8b42a8d018b49ba/html5/thumbnails/32.jpg)
CIS 068
Review
• A stack is a simple LIFO datastructure used e.g. by the JVM to create local variable spaces
• Recursion is a divide and conquer designing technique that often provides a simple algorithmic structure
• Recursion can be replaced by iteration for reasons of efficiency
• There is a connection between recursion and the use of stacks
• There are interesting problems out there that can be solved by recursion