Ch 8 Recursion - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Ch 8 Recursion

Description:

Each time we select a horse that horse moves closer to the finish. ... Move 1 disk at a time. A larger disk may never be on top of a smaller disk. Source Spare Target ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 22
Provided by: wand85
Category:
Tags: recursion

less

Transcript and Presenter's Notes

Title: Ch 8 Recursion


1
Ch 8 Recursion
Recursion occurs when a method (or function)
calls itself
2
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
Examples of recursion
  • The natural numbers
  • 1 is in N
  • if n is in N, then n 1 is in N

4
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
Fibonacci number sequence
  • Fibonacci number sequence
  • F(n) F(n - 1) F(n - 2).
  • Base cases
  • F(0) 0
  • F(1) 1.

6
Factorials
  • Factorials
  • n! n (n - 1)!
  • 3!32!
  • 4!43!
  • 2!21!
  • Base cases
  • 0! 1
  • 1! 1

7
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
  • recursion 22, 45, 80, 189

8
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 gt 0.5 Do together shark swims toward
goldfish goldfish flees call CHASER Shark eats
Recursive call
Shark swims
Goldfish flees
Shark eats
chaseRecursion.a2w
9
Example shark/goldfish again
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
10
Example shark/goldfish again
  • 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.

11
Example shark/goldfish again
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.
12
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
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.a2w Seco
nd HorseRaceV2.a2w
14
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
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.a2w
16
A Towers Problem
Source Spare
Target (coneFrom) (coneSpare) (coneTo)
  • 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

Run the solution to observe the process
towers.a2w
17
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
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
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
Wishful thinking in practice
Assume I could move 3 of the disks to the spare
cone.
Then I could move the 4th disk (base case) to the
target cone.
21
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.
Write a Comment
User Comments (0)
About PowerShow.com