Question

In: Computer Science

Describe an algorithm to solve the variant of the Towers of Hanoi in as few moves...

Describe an algorithm to solve the variant of the Towers of Hanoi in as few moves as possible. Prove that your algorithm is correct. Initially, all the n disks are on peg 1, and you need to move the disks to peg 2. You are not allowed to put a bigger disk on top of a smaller disk.

1. Suppose you are forbidden to move any disk directly between peg 1 and peg 2, and every move must involve (the third peg). Exactly (i.e., not asymptotically) how many moves does your algorithm make as a function of n?

Solutions

Expert Solution

How many moves will it take to transfer n disks from the left post to the right post?

1 disk: 1 move

  • Move 1: move disk 1 to post C

2 disks: 3 moves

  • Move 1: move disk 2 to post B
    Move 2: move disk 1 to post C
    Move 3: move disk 2 to post C

3 disks: 7 moves

  • Move 1: move disk 3 to post C
    Move 2: move disk 2 to post B
    Move 3: move disk 3 to post B
    Move 4: move disk 1 to post C
    Move 5: move disk 3 to post A
    Move 6: move disk 2 to post C
    Move 7: move disk 3 to post C

A. Recursive pattern

Here M = the number of moves

  1. for 1 disk it takes 1 move to transfer 1 disk from post A to post C;
  2. for 2 disks, it will take 3 moves:    2M + 1 = 2(1) + 1 = 3
  3. for 3 disks, it will take 7 moves:    2M + 1 = 2(3) + 1 = 7
  4. for 4 disks, it will take 15 moves: 2M + 1 = 2(7) + 1 = 15
  5. for 5 disks, it will take 31 moves: 2M + 1 = 2(15) + 1 = 31

B. Explicit Pattern

  • Number of Disks (n) Number of Moves
    1 2^1 - 1 = 2 - 1 = 1
    2 2^2 - 1 = 4 - 1 = 3
    3 2^3 - 1 = 8 - 1 = 7
    4 2^4 - 1 = 16 - 1 = 15
    5 2^5 - 1 = 32 - 1 = 31

So the formula for finding the number of steps it takes to transfer n disks from post A to post B is: 2^n - 1.

Algoithm:

 START Procedure Hanoi(disk, A, C, B) IF disk == 1, THEN move disk from A to C ELSE Hanoi(disk - 1, A, B, C) // Step 1 move disk from A to C // Step 2 Hanoi(disk - 1, B, C, A) // Step 3 END IF END Procedure STOP

Related Solutions

Consider the following variants of the Towers of Hanoi. For each of variant, describe an algorithm...
Consider the following variants of the Towers of Hanoi. For each of variant, describe an algorithm to solve it in as few moves as possible. Prove that your algorithm is correct. Initially, all the n disks are on peg 1, and you need to move the disks to peg 2. In all the following variants, you are not allowed to put a bigger disk on top of a smaller disk. Consider the disappearing Tower of Hanoi puzzle where the largest...
How many moves are required to solve the Towers of Hanoi problem when we have 3...
How many moves are required to solve the Towers of Hanoi problem when we have 3 disks? Group of answer choices 5 6 8 4 7 After the following statements execute, what item is at the front of the queue? QueueInterface<String> zooDelivery = new LinkedQueue<>(); zooDelivery.enqueue("lion"); zooDelivery.enqueue("tiger"); zooDelivery.enqueue("cheetah"); String next = zooDelivery.dequeue(); next = zooDelivery.dequeue(); zooDelivery.enqueue("jaguar"); zooDelivery.enqueue("cat"); Group of answer choices "lion" "tiger" "cheetah" "jaguar" "cat" When a recursive method does not include a base case, calling the method results...
Implement an iterator that produces the moves for the Towers of Hanoi puzzle described in Worked...
Implement an iterator that produces the moves for the Towers of Hanoi puzzle described in Worked Example 11.2. Provide functions has_more_moves and next_move. The next_move function should yield a string describing the next move. For example, the following code prints all moves needed to move five disks from peg 1 to peg 3: DiskMover mover(5, 1, 3); while (mover.has_more_moves()) { cout << mover.next_move() << endl; } Hint: A disk mover that moves a single disk from one peg to another...
Write a java program to solve Towers of Hanoi with the condition that there are "m"...
Write a java program to solve Towers of Hanoi with the condition that there are "m" number of rods and "n" number of disks. Where m >= 3 and n >=1.
i want it in C++.You will solve the Towers of Hanoi problem in an iterative manner...
i want it in C++.You will solve the Towers of Hanoi problem in an iterative manner (using Stack) in C++(using data structure). Note: you have to solve for N number of disks, 3 Towers (Stacks). Do not use recursion. For better understanding play the game at least once. Link:https://www.mathsisfun.com/games/towerofhanoi.html
There is a famous puzzle called the Towers of Hanoi that consists of three pegs and...
There is a famous puzzle called the Towers of Hanoi that consists of three pegs and n circular disks, all of different sizes. The disks start on the leftmost peg, with the largest disk on the bottom, the second largest on top of it, and so on, up to the smallest disk on top. The goal is to move the disks so that they are stacked in this same order on the rightmost peg. However, you are allowed to move...
towers of hanoi c++ program using stacks and singly linked lists.
towers of hanoi c++ program using stacks and singly linked lists.
Program a solver for the Towers of Hanoi problem presented below. Submit the following to canvas:...
Program a solver for the Towers of Hanoi problem presented below. Submit the following to canvas: Hanoi.java, Driver.java (contains your main method). This is an individual project. Students may discuss solutions to the problem but may not share code with one another. I will discuss this project during our next lecture. Rules: a) There are three Pillars (Pillar1, Pillar2, Pillar3). b) There are N number of disks of increasing size (disk size is indicated by an integer). c) At the...
By using javaFX, write and design the Towers of Hanoi game with Redo and Undo features...
By using javaFX, write and design the Towers of Hanoi game with Redo and Undo features with play and solve options.
JAVA Program a solver for the Towers of Hanoi problem presented below. Submit the Following: Hanoi.java,...
JAVA Program a solver for the Towers of Hanoi problem presented below. Submit the Following: Hanoi.java, Driver.java (contains your main method). Rules: a) There are three Pillars (Pillar1, Pillar2, Pillar3). b) There are N number of disks of increasing size (disk size is indicated by an integer). c) At the start, all disks are stacked on one of the pillars. d) At no point can a disk of larger size be placed above a disk of smaller size (including the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT