32
1 COMP 250 Lecture 11 recursive algorithms 1 Oct. 2, 2017

Lecture 11 - McGill University

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 11 - McGill University

1

COMP 250

Lecture 11

recursive algorithms 1

Oct. 2, 2017

Page 2: Lecture 11 - McGill University

Example 1: Factorial (iterative)

2

factorial( n ){ // assume n >= 1result = 1for (k = 2; k <= n; k++)

result = result * kreturn result

}

! = 1 ∗ 2 ∗ 3 ∗ … ∗ − 1 ∗

Page 3: Lecture 11 - McGill University

Factorial (recursive)

3

factorial( n ){ // assume n >= 1if n == 1

return 1else

return factorial( n – 1 ) * n}

! = − 1 ! ∗

Page 4: Lecture 11 - McGill University

4

Claim: the recursive factorial(n) algorithm returns ! .Proof (by mathematical induction):

Base case: factorial(1) returns 1.

Induction step:

Take any >= 1.

if factorial( ) returns !then factorial( + 1 ) returns ( + 1) !

Page 5: Lecture 11 - McGill University

Example 2: Fibonacci

5

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ….0 = 01 = 1+ 2 = + 1 + ( ) , for ≥ 0.

Page 6: Lecture 11 - McGill University

Fibonacci (iterative)

6

fibonacci(n){if ((n == 0) | (n == 1))

return nelse{

fib0 = 0fib1 = 1for k = 2 to n{

fib2 = fib1 + fib0 // Fib(n+2)fib0 = fib1 // Fib(n) in next passfib1 = fib2 // Fib(n+1) in next pass

}return fib2

}}

Page 7: Lecture 11 - McGill University

Fibonacci (recursive)

7

fibonacci(n){ // assume n > 0if ((n == 0) || (n == 1))

return nelse

return fibonacci(n-1) + fibonacci(n-2)}

This is much simpler to express than the iterative version.

Page 8: Lecture 11 - McGill University

8

Claim: the recursive Fibonacci algorithm is correct.

Proof:

Base case: Fib (0) returns 0. Fib(1) returns 1.

Induction step:

for k > 1if fibonacci(k-1) and fibonacci(k) return F(k-1) and F(k)then fibonacci(k+1) returns F(k+1).

Page 9: Lecture 11 - McGill University

9

fibonacci( 247 )

fibonacci( 246 ) fibonacci( 245 )

fibonacci( 245 ) fibonacci( 244 ) fibonacci( 244 ) fibonacci( 243)

fibonacci( 244 ) fibonacci( 243) fibonacci( 243 ) fibonacci( 242) etc

However, the recursive Fibonacci algorithm is veryinefficient. It computes the same quantity manytimes, for example:

Page 10: Lecture 11 - McGill University

Example 3: Reversing a list

10

input ( a b c d e f g h )

output ( h g f e d c b a )

Page 11: Lecture 11 - McGill University

Example 3: Reversing a list

11

input ( a b c d e f g h )

output ( h g f e d c b a )

Idea of recursion:

a ( b c d e f g h )

( h g f e d c b ) a

Page 12: Lecture 11 - McGill University

Example 3: Reversing a list(recursive)

12

reverse( list ){ // assume n > 0if list.size == 1 // base case

return listelse{

firstElement = removeFirst(list)list = reverse(list) // list has only n-1 elementsreturn addLast(list, firstElement )}

}

Page 13: Lecture 11 - McGill University

Example 4: Sorting a list(recursive)

13

sort( list ) { // assume size > 0if list.size == 1 // base case

return listelse{

minElement = removeMin(list)list = sort( list ) // has n-1 elementsreturn addFirst(list, minElement)

}}// reminiscent of selection sort

Page 14: Lecture 11 - McGill University

Example 5: Tower of Hanoi

14

Tower A(start)

Tower B(finish)

Tower C

Problem: Move n disks from start tower to finish tower such that:

- move one disk at a time

- you can have a smaller disk on top of bigger disk (but you can’t havea bigger disk onto a smaller disk)

Page 15: Lecture 11 - McGill University

15

start finish

Example: n = 1

Page 16: Lecture 11 - McGill University

16

start finish

start finish

Example: n = 1

Example: n = 2

Page 17: Lecture 11 - McGill University

17

Example: n = 2 move from A to C

move from A to B

move from C to B

Page 18: Lecture 11 - McGill University

18

start finish

Q: How to move 5 disks from tower 1 to 2 ?

A: Think recursively.

Page 19: Lecture 11 - McGill University

19

Example: n = 5 Somehowmove 4 disks from A to C

move 1 disk from A to B

Somehowmove 4 disks from C to B

Page 20: Lecture 11 - McGill University

20

tower(n, start, finish, other){ // e.g. tower(5, A, B, C)

if n > 0 {

tower( n-1, start, other, finish)

move from start to finish

tower( n-1, other, finish, start)}

}

Page 21: Lecture 11 - McGill University

21

Example: n = 5 tower( 5, A, B, C )

tower( 4, A, C, B )

tower( 1, A, B, C)

tower( 4, C, B, A)

Page 22: Lecture 11 - McGill University

22

Claim: the tower( ) algorithm is correct, namely itmoves the blocks from start to finish withoutbreaking the two rules (one at a time, and can’t putbigger one onto smaller one).

Proof: (sketch)

Base case: tower( 0, *, *, *) is correct.

Induction step:

for any k > 0, if tower(k, *, *, *) is correctthen tower(k + 1, *, *, *) is correct.

Page 23: Lecture 11 - McGill University

How many moves does tower(1, … ) make ?

23

tower( 1, start, finish, other )

tower( 0, start, other, finish) tower( 0, other, finish, start )move startto other

Answer: 1

Page 24: Lecture 11 - McGill University

How many moves does tower(2, … ) make ?

24

tower( 2, start, finish, other )

tower( 1, start, other, finish) tower( 1, other, finish, start )move

move move

Answer: 1 + 2move from A to C

move from A to B

move from C to B

Page 25: Lecture 11 - McGill University

How many moves does tower(3, … ) make ?

25

tower( 3, start, finish, other )

tower( 2, start, other, finish) tower( 2, other, finish, start )move

move move

movemovemovemove

Answer: 1 + 2 + 4 = 2 + 2 + 2

Page 26: Lecture 11 - McGill University

How many moves does tower(n, … ) make ?

26

tower( n, start, finish, other )

tower( n – 1, start, other, finish) tower( n – 1, other, finish, start )move

move move

movemovemovemove… … … … …… …

Answer: 1 + 2 + 4 + …+ 2 = 2 − 1

Page 27: Lecture 11 - McGill University

Recall (lecture 7): “call stack”

27

void mA( ) {mB( );mC( );

}

void main( ){mA( );

}

mB mCmA mA mA mA mA

main main main main main main main

Page 28: Lecture 11 - McGill University

Recursive methods & Call stack

28

factorial( n ){if n == 1

return 1else

return factorial( n – 1 ) * n}

factorial(1)factorial(2) factorial(2) factorial(2)

factorial(3) factorial(3) factorial(3) factorial(3) factorial(3)main main main main main main main

Page 29: Lecture 11 - McGill University

29

Call stack for TestFactorial

Page 30: Lecture 11 - McGill University

Stack frame (details in COMP 273)

The call stack consists of “frames” that contain:

• the parameters passed to the method

• local variables of a method

• information about where to return (“which linenumber in which method in which class?”)

30

Page 31: Lecture 11 - McGill University

31

parameters in current stack frameCall stack for TestTowerOfHanoi

Page 32: Lecture 11 - McGill University

Call stack

32

void mA( ) {mB( );mA( );mC( );

}

A method can make both recursive and non-recursivecalls.

There is a single call stack for all methods.