SlideShare a Scribd company logo
The Towers of Hanoi
Algorithm
In Matlab
• In this demonstration we’ll use 4 disks. The
objective of the puzzle is to move all of the
disks from tower A to tower C.
Towers of Hanoi Algorithm
Towers of Hanoi Algorithm
Two Easy Rules:
•Only one disk can be moved at a time and it can
only be the top disk of any tower.
•Disks cannot be stacked on top of smaller disks.
Towers of Hanoi Algorithm
We’re going to solve the puzzle by using
recursion.
Recursion is a computer programming technique
that involves the use of a procedure that calls
itself one or several times until a specified
condition is met.
Towers of Hanoi Algorithm
If we want to move disk 4 from A to C, after
several moves and at some point, we MUST be
in this situation:
Towers of Hanoi Algorithm
And now, we can accomplish our first goal: move
the largest disk to its final destination.
Towers of Hanoi Algorithm
And now, we can accomplish our first goal: move
the largest disk to its final destination.
Towers of Hanoi Algorithm
But now, if we ignore tower C, we are just like we
were at the beginning, but with just 3 disks left
(instead of 4)! We’ve simplified the puzzle!
Towers of Hanoi Algorithm
If we want to move disk 3 from B to C, after
several moves and at some point, we MUST be
in this situation:
Towers of Hanoi Algorithm
And now, we can accomplish our second goal:
move the second largest disk to its final
destination.
Towers of Hanoi Algorithm
And now, we can accomplish our second goal:
move the second largest disk to its final
destination.
Towers of Hanoi Algorithm
Now, the position is trivial to finish… but we still
can repeat the ideas above.
Towers of Hanoi Algorithm
Let’s code that with recursion. We’ll call our
function m (for move) and we’ll have four input
parameters:
m(n, init, temp, fin)
where
n is the number of disks to move
init is the initial tower
temp is the temporary peg
fin is the final tower
Towers of Hanoi Algorithm
We have three situations to consider:
1.- m(n-1, init, fin, temp)
we’ll move n-1 disks from A to B, with C as
temporary peg.
Towers of Hanoi Algorithm
2.- m(1, init, temp, fin)
we’ll move 1 disk (our partial goal) from
A to C, with B as temporary peg.
Towers of Hanoi Algorithm
3.- m(n-1, temp, init, fin)
we’ll move n-1 disks from B to C, with A as
temporary peg.
Towers of Hanoi Algorithm
In Matlab, our function would become:
Towers of Hanoi Algorithm
We can call it like this:
To get this result:
Towers of Hanoi Algorithm
For more examples and details, visit:
matrixlab-examples.com/tower-of-hanoi-algorithm.html

More Related Content

What's hot

Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
Iffat Anjum
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithms
samairaakram
 
Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)
Gaditek
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
Muhammad Muzammal
 
Disk Scheduling Algorithm in Operating System
Disk Scheduling Algorithm in Operating SystemDisk Scheduling Algorithm in Operating System
Disk Scheduling Algorithm in Operating System
Meghaj Mallick
 
Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap Algorithm
Learning Courses Online
 
Circular queue
Circular queueCircular queue
Tower of Hanoi Method
Tower of Hanoi MethodTower of Hanoi Method
Tower of Hanoi Method
MDHALIM7
 
Disk Scheduling Algorithms
Disk Scheduling AlgorithmsDisk Scheduling Algorithms
Disk Scheduling Algorithms
ali jawad
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
Andres Mendez-Vazquez
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
Protap Mondal
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
Dr Shashikant Athawale
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
Extensible hashing
Extensible hashingExtensible hashing
Extensible hashing
rajshreemuthiah
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
Muhammad Amjad Rana
 
Disk scheduling
Disk schedulingDisk scheduling
Disk scheduling
NEERAJ BAGHEL
 
Multiplication algorithm, hardware and flowchart
Multiplication algorithm, hardware and flowchartMultiplication algorithm, hardware and flowchart
Multiplication algorithm, hardware and flowchart
Tanjarul Islam Mishu
 

What's hot (20)

Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithms
 
Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)Instruction Set Architecture (ISA)
Instruction Set Architecture (ISA)
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Disk Scheduling Algorithm in Operating System
Disk Scheduling Algorithm in Operating SystemDisk Scheduling Algorithm in Operating System
Disk Scheduling Algorithm in Operating System
 
Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap Algorithm
 
Circular queue
Circular queueCircular queue
Circular queue
 
Tower of Hanoi Method
Tower of Hanoi MethodTower of Hanoi Method
Tower of Hanoi Method
 
Disk Scheduling Algorithms
Disk Scheduling AlgorithmsDisk Scheduling Algorithms
Disk Scheduling Algorithms
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
08 Hash Tables
08 Hash Tables08 Hash Tables
08 Hash Tables
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Extensible hashing
Extensible hashingExtensible hashing
Extensible hashing
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Disk scheduling
Disk schedulingDisk scheduling
Disk scheduling
 
Multiplication algorithm, hardware and flowchart
Multiplication algorithm, hardware and flowchartMultiplication algorithm, hardware and flowchart
Multiplication algorithm, hardware and flowchart
 

Viewers also liked

towers of hanoi
towers of hanoitowers of hanoi
towers of hanoistudent
 
Read electric meter
Read electric meterRead electric meter
Read electric meter
Jorge Jasso
 
Summer Youth Camp - Baby Mixup
Summer Youth Camp - Baby MixupSummer Youth Camp - Baby Mixup
Summer Youth Camp - Baby Mixup
Ken Sapp
 
Dossier general(school programs)
Dossier general(school programs)Dossier general(school programs)
Dossier general(school programs)
Aaquib Hussain
 
Summer Day Camp for the Sports Minded Child
Summer Day Camp for the Sports Minded ChildSummer Day Camp for the Sports Minded Child
Summer Day Camp for the Sports Minded Child
Buzz Marketing Pros
 
Summer Youth Camp - Knock Down, or Build Up
Summer Youth Camp - Knock Down, or Build UpSummer Youth Camp - Knock Down, or Build Up
Summer Youth Camp - Knock Down, or Build Up
Ken Sapp
 
Tower of hanoi problem
Tower of hanoi problemTower of hanoi problem
Tower of hanoi problem
Tyler Murphy
 
Algorithmic puzzles - #83 restricted tower of hanoi
Algorithmic puzzles - #83  restricted tower of hanoiAlgorithmic puzzles - #83  restricted tower of hanoi
Algorithmic puzzles - #83 restricted tower of hanoi
Nicholas Chen
 
Presentation Summer Camp
Presentation Summer CampPresentation Summer Camp
Presentation Summer Campsanjibk
 
Piecewise Functions in Matlab
Piecewise Functions in MatlabPiecewise Functions in Matlab
Piecewise Functions in Matlab
Jorge Jasso
 
Compensador pid
Compensador pidCompensador pid
Compensador pid
Gabriel Ramos
 
Scilab - Piecewise Functions
Scilab - Piecewise FunctionsScilab - Piecewise Functions
Scilab - Piecewise Functions
Jorge Jasso
 
How to make a proposal
How to make a proposalHow to make a proposal
How to make a proposal
silasyao
 
2D Plot Matlab
2D Plot Matlab2D Plot Matlab
2D Plot Matlab
Jorge Jasso
 
N301 Von Neumann Architecture
N301 Von Neumann ArchitectureN301 Von Neumann Architecture
N301 Von Neumann Architectureguest3b9707
 

Viewers also liked (17)

Tower of Hanoi
Tower of HanoiTower of Hanoi
Tower of Hanoi
 
towers of hanoi
towers of hanoitowers of hanoi
towers of hanoi
 
Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
 
Read electric meter
Read electric meterRead electric meter
Read electric meter
 
Summer Youth Camp - Baby Mixup
Summer Youth Camp - Baby MixupSummer Youth Camp - Baby Mixup
Summer Youth Camp - Baby Mixup
 
Dossier general(school programs)
Dossier general(school programs)Dossier general(school programs)
Dossier general(school programs)
 
Summer Day Camp for the Sports Minded Child
Summer Day Camp for the Sports Minded ChildSummer Day Camp for the Sports Minded Child
Summer Day Camp for the Sports Minded Child
 
Summer Youth Camp - Knock Down, or Build Up
Summer Youth Camp - Knock Down, or Build UpSummer Youth Camp - Knock Down, or Build Up
Summer Youth Camp - Knock Down, or Build Up
 
Tower of hanoi problem
Tower of hanoi problemTower of hanoi problem
Tower of hanoi problem
 
Algorithmic puzzles - #83 restricted tower of hanoi
Algorithmic puzzles - #83  restricted tower of hanoiAlgorithmic puzzles - #83  restricted tower of hanoi
Algorithmic puzzles - #83 restricted tower of hanoi
 
Presentation Summer Camp
Presentation Summer CampPresentation Summer Camp
Presentation Summer Camp
 
Piecewise Functions in Matlab
Piecewise Functions in MatlabPiecewise Functions in Matlab
Piecewise Functions in Matlab
 
Compensador pid
Compensador pidCompensador pid
Compensador pid
 
Scilab - Piecewise Functions
Scilab - Piecewise FunctionsScilab - Piecewise Functions
Scilab - Piecewise Functions
 
How to make a proposal
How to make a proposalHow to make a proposal
How to make a proposal
 
2D Plot Matlab
2D Plot Matlab2D Plot Matlab
2D Plot Matlab
 
N301 Von Neumann Architecture
N301 Von Neumann ArchitectureN301 Von Neumann Architecture
N301 Von Neumann Architecture
 

Similar to Towers Hanoi Algorithm

Control System Homework Help
Control System Homework HelpControl System Homework Help
Control System Homework Help
Matlab Assignment Experts
 
Tower of hanoi algorithm
Tower of hanoi algorithmTower of hanoi algorithm
Tower of hanoi algorithm
WeaamRaed
 
Design and Analysis of algorithms
Design and Analysis of algorithmsDesign and Analysis of algorithms
Design and Analysis of algorithms
Dr. Rupa Ch
 
Recusion
RecusionRecusion
Recusion
Hamza Butt
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
DAA Notes.pdf
DAA Notes.pdfDAA Notes.pdf
DAA Notes.pdf
SauravPawar14
 
Generational Layered Canvas Mechanism for Collaborative Web Applications
Generational Layered Canvas Mechanism for Collaborative Web ApplicationsGenerational Layered Canvas Mechanism for Collaborative Web Applications
Generational Layered Canvas Mechanism for Collaborative Web Applications
kata shin
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
2022cspaawan12556
 
lecture 5
lecture 5lecture 5
lecture 5sajinsc
 
Dsoop (co 221) 1
Dsoop (co 221) 1Dsoop (co 221) 1
Dsoop (co 221) 1
Puja Koch
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
chandsek666
 
lecture 6
lecture 6lecture 6
lecture 6sajinsc
 
Control System Assignment Help
Control System Assignment HelpControl System Assignment Help
Control System Assignment Help
Matlab Assignment Experts
 
Queue (1)(1).ppt
Queue (1)(1).pptQueue (1)(1).ppt
Queue (1)(1).ppt
BuddheshwarRajput
 
Tower of Hanoi
Tower of HanoiTower of Hanoi
Tower of Hanoi
Varunjeet Singh Rekhi
 

Similar to Towers Hanoi Algorithm (16)

Control System Homework Help
Control System Homework HelpControl System Homework Help
Control System Homework Help
 
Tower of hanoi algorithm
Tower of hanoi algorithmTower of hanoi algorithm
Tower of hanoi algorithm
 
algorithm unit 1
algorithm unit 1algorithm unit 1
algorithm unit 1
 
Design and Analysis of algorithms
Design and Analysis of algorithmsDesign and Analysis of algorithms
Design and Analysis of algorithms
 
Recusion
RecusionRecusion
Recusion
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
DAA Notes.pdf
DAA Notes.pdfDAA Notes.pdf
DAA Notes.pdf
 
Generational Layered Canvas Mechanism for Collaborative Web Applications
Generational Layered Canvas Mechanism for Collaborative Web ApplicationsGenerational Layered Canvas Mechanism for Collaborative Web Applications
Generational Layered Canvas Mechanism for Collaborative Web Applications
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
 
lecture 5
lecture 5lecture 5
lecture 5
 
Dsoop (co 221) 1
Dsoop (co 221) 1Dsoop (co 221) 1
Dsoop (co 221) 1
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
lecture 6
lecture 6lecture 6
lecture 6
 
Control System Assignment Help
Control System Assignment HelpControl System Assignment Help
Control System Assignment Help
 
Queue (1)(1).ppt
Queue (1)(1).pptQueue (1)(1).ppt
Queue (1)(1).ppt
 
Tower of Hanoi
Tower of HanoiTower of Hanoi
Tower of Hanoi
 

Recently uploaded

"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
AG2 Design
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Ashish Kohli
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
NelTorrente
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 

Recently uploaded (20)

"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 

Towers Hanoi Algorithm

  • 1. The Towers of Hanoi Algorithm In Matlab
  • 2. • In this demonstration we’ll use 4 disks. The objective of the puzzle is to move all of the disks from tower A to tower C. Towers of Hanoi Algorithm
  • 3. Towers of Hanoi Algorithm Two Easy Rules: •Only one disk can be moved at a time and it can only be the top disk of any tower. •Disks cannot be stacked on top of smaller disks.
  • 4. Towers of Hanoi Algorithm We’re going to solve the puzzle by using recursion. Recursion is a computer programming technique that involves the use of a procedure that calls itself one or several times until a specified condition is met.
  • 5. Towers of Hanoi Algorithm If we want to move disk 4 from A to C, after several moves and at some point, we MUST be in this situation:
  • 6. Towers of Hanoi Algorithm And now, we can accomplish our first goal: move the largest disk to its final destination.
  • 7. Towers of Hanoi Algorithm And now, we can accomplish our first goal: move the largest disk to its final destination.
  • 8. Towers of Hanoi Algorithm But now, if we ignore tower C, we are just like we were at the beginning, but with just 3 disks left (instead of 4)! We’ve simplified the puzzle!
  • 9. Towers of Hanoi Algorithm If we want to move disk 3 from B to C, after several moves and at some point, we MUST be in this situation:
  • 10. Towers of Hanoi Algorithm And now, we can accomplish our second goal: move the second largest disk to its final destination.
  • 11. Towers of Hanoi Algorithm And now, we can accomplish our second goal: move the second largest disk to its final destination.
  • 12. Towers of Hanoi Algorithm Now, the position is trivial to finish… but we still can repeat the ideas above.
  • 13. Towers of Hanoi Algorithm Let’s code that with recursion. We’ll call our function m (for move) and we’ll have four input parameters: m(n, init, temp, fin) where n is the number of disks to move init is the initial tower temp is the temporary peg fin is the final tower
  • 14. Towers of Hanoi Algorithm We have three situations to consider: 1.- m(n-1, init, fin, temp) we’ll move n-1 disks from A to B, with C as temporary peg.
  • 15. Towers of Hanoi Algorithm 2.- m(1, init, temp, fin) we’ll move 1 disk (our partial goal) from A to C, with B as temporary peg.
  • 16. Towers of Hanoi Algorithm 3.- m(n-1, temp, init, fin) we’ll move n-1 disks from B to C, with A as temporary peg.
  • 17. Towers of Hanoi Algorithm In Matlab, our function would become:
  • 18. Towers of Hanoi Algorithm We can call it like this: To get this result:
  • 19. Towers of Hanoi Algorithm For more examples and details, visit: matrixlab-examples.com/tower-of-hanoi-algorithm.html