1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)

Slides:



Advertisements
Similar presentations
3/25/2017 Chapter 16 Recursion.
Advertisements

Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
CSC 205 Programming II Lecture 10 Towers of Hanoi.
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Representational Choices The Towers of Hanoi Problem.
Recursion … just in case you didn’t love loops enough …
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L10 (Chapter 19) Recursion.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
1 Chapter 1: Introduction What you have learnt in Comp1220 or Comp1170? What will be taught in Comp 1200? - more Abstract Data Types -efficient algorithms.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
Recursion.
Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain.
FIT FIT1002 Computer Programming Unit 20 Recursion.
Recursion!. Question Stephen King’s books J. R. R. Tolkien’s books Recursion Do they have something in common?
1 CSE1301 Computer Programming Lecture 27 Recursion (Part 1)
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Chapter 8 Recursion Modified.
 Prentice Hall. All rights reserved. 1 CS 1120 – Computer Science II Recursion (Ch.15) Many slides modified by Prof. L. Lilien (even many without.
 Prentice Hall. All rights reserved. 1 Recursion (Section 7.13 & Exercise7.40 from ed.3) (Sections 6.15, 6.16 from ed.1) Many slides modified.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
22C:19 Discrete Math Advanced Counting Fall 2010 Sukumar Ghosh.
1 Examples of Recursion Instructor: Mainak Chaudhuri
CSE 251 Dr. Charles B. Owen Programming in C1 Functions.
 Prentice Hall. All rights reserved. 1 CS 1120 – Computer Science II Recursion (Ch.15) Many slides modified by Prof. L. Lilien (even many without.
UNIT 17 Recursion: Towers of Hanoi.
CMPF144 FUNDAMENTALS OF COMPUTING THEORY Module 9: The Tower of Hanoi.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
Recursively Defined Sequences Lecture 40 Section 8.1 Wed, Apr 11, 2007.
1 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursive. Recursive F(n) = F(n-1) + F(n-2) n! = (n-1)! x n C(m,n) = C(m-1,n-1)+C(m-1,n)......
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
CS212: Data Structures and Algorithms
Tower of Hanoi problem: Move the pile of rings from one peg to another
Chapter Topics Chapter 16 discusses the following main topics:
COMP 51 Week Fourteen Recursion.
Recursion.
Data Structures.
Chapter 15 Recursion.
Chapter 15 Recursion.
COMP108 Algorithmic Foundations Divide and Conquer
Chapter 19 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion (part 2) October 26, 2007 ComS 207: Programming I (in Java)
CS1120: Recursion.
Tower of Hanoi problem: Move the pile of rings from one peg to another
The Hanoi Tower Problem
Recursion When performing a repetitive task either: a loop recursion
Tower of Hanoi Algorithm
Recursion.
CSC 143 Recursion.
Chapter 19: Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion (part 2) March 22, 2006 ComS 207: Programming I (in Java)
Tower of Hanoi problem: Move the pile of rings from one peg to another
Presentation transcript:

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 5 Exercises 5.37 to 5.40, and 5.42

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.

4 Towers of Hanoi Base case: N = 1 Peg 1Peg 2Peg 3 Peg 1  Peg 3

5 Towers of Hanoi Base case: N = 1 Peg 1Peg 2Peg 3 Peg 1  Peg 3

6 Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Top N-1 discs: Peg 1  Peg 2

7 Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Top N-1 discs: Peg 1  Peg 2

8 Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Largest disc: Peg 1  Peg 3

9 Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Largest disc: Peg 1  Peg 3

10 Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Top N-1 discs: Peg 2  Peg 3

11 Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Top N-1 discs: Peg 2  Peg 3

12 Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Acted as temporary holding peg.

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

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

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

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

17 Towers 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

18 Towers of Hanoi otherPeg is always 6 - (fromPeg + toPeg)

19 Towers of Hanoi function theOtherPeg (fromPeg, toPeg ) { return ( 6 - fromPeg - toPeg ) } Solution 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) }

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

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

23 #include #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

24 Example: A Sierpinski Triangle Level 0: The initial triangle (equilateral) AB C

25 Example: A Sierpinski Triangle Determine mid-point of each side. AB C X YZ

26 Example: A Sierpinski Triangle Form smaller triangles.

27 Example: A Sierpinski Triangle Level 1: The length of each side of the smaller triangles is half that of the initial triangle.

28 Example: A Sierpinski Triangle Apply same procedure to each shaded triangle.

29 Example: A Sierpinski Triangle Apply same procedure to each shaded triangle.

30 Example: A Sierpinski Triangle Level 2

31 Example: A Sierpinski Triangle Apply same procedure again to smaller triangles.

32 Example: A Sierpinski Triangle Level 3, and so on...

33 Example: A Sierpinski Triangle..., Level 7, etc... Is it possible to write a program for drawing a Sierpinski Triangle?

34 Algorithm: A Sierpinski Triangle AB C procedure Sierp ( A, B, C ) { drawTriangle ( A, B, C ) }

35 Algorithm: A Sierpinski Triangle AB C X Y Z 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 ) }

36 Algorithm: A Sierpinski Triangle AB C X Y Z 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 ) ???? }

37 Algorithm: A Sierpinski Triangle AB C X YZ

38 Algorithm: A Sierpinski Triangle Z X A Perform: Sierp ( A, X, Z ) B C Y

39 Algorithm: A Sierpinski Triangle Z X A Perform: Sierp ( X, B, Y ) B C Y

40 Algorithm: A Sierpinski Triangle Z X A Perform: Sierp ( Z, Y, C ) B C Y

41 Algorithm: A Sierpinski Triangle AB C X Y Z 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 ) }

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

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?

44 Algorithm: A Sierpinski Triangle procedure 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 ) }

45 Algorithm: A Sierpinski Triangle procedure 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

46 Algorithm: A Sierpinski Triangle procedure 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

47 Algorithm: A Sierpinski Triangle procedure 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.

48 Reading King 9.6 D&D Chapter 5 –Exercises 5.37 to 5.40, and 5.42 Kernighan and Ritchie 4.10