Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion © Dave Bockus.

Similar presentations


Presentation on theme: "Recursion © Dave Bockus."— Presentation transcript:

1 Recursion © Dave Bockus

2 Principles and Rules Base cases: you must always have some base cases, which can be solved without recursion. Solving the recursive function f(x) = 2f(x-1) + x2 Example 1 Note a base case generally kills the recursion by not allowing future recursive calls. int f (int x) { if (x == 0) return 0 else return 2*f(x-1)+x*x Base Case: since at this point recursion is not needed to determine a return value.

3 Example 2 PrintList (node ptr){ if (ptr != null) { print(ptr.data); PrintList(ptr.next); } } If statement prevents recursive part from being called.

4 Principles and Rules Cont...
Making progress: For the cases that are solved recursively, the recursive call must always be to a case that makes progress toward a base case int f (int x) { if (x == 0) return 0 else return 2*f(x-1)+x*x X is reduced toward the base case. PrintList (node ptr){ if (ptr != null) { print(ptr.data); PrintList(ptr.next); } } We assume that ptr.next will take us toward the end of the list, hence a null pointer.

5 Principles and Rules Cont.
Design rule: Assume that all recursive calls work. In general the machine uses its own stack to organize the calls. Very difficult to trace the calls. Important to ensure the code adheres to the first two principles, this rule then takes care of itself.

6 Principles and Rules Cont.
Compound interest rule: Never duplicate work by solving the same instance of a problem in separate recursive calls. fib( int n) { if (n <= 1) return 1; else return fib(n-1) + fib(n-2); } 2 3 1 1 13 5 8

7 Principles and Rules Cont.
n = 3 the first instance of fib calculates: fib(n-1) ==> fib(2) and fib(n-2) ==> fib(1) line 1 fib(2) results in instances fib(1) and fib(0) line 2 Notice: Instance of fib(1) is now known form line 2 But fib(1) in line 1 has yet to be processed So fib(1) is recalculated. Results in: Slow algorithms Increases the magnitude unnecessarily

8 What the Machine does - Call Entry
Each program has a “Call Stack”. When a method is called the state of the system is stored in a Call Frame. This includes all local variables and state information. It is like taking a snap shot of the system. Method B{…} Method A { Call Method B } Main(){ Call Method A Call Stack with Call Frames for each suspend process Current state of Method A is stored when Method B is Called, including local variables System state and local variables of Main Method Method B is called, forcing Method A to be suspended Method A is Called

9 What the Machine does - Call Return
When a method returns: The call frame is removed from the stack Local variables are restored System continues from this returned state. Since a call frame stores a system state, we can say a history is also stored. Previous values, Previous locations. History is a state of suspended animation which can be woke at the correct time

10 We wish to print the list forward and backward using recursion.
Example of Recursion We wish to print the list forward and backward using recursion. PrintList (node ptr){ if (ptr != null) { print(ptr.data); PrintList(ptr.next); } } D A B C Head The result of the code would be: A B C D D C B A

11 Notice that B C & D are simply a sub-problem of the entire list
Entry Ptr => Node A PrintList (node ptr){ if (ptr != null) { print(ptr.data); PrintList(ptr.next); } } Ptr.Next => Node C Ptr.Next => Null Ptr.Next => Node D Ptr.Next => Node B A B C D D C B A D A B C Head Notice that B C & D are simply a sub-problem of the entire list


Download ppt "Recursion © Dave Bockus."

Similar presentations


Ads by Google