fit1002 2006 1 fit1002 computer programming unit 20 recursion

91
FIT1002 2006 1 FIT1002 FIT1002 Computer Programming Computer Programming Unit 20 Unit 20 Recursion Recursion

Post on 21-Dec-2015

246 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

1

FIT1002FIT1002Computer ProgrammingComputer Programming

Unit 20Unit 20

RecursionRecursion

Page 2: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

2

ObjectivesObjectives

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

Page 3: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

3

What is Recursion?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

Page 4: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

4

Example: FactorialExample: Factorial

n! = n (n - 1) (n - 2) ... 2 1

0! = 1Example:

5! = 5 4 3 2 1 = 120

Given n 0:

Page 5: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

5

Example: FactorialExample: Factorial

Recursion:

n! = n (n - 1) (n - 2) ... 2 1

(n - 1)!

If n > 1: Factorial(n) = n Factorial(n - 1)

Page 6: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

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: FactorialExample: Factorial

Page 7: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

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: FactorialExample: Factorial

Base case

Page 8: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

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: FactorialExample: Factorial

General Case

Page 9: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

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: FactorialExample: Factorial

Recursion

Page 10: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

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: FactorialExample: Factorial

Convergence

Page 11: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

11

Unary RecursionUnary Recursion

Functions calls itself once (at most)Usual format:

function RecursiveFunction ( <parameter(s)> )

{

if ( <base case> ) then

return <base value>

else

return RecursiveFunction ( <expression> )}

Winding and unwinding the “stack frames”

Page 12: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

12

Example: FactorialExample: Factorial

Factorial(4)

Factorial(3)4

Factorial(2)3

Factorial(1)2

1

Page 13: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

13

Example: FactorialExample: Factorial

Factorial(4)

Factorial(3)4

Factorial(2)3

Factorial(1)2

1

Page 14: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

14

Example: FactorialExample: Factorial

Factorial(4)

Factorial(3)4

Factorial(2)3

12

Page 15: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

15

Example: FactorialExample: Factorial

Factorial(4)

Factorial(3)4

Factorial(2)3

12

Page 16: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

16

Example: FactorialExample: Factorial

Factorial(4)

Factorial(3)4

23

Page 17: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

17

Example: FactorialExample: Factorial

Factorial(4)

Factorial(3)4

23

Page 18: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

18

Example: FactorialExample: Factorial

Factorial(4)

64

Page 19: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

19

Example: FactorialExample: Factorial

Factorial(4)

64

Page 20: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

20

Example: FactorialExample: Factorial

24

Page 21: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

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: Example: factorial.javafactorial.java

Page 22: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

22

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

int x = factorial(4));

Page 23: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

23

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4

44

4n:

Page 24: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

24

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4

34

4n:

Page 25: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

25

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4

34

4n:

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1); }}

3

33

3n:

Page 26: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

26

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4

34

4n:

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

3

23

3n:

Page 27: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

27

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4

34

4n:

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

3

23

3n:

4

34

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1); }}

2

22

2n:

Page 28: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

28

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4

34

4n:

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

3

23

3n:

4

34

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

2

12

2n:

Page 29: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

29

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4

34

4n:

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

3

23

3n:

4

34

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

2

12

2n:

34

3

23

4

34

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n -1); }

1

11

1n:

Base case:

factorial(1) is 1

Page 30: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

30

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4

4n:

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

3n:

4int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

2n:

1

2

2 1

factorial(1) is 1

Page 31: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

31

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4

4n:

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

3n:

4int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * }}

2n:

2

2 1

factorial(1) is 1

Page 32: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

32

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4

4n:

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

3n:

4int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n }}

2n:

2

2

Page 33: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

33

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4

4n:

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

3n:

2

23

3

factorial(2) is 2

Page 34: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

34

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4

4n:

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * }}

3n:

23

3

factorial(2) is 2

Page 35: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

35

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4

4n:

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n }}

3n:

6

3

Page 36: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

36

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * factorial( n ); }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4n:

6

34

4

factorial(3) is 6

Page 37: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

37

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n * }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4n:

64

4

factorial(3) is 6

Page 38: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

38

int x = factorial(4));

int factorial ( int n ){ if ( n <= 1 ) { return 1; } else { return n }}

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

4n:

24

4

Page 39: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

39

int x = factorial(4));

Example:Example: “Frames” during calculation of “Frames” during calculation of factorial(4)factorial(4)

24

factorial(4) is 24

Page 40: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

40

NN-ary Recursion-ary Recursion

Sometimes a function can only be defined in terms of two or more calls to itself.

Efficiency is often a problem.

Page 41: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

41

Example: FibonacciExample: Fibonacci

A series of numbers whichbegins 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,...)

Page 42: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

42

Example: FibonacciExample: Fibonacci

The Fibonacci series can be defined recursively as follows:

Fibonacci(0) = 0

Fibonacci(1) = 1

Fibonacci(n) = Fibonacci(n - 2) + Fibonacci(n - 1)

Page 43: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

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

FibonacFibonaccici

Page 44: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

44

Example:Example: Computation of Computation of fib(4)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 );}

Page 45: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

45

Example:Example: Computation of Computation of fib(4)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 );}

Page 46: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

46

Example:Example: Computation of Computation of fib(4)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 );}

Page 47: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

47

Example:Example: Computation of Computation of fib(4)fib(4)

fib(2) fib(3)+

fib(4)

+0 fib(1)

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 48: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

48

Example:Example: Computation of Computation of fib(4)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 );}

Page 49: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

49

Example:Example: Computation of Computation of fib(4)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 );}

Page 50: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

50

Example:Example: Computation of Computation of fib(4)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 );}

Page 51: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

51

Example:Example: Computation of Computation of fib(4)fib(4)

fib(2) fib(3)+

fib(4)

+0 1

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 52: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

52

Example:Example: Computation of Computation of fib(4)fib(4)

1 fib(3)+

fib(4)

+0 1

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 53: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

53

Example:Example: Computation of Computation of fib(4)fib(4)

+ fib(3)

fib(4)

1

+0 1

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 54: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

54

Example:Example: Computation of Computation of fib(4)fib(4)

+ fib(3)

fib(4)

1

+0 1 +fib(1) fib(2)

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 55: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

55

Example:Example: Computation of Computation of fib(4)fib(4)

+ fib(3)

fib(4)

1

+0 1 +fib(1) fib(2)

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 56: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

56

Example:Example: Computation of Computation of fib(4)fib(4)

+ fib(3)

fib(4)

1

+0 1 +1 fib(2)

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 57: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

57

Example:Example: Computation of Computation of fib(4)fib(4)

+ fib(3)

fib(4)

1

+0 1 +1 fib(2)

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 58: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

58

Example:Example: Computation of Computation of fib(4)fib(4)

+ fib(3)

fib(4)

1

+0 1 +1 fib(2)

+fib(0) fib(1)

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 59: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

59

Example:Example: Computation of Computation of fib(4)fib(4)

+ fib(3)

fib(4)

1

+0 1 +1 fib(2)

+fib(0) fib(1)

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 60: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

60

Example:Example: Computation of Computation of fib(4)fib(4)

+ fib(3)

fib(4)

1

+0 1 +1 fib(2)

+0 fib(1)

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 61: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

61

Example:Example: Computation of Computation of fib(4)fib(4)

+ fib(3)

fib(4)

1

+0 1 +1 fib(2)

+0 fib(1)

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 62: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

62

Example:Example: Computation of Computation of fib(4)fib(4)

+ fib(3)

fib(4)

1

+0 1 +1 fib(2)

+0 fib(1)

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 63: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

63

Example:Example: Computation of Computation of fib(4)fib(4)

+ fib(3)

fib(4)

1

+0 1 +1 fib(2)

+0 1

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 64: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

64

Example:Example: Computation of Computation of fib(4)fib(4)

+ fib(3)

fib(4)

1

+0 1 +1 fib(2)

+0 1

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 65: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

65

Example:Example: Computation of Computation of fib(4)fib(4)

+ fib(3)

fib(4)

1

+0 1 +1 1

+0 1

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 66: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

66

Example:Example: Computation of Computation of fib(4)fib(4)

+ fib(3)

fib(4)

1

+0 1 +1 1

+0 1

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 67: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

67

Example:Example: Computation of Computation of fib(4)fib(4)

+ 2

fib(4)

1

+0 1 +1 1

+0 1

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 68: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

68

Example:Example: Computation of Computation of fib(4)fib(4)

+ 2

fib(4)

1

+0 1 +1 1

+0 1

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 69: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

69

Example:Example: Computation of Computation of fib(4)fib(4)

+ 2

3

1

+0 1 +1 1

+0 1

long fib ( long n ){ if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 );}

Page 70: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

70

Example:Example: Computation of Computation of fib(4)fib(4)

+ 2

3

1

+0 1 +1 1

+0 1

Thus, fib(4) returns the value 3.

Page 71: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

71

Towers of HanoiTowers of Hanoi

A classic problemThree pegsN discs, arranged bottom to top by decreasing

sizeObjective: Move the discs from peg 1 to peg 3Two 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.

Page 72: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

72

Towers of HanoiTowers of Hanoi

Base case: N = 1

Peg 1 Peg 2 Peg 3

Peg 1 Peg 3

Page 73: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

73

Towers of HanoiTowers of Hanoi

Base case: N = 1

Peg 1 Peg 2 Peg 3

Peg 1 Peg 3

Page 74: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

74

Towers of HanoiTowers of Hanoi

Recursion: N > 1

Peg 1 Peg 2 Peg 3

Top N-1 discs: Peg 1 Peg 2

Page 75: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

75

Towers of HanoiTowers of Hanoi

Recursion: N > 1

Peg 1 Peg 2 Peg 3

Top N-1 discs: Peg 1 Peg 2

Page 76: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

76

Towers of HanoiTowers of Hanoi

Recursion: N > 1

Peg 1 Peg 2 Peg 3

Largest disc: Peg 1 Peg 3

Page 77: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

77

Towers of HanoiTowers of Hanoi

Recursion: N > 1

Peg 1 Peg 2 Peg 3

Largest disc: Peg 1 Peg 3

Page 78: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

78

Towers of HanoiTowers of Hanoi

Recursion: N > 1

Peg 1 Peg 2 Peg 3

Top N-1 discs: Peg 2 Peg 3

Page 79: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

79

Towers of HanoiTowers of Hanoi

Recursion: N > 1

Peg 1 Peg 2 Peg 3

Top N-1 discs: Peg 2 Peg 3

Page 80: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

80

Towers of HanoiTowers of Hanoi

Recursion: N > 1

Peg 1 Peg 2 Peg 3

Acted as temporary holding peg.

Page 81: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

81

Towers of HanoiTowers 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

Page 82: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

82

Towers of HanoiTowers 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

Page 83: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

83

Towers of HanoiTowers 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

Page 84: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

84

Towers of HanoiTowers 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) }}

Page 85: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

85

Towers of HanoiTowers 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

Page 86: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

86

Towers of HanoiTowers of Hanoi

otherPeg is always 6 - (fromPeg + toPeg)

Page 87: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

87

Towers of HanoiTowers of Hanoi

function theOtherPeg (fromPeg, toPeg ){ return ( 6 - fromPeg - toPeg )}

Solution 2

Page 88: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

88

Towers of HanoiTowers 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) }}

Page 89: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

89

Towers of HanoiTowers 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

Page 90: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

90hanoi.javahanoi.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); }}

Page 91: FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion

FIT1002 2006

91

ReadingReading

• Savitch, Chapter 11• for the keen: Thinking Recursively with Java

Eric Roberts, Wiley 2006(this will help you a lot with FIT1008)