Top Banner
Computer Science Recursion Yuting Zhang llegheny College, 04/24/06
35

Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

Mar 29, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

Computer Science

Recursion

Yuting Zhang

Allegheny College, 04/24/06

Page 2: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

2

A Game

My number =10* the number inside

+ 1

Page 3: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

3

A Game

My number =10* the number inside

+ 1

Page 4: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

4

A Game

My number =10* the number inside

+ 1

Page 5: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

5

A Game

My number =1

Page 6: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

6

A Game

My number =10* 1 + 1 =

11

Page 7: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

7

A Game

My number =10* 11 + 1

= 111

Page 8: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

8

A Game

My number =10* 111 + 1

=1111

Page 9: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

9

Examples of Recursion

Page 10: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

10

Examples of Recursion

Divide the line into 16 segments:

Page 11: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

11

Outline

The concept of recursion How to write and use recursive method How the recursive method are executed Recursion vs. iteration Examples: Tower of Hanoi Summary

Page 12: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

12

Previous Game

My number =1

Simple case

Produce a same problem with smaller

size

Page 13: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

13

Definition of Recursion

A computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself in a step having a termination condition so that successive repetitions are processed up to the critical step until the condition is met at which time the rest of each repetition is processed from the last one called to the first

Page 14: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

14

Recursion Concepts Two parts to recursion:

Base case(s) – termination If the problem is easy, solve it

immediately.

Recursion step – call itself If the problem can't be solved

immediately, divide it into smaller problems, then

Solve the smaller problems by applying this procedure to each of them.

.

Divide

Conquer

Converge

Page 15: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

15

Previous Game

For any n>0: num(n) -> num(n-1) -> num(n-2)….-> num(0)

n: # of boxesn: # of boxes

Recursion step: Recursion step: num(n) = 10*num(n-1) + 1num(n) = 10*num(n-1) + 1

My number =1

Base case: Base case: num(0) = 1num(0) = 1

My number =10* the number inside

+ 1

Page 16: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

16

Recursion Method

Recursion step: num(n) = 10*num(n-1) + 1Base case: num(0) = 1

int Num(int n) {

if (n == 0) return 1;

else return 10*Num(n-1) +1;

}

Page 17: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

17

Complete Programclass NumCalc{ int Num( int n ) { if ( n == 0 )

return 1; else

return 10*Num(n-1) +1; } } class NumCaclTester { public static void main ( String[] args) { NumCalc numcalc = new NumCalc(); int result = numcalc.Num( 3 ); System.out.println(“numcalc(3) is " + result ); } }

Result:

numcalc(3) is 1111

Page 18: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

18

Recap

Skeleton for a recursive Java method

type solution(type para )

{

if ( base case ) {

return something easily computed

} else {

divide problem into pieces

return something calculated from the solution to each piece

}

}

Page 19: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

19

Outline

The concept of recursion How to write and use recursive method How the recursive method are executed Recursion vs. iteration Examples: Tower of Hanoi Summary

Page 20: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

20

Recursive Evaluation

num(num(33) ) = 10 * num(= 10 * num(22) + 1 ) + 1 = 10 * (10 * num(= 10 * (10 * num(11) + 1) + 1) + 1) + 1 = 10 * (10 * (10 * num(= 10 * (10 * (10 * num(00) + 1) +1) + 1) + 1) +1) + 1

= 10 * (10 * (10 * = 10 * (10 * (10 * 11 + 1) + 1 ) + 1 + 1) + 1 ) + 1 = 10 * (10 * = 10 * (10 * 1111 + 1 ) + 1 + 1 ) + 1 = 10 * = 10 * 111111 + 1 + 1 = = 11111111

Num

Num

parameter 3

parameter 0

Num

parameter 1

Num

parameter 2

return 1111

return1

return 11

return 111

Page 21: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

21

Recursion and Method Call Stack

Num(3)

Num

Num

parameter 3

parameter 0

Num

parameter 1

Num

parameter 2Num(2)

Num(1)

Num(0)

top of stack

top of stack

top of stack

top of stack

top of stack

When a method is called,push the method and parameters into the stack

Page 22: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

22

Recursion and Method Call Stack

Num(3)

Num

Num

parameter 3

parameter 0

Num

parameter 1

Num

parameter 2

return 1111

return1

return 11

return 111Num(2)

Num(1)

Num(0)

top of stack

top of stack

top of stack

top of stack

top of stack

When a method is returned,pop the method and parameters off the stack

Page 23: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

23

Factorials

n! = n* (n-1) * … * 1, (n> 0)0! = 1Recursion step: n! = n*(n-1)!Base case: 0! = 1, 1! = 1

int factorial(int n) { if (n <= 1)

return 1; else

return n*factorial(n-1); }

1!

2!=2*1!

3!=3*2!

4!=4*3!

parameter 4

parameter 1

parameter 2

parameter 3

return 24

return1

return 2

return 6

Page 24: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

24

Iteration

int factorial(int n) { int result = 1; for (int i = n; i>=1; i--); result *= i; return result;}

n! = n* (n-1) * … * 1, 0! = 1

Page 25: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

25

Recursion vs. Iteration

int factorial(int n) { int result = 1; for (int i = n; i>=1; i--); result *= i; return result;}

int factorial(int n) { if (n <= 1)

return 1; else

return n*factorial(n-1); }

Iteration Recursion

Repetition Explicit statement e.g. for, while

Repeat method calls

Termination

test

Loop-continuation condition fails

Base case is reached

Approach termination

Modify a counter toward fail condition

Produce simpler version of the original problem

Overhead Less More

Page 26: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

26

Towers of Hanoi

45

123

11 22 33

Problem: How to move disks from peg 1 to 3, subjected to:- Only one disk is moved at a time- A larger disk can’t be placed above a smaller disk at any time- Peg 2 is used for temporarily holding disks

Page 27: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

27

Towers of Hanoi

11 22 33

Case 1: move 1 disk from peg 1 to 3, using peg 2

-- Move disk 1 from peg 1 to peg 3 directly

11 11

Page 28: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

28

Towers of Hanoi

11 22 33

11 1122

2222

Case 2: move 2 disks from peg 1 to 3, using peg 2

- Move disk 2 from peg 1 to 2- Move disk 1 from peg 1 to 3 - Move disk 2 from peg 2 to 3

Page 29: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

29

45

23

Towers of Hanoi

11 22 33

111

45

23

Case 3: move 5 disks from peg 1 to 3

- Move 4 disks(2-5) from peg 1 to 2, using peg 3- Move disk 1 from peg 1 to 3 - Move 4 disks(2-5) from peg 2 to 3, using peg 1

45

23

45

2345

23

Page 30: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

30

Towers of Hanoi

11 22 33

1

45

2

3

Case 3: move 5 disks from peg 1 to 3

- Move 4 disks(2-5) from peg 1 to 2, using peg 3- Move disk 1 from peg 1 to 3 - Move 4 disks(2-5) from peg 2 to 3, using peg 1

How?How?

45

3

45

32

Same way Same way with less diskswith less disks

Page 31: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

31

Towers of Hanoi

General Case : move n disks from peg 1 to 3, using peg 2

- Move n-1 disks from peg 1 to 2, using peg 3- Move last disk from peg 1 to 3 - Move n-1 disks from peg 2 to 3, using peg 1

void solveTowers (int disks, int srcpeg, int destpeg, int temppeg){

if (disks == 1) { System.out.printf(“\n %d -> %d”, srcpge,destpeg);

} else {solveTowers(disks -1, srcpeg, temppeg, destpeg);System.out.printf(“\n%d -> %d”, srcpeg,destpeg);solveTowers(disks -1, temppeg, destpeg, srcpeg);

}return;

}

Page 32: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

32

Towers of Hanoi

Question:

How many moves are need to move n disks from peg 1 to 3?

void solveTowers (int disks, int srcpeg, int destpeg, int temppeg){

if (disks == 1) { System.out.printf(“\n %d -> %d”, srcpge,destpeg);

} else {solveTowers(disks -1, srcpeg, temppeg, destpeg);System.out.printf(“\n%d -> %d”, srcpeg,destpeg);solveTowers(disks -1, temppeg, destpeg, srcpeg);

}return;

}

2n -1

Page 33: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

33

Towers of Hanoi

Case : move 5 disks from peg 1 to 3, using peg 2

Result:

1 --> 3 1 --> 2 3 --> 2 1 --> 32 --> 1 2 --> 3 1 --> 3 1 --> 23 --> 2 3 --> 1 2 --> 1 3 --> 21 --> 3 1 --> 2 3 --> 2 1 --> 32 --> 1 2 --> 3 1 --> 3 2 --> 13 --> 2 3 --> 1 2 --> 1 2 --> 31 --> 3 1 --> 2 3 --> 2 1 --> 32 --> 1 2 --> 3 1 --> 3

Page 34: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

34

Other Examples

Fibonacci Series String Permutations Fractals Binary Search …

Page 35: Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.

35

Summary

The concept of recursion How to write and use recursive method How the recursive method are executed Recursion vs. iteration Examples: Tower of hanoi

In order to understand recursion you must first understand recursion.

— Unknown

http://cs-people.bu.edu/danazh/cs111-recursion/index.html