Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.

Similar presentations


Presentation on theme: "Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself."— Presentation transcript:

1 Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself

2 Fall 2008ACS-1805 Ron McFadyen2 Indefinite Repetition In programs where a count of repetitions is not known (indefinite), we can use one of two repetition control mechanisms: Recursion While statement Many of the pieces we use to create a program are identified by using special words. For example, Do in order Do together If/Else Loop Recursion is not a program statement with a special word that identifies it as part of the programming language. Recursion means that a method (or a function) calls itself.

3 Fall 2008ACS-1805 Ron McFadyen3 Examples of recursion The natural numbers: 1 is in {N} if n is in {N}, then n + 1 is in {N}

4 Fall 2008ACS-1805 Ron McFadyen4 Ancestors For example, the following is a recursive definition of a person's ancestors: * One's parents are one's ancestors; * The parents of any ancestor are also ancestors of the person under consideration (recursion step).

5 Fall 2008ACS-1805 Ron McFadyen5 Fibonacci number sequence Fibonacci number sequence: F(n) = F(n − 1) + F(n − 2). Base cases: F(0) = 0 F(1) = 1.

6 Fall 2008ACS-1805 Ron McFadyen6 Factorials n! = n (n - 1)! 3!=3*2! 4!=4*3! 2!=2*1! Base cases: 0! = 1 1! = 1

7 Fall 2008ACS-1805 Ron McFadyen7 Humour Recursive humor A common geeky joke is the following "definition" of recursion. recursion see “recursion” In the index of a book, on page 189 recursion22, 45, 80, 189

8 Fall 2008ACS-1805 Ron McFadyen8 Example – shark/goldfish again Previously we had a loop which executed until the shark was close enough to the goldfish that it could eat it. Consider : CHASER If the distance between the shark and the goldfish > 0.5 Do together shark swims toward goldfish goldfish flees call CHASER Shark eats Shark swims … Goldfish flees … Shark eats … chaseRecursion.a2w Recursive call

9 Fall 2008ACS-1805 Ron McFadyen9 This is an example of tail recursion. In tail recursion the last statement in the recursive method is the recursive call. The method begins with an if statement to determine if a base condition is met. If the base condition is met the method terminates; if it is not then we invoke the method again on a smaller problem. The general form of the algorithm we have just used is: MethodX: if base condition is satisfied exit else do something call MethodX Example – shark/goldfish again

10 Fall 2008ACS-1805 Ron McFadyen10 tail recursion: To formulate a solution this way we need: To understand some base conditions where the problem has a known answer or solution To be able to express a solution in terms of smaller problems. As the problems get smaller and smaller, we eventually converge to one of the base conditions. Example – shark/goldfish again

11 Fall 2008ACS-1805 Ron McFadyen11 Shark/goldfish chase: The problem is contrived so the shark is gaining on the goldfish and so we know the “problem” is getting smaller with each swim action (the gap between the two is getting smaller). We have decided that the chase ends when the gap between the two is less than 0.5 metres. This is the base condition … when it is true we stop, the method does not call itself again. Example – shark/goldfish again

12 Fall 2008ACS-1805 Ron McFadyen12 Example – horse race A carnival style horse race. In repeated moves, one horse is randomly selected to move forward. The selected horse “runs” straight ahead towards the finish line. Each time we select a horse that horse moves closer to the finish. A horse is the winner if it gets to the finish line before any other horse. When one horse wins, the game ends. We know the race will end. The unknown is which horse gets to the finish first.

13 Fall 2008ACS-1805 Ron McFadyen13 Storyboard race if one of the horses has won the winner says, “I won!!!” else randomly choose one horse and move it forward a small amount race again When this is true, the method ends Each time we move a little closer to an ending situation Recursive call to do this all again A couple of solutions: First:HorseRaceV1.a2wHorseRaceV1.a2w Second :HorseRaceV2.a2wHorseRaceV2.a2w

14 Fall 2008ACS-1805 Ron McFadyen14 More general forms of recursion Suppose there is something to do both before and after the recursive call: if a base condition is satisfied the method teminates else do something A recursive call do something B

15 Fall 2008ACS-1805 Ron McFadyen15 More general forms of recursion Suppose there is more than one recursive call: if a base condition is satisfied the method teminates else recursive call do something recursive call See mischief.a2wmischief.a2w

16 Fall 2008ACS-1805 Ron McFadyen16 A Towers Problem The challenge is to move all the disks from the source cone to the target cone. Move 1 disk at a time A larger disk may never be on top of a smaller disk Source Spare Target (coneFrom) (coneSpare) (coneTo) Run the solution to observe the process: towers.a2wtowers.a2w

17 Fall 2008ACS-1805 Ron McFadyen17 Initial world The disks are instances of the Torus class. (A torus is a doughnut shaped object.) Each cone is positioned exactly 1 meter from its nearest neighbor. Other than the bottom disk, each disk is positioned exactly 0.1 meter above the disk below.

18 Fall 2008ACS-1805 Ron McFadyen18 Identifying the disks To make it easier to describe our solution, we give each disk an id number and a name. id number name 1 disk1 2 disk2 3 disk3 4 disk4

19 Fall 2008ACS-1805 Ron McFadyen19 Solving the problem Our solution will use the Principle of “wishful thinking” assume we could solve a smaller version of the same problem if we could solve the smaller version, it would make it easier to solve this problem. Base case – the simplest possible version of this problem, one that can obviously be solved.

20 Fall 2008ACS-1805 Ron McFadyen20 Wishful thinking in practice Assume I could move 3 of the disks to the spare cone. Then I could move the 4 th disk (base case) to the target cone.

21 Fall 2008ACS-1805 Ron McFadyen21 Storyboard To solve the towers problem, we need to know how many disks we have and which cone is the source, the target, and the spare: towers Parameters: howmany, source, target, spare If howmany is equal to 1 move it (the smallest disk) from the source to the target Else Do in order call towers to move howmany-1 disks from source to spare (using target as spare) move it (disk # howmany) from the source to the target call towers to move howmany-1 disks from the spare to the target (using the source as the spare) base case – move 1 disk a smaller problem -- recursively move the rest of the disks Two recursive calls are used in this method.


Download ppt "Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself."

Similar presentations


Ads by Google