Transferring the top n 1 disks from source to

This preview shows page 5 - 11 out of 11 pages.

tower. Transferring the top N – 1 disks from Source to Auxiliary tower can again be thought of as a fresh problem and can be solved in the same manner.
Image of page 5

Subscribe to view the full document.

Tower of Hanoi TOWER(N, BEG, AUX, END) 1. If N = 1 then a. Write BEG -> END b. Return 2. [Move N-1 disks from peg BEG to peg AUX] Call TOWER(N-1, BEG, END, AUX) 3. Write BEG -> END 4. [Move N-1 disks from peg AUX to peg END] Call TOWER(N-1, AUX, BEG, END) 5. Return.
Image of page 6
Tower of Hanoi For N = 4 we get the following sequence 1. Move from Src to Aux 2. Move from Src to Dst 3. Move from Aux to Dst 4. Move from Src to Aux 5. Move from Dst to Src 6. Move from Dst to Aux 7. Move from Src to Aux 8. Move from Src to Dst 9. Move from Aux to Dst 10. Move from Aux to Src 11. Move from Dst to Src 12. Move from Aux to Dst 13. Move from Src to Aux 14. Move from Src to Dst 15. Move from Aux to Dst
Image of page 7

Subscribe to view the full document.

Tower of Hanoi How many moves will it take to transfer n disks from the left post to the right post? 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 for 6 disks ... ?
Image of page 8
Tower of Hanoi Explicit Pattern Number of Disks Number of Moves 1 1 2 3 3 7 4 15 5 31 Powers of two help reveal the 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
Image of page 9

Subscribe to view the full document.

Image of page 10
Tower of Hanoi Game link mes/tower_of_hanoi/
Image of page 11
  • Fall '19

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes