0% found this document useful (0 votes)
113 views9 pages

Tower of Hanoi - Algorithm Computer Science - BSCS

The document discusses the recursive algorithm for solving the Tower of Hanoi puzzle with 3 disks. It explains that the algorithm involves recursively moving one disk at a time from the starting peg to the ending peg, using a temporary peg, until all disks have been moved. Pseudocode is provided to demonstrate how the recursive function works, moving one disk on each recursive call until the smallest disk is moved in the final step. An example execution is shown step-by-step to illustrate the disk movements.

Uploaded by

Azfar Javaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views9 pages

Tower of Hanoi - Algorithm Computer Science - BSCS

The document discusses the recursive algorithm for solving the Tower of Hanoi puzzle with 3 disks. It explains that the algorithm involves recursively moving one disk at a time from the starting peg to the ending peg, using a temporary peg, until all disks have been moved. Pseudocode is provided to demonstrate how the recursive function works, moving one disk on each recursive call until the smallest disk is moved in the final step. An example execution is shown step-by-step to illustrate the disk movements.

Uploaded by

Azfar Javaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

C

DATA
O
V

STRUCTURES
E
R

ALGORITHM

Recommended Text Book: Data Structures,


by: Seymour Lipschutz, McGraw Hill Book Company

Bachelor of Science in Computer Sciences Lecturer: Azfar Javaid


The Tower of Hanoi 2
Here is the algorithm used in recursive version of the puzzle. Despite of how many disks you may have, there must exist a way
to transfer all the disks as they were at the beginning (largest below and smallest at the top) from the initial position PEG1 to
the final position PEG3, whereas you may use a temporary position PEG2.

In this simulation, we are considering


3 disks. They are placed in decreasing
order w.r.t their sizes from bottom to
top

1
2
3

PEG 1 PEG 2 PEG 3


How to solve Tower of Hanoi? 3
To solve this game, we will follow 3 simple steps, recursively. We will use a general notation:
hanoi(int NoDisk, string FromTower, string ToTower, string AuxTower)

hanoi – is our recursive function


NoDisk - denotes the number of disk
FromTower - is the initial tower
ToTower - is the destination tower
AuxTower - is the auxiliary (temporary) tower

1
2
3

PEG 1 PEG 2 PEG 3


We have 3 disks all placed in PEG1. 4
So, NoDisk = 3 // no of disks

Therefore, we will start with hanoi(3, “PEG1”, “PEG2”, “PEG3”)

 If the NoDisk is not 1, then, the function would call itself by deducting 1 from 3, i.e.,
hanoi(2, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 3-1 = 2
 Again, NoDisk is not 1, the function calls itself by deducting 1 from 2, i.e.,
hanoi(1, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 2-1 = 1

1
2
3

PEG 1 PEG 2 PEG 3


We have 3 disks all placed in PEG1. 5
So, NoDisk = 3 // no of disks

Therefore, we will start with hanoi(3, “PEG1”, “PEG2”, “PEG3”)

 If the NoDisk is not 1, then, the function would call itself by deducting 1 from 3, i.e.,
hanoi(2, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 3-1 = 2
 Again, NoDisk is not 1, the function calls itself by deducting 1 from 2, i.e.,
hanoi(1, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 2-1 = 1
 Now, NoDisk is 1, therefore, the function Moves Disk 1 from PEG1 to PEG3..

2
3 1

PEG 1 PEG 2 PEG 3


We have 3 disks all placed in PEG1. 6
So, NoDisk = 3 // no of disks

Therefore, we will start with hanoi(3, “PEG1”, “PEG2”, “PEG3”) // initial call from the main()

 If the NoDisk is not 1, then, the function would call itself by deducting 1 from 3, i.e.,
hanoi(2, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 3-1 = 2
 Again, NoDisk is not 1, the function calls itself by deducting 1 from 2, i.e.,
hanoi(1, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 2-1 = 1
 Now, NoDisk is 1, therefore, the function Moves Disk 1 from PEG1 to PEG3..

The control returns back to the first call (here again the value of NoDisk is 2),
 The function Moves Disk 2 from PEG1 to PEG2

3 2 1

PEG 1 PEG 2 PEG 3


We have 3 disks all placed in PEG1. 7
So, NoDisk = 3 // no of disks

Therefore, we will start with hanoi(3, “PEG1”, “PEG2”, “PEG3”) // initial call from the main()

 If the NoDisk is not 1, then, the function would call itself by deducting 1 from 3, i.e.,
hanoi(2, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 3-1 = 2
 Again, NoDisk is not 1, the function calls itself by deducting 1 from 2, i.e.,
hanoi(1, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 2-1 = 1
 Now, NoDisk is 1, therefore, the function Moves Disk 1 from PEG1 to PEG3..

The control returns back to the first call (here again the value of NoDisk is 2),
 The function Moves Disk 2 from PEG1 to PEG2

The function again calls itself by a little change, this time…


 The function calls - hanoi(1, “PEG2”, “PEG3”, “PEG1”) //NoDisk-1 = 2-1 = 1

3 2 1

PEG 1 PEG 2 PEG 3


We have 3 disks all placed in PEG1. 8
So, NoDisk = 3 // no of disks

Therefore, we will start with hanoi(3, “PEG1”, “PEG2”, “PEG3”) // initial call from the main()

 If the NoDisk is not 1, then, the function would call itself by deducting 1 from 3, i.e.,
hanoi(2, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 3-1 = 2
 Again, NoDisk is not 1, the function calls itself by deducting 1 from 2, i.e.,
hanoi(1, “PEG1”, “PEG3”, “PEG2”) //NoDisk-1 = 2-1 = 1
 Now, NoDisk is 1, therefore, the function Moves Disk 1 from PEG1 to PEG3..

The control returns back to the first call (here again the value of NoDisk is 2),
 The function Moves Disk 2 from PEG1 to PEG2

The function again calls itself by a little change, this time…


 The function calls - hanoi(1, “PEG3”, “PEG2”, “PEG1”) //NoDisk-1 = 2-1 = 1
 Now, NoDisk is 1, therefore, the function Moves Disk 1 from PEG3 to PEG2..

1
3 2

PEG 1 PEG 2 PEG 3


Linked List Reversal – Code in C++ void My_Linked_List::Reverse()
9
{
class My_Linked_List struct node *current, *next, *prev;
{ cout << endl << "\t\t List Reversal";
//Structure declaration for the node cout << endl << "\t\t =============" << endl;
struct node
{ if (start == nullptr) {
int info; cout << endl << "\t\t The List is empty!" << endl;
struct node *link; return;
}; }

//private structure variable declared if (start->link == nullptr) { //only one element


struct node *start; cout << endl << "\t\t There is only one Element in the List!" << endl;
return;
}
public:
My_Linked_List() //Constructor definition
// if list is not empty and has more than one element...
{ next = nullptr; current = nullptr; prev = nullptr;
start = nullptr; current = start;
}
cout << endl << "\t\t Now... Reversing the List..." << endl << "\t\t";
//public function declared while (current != nullptr)
void Create_List(int); {
void AddAtBeg(); next = current->link;
void AddAfter(); current->link = prev;
void AddAtEnd(); prev = current;
void Delete(); current = next;
int Count(); }
void Search(); start = prev;
void Display(); cout << endl << "\t\t The List has been Reversed Successfully!" << endl;
void Reverse(); free(current);
}; free(next);
}

End of Linked List – Reversal Code in C++

You might also like