Presentation is loading. Please wait.

Presentation is loading. Please wait.

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Recursive Algorithm.

Similar presentations


Presentation on theme: "LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Recursive Algorithm."— Presentation transcript:

1 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Recursive Algorithm

2 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 2 Examples of recursion…

3 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 3 5. Recursion  A problem solving method of “decomposing bigger problems into smaller sub-problems that are identical to itself.”  General Idea: oSolve simplest (smallest) cases DIRECTLY uusually these are very easy to solve oSolve bigger problems using smaller sub-problems uthat are identical to itself (but smaller and simpler)  Abstraction: oTo solve a given problem, we first assume that we ALREADY know how to solve it for smaller instances!!  Dictionary definition: recursion see recursion

4 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 4 5. Recursion  Dictionary definition: recursion see recursion  Simple Examples from Real Life… oTV within a TV o2 parallel mirrors o1 + the previous number o“Tomorrow”  Recursion Examples from the Web. oRecursive Trees (turtle) – herehere oTrees and Tower-of-Hanoi – herehere oRecursion and Biology – herehere

5 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 5 Example: Fibonacci Numbers…  Definition of Fibonacci numbers 1.F 1 = 1, 2.F 2 = 1, 3.for n>2, F n = F n-1 + F n-2  Problem: Compute F n for any n.  The above is a recursive definition. oF n is computed in-terms of itself oactually, smaller copies of itself – F n-1 and F n-2  Actually, Not difficult: F 3 = 1 + 1 = 2F 6 = 5 + 3 = 8F 9 = 21 + 13 = 34 F 4 = 2 + 1 = 3F 7 = 8 + 5 = 13 F 10 = 34 + 21 = 55 F 5 = 3 + 2 = 5F 8 = 13 + 8 = 21F 11 = 55 + 34 = 89 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

6 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 6 Fibonacci Numbers: Recursive alg  The above is a recursive algorithm  It is simple to understand and elegant!  But, very SLOW Fibonacci(n) (* Recursive, SLOW *) begin if (n=1) or (n=2) then Fibonacci(n)  1 (*simple case*) else Fibonacci(n)  Fibonacci(n-1) + Fibonacci(n-2) endif end;

7 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 7 Recursive Fibonacci Alg -- Remarks  How slow is it? oEg: To compute F(6)… F(5) F(4)F(3) F(2) F(1) F(2) F(1) F(4) F(3)F(2) F(1) F(6) J HW: Can we compute it faster?

8 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 8 Given: Three Pegs A, B and C Peg A initially has n disks, different size, stacked up, larger disks are below smaller disks Problem: to move the n disks to Peg C, subject to 1.Can move only one disk at a time 2.Smaller disk should be above larger disk 3.Can use other peg as intermediate Example: Tower of Hanoi ABCABC

9 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 9 Tower of Hanoi  How to Solve: Strategy… oGeneralize first: Consider n disks for all n  1 oOur example is only the case when n=4  Look at small instances… oHow about n=1 uOf course, just “Move disk 1 from A to C” oHow about n=2? 1.“Move disk 1 from A to B” 2.“Move disk 2 from A to C” 3.“Move disk 1 from B to C”

10 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 10 Tower of Hanoi (Solution!)  General Method: oFirst, move first (n-1) disks from A to B oNow, can move largest disk from A to C oThen, move first (n-1) disks from B to C  Try this method for n=3 1.“Move disk 1 from A to C” 2.“Move disk 2 from A to B” 3.“Move disk 1 from C to B” 4.“Move disk 3 from A to C” 5.“Move disk 1 from B to A” 6.“Move disk 1 from B to C” 7.“Move disk 1 from A to C”

11 LeongHW, SoC, NUS (UIT2201: Algorithms) Page 11 Algorithm for Towel of Hanoi (recursive)  Recursive Algorithm owhen (n=1), we have simple case oElse (decompose problem and make recursive-calls) Hanoi(n, A, B, C); (* Move n disks from A to C via B *) begin if (n=1) then “Move top disk from A to C” else (* when n>1 *) Hanoi (n-1, A, C, B); “Move top disk from A to C” Hanoi (n-1, B, C, A); endif end;


Download ppt "LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Recursive Algorithm."

Similar presentations


Ads by Google