Question

Refer the "Tower oh Hanoi" concept 1. What is the minimum number of steps if there...

Refer the "Tower oh Hanoi" concept

1. What is the minimum number of steps if there are 1, 2, 3, 4, or more disks? Try and solve the puzzle and fill in the table.

1 1
2 3
3 7
4 15
5 ?
6 ?
7 ?
8 ?

2. Write an explicit formula for the minimum number of steps required for the puzzle? Use H(n) = ? where n is the number of disks

3. What would be the best ADT to use for the posts in the puzzle? Why is that data structure best?

4. What would be the base case (n = 1) of this problem if a recursive solution was employed? Use three of the ADTs from the previous question for storage. The three posts are labeled A, B, C. Post A is the the starting post and Post C is the ending post. Write the answer in in pseudo code.

5. What are the rest of the cases given the base case?

if (n == 1)

            disk = A.top()

            A.pop()

            C.push(disk)

C++

Homework Answers

Answer #1

SOLUTION-

1. What is the minimum number of steps if there are 1, 2, 3, 4, or more disks? Try and solve the puzzle and fill in the table.

1 1
2 3
3 7
4 15
5 31
6 63
7 127
8

255

2. Write an explicit formula for the minimum number of steps required for the puzzle? Use H(n) = ? where n is the number of disks

We can derive a common pattern using values in the above table;

for number of disks (n=1), we have number of steps: 1 (2^1-1)

for number of disks (n=2), we have number of steps: 3 (2^2-1)

for number of disks (n=3), we have number of steps: 7 (2^3-1)

for number of disks (n=4), we have number of steps: 15 (2^4-1)

for number of disks (n=5), we have number of steps: 31 (2^5-1)

for number of disks (n=6), we have number of steps: 63 (2^6-1)

for number of disks (n=7), we have number of steps: 127 (2^7-1)

for number of disks (n=8), we have number of steps: 255 (2^8-1)

Thus,formula: H(n)= 2^n - 1; where n=number of disks

3. What would be the best ADT to use for the posts in the puzzle? Why is that data structure best?

So the Best data structure for the posts in this puzzle is a stack. Stack is used to  follows convention of first in last out(FILO). In this puzzle, we have three rings arranged in ascending order from top to bottom residing on a tower(stack) at any given point of time. The largest ring is placed first on the post and removed last following the convention of stack. Similarly, the smallest ring is placed last on top of other rings and removed first before any other ring thus, following he convention of a stack. Thus, stack is the best data structure.

4. What would be the base case (n = 1) of this problem if a recursive solution was employed? Use three of the ADTs from the previous question for storage. The three posts are labeled A, B, C. Post A is the the starting post and Post C is the ending post. Write the answer in in pseudo code.

If we follow recursive appeoach, the best case scenario would when our problem is reduced to only 1 disk out of n disks. This means we need to transfer only one disk from our source to destination. Using the formula we derived above, this can be achieved in 1 step only (2^1-1). This would be the case when we would want to move our largest disk whoch is being kept at the bottom of the source to our destination. If we wish to track the movement of every disk form one tower to another we can write the best case as follows:

void towerOfHanoi(int n, char source, char auxillary, char destination){

if ( n == 1){

disk=source.top;

source.pop();

destination.push(disk);

cout<<"Move disk 1 from "<<source<<" to "<<destination;

         return;

}

}

5. What are the rest of the cases given the base case?

Rest of the function includes recursive calls to the function towerOfHanoi with no of disks equals one less than the given number as 1 dis is already shifted to its correct place.

towerOfHamoi(n-1,source,destination,auxillary);

cout<<"Move disk 1 from "<<source<<" to "<<destination;

towerOfHanoi(n-1,auxillary,source,destination);

In this algorithm we are calling the funtion towerOfHanoi recursively. Base case if achieved when we have already one disk left to be moved. This is shown above. For rest of the n-1 disks, we will first move them from source to destination using auxillary rod. Once n-1 disks are moved to auxillary rod, we can easily move the last remaining disk in source to destination. We will then recursively repeat our approach considering there are n-1 disks.


IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I have a recursive Tower of Hanoi program but don't know how to include the count...
I have a recursive Tower of Hanoi program but don't know how to include the count variable. Could you tell me how? Main.java import java.io.*; import java.util.List; public class Main { //int count = 0; /**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is to move the disks from pole A to pole B without ever putting a larger disk on top of a smaller disk.*/...
(javascript/css/html) Follow these steps to implement the following browser-based puzzle game: 1. Get a photo of...
(javascript/css/html) Follow these steps to implement the following browser-based puzzle game: 1. Get a photo of yourself and save it as an image file 2. Use a image-splitting program such as splitter.imageonline.co to break the image into 9 roughly equal parts (3 x 3). Save those files in a directory 3. Write Javascript that takes these nine images and randomly rearranges them in a 3 x 3 grid. 4. Each cell in the grid will also have a checkbox. 5....
LANGUAGE IS C++ 1- A link based getEntry method requires how many steps to access the...
LANGUAGE IS C++ 1- A link based getEntry method requires how many steps to access the nth item in the list? a)n b)n+1 c)n^2 d)n-1 2- When calling the insert or remove methods, what is an disadvantage for the link-based implementation of the ADT List? a)harder to understand b)must shift data c)searching for that position is slower d)takes more memory 3-What is the last step in the insertion process for a linked implementation of the ADT List? a)Connect the new...
Place a number (1-7) next to each of the steps given below to put them in...
Place a number (1-7) next to each of the steps given below to put them in the correct chronological order. There are a few processes that happen simultaneously – give them the same number with a sub-letter (e.g. 6a and 6b), but be sure to indicate which numbers are happening at roughly the same time. 1. ___ Binding of glutamate to NMDA receptors 2. ___ Entry of Na+ and exit of K+ from post-synaptic membrane 3. ___ Depolarization of post-synaptic...
We are given a sequence of numbers: 1, 3, 5, 7, 9, . . . and...
We are given a sequence of numbers: 1, 3, 5, 7, 9, . . . and want to prove that the closed formula for the sequence is an = 2n – 1.          What would the next number in the sequence be? What is the recursive formula for the sequence? Is the closed formula true for a1? What about a2? What about a3? Critical Thinking How many values would we have to check before we could be sure that the...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n == 0)    return 0; else    return n * f(n - 1); } A. 120 B. 60 C. 1 D. 0 10 points    QUESTION 2 Which of the following statements could describe the general (recursive) case of a recursive algorithm? In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt(int n ) // line 1 { // line 2...
CAN YOU PLEASE ANSWER ALL THESE QUSTIONS 1.In Merge sort, how many additional recursive partitioning levels...
CAN YOU PLEASE ANSWER ALL THESE QUSTIONS 1.In Merge sort, how many additional recursive partitioning levels are required for a list of 64 elements compared to a list of 8 elements? a. 3 b. 9 c. 8 d. 6 2. Which function best represents the number of operations in the worst-case for the following code fragment? for (i = 0; i < N; ++i) {    if (numbers[i] % 2 == 1)       factor = 2.5 } a. f(N) = 6N2 b....
What is the correct order of enzyme action during DNA replication? Number the steps from 1...
What is the correct order of enzyme action during DNA replication? Number the steps from 1 to 7. _____ Synthesis of RNA primers (priming) _____ Helicase/topoisomerase activity _____ DNA polymerization/proofreading _____ 5’-3’ exonuclease activity/DNA polymerization _____ Ligation _____ Single-strandedbindingproteinsbindand prevent reannealing _____ Binding to origin of replication (AT rich sequence)
Create in JAVA Suppose you are designing a game called King of the Stacks. The rules...
Create in JAVA Suppose you are designing a game called King of the Stacks. The rules of the game are as follows:  The game is played with two (2) players.  There are three (3) different Stacks in the game.  Each turn, a player pushes a disk on top of exactly one of the three Stacks. Players alternate turns throughout the game. Each disk will include some marker to denote to whom it belongs.  At the end...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT