The importance of pre-condition in Recursion: Tower of Hanoi
- Recall that a problem suitable for
Recursive Method must satisfy:
- The Problem of size N can
be divided into:
- One or more problems of smaller size
- The smaller problems
MUST
be identical in nature
as Problem(N).
- The Original Problem
can be solved
easily
using the solutions
of the smaller problems
- Some base case(s)
(smallest problems) of the problem
have easy solutions
- A better to state
identical in nature is:
- have the IDENTICAL pre-conditions
- Pre-condition:
- Condition that
exists at the moment
you start solving a problem.
- To illustrate what
having the same pre-conditions means,
consider the "Tower of Hanoi" problem:
- 3 pegs
- N disks, with a hole in the center, each of different diameter
- Initially, all N disks at stacked on peg 1, in decreasing diameter
Problem(N):
- Move the N disks from peg 1 to peg 3
- You can only one disk at a time (from any peg to any other peg), and
- and no disk may rest on top of a disk with smaller diameter
- Here is a web site with a nice Tower of Hanoi applet
for you to play:
click here
- Recursive solution for Tower of Hanoi:
reality check
- Can
the Tower of Hanoi problem with N disks
be divided into:
- One or more
Tower of Hanoi problem with < N disks
?
- Do all the smaller
Tower of Hanoi problems
have
IDENTICAL PRE-CONDITIONS
as Tower of Hanoi problem with N disks ?
- Can Tower of Hanoi problem with N disks
be solved easily
using the solution
of the smaller
Tower of Hanoi problems ?
- What is the base case ???
- Recursive Solution 1:
click here
- Recursive Solution 2:
click here