- 162 Views
- Uploaded on
- Presentation posted in: General

Recursion

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Recursion

Yuting Zhang

Allegheny College, 04/24/06

My number =

10* the number inside

+ 1

My number =

10* the number inside

+ 1

My number =

10* the number inside

+ 1

My number

=1

My number =

10* 1 + 1 =

11

My number =

10* 11 + 1

= 111

My number =

10* 111 + 1

=1111

Divide the line into 16 segments:

- 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

Produce a same problem with smaller size

My number

=1

Simple case

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

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.
.

- Base case(s) – termination

Conquer

Converge

Divide

My number

=1

My number =

10* the number inside

+ 1

n: # of boxes

Recursion step:

num(n) = 10*num(n-1) + 1

Base case: num(0) = 1

For any n>0:

num(n) -> num(n-1) -> num(n-2)….-> num(0)

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

Base case: num(0) = 1

int Num(int n)

{

if (n == 0)

return 1;

else

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

}

class 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

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

}

}

- 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

Num

parameter 1

Num

return 111

parameter 2

return 1111

Num

num(3)

= 10 * num(2) + 1

= 10 * (10 * num(1) + 1) + 1

= 10 * (10 * (10 * num(0) + 1) +1) + 1

= 10 * (10 * (10 * 1 + 1) + 1 ) + 1

= 10 * (10 * 11 + 1 ) + 1

= 10 * 111 + 1

= 1111

return1

parameter 0

return 11

Num

parameter 3

Num

parameter 1

Num

parameter 2

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

Num

parameter 0

Num(0)

Num(1)

Num(2)

Num

Num(3)

parameter 3

Num

parameter 1

Num

return 111

parameter 2

top of stack

top of stack

top of stack

top of stack

top of stack

return 1111

When a method is returned,

pop the method and parameters off the stack

Num

return1

parameter 0

Num(0)

return 11

Num(1)

Num(2)

Num

Num(3)

parameter 3

1!

return1

parameter 1

2!=2*1!

parameter 2

return 2

3!

=3*2!

return 6

parameter 3

4!

=4*3!

return 24

parameter 4

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

0! = 1

Recursion 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);

}

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

int factorial(int n)

{

int result = 1;

for (int i = n; i>=1; i--);

result *= i;

return result;

}

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

}

- 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

3

1

2

5

4

3

2

1

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

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

3

1

2

1

1

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

3

1

2

2

2

1

1

2

5

5

5

5

5

4

4

4

4

4

3

3

3

3

3

2

2

2

2

2

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

3

1

2

1

1

How?

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

3

1

2

Same way with less disks

5

4

5

5

3

4

4

2

3

3

1

2

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;

}

Question:

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

2n -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;

}

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

Result:

1 --> 3 1 --> 2 3 --> 2 1 --> 3

2 --> 1 2 --> 3 1 --> 3 1 --> 2

3 --> 2 3 --> 1 2 --> 1 3 --> 2

1 --> 3 1 --> 2 3 --> 2 1 --> 3

2 --> 1 2 --> 3 1 --> 3 2 --> 1

3 --> 2 3 --> 1 2 --> 1 2 --> 3

1 --> 3 1 --> 2 3 --> 2 1 --> 3

2 --> 1 2 --> 3 1 --> 3

- Fibonacci Series
- String Permutations
- Fractals
- Binary Search
- …

- 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