Sie sind auf Seite 1von 22

Tower Of Hanoi

Research Report

21 December 2015

Prepared by:
Faizan Ahmed
EP 1449027

Submitted To:
Sir Usman Amjad

Tower of Hanoi
21 December 2015

Acknowledgements
To complete this assignment, we took the some experience and
guideline of some respected persons, who deserve their thank-full
attitude. The completion of this report gives me much Pleasure. I would
like to give my Respect to Mr. Usman Amjad, Course Instructor, 402
for giving us a good chance for this assignment although numerous
consultations. I would also like to pay my deepest respect to all those
who have directly and indirectly guided us in writing this report.

Tower of Hanoi
21 December 2015

Contents
Acknowledgements ............................................................................................................. i
Executive Summary ............................................................................................................ 1
1

Introduction .................................................................................................................. 2
1.1

Background............................................................................................................. 2

1.2

Objectives ............................................................................................................... 3

1.3

Scope ..................................................................................................................... 4

Recursive Pattern of Hanoi .......................................................................................... 6

Methodology ................................................................................................................. 9
3.1

Research Questions ............................................................................................... 9

3.2

Research Design .................................................................................................... 9

3.3

Sample ................................................................................................................. 10

Algorithm Explaination .............................................................................................. 11

C# Hanoi Application with Code:............................................................................... 13

Results ........................................................................................................................ 18
6.1

Research Questions: ............................................................................................ 18

References .................................................................................................................. 18

ii

Tower of Hanoi
21 December 2015

Executive Summary

Abstract
We are going to examine a variation of the famous Tower of Hanoi puzzle that is
posed but not solved, in a 1944 paper by Scorer et al, In this variation, disks of
adjacent sizes can be moved, by the provided position that they are at the top of
their respective stacks. We present an algorithm for solving this problem, and
analyze its performance, and prove that it is optimal.

Methodology
In this research, we are going to discuss Iterative and Recursive problem Tower of Hanoi.

Key Terms Used


Tower of Hanoi
Representation approach
Algorithms

Tower of Hanoi
21 December 2015

1 Introduction
1.1 Background
This puzzle was invented by the French mathematician douard Lucas in 1883. There
is a story about an Indian temple in Kashi Vishwanath which contains a large room
with three time-worn posts in it surrounded by 64 golden disks. Brahmin priests,
acting out the command of an ancient prophecy, have been moving these disks, in
accordance with the immutable rules of the Brahma, since that time. The puzzle is
therefore also known as the Tower of Brahma puzzle.
This link is for reference
https://en.wikipedia.org/wiki/Tower_of_Hanoi#cite_note-2

Tower of Hanoi
21 December 2015

1.2 Objectives

The objectives of the report are to:

1- Introduction
2- Review of Key terms
3- Algorithm Explanation for each method
4- Application
5- Complete Code

Tower of Hanoi
21 December 2015

1.3 Scope

The Tower of Hanoi has been studied extensively in both Artificial Intelligence and
Psychology.

The Tower of Hanoi is used to solve problems hierarchically.

Where 'Hierachy' defines arranged in order of rank.

For this Recursive pattern of Hanoi is to be used to resolve problems.

Tower of Hanoi
21 December 2015

Tower of Hanoi
21 December 2015

2 Recursive Pattern of Hanoi

Since this is a classic puzzle. Our goal is to transfer all the pieces from the left position 1 to
the right position 3.

Tower of Hanoi
21 December 2015

Rule: We can only move one disk at a time and you can never put a bigger disk on top of a
smaller disk.

From the Algorithm to transfer one, two, and three disks, we can easily find a recursive
pattern for additional disks.
- a pattern that uses information from one step to find the next step
- for moving n disks from post A to post C:

First, transfer n-1 disks from post A to post B. The number of moves will be the same as
those needed to transfer n-1 disks from post A to post C. Call this number M moves. [As you
can see above, with three disks it takes 3 moves to transfer two disks (n-1) from post A to
post C.]
Next, transfer disk 1 to post C [1 move].
Finally, transfer the remaining n-1 disks from post B to post C. [Again, the number of moves
will be the same as those needed to transfer n-1 disks from post A to post C, or M moves.]

Tower of Hanoi
21 December 2015

Therefore the number of moves needed to transfer n disks from post A to post C is 2M+1,
where M is the number of moves needed to transfer n-1 disks from post A to post C.

However, the recursive pattern can help us generate more numbers to find an explicit (nonrecursive) pattern. Here's how to find the number of moves needed to transfer larger
numbers of disks from post A to post C, remembering that M = the number of moves needed
to transfer n-1 disks from post A to post C:

for 1 disk it takes 1 move to transfer 1 disk from post A to post C;


for 2 disks, it will take 3 moves:

2M + 1 = 2(1) + 1 = 3

for 3 disks, it will take 7 moves:

2M + 1 = 2(3) + 1 = 7

for 4 disks, it will take 15 moves: 2M + 1 = 2(7) + 1 = 15


for 5 disks, it will take 31 moves: 2M + 1 = 2(15) + 1 = 31

And simply:

The minimum number of moves required to solve a Tower of Hanoi puzzle is 2n - 1, where n
is the number of disks.

Tower of Hanoi
21 December 2015

3 Methodology
3.1 Research Questions

The research questions to be answered by this Report are:

1- Introduction
2- Review of Key terms
3- Algorithm Explanation for each method
4- Application
5- Complete Code

3.2 Research Design

This Report will use Descriptive methods to answer the research questions. The following
table summarises the questions and the other methods are used to answer the each
questions:

Table 1 Methods used to answer research questions

Research Question

Method Used to Answer Question

Introduction

Mathematical Puzzle

Algorithm

Recursive

Minimum moves

Define in this paper

Tower of Hanoi
21 December 2015

3.3 Sample
Some samples for this report are used that are:
https://www2.bc.edu/~grigsbyj/Spafford_Final.pdf
www.ro.uow.edu.au/cgi/viewcontent.cgi?article=1017&context=compsciwp
www.cs.wm.edu/~pkstoc/gov.pdf
www.cs.nyu.edu/courses/summer07/G22.2340-001/.../McCann.pdf

www.cs.rutgers.edu/~szegedy/PUBLICATIONS/tower1.pdf

10

Tower of Hanoi
21 December 2015

4 Algorithm Explaination
Explaination:
The algorithm is pretty clearly show that we first remove n 1 discs to get access to
the nth column. And then we have to move the discs first to another peg after than,
where you want the full tower.
The algorithm has three arguments every time, ignoring the number of discs:
A source peg, a destination peg and a temporary peg on which discs are initially set,
in between (where every disc with size n 1 fits).
The recursion happens actually there two times, in every time procedure call. Once
before the WRITELN and once after that.
The one before the WRITELN will move n 1 discs onto the temporary peg, using
the destination peg as temporary storage (the arguments in the recursive call are in
different order).
After that, the remaining disc will be moved to the destination peg and afterwards the
second recursion completes the moving of the entire tower, by moving the n 1
tower from the temp peg to the destination peg, above disc n.

11

Tower of Hanoi
21 December 2015

Here is Algorithm:

procedure Hanoi(n: integer; source, dest, by: char);


Begin
if (n=1) then
writeln('Move the plate from ', source, ' to ', dest)
else begin
Hanoi(n-1, source, by, dest);
writeln('Move the plate from ', source, ' to ', dest);
Hanoi(n-1, by, dest, source);
end;
End;

12

Tower of Hanoi
21 December 2015

5 C# Hanoi Application with Code:

13

Tower of Hanoi
21 December 2015

Complete Code:
using System.Collections.Generic;
using System.Linq;
namespace HanoiTower
{
public class HanoiTowerClass
{
private int steps = 0;
Stack<int> Tower1 = new Stack<int>();
Stack<int> Tower2 = new Stack<int>();
Stack<int> Tower3 = new Stack<int>();
private void Reset()
{
Tower1.Clear();
Tower2.Clear();
Tower3.Clear();
}
private Stack<int> GetTower(Towers tower)
{
if (tower == Towers.Left)
return Tower1;
else if (tower == Towers.Middle)
return Tower2;
else
return Tower3;
}
public List<int> GetTowerLeft()
{
return Tower1.ToList<int>();
}
public List<int> GetTowerMiddle()
{
return Tower2.ToList<int>();
}
public List<int> GetTowerRight()
{
return Tower3.ToList<int>();
}
public void Init(int steps)
{
Reset();
this.steps = steps > Constant.MaxSteps ? Constant.MaxSteps : steps;
for (int i = this.steps; i > 0; i--)
Tower1.Push(i);
}

14

Tower of Hanoi
21 December 2015

public int GetStep(Towers from)


{
Stack<int> fromTower = GetTower(from);
if (fromTower.Count < 1)
return 0;
return fromTower.Peek();
}
public bool Move(Towers from, Towers to)
{
int fromStep = 0;
int toStep = 0;
Stack<int> fromTower = GetTower(from); ;
Stack<int> toTower = GetTower(to); ;
if (fromTower.Count < 1) return false;
fromStep = fromTower.Peek();
if (toTower.Count < 1) toStep = 10;
else toStep = toTower.Peek();
if (fromStep < toStep)
{
toTower.Push(fromTower.Pop());
return true;
}
else if (fromStep == toStep)
return true;
else
return false;
}
public bool IsComplete()
{
return Tower1.Count == 0 && (Tower2.Count == steps || Tower3.Count == steps);
}
}
public enum Towers
{
Left,
Middle,
Right
}
public class Constant
{
public const
public const
public const
public const
public const
public const
}
}

int
int
int
int
int
int

BottomTop = 333;
StepThickness = 25;
ColumnWidth = 150;
HandTop = 20;
Offset = 12;
MaxSteps = 6;

15

Tower of Hanoi
21 December 2015

Screenshots:

16

Tower of Hanoi
21 December 2015

17

Tower of Hanoi
21 December 2015

6 Results
6.1 Research Questions:

Hance all research questions are answered.

7 References
All necessary references are provided in related sections.

18

Tower of Hanoi
21 December 2015

19

Das könnte Ihnen auch gefallen