• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

Tower of Hanoi

June 21, 2011 by ProjectsGeek Leave a Comment

To implement a Tower of Hanoi (Recursive implementation)

Aim:-

Write a program to implement a Tower of Hanoi (Recursive implementation) in

C /C++ language.

Objective:-

To understand and implement Recursive algorithm using the Tower of Hanoi

problem and study Divide and Conquer strategy.

Theory:-

Divide and conquer strategy:

Divide: Divide the problem into a number of subproblem that are smaller instances of the same problem.

Conquer: Conquer the subproblem by solving them recursively. If the subproblem sizes are small enough, just solve the subproblems in a straight forward manner.

Combine: Combine the solutions to the sub problems into the solutions for the original problem.

When the subproblems are large enough to solve recursively, we call that the recursive case. Once the subprobles become small enough that we no longer recurse, recursion “bottoms out” and that we have gone down to the base case. In combine step, we solve subproblems that are quite not same as original.

Recurrences go hand in hand with divide-and-conquer paradigm, because they give us a natural way to characterize the running times of divide-and-conquer algorithms.

Algorithm DAndC(P)

{

If small((P) then return S(P);

Else

{

Divide P into smaller instances say P1,P2,P3……….Pk ,

k>=1;

Apply DAndC to each of thse subproblems

Return Combine(DAndC(P1),DAndC(P2),……DAndC(Pk));

}

}

Tower of Hanoi problem:

Assume that there are ‘n’ number of disks on tower A. The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top. Besides this tower, there are two other towers labeled B and C. The disks from tower A are to be moved to tower B using tower C as intermediate storage. The disks are to be moved one at a time. In addition, at no time can a disk be on top of a smaller disk.

Assume that there are n number of disks on tower A. To get the largest disk to the bottom of tower B, we move the remaining n-1 disks to tower C and then move the largest disk to tower B. Now ,we are left with the task of moving the disks from tower C to tower B. To do this, we have tower A and B available. The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it.

Algorithm TowerofHanoi(n,x,y,z)

//Move the top n disks from tower x to tower y

{

If(n >= 1)then

{

TowerofHanoi(n-1,x,z,y);

Write(“move top disk from tower”,x,”to top of tower”,y);

TowerofHanoi(n-1,z,y,x);

}

}

The recursive nature of the solution is apparent from the above algorithm. Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems.

Analysis

Recurrence relation:

T(n)=g(n)…….. n is small

=T(n1)+T(n2)+……+T(nk)+f(n)

In general,

T(n)=T(1)

=aT(n/b)+f(n). n=2k

Example:

tn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C. 1 is for the movement of the bottommost disk from tower A to tower B.

Second t(n-1) = movement of the (n-1) disks from tower C to tower B.

tn = 2t(n-1) + 1

Thus,

tn – 2tn-1 = 1 b=1

p(n)=1

Characteristic polynomial is (x-2) (x-1)

tn = c1 1n + c2 2n

t1= 2t0 + 1

t0 = 0 …..Original recursive relation

t1 = 1

when n=0

tn = c1 + c2 = 0, t0 is0

When n=1

tn = c1 + 2c2 =1, t1 is0

-c2 = -1

c2 = 1

c1 = -1

tn = 2n-1 ……tower of hanoi

tn = number of disk movements.

Other Projects to Try:

  1. Tower of Hanoi problem code in C Language
  2. 8 Queen Problem
  3. Software Design Laboratory Assignments
  4. Finding Real solutions of Quadratic equations in Java code
  5. N queen problem code in C Language

Filed Under: Design and Analysis of Algorithms

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy