# COSO 1030 Section 4 Recursion. What is about Towers of Hanoi Divide and Conquer Strategy Recursion and Induction Thinking Recursively Recursion Pitfalls.

## Presentation on theme: "COSO 1030 Section 4 Recursion. What is about Towers of Hanoi Divide and Conquer Strategy Recursion and Induction Thinking Recursively Recursion Pitfalls."— Presentation transcript:

COSO 1030 Section 4 Recursion

What is about Towers of Hanoi Divide and Conquer Strategy Recursion and Induction Thinking Recursively Recursion Pitfalls Analyze Efficiency of Recursion Tail Recursion Elimination

Towers of Hanoi Move n disks from one peg to another with a help peg, can’t put a big disk on a small Mark pegs as source, destination and spare Or simply 1, 2 and 3 Print each move by saying “Move disk k from peg i to j”.

Divide and Conquer If there is only one disk, just move it. If we can move n-1 disks from source to spare, then we can move the biggest one from source to destination. And the problem size reduced by 1, we only need to move n-1 disks from spare to destination using source as helper

Pseudo Code (level 1) /** * Towers of Hanoi * @param n int – n disks * @param I int – source peg * @param j int – destination peg * @param k int – spare peg * print out each move */ Void hanoi(int n, int I, int j, int k)

{ /* divide */ if( n == 1) { print(“Move ” + n + “th disk from ”+ i + “ to ” + j); } else { // 1. move n-1 disks from i to k using j for help // 2. move nth disk from i to j // 3. move n-1 disks from k to j using i. } The sub problem 2. is easy to solve print(“Move ” + n + “th disk from ”+ i + “ to ” + j); To the 1 and 3, problem size is n-1

Conquer Solve Sub Problems Recursively To the 1: – Move n-1 disks from i to k using j – hanoi(n-1, i, k, j); To the 3: – Move n-1 disks from k to j using I – hanoi(n-1, k, j, I);

void hanoi(int n, int i, int j, int k) { /* merge */ if( n == 1) { print(“Move ” + n + “th disk from ”+ i + “ to ” + j); } else { // 1. move n-1 disks from i to k using j for help hanoi(n-1, i, k, j); // 2. move nth disk from i to j print(“Move ” + n + “th disk from ”+ i + “ to ” + j); // 3. move n-1 disks from k to j using i. hanoi(n-1, k, j, i); }

Recursion A function call it self directly or indirectly Recursive Function Termination – If it is defined on Natural numbers (N) A sequence with a minimal value – Base - the minimal value – N with 0 as base – Can’t recursively calculate the function on base – Any other well defined sequence can be mapped to a sub set of N

Recursion and Induction Inductive definition of String – String ::= EMPTY – String ::= String + CHAR Recursively counts space characters in a string int numOfSpaceCh(String string) { if(string == null) { return 0; // base } else { int n = numOfSpaceCh(string.substring(1)); // recursion return ((string.charAt(0) == ‘ ’) ? 1: 0) + n; // merge }

Think Recursively Divide into small problem(s) – Half and half – Head and tail – Random divide – Cut one at end Solve base directly Recursively solve sub problem(s) Merge the result

Recursion Pitfalls Infinite recursion – Not well ordered …, -n, -(n-1), …, -2, -1, 0 – hanoi(0, 1, 2, 3) Exponent complexity class – Hanoi(n) is O(2^n) – Fibonacci(n) could be O(2^n)

Analyze Recursion Efficiency Recursion Efficiency depends on – Number of recursive calls – Size reduced Hanoi(n) – Has 2 recursive calls – Size reduced by 1 – O(2*O(Hanoi(n-1))) – O(2^n)

Inductive Proof There exists K and n0 such that for any n >= n0, K 2^n <= Hanoi(n) There exists K’ and n0’ such that for any n >= n0’, Hanoi(n) <= K’ 2^n Let K = 1 and n0 = 2 Base n = n0: if; Hanoi(1); print; Hanoi(1); 2^2 = 4 = Steps of Hanoi(2) Use SofH(n) for Steps of Hanoi(n)

Induction Assume for any m = n - 1, m >= n0, 2^m <= SofH(m) SofH(m + 1) are no less than SofH(m) + 1 + SofH(m). By assumption 2^m <= SofH(m), we get SofH(m+1)<= 2 * 2^m = 2 ^(m+1) Conclusion: for any n > n0 Steps of Hanoi(n) >= 2 ^ n.

O(numOfSpaceCh()) One recursive call Length reduced by 1 It is O(n) where n is the length of a string 1 * Steps of numOfSpaceCh(n-1)

Tail Recursion A recursive function is tail recursion, if – It has only one recursive call – And the call is the last statement of the function numOfSpaceCh is a tail recursive function Iteration version int n = 0; for (int I = 0; I < string.length; I ++) { if (string.charAt(I) == ‘ ’) n++; }; return n;

Tail Recursion Elimination Recursive Version F(n) { if (n == 0) base; else { m = non-recursive(n); F(m); // m < n } Iteration Version F(n) { base; while (n != 0) { n = non-recursive(n); }

Why Eliminate Recursion Efficiency – Tail Recursion O(n) Time, O(n) Stack Space – Iteration O(n) Time, O(1) Stack Space

Summary Recursion – A Powerful Tool Apply Divide and Conquer Strategy Inductive Definition, Proof and Recursive Computation How to Efficiently Divide Problems Analyze Computation Complexity Avoidable Pitfalls Eliminate Tail Recursion

Download ppt "COSO 1030 Section 4 Recursion. What is about Towers of Hanoi Divide and Conquer Strategy Recursion and Induction Thinking Recursively Recursion Pitfalls."

Similar presentations