SlideShare a Scribd company logo
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

What's hot

Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
Ninad Mankar
 
stack & queue
stack & queuestack & queue
stack & queue
manju rani
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Floating point representation
Floating point representationFloating point representation
Floating point representation
missstevenson01
 
Hill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligenceHill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligence
sandeep54552
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
DurgaDeviCbit
 
Hashing
HashingHashing
Hashing
Amar Jukuntla
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
Jeanie Arnoco
 
Selection sort and insertion sort
Selection sort and insertion sortSelection sort and insertion sort
Selection sort and insertion sort
May Ann Mendoza
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
FarihaHabib123
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListPTCL
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
Radhakrishnan Chinnusamy
 
Linked list
Linked listLinked list
Linked list
KalaivaniKS1
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
University of Science and Technology Chitttagong
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
Shuvongkor Barman
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
Anandhasilambarasan D
 
Stack and its usage in assembly language
Stack and its usage in assembly language Stack and its usage in assembly language
Stack and its usage in assembly language
Usman Bin Saad
 
Processor Organization and Architecture
Processor Organization and ArchitectureProcessor Organization and Architecture
Processor Organization and Architecture
Vinit Raut
 
Sorting
SortingSorting

What's hot (20)

Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
stack & queue
stack & queuestack & queue
stack & queue
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Floating point representation
Floating point representationFloating point representation
Floating point representation
 
Hill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligenceHill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligence
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Hashing
HashingHashing
Hashing
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Selection sort and insertion sort
Selection sort and insertion sortSelection sort and insertion sort
Selection sort and insertion sort
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Linked list
Linked listLinked list
Linked list
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Stack and its usage in assembly language
Stack and its usage in assembly language Stack and its usage in assembly language
Stack and its usage in assembly language
 
Processor Organization and Architecture
Processor Organization and ArchitectureProcessor Organization and Architecture
Processor Organization and Architecture
 
Sorting
SortingSorting
Sorting
 

Similar to Data Structure and Algorithms The Tower of Hanoi

Tower of hanoi algorithm
Tower of hanoi algorithmTower of hanoi algorithm
Tower of hanoi algorithm
WeaamRaed
 
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towersDc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towersSumedha
 
Tower of Hanoi Investigation
Tower of Hanoi InvestigationTower of Hanoi Investigation
Tower of Hanoi Investigation
Writers Per Hour
 
Tower of Hanoi presentation
Tower of Hanoi presentationTower of Hanoi presentation
Tower of Hanoi presentation
FahadQaiser1
 
Tower of Hanoi
Tower of HanoiTower of Hanoi
Tower of Hanoi
Varunjeet Singh Rekhi
 
Data structure lab
Data structure labData structure lab
Data structure lab
prafulkumar15
 
Recurrent Problem in Discrete Mathmatics.pptx
Recurrent Problem in Discrete Mathmatics.pptxRecurrent Problem in Discrete Mathmatics.pptx
Recurrent Problem in Discrete Mathmatics.pptx
gbikorno
 
Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
murdhani heena
 
The Towers of Hanoi puzzle has three posts and some number n of disk.pdf
The Towers of Hanoi puzzle has three posts and some number n of disk.pdfThe Towers of Hanoi puzzle has three posts and some number n of disk.pdf
The Towers of Hanoi puzzle has three posts and some number n of disk.pdf
fcsondhiindia
 
Stochastic Process Assignment Help
Stochastic Process Assignment HelpStochastic Process Assignment Help
Stochastic Process Assignment Help
Statistics Assignment Help
 
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems ReviewRoman Elizarov
 
Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
AvichalVishnoi
 
The Disk Method
The Disk MethodThe Disk Method
The Disk Method
jason nakonechny
 
Recursion - Computer Algorithms
Recursion - Computer AlgorithmsRecursion - Computer Algorithms
Recursion - Computer Algorithms
Alaa Al-Makhzoomy
 
Control System Homework Help
Control System Homework HelpControl System Homework Help
Control System Homework Help
Matlab Assignment Experts
 
Stochastic Process Exam Help
Stochastic Process Exam HelpStochastic Process Exam Help
Stochastic Process Exam Help
Statistics Exam Help
 
Network Design Assignment Help
Network Design Assignment HelpNetwork Design Assignment Help
Network Design Assignment Help
Computer Network Assignment Help
 
Tower of Hanoi using AI technique - means ends analysis
Tower of Hanoi using AI technique - means ends analysisTower of Hanoi using AI technique - means ends analysis
Tower of Hanoi using AI technique - means ends analysis
Shubham Nimiwal
 
Towers Hanoi Algorithm
Towers Hanoi AlgorithmTowers Hanoi Algorithm
Towers Hanoi Algorithm
Jorge Jasso
 

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

Tower of hanoi algorithm
Tower of hanoi algorithmTower of hanoi algorithm
Tower of hanoi algorithm
 
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towersDc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
 
Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
 
Tower of Hanoi Investigation
Tower of Hanoi InvestigationTower of Hanoi Investigation
Tower of Hanoi Investigation
 
Tower of Hanoi presentation
Tower of Hanoi presentationTower of Hanoi presentation
Tower of Hanoi presentation
 
Tower of Hanoi
Tower of HanoiTower of Hanoi
Tower of Hanoi
 
Data structure lab
Data structure labData structure lab
Data structure lab
 
Recurrent Problem in Discrete Mathmatics.pptx
Recurrent Problem in Discrete Mathmatics.pptxRecurrent Problem in Discrete Mathmatics.pptx
Recurrent Problem in Discrete Mathmatics.pptx
 
Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
 
The Towers of Hanoi puzzle has three posts and some number n of disk.pdf
The Towers of Hanoi puzzle has three posts and some number n of disk.pdfThe Towers of Hanoi puzzle has three posts and some number n of disk.pdf
The Towers of Hanoi puzzle has three posts and some number n of disk.pdf
 
Stochastic Process Assignment Help
Stochastic Process Assignment HelpStochastic Process Assignment Help
Stochastic Process Assignment Help
 
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2013 NEERC (Northeastern European Regional Contest) Problems Review
 
Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
 
The Disk Method
The Disk MethodThe Disk Method
The Disk Method
 
Recursion - Computer Algorithms
Recursion - Computer AlgorithmsRecursion - Computer Algorithms
Recursion - Computer Algorithms
 
Control System Homework Help
Control System Homework HelpControl System Homework Help
Control System Homework Help
 
Stochastic Process Exam Help
Stochastic Process Exam HelpStochastic Process Exam Help
Stochastic Process Exam Help
 
Network Design Assignment Help
Network Design Assignment HelpNetwork Design Assignment Help
Network Design Assignment Help
 
Tower of Hanoi using AI technique - means ends analysis
Tower of Hanoi using AI technique - means ends analysisTower of Hanoi using AI technique - means ends analysis
Tower of Hanoi using AI technique - means ends analysis
 
Towers Hanoi Algorithm
Towers Hanoi AlgorithmTowers Hanoi Algorithm
Towers Hanoi Algorithm
 

More from ManishPrajapati78

Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 
Data Structure and Algorithms Queues
Data Structure and Algorithms QueuesData Structure and Algorithms Queues
Data Structure and Algorithms Queues
ManishPrajapati78
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
ManishPrajapati78
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms Stacks
ManishPrajapati78
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Data Structure and Algorithms Sorting
Data Structure and Algorithms SortingData Structure and Algorithms Sorting
Data Structure and Algorithms Sorting
ManishPrajapati78
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
ManishPrajapati78
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
ManishPrajapati78
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
ManishPrajapati78
 
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph TraversalData Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph Traversal
ManishPrajapati78
 
Data Structure and Algorithms Graphs
Data Structure and Algorithms GraphsData Structure and Algorithms Graphs
Data Structure and Algorithms Graphs
ManishPrajapati78
 
Data Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding AlgorithmData Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding Algorithm
ManishPrajapati78
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and Trees
ManishPrajapati78
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 

More from ManishPrajapati78 (15)

Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
Data Structure and Algorithms Queues
Data Structure and Algorithms QueuesData Structure and Algorithms Queues
Data Structure and Algorithms Queues
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms Stacks
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Data Structure and Algorithms Sorting
Data Structure and Algorithms SortingData Structure and Algorithms Sorting
Data Structure and Algorithms Sorting
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
 
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph TraversalData Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph Traversal
 
Data Structure and Algorithms Graphs
Data Structure and Algorithms GraphsData Structure and Algorithms Graphs
Data Structure and Algorithms Graphs
 
Data Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding AlgorithmData Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding Algorithm
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and Trees
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
 

Recently uploaded

Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Jay Das
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 

Recently uploaded (20)

Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 

Data Structure and Algorithms The Tower of Hanoi

  • 1.
  • 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