Presentation is loading. Please wait.

Presentation is loading. Please wait.

FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion.

Similar presentations


Presentation on theme: "FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion."— Presentation transcript:

1 FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

2 FIT1002 2006 2 Objectives By the end of this lecture, students should: understand recursive method calls understand the basic principles of solving problems with a recursive approach be able to write simple recursive programs

3 FIT1002 2006 3 What is Recursion? A definition in terms of itself: “Your parents are your ancestors and the parents of your ancestors are also your ancestors” To avoid infinite recursion we need a base case. We can define (problem solving) methods recursively: – Base case – Recursive definition – Convergence to base case

4 FIT1002 2006 4 Example: Factorial n! = n  (n - 1)  (n - 2) ...  2  1 0! = 1 Example: 5! = 5  4  3  2  1 = 120 Given n  0:

5 FIT1002 2006 5 Example: Factorial Recursion : n! = n  (n - 1)  (n - 2) ...  2  1 (n - 1)! If n > 1: Factorial(n) = n  Factorial(n - 1)

6 FIT1002 2006 6 int factorial ( int n ) { if ( n is less than or equal to 1 ) then return 1 else return n  factorial ( n - 1 ) } Example: Factorial

7 FIT1002 2006 7 function Factorial ( n ) { if ( n is less than or equal to 1 ) then return 1 else return n  Factorial ( n - 1 ) } Example: Factorial Base case

8 FIT1002 2006 8 function Factorial ( n ) { if ( n is less than or equal to 1 ) then return 1 else return n  Factorial ( n - 1 ) } Example: Factorial General Case

9 FIT1002 2006 9 function Factorial ( n ) { if ( n is less than or equal to 1 ) then return 1 else return n  Factorial ( n - 1 ) } Example: Factorial Recursion

10 FIT1002 2006 10 function Factorial ( n ) { if ( n is less than or equal to 1 ) then return 1 else return n  Factorial ( n - 1 ) } Example: Factorial Convergence

11 FIT1002 2006 11 Unary Recursion Functions calls itself once (at most) Usual format: function RecursiveFunction ( ) { if ( ) then return else return RecursiveFunction ( ) } Winding and unwinding the “stack frames”

12 FIT1002 2006 12 Example: Factorial Factorial(4) Factorial(3)4  Factorial(2)3  Factorial(1)2  1

13 FIT1002 2006 13 Example: Factorial Factorial(4) Factorial(3)4  Factorial(2)3  Factorial(1)2  1

14 FIT1002 2006 14 Example: Factorial Factorial(4) Factorial(3)4  Factorial(2)3  12 

15 FIT1002 2006 15 Example: Factorial Factorial(4) Factorial(3)4  Factorial(2)3  12 

16 FIT1002 2006 16 Example: Factorial Factorial(4) Factorial(3)4  23 

17 FIT1002 2006 17 Example: Factorial Factorial(4) Factorial(3)4  23 

18 FIT1002 2006 18 Example: Factorial Factorial(4) 64 

19 FIT1002 2006 19 Example: Factorial Factorial(4) 64 

20 FIT1002 2006 20 Example: Factorial 24

21 FIT1002 2006 21 /* Compute the factorial of n */ int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial(n-1); } Computes the factorial of a number function Factorial ( n ) { if ( n is less than or equal to 1) then return 1 else return n  Factorial ( n - 1 ) } Example: factorial.java

22 FIT1002 2006 22 Example: “Frames” during calculation of factorial(4) int x = factorial(4));

23 FIT1002 2006 23 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) 4 44 4 n:

24 FIT1002 2006 24 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 34 4 n:

25 FIT1002 2006 25 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 34 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1); } 3 33 3 n:

26 FIT1002 2006 26 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 34 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 3 23 3 n:

27 FIT1002 2006 27 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 34 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 3 23 3 n: 4 34 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1); } 2 22 2 n:

28 FIT1002 2006 28 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 34 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 3 23 3 n: 4 34 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 2 12 2 n:

29 FIT1002 2006 29 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 34 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 3 23 3 n: 4 34 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 2 12 2 n: 34 3 23 4 34 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n -1); } 1 11 1 n: Base case: factorial(1) is 1

30 FIT1002 2006 30 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 3 n: 4 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 2 n: 1 2 21 factorial(1) is 1

31 FIT1002 2006 31 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 3 n: 4 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * } 2 n: 2 21 factorial(1) is 1

32 FIT1002 2006 32 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 3 n: 4 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n } 2 n: 2 2

33 FIT1002 2006 33 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 3 n: 2 23 3 factorial(2) is 2

34 FIT1002 2006 34 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * } 3 n: 23 3 factorial(2) is 2

35 FIT1002 2006 35 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n } 3 n: 6 3

36 FIT1002 2006 36 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 n: 6 34 4 factorial(3) is 6

37 FIT1002 2006 37 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * } Example: “Frames” during calculation of factorial(4) 4 n: 64 4 factorial(3) is 6

38 FIT1002 2006 38 int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n } Example: “Frames” during calculation of factorial(4) 4 n: 24 4

39 FIT1002 2006 39 int x = factorial(4)); Example: “Frames” during calculation of factorial(4) 24 factorial(4) is 24

40 FIT1002 2006 40 N -ary Recursion Sometimes a function can only be defined in terms of two or more calls to itself. Efficiency is often a problem.

41 FIT1002 2006 41 Example: Fibonacci A series of numbers which begins with 0 and 1 every subsequent number is the sum of the previous two numbers 0, 1, 1, 2, 3, 5, 8, 13, 21,... Write a recursive function which computes the n -th number in the series ( n = 0, 1, 2,...)

42 FIT1002 2006 42 Example: Fibonacci The Fibonacci series can be defined recursively as follows: Fibonacci(0) = 0 Fibonacci(1) = 1 Fibonacci(n) = Fibonacci(n - 2) + Fibonacci(n - 1)

43 FIT1002 2006 43 /* Compute the n-th Fibonacci number, when=0,1,2,... */ long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } function Fibonacci ( n ) { if ( n is less than or equal to 1 ) then return n else return Fibonacci ( n - 2 ) + Fibonacci ( n - 1 ) } Fibonacci

44 FIT1002 2006 44 Example: Computation of fib(4) + fib(2)fib(3) fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

45 FIT1002 2006 45 Example: Computation of fib(4) fib(2)fib(3) + fib(4) + fib(0)fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

46 FIT1002 2006 46 Example: Computation of fib(4) fib(2)fib(3) + fib(4) + fib(0)fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

47 FIT1002 2006 47 Example: Computation of fib(4) fib(2)fib(3) + fib(4) + 0fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

48 FIT1002 2006 48 Example: Computation of fib(4) fib(2)fib(3) + fib(4) + fib(1)0 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

49 FIT1002 2006 49 Example: Computation of fib(4) fib(2)fib(3) + fib(4) + fib(1)0 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

50 FIT1002 2006 50 Example: Computation of fib(4) fib(2)fib(3) + fib(4) + 10 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

51 FIT1002 2006 51 Example: Computation of fib(4) fib(2)fib(3) + fib(4) + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

52 FIT1002 2006 52 Example: Computation of fib(4) 1fib(3) + fib(4) + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

53 FIT1002 2006 53 Example: Computation of fib(4) + fib(3) fib(4) 1 + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

54 FIT1002 2006 54 Example: Computation of fib(4) + fib(3) fib(4) 1 + 01 + fib(1)fib(2) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

55 FIT1002 2006 55 Example: Computation of fib(4) + fib(3) fib(4) 1 + 01 + fib(1)fib(2) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

56 FIT1002 2006 56 Example: Computation of fib(4) + fib(3) fib(4) 1 + 01 + 1 fib(2) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

57 FIT1002 2006 57 Example: Computation of fib(4) + fib(3) fib(4) 1 + 01 + 1 fib(2) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

58 FIT1002 2006 58 Example: Computation of fib(4) + fib(3) fib(4) 1 + 01 + 1fib(2) + fib(0)fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

59 FIT1002 2006 59 Example: Computation of fib(4) + fib(3) fib(4) 1 + 01 + 1fib(2) + fib(0)fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

60 FIT1002 2006 60 Example: Computation of fib(4) + fib(3) fib(4) 1 + 01 + 1fib(2) + 0 fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

61 FIT1002 2006 61 Example: Computation of fib(4) + fib(3) fib(4) 1 + 01 + 1fib(2) + 0 fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

62 FIT1002 2006 62 Example: Computation of fib(4) + fib(3) fib(4) 1 + 01 + 1fib(2) + 0fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

63 FIT1002 2006 63 Example: Computation of fib(4) + fib(3) fib(4) 1 + 01 + 1fib(2) + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

64 FIT1002 2006 64 Example: Computation of fib(4) + fib(3) fib(4) 1 + 01 + 1fib(2) + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

65 FIT1002 2006 65 Example: Computation of fib(4) + fib(3) fib(4) 1 + 01 + 11 + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

66 FIT1002 2006 66 Example: Computation of fib(4) + fib(3) fib(4) 1 + 01 + 11 + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

67 FIT1002 2006 67 Example: Computation of fib(4) + 2 fib(4) 1 + 01 + 11 + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

68 FIT1002 2006 68 Example: Computation of fib(4) + 2 fib(4) 1 + 01 + 11 + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

69 FIT1002 2006 69 Example: Computation of fib(4) + 2 3 1 + 01 + 11 + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

70 FIT1002 2006 70 Example: Computation of fib(4) + 2 3 1 + 01 + 11 + 01 Thus, fib(4) returns the value 3.

71 FIT1002 2006 71 Towers of Hanoi A classic problem Three pegs N discs, arranged bottom to top by decreasing size Objective: Move the discs from peg 1 to peg 3 Two constraints: One disk is moved at a time No larger disc can be placed above a smaller disk Write a program which will print the precise sequence of peg-to- peg disc transfers.

72 FIT1002 2006 72 Towers of Hanoi Base case: N = 1 Peg 1Peg 2Peg 3 Peg 1  Peg 3

73 FIT1002 2006 73 Towers of Hanoi Base case: N = 1 Peg 1Peg 2Peg 3 Peg 1  Peg 3

74 FIT1002 2006 74 Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Top N-1 discs: Peg 1  Peg 2

75 FIT1002 2006 75 Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Top N-1 discs: Peg 1  Peg 2

76 FIT1002 2006 76 Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Largest disc: Peg 1  Peg 3

77 FIT1002 2006 77 Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Largest disc: Peg 1  Peg 3

78 FIT1002 2006 78 Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Top N-1 discs: Peg 2  Peg 3

79 FIT1002 2006 79 Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Top N-1 discs: Peg 2  Peg 3

80 FIT1002 2006 80 Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Acted as temporary holding peg.

81 FIT1002 2006 81 Towers of Hanoi Q: But how do you move those N -1 discs from Peg 1 to the temporary holding peg? A: Recursively, of course! E.g., “move N -1 discs from Peg 1 to Peg 2 using Peg 3 as temporarily holding area,” and so on. Denote: fromPeg, toPeg Temporary holding peg: otherPeg

82 FIT1002 2006 82 Towers of Hanoi procedure ToH ( numDiscs, fromPeg, toPeg ) { if ( numDiscs is 1 ) then output fromPeg, “ -> ”, toPeg else { otherPeg = /* determine otherPeg */ ToH ( numDiscs - 1, fromPeg, otherPeg ) output fromPeg, “ -> ”, toPeg ToH ( numDiscs - 1, otherPeg, toPeg ) } Base case

83 FIT1002 2006 83 Towers of Hanoi procedure ToH ( numDiscs, fromPeg, toPeg ) { if ( numDiscs is 1 ) then output fromPeg, “->”, toPeg else { otherPeg = /* determine temporary holding peg here */ ToH ( numDiscs - 1, fromPeg, otherPeg ) output fromPeg, “->”, toPeg ToH ( numDiscs - 1, otherPeg, toPeg ) } Recursion

84 FIT1002 2006 84 Towers of Hanoi procedure ToH ( numDiscs, fromPeg, toPeg ) { if ( numDiscs is 1 ) then output fromPeg, “ -> ”, toPeg else { otherPeg = theOtherPeg(fromPeg, toPeg) ToH(numDiscs - 1, fromPeg, otherPeg) output fromPeg, “ -> ”, toPeg ToH(numDiscs - 1, otherPeg, toPeg) }

85 FIT1002 2006 85 Towers of Hanoi function theOtherPeg ( pegA, pegB ) { if ( pegA is 1 ) then { if ( pegB is 2 ) then return 3 else return 2 } else if ( pegA is 2 ) then { if ( pegB is 1 ) then return 3 else return 1; } else if ( pegA is 3 ) then { if ( pegB is 2 ) then return 1 else return 2; } Solution 1

86 FIT1002 2006 86 Towers of Hanoi otherPeg is always 6 - (fromPeg + toPeg)

87 FIT1002 2006 87 Towers of Hanoi function theOtherPeg (fromPeg, toPeg ) { return ( 6 - fromPeg - toPeg ) } Solution 2

88 FIT1002 2006 88 Towers of Hanoi procedure ToH ( numDiscs, fromPeg, toPeg ) { if ( numDiscs is 1 ) then output fromPeg, “ -> ”, toPeg else { otherPeg = theOtherPeg(fromPeg, toPeg) ToH(numDiscs - 1, fromPeg, otherPeg) output fromPeg, “ -> ”, toPeg ToH(numDiscs - 1, otherPeg, toPeg) }

89 FIT1002 2006 89 Towers of Hanoi procedure ToH ( numDiscs, fromPeg, toPeg ) { if ( numDiscs is 1 ) then output fromPeg, “ -> ”, toPeg else { otherPeg = theOtherPeg(fromPeg, toPeg) ToH(numDiscs - 1, fromPeg, otherPeg) output fromPeg, “ -> ”, toPeg ToH(numDiscs - 1, otherPeg, toPeg) } Convergence

90 FIT1002 2006 90 hanoi.java /* Given two pegs, this function determines the other peg. */ int theOtherPeg ( int fromPeg, int toPeg ) { return (6 - fromPeg - toPeg); } /* This functions prints out the precise sequence of peg-to-peg disc transfers needed to solve the Towers of Hanoi problem. */ void ToH ( int numDiscs, int fromPeg, int toPeg ) { int otherPeg; if ( numDiscs == 1 ) System.out.println(“move from “+fromPeg+“ to “ + toPeg); else { otherPeg = theOtherPeg(fromPeg, toPeg); ToH(numDiscs - 1, fromPeg, otherPeg); printf("%d -> %d\n", fromPeg, toPeg); ToH(numDiscs - 1, otherPeg, toPeg); }

91 FIT1002 2006 91 Reading Savitch, Chapter 11 for the keen: Thinking Recursively with Java Eric Roberts, Wiley 2006 (this will help you a lot with FIT1008)


Download ppt "FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion."

Similar presentations


Ads by Google