# 1# 1 VBA Recursion What is the “base case”? What is the programming stack? CS 105 Spring 2010.

Slides:



Advertisements
Similar presentations
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.
Advertisements

More Recursion: Permutations and Towers of Hanoi COP 3502.
Factorial Recursion stack Binary Search 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.
The Tower of Hanoi Ben Epstein Special Topics 2003.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
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.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
1 Section 6.1 Recurrence Relations. 2 Recursive definition of a sequence Specify one or more initial terms Specify rule for obtaining subsequent terms.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
Stacks CSE, POSTECH. 2 2 Stacks Linear list One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Recursion Chapter 5.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Copyright © Cengage Learning. All rights reserved.
Data Structures Using C++ 2E Chapter 6 Recursion.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
25-2 Recursive Functions Related Chapter: ABC 5.14, 5.15.
Data Structures Using C++ 2E Chapter 6 Recursion.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
© 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.
Recursion Jordi Cortadella Department of Computer Science.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Lecture - 8 On Stacks, Recursion. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline Quick sort Algorithm Recursion –Calculate n factorial –Fibonacci.
 STACK STACK  STACK OPERATIONS STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  USES OF STACK USES OF STACK  THE TOWER.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
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.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion (Continued)
CSCE 3110 Data Structures & Algorithm Analysis
Recursion.
Recursion.
Chapter 4 Stacks
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Chapter 15 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
CSCE 3110 Data Structures & Algorithm Analysis
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 12 Recursion (methods calling themselves)
Chapter 11 Recursion.
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Dr. Sampath Jayarathna Cal Poly Pomona
Java Software Solutions Foundations of Program Design Sixth Edition
Presentation transcript:

# 1# 1 VBA Recursion What is the “base case”? What is the programming stack? CS 105 Spring 2010

# 2# 2 Recursive procedures are defined in terms of themselves; e.g., a function is recursive if it contains calls back to itself or to another function that calls the original function. Recursive programming usually requires more memory and runs more slowly than non-recursive programs. This is due to the cost of implementing the “stack”. However, recursion is often a natural way to define a problem. Recursive Procedures

# 3# 3 1. Problem Definition Write a factorial function. 0! = 1 and n! = n*(n-1)!. Use “recursion” to implement the factorial function. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = Non-negative integers as input. Output= return the factorial of the input value. Note: that 69! = x so our function will only work for small integer values. Example: Factorial Function

# 4# 4 Function fact(intN As Integer) As Integer If intN = 0 Then ' base case fact = 1 Else fact = intN * fact(intN - 1) 'recursive call End If End Function Example: Factorial Function

# 5# 5 (push) fact = 3 * fact(3 - 1) (intN = 3) (push) fact = 2 * fact(2 - 1) (intN = 2) (push) fact = 1 * fact(0) (intN = 1) (push/pop) fact = 1 (intN = 0) (pop) fact = 1 * 1 (intN = 1) (pop) fact = 2 * 1 (intN = 2) (pop) fact = 3 * 2 (intN = 3) fact = 1 (intN = 0) fact = 1 * fact(0) (intN = 1) fact = 2 * fact(2- 1) (intN = 2) fact = 3 * fact(3- 1) (intN = 3) (STACK) The “stack” contains “stack frames” fact = 1 * 1 (intN = 1) fact = 2 * fact(2- 1) (intN = 2) fact = 3 * fact(3- 1) (intN = 3) fact = 2 * 1 (intN = 2) fact = 3 * fact(3- 1) (intN = 3) (empty) fact = 3 * 2 (intN = 3)

# 6# 6 1. Problem Definition Write a solve_maze function. We assume that a maze has been created in the range A1:J10. Write a Subprocedure to display one path that traverses the maze. Use “recursion” to implement the maze subprocedure. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = The maze in the range A1:J10 Your program will have to find its way through a 10x10 maze where the symbols “ * ”, “O” and “X” denote: “ * ” are solid walls through which you cannot travel "O" denotes the starting position, "X" is the exit for which you are looking for Example: Traversing a Maze

# 7# 7 ********** ** ******O* ***** ***X* ******* *** ******* *** **********

# 8# 8 2. Refine, Generalize, Decompose the problem definition Input (continued): Read the maze into the 2D array, ********** ** **** * *O* * * *** * * *X* * ** *** * * * * * *** ** * * * * ********** Dim strMaze(1 to 10, 1 to 10) as String The upper left-hand corner of the maze has the value strMaze(1,1). If we want to test whether the cell in the fourth row and fourth column contains a wall then it's enough to use an if statement like this: if (strMaze(4,4) = “ * “) Example: Traversing a Maze

# 9# 9 2. Refine, Generalize, Decompose the problem definition Output : Display a solution as follows: Example: Traversing a Maze

# Develop Algorithm (processing steps to solve problem) Step 1 Read in the “maze” and find the starting Row and Column (the position of the “O”). Use variables “intRow” and “intCol” to keep track of the current position as we traverse through the maze (the 2D matrix strMaze). Step 2 Display the maze. Step 3 Check to see if current position is an “X” then we are done. Otherwise first try to go up and if not then down and if not then left and if not then right and if you can go up/down/left/right then go back to Step 2. Otherwise, go back to the previous position (intRow,intCol) and try another direction. Example: Traversing a Maze

# 11 Private Sub cmdSolve_Click() Dim strMaze(1 To 10, 1 To 10) As String Dim intRow As Integer, intCol As Integer Dim intStartRow As Integer, intStartCol As Integer ' Read the maze into strMaze and ' find intStartRow, intStartCol For intRow = 1 To 10 For intCol = 1 To 10 strMaze(intRow, intCol) = Cells(intRow, intCol).Value If strMaze(intRow, intCol) = "O" Then intStartRow = intRow intStartCol = intCol End If Next intCol Next intRow ' SolvMaze is a recursive subprocedure SolveMaze strMaze, intStartRow, intStartCol End Sub

# 12 Sub SolveMaze(strMaze() As String, intRow As Integer, intCol As Integer) 'Draw the maze DrawMaze strMaze, 10, 10 'Check if solution found. If strMaze(intRow - 1, intCol) = "X" Or _ strMaze(intRow + 1, intCol) = "X" Or _ strMaze(intRow, intCol + 1) = "X" Or _ strMaze(intRow, intCol - 1) = "X" Then End 'End terminates ALL recursive calls End If ' Recurse in each possible direction that is empty. 'Move up If strMaze(intRow - 1, intCol) = "" Then strMaze(intRow - 1, intCol) = "O" SolveMaze strMaze, intRow - 1, intCol strMaze(intRow - 1, intCol) = "“ DrawMaze strMaze, 10, 10 End If ' continued on next slide

# 13 'Move down If strMaze(intRow + 1, intCol) = "" Then strMaze(intRow + 1, intCol) = "O" SolveMaze strMaze, intRow + 1, intCol strMaze(intRow + 1, intCol) = "“ DrawMaze strMaze, 10, 10 End If 'Move left If strMaze(intRow, intCol - 1) = "" Then strMaze(intRow, intCol - 1) = "O" SolveMaze strMaze, intRow, intCol - 1 strMaze(intRow, intCol - 1) = "“ DrawMaze strMaze, 10, 10 End If 'Move right If strMaze(intRow, intCol + 1) = "" Then strMaze(intRow, intCol + 1) = "O" SolveMaze strMaze, intRow, intCol + 1 strMaze(intRow, intCol + 1) = "“ DrawMaze strMaze, 10, 10 End If End Sub

# 14 Sub DrawMaze(strMaze() As String, intRows As Integer, intCols As Integer) Dim intRow As Integer, intCol As Integer For intRow = 1 To intRows For intCol = 1 To intCols Cells(intRow, intCol).Value = strMaze(intRow, intCol) Next intCol Next intRow Pause 0.4 End Sub Sub Pause(sngTime As Single) Dim sngStart As Single sngStart = Timer ' Set start time. Do While Timer < sngStart + sngTime DoEvents Loop End Sub

# 15 According to legend, in the great temple of Benares, beneath the dome which marks the center of the world, rests a brass plate on which are fixed three diamond needles. On one of these needles at creation, there were placed 64 discs of pure gold, the largest disc resting on the brass plate and the others getting smaller up to the top one. This is the TOWERS OF HANOI. Day and night, the people on duty move the discs from one needle to another, according to the two following laws: Law 1: Only one disc at a time may be moved. Law 2: A larger disc may never rest on a smaller disc. The workers labor in the belief that once the tower has been transferred to another needle there will be heaven on earth, so they want to complete the task in the least number of moves. Example: Towers of Hanoi

# 16 Actually, the Tower of Hanoi puzzle was invented in 1883 by the French mathematician Edouard Lucas ( ), who made up the legend to accompany it. Example: Towers of Hanoi

# 17 An elegant and efficient way to solve this problem is to think recursively. Suppose that you, somehow or other, have found the most efficient way possible to transfer a tower of n-1 disks one by one from one pole to another obeying the restriction that you never place a larger disk on top of a smaller one. Then, what is the most efficient way to move a tower of n disks from one pole to another? Example: Towers of Hanoi

# 18 Assume we know how to move n-1 disks from one peg to another.Then can we move n disks from peg 1 to peg 3 ? 1. Move n-1 disks from peg 1 to peg 2, peg 3 is just a temporary holding area 2. Move the last disk(the largest) from peg 1 to peg 3 3. Move the n-1 disks from peg 2 to peg 3, peg 1 is just a temporary holding area n disks Pseudo-code

# 19 Click on Picture to start game Animation

# 20 Private Sub cmdTower_Click() mintNumDisks = InputBox("How many disks?", "Tower of Hanoi", 8) ' draw the tower DrawTower ' call recursive subprocedure, hanoi ' peg 1 is the origin, peg 3 is the destination and ' peg 2 is the spare Hanoi 1, 3, 2, mintNumDisks End Sub Towers of Hanoi

# 21 Sub Hanoi(intOrigin As Integer, intDestination As Integer, intSpare As Integer, _ intNumDisks As Integer) If intNumDisks = 1 Then ' move top disk on peg intOrigin to peg intDestination Pause 0.6 MoveDisks intOrigin, intDestination Else ' peg intOrigin is the origin, peg intSpare is the ' destination and peg intNumDisks is the spare Hanoi intOrigin, intSpare, intDestination, intNumDisks - 1 ' move top disk on peg intOrigin to peg intDestination Pause 0.6 MoveDisks intOrigin, intDestination Hanoi intSpare, intDestination, intOrigin, intNumDisks - 1 End If End Sub

# 22 Going back to the legend, suppose the workers rapidly move one disk every second. As shown earlier, the minimum sequence of moves must be : The minimum number of moves needed to transfer a tower of n disks from peg1 to peg3 The minimum number of moves needed to transfer n-1 disks from peg1 to peg2 The minimum number of moves needed to transfer the n th disk from peg1 to peg3 The minimum number of moves needed to transfer n-1 disks from peg2 to peg3 on top of the n th disk Therefore, the recurrence relation is moves(n) = 2*moves(n-1) + 1 and initial case is moves(1) = 1 second. For example, moves(2) = 2*moves(1) + 1 = 3, moves(3) = 2*moves(2) + 1 = 7, or in general, moves(n) = 2 n -1 Then, the time to move all 64 disks from one peg to the other, and end the universe would be moves(64) seconds or billion years!! = + + Computational Complexity