# The importance of pre-condition in Recursion: Tower of Hanoi

1. 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

2. A better to state identical in nature is:

• have the IDENTICAL pre-conditions

3. Pre-condition:

• Condition that exists at the moment you start solving a problem.

4. 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

5. Here is a web site with a nice Tower of Hanoi applet for you to play: click here

6. Recursive solution for Tower of Hanoi: reality check

• Can the Tower of Hanoi problem with N disks be divided into:

1. One or more Tower of Hanoi problem with < N disks ?

2. 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 ???