48
1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

  • View
    223

  • Download
    3

Embed Size (px)

Citation preview

Page 1: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

1

CSE1301Computer Programming

Lecture 28Recursion (Part 2)

Page 2: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

2

Topics

• Finding recursive solutions to problems

• Towers of Hanoi

• Sierpinski Triangle

Reading: D&D Chapter 5Exercises 5.37 to 5.40, and 5.42

Page 3: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

3

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.

Page 4: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

4

Towers of Hanoi

Base case: N = 1

Peg 1 Peg 2 Peg 3

Peg 1 Peg 3

Page 5: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

5

Towers of Hanoi

Base case: N = 1

Peg 1 Peg 2 Peg 3

Peg 1 Peg 3

Page 6: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

6

Towers of Hanoi

Recursion: N > 1

Peg 1 Peg 2 Peg 3

Top N-1 discs: Peg 1 Peg 2

Page 7: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

7

Towers of Hanoi

Recursion: N > 1

Peg 1 Peg 2 Peg 3

Top N-1 discs: Peg 1 Peg 2

Page 8: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

8

Towers of Hanoi

Recursion: N > 1

Peg 1 Peg 2 Peg 3

Largest disc: Peg 1 Peg 3

Page 9: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

9

Towers of Hanoi

Recursion: N > 1

Peg 1 Peg 2 Peg 3

Largest disc: Peg 1 Peg 3

Page 10: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

10

Towers of Hanoi

Recursion: N > 1

Peg 1 Peg 2 Peg 3

Top N-1 discs: Peg 2 Peg 3

Page 11: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

11

Towers of Hanoi

Recursion: N > 1

Peg 1 Peg 2 Peg 3

Top N-1 discs: Peg 2 Peg 3

Page 12: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

12

Towers of Hanoi

Recursion: N > 1

Peg 1 Peg 2 Peg 3

Acted as temporary holding peg.

Page 13: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

13

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

Page 14: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

14

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

Page 15: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

15

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

Page 16: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

16

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

Page 17: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

17

Towers of Hanoifunction 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 18: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

18

Towers of Hanoi

otherPeg is always 6 - (fromPeg + toPeg)

Page 19: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

19

Towers of Hanoi

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

Solution 2

Page 20: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

20

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

Page 21: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

21

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

Page 22: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

22hanoi.c

/* 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 to solve the Towers of Hanoi problem. */

void ToH ( int numDiscs, int fromPeg, int toPeg ){ int otherPeg; if ( numDiscs == 1 ) { printf("%d -> %d\n", fromPeg, toPeg); } else { otherPeg = theOtherPeg(fromPeg, toPeg); ToH(numDiscs - 1, fromPeg, otherPeg); printf("%d -> %d\n", fromPeg, toPeg); ToH(numDiscs - 1, otherPeg, toPeg); }}

Page 23: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

23

#include <stdio.h>

#include “hanoi.c”

/* This is a test program for the recursive

* function for the Towers of Hanoi.

*/

int main(void)

{

int n;

printf("Enter number of discs: ");

scanf("%d", &n);

if (n > 0)

ToH(n, 1, 3);

else

printf("Invalid n value.\n");

return 0;

}testprog.c

Page 24: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

24

Example: A Sierpinski Triangle

Level 0: The initial triangle (equilateral)

A B

C

Page 25: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

25

Example: A Sierpinski Triangle

Determine mid-point of each side.

A B

C

X

YZ

Page 26: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

26

Example: A Sierpinski Triangle

Form smaller triangles.

Page 27: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

27

Example: A Sierpinski Triangle

Level 1: The length of each side of the smaller triangles is half that of the initial triangle.

Page 28: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

28

Example: A Sierpinski Triangle

Apply same procedure to each shaded triangle.

Page 29: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

29

Example: A Sierpinski Triangle

Apply same procedure to each shaded triangle.

Page 30: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

30

Example: A Sierpinski Triangle

Level 2

Page 31: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

31

Example: A Sierpinski Triangle

Apply same procedure again to smaller triangles.

Page 32: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

32

Example: A Sierpinski Triangle

Level 3, and so on...

Page 33: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

33

Example: A Sierpinski Triangle

..., Level 7, etc...

Is it possible to write a program

for drawing a Sierpinski Triangle?

Page 34: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

34

Algorithm: A Sierpinski Triangle

A B

C

procedure Sierp ( A, B, C ){ drawTriangle ( A, B, C )

}

Page 35: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

35

Algorithm: A Sierpinski Triangle

A B

C

X

YZ

procedure Sierp ( A, B, C ){ drawTriangle ( A, B, C ) let X be midPoint ( A, B ) let Y be midPoint ( B, C ) let Z be midPoint ( C, A )

}

Page 36: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

36

Algorithm: A Sierpinski Triangle

A B

C

X

YZ

procedure Sierp ( A, B, C ){ drawTriangle ( A, B, C ) let X be midPoint ( A, B ) let Y be midPoint ( B, C ) let Z be midPoint ( C, A )

????

}

Page 37: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

37

Algorithm: A Sierpinski Triangle

A B

C

X

YZ

Page 38: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

38

Algorithm: A Sierpinski Triangle

Z

XA

Perform: Sierp ( A, X, Z )

B

C

Y

Page 39: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

39

Algorithm: A Sierpinski Triangle

Z

XA

Perform: Sierp ( X, B, Y )

B

C

Y

Page 40: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

40

Algorithm: A Sierpinski Triangle

Z

XA

Perform: Sierp ( Z, Y, C )

B

C

Y

Page 41: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

41

Algorithm: A Sierpinski Triangle

A B

C

X

YZ

procedure Sierp ( A, B, C ){ drawTriangle ( A, B, C ) let X be midPoint ( A, B ) let Y be midPoint ( B, C ) let Z be midPoint ( C, A )

Sierp ( A, X, Z ) Sierp ( X, B, Y ) Sierp ( Z, Y, C )}

Page 42: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

42

Algorithm: A Sierpinski Triangle

procedure Sierp ( A, B, C ){ drawTriangle ( A, B, C ) let X be midPoint ( A, B ) let Y be midPoint ( B, C ) let Z be midPoint ( C, A )

Sierp ( A, X, Z ) Sierp ( X, B, Y ) Sierp ( Z, Y, C )}

Function calls itself: Recursion

Page 43: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

43

Algorithm: A Sierpinski Triangle

procedure Sierp ( A, B, C ){ drawTriangle ( A, B, C ) let X be midPoint ( A, B ) let Y be midPoint ( B, C ) let Z be midPoint ( C, A )

Sierp ( A, X, Z ) Sierp ( X, B, Y ) Sierp ( Z, Y, C )}

When and how will it

stop?

Page 44: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

44

Algorithm: A Sierpinski Triangleprocedure Sierp ( level, A, B, C ){ if ( level > MAXLEVEL ) then return else { drawTriangle ( A, B, C ) let X be midPoint ( A, B ) let Y be midPoint ( B, C ) let Z be midPoint ( C, A )

Sierp ( level + 1, A, X, Z ) Sierp ( level + 1, X, B, Y ) Sierp ( level + 1, Z, Y, C ) }}

Page 45: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

45

Algorithm: A Sierpinski Triangleprocedure Sierp ( level, A, B, C ){ if ( level > MAXLEVEL ) then return else { drawTriangle ( A, B, C ) let X be midPoint ( A, B ) let Y be midPoint ( B, C ) let Z be midPoint ( C, A )

Sierp ( level + 1, A, X, Z ) Sierp ( level + 1, X, B, Y ) Sierp ( level + 1, Z, Y, C ) }}

Base Case:

No recursive call

Page 46: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

46

Algorithm: A Sierpinski Triangleprocedure Sierp ( level, A, B, C ){ if ( level > MAXLEVEL ) then return else { drawTriangle ( A, B, C ) let X be midPoint ( A, B ) let Y be midPoint ( B, C ) let Z be midPoint ( C, A )

Sierp ( level + 1, A, X, Z ) Sierp ( level + 1, X, B, Y ) Sierp ( level + 1, Z, Y, C ) }}

General Case:

Recursive call

Page 47: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

47

Algorithm: A Sierpinski Triangleprocedure Sierp ( level, A, B, C ){ if ( level > MAXLEVEL ) then return else { drawTriangle ( A, B, C ) let X be midPoint ( A, B ) let Y be midPoint ( B, C ) let Z be midPoint ( C, A )

Sierp ( level + 1, A, X, Z ) Sierp ( level + 1, X, B, Y ) Sierp ( level + 1, Z, Y, C ) }}

The general case converges towards

the base case.

Page 48: 1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

48

Reading

• King 9.6• D&D Chapter 5

– Exercises 5.37 to 5.40, and 5.42• Kernighan and Ritchie 4.10