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...
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.
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...
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...
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...
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the...
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the following declaration: void towers(int n, char source, char dest, char spare); The function outputs a message describing each move. The source, destination, and spare poles are indicated with a character identifier of your choosing ('A', 'B', 'C' are common). Write a MIPS assembly language program that demonstrates the Towers of Hanoi procedure. Your program should ask the user for the number of disks. The...
Design a variant of the hybrid merge-join algorithm for the case where both relations are not...
Design a variant of the hybrid merge-join algorithm for the case where both relations are not physically sorted, but both have a sorted secondary index on the join attributes.
In Lecture 5, we discussed how to solve the Tower of Hanoi problem (Exercise 5.36 in...
In Lecture 5, we discussed how to solve the Tower of Hanoi problem (Exercise 5.36 in the textbook). Consider the following problem variant in which one extra constraint has been added: There are three pegs and n disks of different sizes. Initially, all disks are on the leftmost peg and arranged in order of decreasing size, with the smallest disk on top. The task is to move all the disks to the rightmost peg, under the constraints that: • Only...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) Requirements Choose one problem with an algorithm and implement it. You should show and explain the result whatever you got. I recommend using N-Queen problem (at least N=8 or more) or any simple perfect games. For example, - N-Queen problem with hill climbing - N-Queen problem with simulated annealing - N-Queen problem with genetic algorithm - Tic-Tac-Toe with Minimax
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) N-Queen problem with genetic algorithm Please use the N-Queen problem (at least N=8 or more) or any simple perfect games. Please provide a screenshot of output and please heavily comment the code. Thanks!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT