coso 1030 section 4

20
COSO 1030 Section 4 COSO 1030 Section 4 Recursion

Upload: echo-jensen

Post on 30-Dec-2015

20 views

Category:

Documents


1 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COSO 1030 Section 4

COSO 1030 Section 4COSO 1030 Section 4

Recursion

Page 2: COSO 1030 Section 4

What is aboutWhat is about

Towers of HanoiDivide and Conquer Strategy Recursion and InductionThinking RecursivelyRecursion PitfallsAnalyze Efficiency of RecursionTail Recursion Elimination

Page 3: COSO 1030 Section 4

Towers of HanoiTowers 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 spareOr simply 1, 2 and 3Print each move by saying “Move disk k

from peg i to j”.

Page 4: COSO 1030 Section 4

Divide and ConquerDivide 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

Page 5: COSO 1030 Section 4

Pseudo Code (level 1)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)

Page 6: COSO 1030 Section 4

{ /* 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

Page 7: COSO 1030 Section 4

ConquerConquer

Solve Sub Problems RecursivelyTo 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);

Page 8: COSO 1030 Section 4

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);

}}

Page 9: COSO 1030 Section 4

RecursionRecursion

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

Page 10: COSO 1030 Section 4

Recursion and InductionRecursion and Induction

Inductive definition of String– String ::= EMPTY– String ::= String + CHAR

Recursively counts space characters in a stringint 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}

}

Page 11: COSO 1030 Section 4

Think RecursivelyThink Recursively

Divide into small problem(s)– Half and half– Head and tail– Random divide– Cut one at end

Solve base directlyRecursively solve sub problem(s)Merge the result

Page 12: COSO 1030 Section 4

Recursion PitfallsRecursion 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)

Page 13: COSO 1030 Section 4

Analyze Recursion EfficiencyAnalyze 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)

Page 14: COSO 1030 Section 4

Inductive ProofInductive 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 = 2Base n = n0: if; Hanoi(1); print; Hanoi(1);

2^2 = 4 = Steps of Hanoi(2)Use SofH(n) for Steps of Hanoi(n)

Page 15: COSO 1030 Section 4

InductionInduction

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 getSofH(m+1)<= 2 * 2^m = 2 ^(m+1)

Conclusion: for any n > n0Steps of Hanoi(n) >= 2 ^ n.

Page 16: COSO 1030 Section 4

O(numOfSpaceCh())O(numOfSpaceCh())

One recursive callLength reduced by 1It is O(n) where n is the length of a string

1 * Steps of numOfSpaceCh(n-1)

Page 17: COSO 1030 Section 4

Tail RecursionTail 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;

Page 18: COSO 1030 Section 4

Tail Recursion EliminationTail Recursion Elimination

Recursive Version

F(n) {

if (n == 0) base;

else {

m = non-recursive(n);

F(m); // m < n

}

}

Iteration VersionF(n) { base; while (n != 0) { n = non-recursive(n); }}

Page 19: COSO 1030 Section 4

Why Eliminate RecursionWhy Eliminate Recursion

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

Page 20: COSO 1030 Section 4

SummarySummary

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