SlideShare a Scribd company logo
2
Most read
5
Most read
10
Most read
Data Structure and Algorithms The Tower of Hanoi
Tower of Hanoi
 Tower of Hanoi is a mathematical puzzle invented by a
French Mathematician in 1883.
 The game starts by having few discs stacked in
increasing order of size. The number of discs can vary,
but there are only three pegs.
Tower of Hanoi
 The Objective is to transfer the entire tower to one of
the other pegs. However you can only move one disk at
a time and you can never stack a larger disk onto a
smaller disk. Try to solve it in fewest possible moves.
Tower of Hanoi
How to solve the 4 discs
Tower of Hanoi
 Recursive Solution for the Tower of Hanoi with
algorithm.
 Let’s call the three peg Src(Source), Aux(Auxiliary) and
Dst(Destination).
 Move the top N – 1 disks from the Source to Auxiliary
tower.
 Move the Nth disk from Source to Destination tower.
 Move the N – 1 disks from Auxiliary tower to Destination
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.
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.
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
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... ?
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
Thank You

More Related Content

PPTX
Tower of hanoi
PPTX
Tower of hanoi
PPTX
Tower Of Hanoi
PPTX
Tower Of Hanoi -A MatheMatical PuZzle
PPTX
Bloom’s Taxonomy PowerPoint Presentation 2017
PDF
Internet of Things(IOT)_Seminar_Dr.G.Rajeshkumar
PPTX
Machine Learning Using Python
PDF
Tower of hanoi
Tower of hanoi
Tower Of Hanoi
Tower Of Hanoi -A MatheMatical PuZzle
Bloom’s Taxonomy PowerPoint Presentation 2017
Internet of Things(IOT)_Seminar_Dr.G.Rajeshkumar
Machine Learning Using Python

What's hot (20)

PPTX
Tower of Hanoi
PPTX
Dfs presentation
PPTX
Trees in data structures
PPTX
Binary Search Tree in Data Structure
PPTX
B and B+ tree
PPTX
Doubly Linked List
PPT
BINARY TREE REPRESENTATION.ppt
PPTX
Bfs and Dfs
PPT
3.9 external sorting
PPTX
Depth-First Search
PPT
Unit 4 external sorting
PPTX
AVL Tree in Data Structure
PPTX
Linked list
PPTX
Graph traversals in Data Structures
PPTX
Asynchronous data transfer
PPTX
Breadth First Search & Depth First Search
PPTX
Dijkstra's algorithm presentation
PPT
Abstract data types
PPTX
Problem reduction AND OR GRAPH & AO* algorithm.ppt
PPTX
1.7. eqivalence of nfa and dfa
Tower of Hanoi
Dfs presentation
Trees in data structures
Binary Search Tree in Data Structure
B and B+ tree
Doubly Linked List
BINARY TREE REPRESENTATION.ppt
Bfs and Dfs
3.9 external sorting
Depth-First Search
Unit 4 external sorting
AVL Tree in Data Structure
Linked list
Graph traversals in Data Structures
Asynchronous data transfer
Breadth First Search & Depth First Search
Dijkstra's algorithm presentation
Abstract data types
Problem reduction AND OR GRAPH & AO* algorithm.ppt
1.7. eqivalence of nfa and dfa
Ad

Similar to Data Structure and Algorithms The Tower of Hanoi (20)

PDF
Tower of hanoi algorithm
PPTX
TOWERS OF HANOI_problemsolutionandexplanationwithalgorithm.pptx
PPT
Dc8c4f010f40.hanoi.towers
DOCX
Tower of hanoi
PPTX
Tower of Hanoi presentation
PPTX
Tower of Hanoi
PPTX
Data structure lab
PPTX
Recurrent Problem in Discrete Mathmatics.pptx
PPT
Tower of hanoi
PPTX
Design and Analysis of Algorithms Tower of Hanoi PPT.pptx
PDF
The Towers of Hanoi puzzle has three posts and some number n of disk.pdf
PPTX
Stochastic Process Assignment Help
PPTX
L2_Time_complexity_recursive_function.pptx
PDF
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
PPTX
Tower of hanoi
PPTX
The Disk Method
PDF
Recursion - Computer Algorithms
PPTX
Control System Homework Help
PPTX
Stochastic Process Exam Help
PPTX
Network Design Assignment Help
Tower of hanoi algorithm
TOWERS OF HANOI_problemsolutionandexplanationwithalgorithm.pptx
Dc8c4f010f40.hanoi.towers
Tower of hanoi
Tower of Hanoi presentation
Tower of Hanoi
Data structure lab
Recurrent Problem in Discrete Mathmatics.pptx
Tower of hanoi
Design and Analysis of Algorithms Tower of Hanoi PPT.pptx
The Towers of Hanoi puzzle has three posts and some number n of disk.pdf
Stochastic Process Assignment Help
L2_Time_complexity_recursive_function.pptx
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
Tower of hanoi
The Disk Method
Recursion - Computer Algorithms
Control System Homework Help
Stochastic Process Exam Help
Network Design Assignment Help
Ad

More from ManishPrajapati78 (15)

PPT
Data Structure and Algorithms Binary Search Tree
PPT
Data Structure and Algorithms Binary Tree
PPT
Data Structure and Algorithms Queues
PPTX
Data Structure and Algorithms Merge Sort
PPT
Data Structure and Algorithms Stacks
PPT
Data Structure and Algorithms Linked List
PPT
Data Structure and Algorithms Sorting
PPT
Data Structure and Algorithms Arrays
PPT
Data Structure and Algorithms
PPT
Data Structure and Algorithms Hashing
PPTX
Data Structure and Algorithms Graph Traversal
PPT
Data Structure and Algorithms Graphs
PPT
Data Structure and Algorithms Huffman Coding Algorithm
PPT
Data Structure and Algorithms Heaps and Trees
PPT
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Queues
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Stacks
Data Structure and Algorithms Linked List
Data Structure and Algorithms Sorting
Data Structure and Algorithms Arrays
Data Structure and Algorithms
Data Structure and Algorithms Hashing
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graphs
Data Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms AVL Trees

Recently uploaded (20)

PPTX
Essential Infomation Tech presentation.pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
medical staffing services at VALiNTRY
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Digital Strategies for Manufacturing Companies
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
System and Network Administration Chapter 2
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Transform Your Business with a Software ERP System
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
top salesforce developer skills in 2025.pdf
PPTX
history of c programming in notes for students .pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
AI in Product Development-omnex systems
Essential Infomation Tech presentation.pptx
Design an Analysis of Algorithms II-SECS-1021-03
medical staffing services at VALiNTRY
Wondershare Filmora 15 Crack With Activation Key [2025
Digital Strategies for Manufacturing Companies
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
VVF-Customer-Presentation2025-Ver1.9.pptx
System and Network Administration Chapter 2
Navsoft: AI-Powered Business Solutions & Custom Software Development
Transform Your Business with a Software ERP System
Odoo Companies in India – Driving Business Transformation.pdf
Softaken Excel to vCard Converter Software.pdf
L1 - Introduction to python Backend.pptx
How to Migrate SBCGlobal Email to Yahoo Easily
How Creative Agencies Leverage Project Management Software.pdf
top salesforce developer skills in 2025.pdf
history of c programming in notes for students .pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
AI in Product Development-omnex systems

Data Structure and Algorithms The Tower of Hanoi

  • 2. Tower of Hanoi  Tower of Hanoi is a mathematical puzzle invented by a French Mathematician in 1883.  The game starts by having few discs stacked in increasing order of size. The number of discs can vary, but there are only three pegs.
  • 3. Tower of Hanoi  The Objective is to transfer the entire tower to one of the other pegs. However you can only move one disk at a time and you can never stack a larger disk onto a smaller disk. Try to solve it in fewest possible moves.
  • 4. Tower of Hanoi How to solve the 4 discs
  • 5. Tower of Hanoi  Recursive Solution for the Tower of Hanoi with algorithm.  Let’s call the three peg Src(Source), Aux(Auxiliary) and Dst(Destination).  Move the top N – 1 disks from the Source to Auxiliary tower.  Move the Nth disk from Source to Destination tower.  Move the N – 1 disks from Auxiliary tower to Destination 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.
  • 6. 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.
  • 7. 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
  • 8. 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... ?
  • 9. 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