SlideShare a Scribd company logo
2
Most read
5
Most read
6
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
Iffat Anjum
 
PPTX
Tower of hanoi
akshat saxena
 
PPTX
Tower Of Hanoi -A MatheMatical PuZzle
purvanahar
 
PPTX
Tower Of Hanoi
Vinit Dantkale
 
PPTX
Machine Learning Using Python
SavitaHanchinal
 
PPTX
Salesforce PPT.pptx
ShaikAllabakshu5
 
PPTX
LED
Javeria600
 
PPT
Algorithm analysis
sumitbardhan
 
Tower of hanoi
Iffat Anjum
 
Tower of hanoi
akshat saxena
 
Tower Of Hanoi -A MatheMatical PuZzle
purvanahar
 
Tower Of Hanoi
Vinit Dantkale
 
Machine Learning Using Python
SavitaHanchinal
 
Salesforce PPT.pptx
ShaikAllabakshu5
 
Algorithm analysis
sumitbardhan
 

What's hot (20)

PPTX
Breadth First Search & Depth First Search
Kevin Jadiya
 
PPTX
Tower of Hanoi
Ankit92Chitnavis
 
PPT
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
PPTX
Binary search
AparnaKumari31
 
PPT
Binary Search
kunj desai
 
PPTX
Prim's algorithm
Pankaj Thakur
 
PPTX
Queues
Ashim Lamichhane
 
PPTX
Binary Tree in Data Structure
Meghaj Mallick
 
PPT
Divide and conquer
Dr Shashikant Athawale
 
PPT
Heaps
Hafiz Atif Amin
 
PPTX
AVL Tree Data Structure
Afaq Mansoor Khan
 
PPTX
Tree - Data Structure
Ashim Lamichhane
 
PPTX
Tree Traversal
Md. Israil Fakir
 
PPTX
Lecture optimal binary search tree
Divya Ks
 
PPTX
trees in data structure
shameen khan
 
PPTX
Graph traversals in Data Structures
Anandhasilambarasan D
 
PDF
Applications of stack
eShikshak
 
PDF
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
PPTX
Data structure - Graph
Madhu Bala
 
Breadth First Search & Depth First Search
Kevin Jadiya
 
Tower of Hanoi
Ankit92Chitnavis
 
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
Binary search
AparnaKumari31
 
Binary Search
kunj desai
 
Prim's algorithm
Pankaj Thakur
 
Binary Tree in Data Structure
Meghaj Mallick
 
Divide and conquer
Dr Shashikant Athawale
 
AVL Tree Data Structure
Afaq Mansoor Khan
 
Tree - Data Structure
Ashim Lamichhane
 
Tree Traversal
Md. Israil Fakir
 
Lecture optimal binary search tree
Divya Ks
 
trees in data structure
shameen khan
 
Graph traversals in Data Structures
Anandhasilambarasan D
 
Applications of stack
eShikshak
 
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Data structure - Graph
Madhu Bala
 
Ad

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

PDF
Tower of hanoi algorithm
WeaamRaed
 
PPTX
TOWERS OF HANOI_problemsolutionandexplanationwithalgorithm.pptx
heyprettyaera
 
PPT
Dc8c4f010f40.hanoi.towers
Sumedha
 
DOCX
Tower of hanoi
Elijah Hanson
 
PPTX
Tower of Hanoi presentation
FahadQaiser1
 
PPTX
Tower of Hanoi
Varunjeet Singh Rekhi
 
PPTX
Data structure lab
prafulkumar15
 
PPTX
Recurrent Problem in Discrete Mathmatics.pptx
gbikorno
 
PPT
Tower of hanoi
murdhani heena
 
PPTX
Design and Analysis of Algorithms Tower of Hanoi PPT.pptx
pkumarisadhanand
 
PDF
The Towers of Hanoi puzzle has three posts and some number n of disk.pdf
fcsondhiindia
 
PPTX
Stochastic Process Assignment Help
Statistics Assignment Help
 
PPTX
L2_Time_complexity_recursive_function.pptx
3cL1Ps3FTMS
 
PDF
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
Roman Elizarov
 
PPTX
Tower of hanoi
AvichalVishnoi
 
PPTX
The Disk Method
jason nakonechny
 
PDF
Recursion - Computer Algorithms
Alaa Al-Makhzoomy
 
PPTX
Control System Homework Help
Matlab Assignment Experts
 
PPTX
Stochastic Process Exam Help
Statistics Exam Help
 
PPTX
Network Design Assignment Help
Computer Network Assignment Help
 
Tower of hanoi algorithm
WeaamRaed
 
TOWERS OF HANOI_problemsolutionandexplanationwithalgorithm.pptx
heyprettyaera
 
Dc8c4f010f40.hanoi.towers
Sumedha
 
Tower of hanoi
Elijah Hanson
 
Tower of Hanoi presentation
FahadQaiser1
 
Tower of Hanoi
Varunjeet Singh Rekhi
 
Data structure lab
prafulkumar15
 
Recurrent Problem in Discrete Mathmatics.pptx
gbikorno
 
Tower of hanoi
murdhani heena
 
Design and Analysis of Algorithms Tower of Hanoi PPT.pptx
pkumarisadhanand
 
The Towers of Hanoi puzzle has three posts and some number n of disk.pdf
fcsondhiindia
 
Stochastic Process Assignment Help
Statistics Assignment Help
 
L2_Time_complexity_recursive_function.pptx
3cL1Ps3FTMS
 
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
Roman Elizarov
 
Tower of hanoi
AvichalVishnoi
 
The Disk Method
jason nakonechny
 
Recursion - Computer Algorithms
Alaa Al-Makhzoomy
 
Control System Homework Help
Matlab Assignment Experts
 
Stochastic Process Exam Help
Statistics Exam Help
 
Network Design Assignment Help
Computer Network Assignment Help
 
Ad

More from ManishPrajapati78 (15)

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

Recently uploaded (20)

PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
PPTX
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PDF
Become an Agentblazer Champion Challenge
Dele Amefo
 
PDF
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
DOCX
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PDF
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Exploring AI Agents in Process Industries
amoreira6
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
oapresentation.pptx
mehatdhavalrajubhai
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
Presentation about variables and constant.pptx
kr2589474
 
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
Activate_Methodology_Summary presentatio
annapureddyn
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Become an Agentblazer Champion Challenge
Dele Amefo
 
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 

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