APPLLICATIONS OF STACK-Tower of Hanoi

20
Data Structure and Algorithm Analysis Slide Set 4 Towers of Hanoi 13 CS- I & II Engr. Maria Shaikh [email protected]. edu.pk

description

DATA STRUCTURES AND ALGORITHM ANALYSIS

Transcript of APPLLICATIONS OF STACK-Tower of Hanoi

Data Structure and Algorithm Analysis Slide Set 2

Data Structure and Algorithm Analysis

Slide Set 4Towers of Hanoi13 CS- I & IIEngr. Maria [email protected] Stack is a list of elements in which an element may be inserted or deleted at one end which is known as TOP of the stack.Last-in first-out data structure (LIFO)Supports the following operationspush(item) inserts an item at the endPop( ) deletes the end itemPeek( ) returns the last elementEngr. Maria Shaikh2STACKStack() creates a new stack that is empty. It needs no parameters and returns an empty stack.isEmpty() tests to see whether the stack is empty. It needs no parameters and returns a boolean value.size() returns the number of items on the stack. It needs no parameters and returns an integer.For example, if s is a stack that has been created and starts out empty.

Engr. Maria Shaikh3 Representation of StackEEE TOPDDDCCCBBBAAAEngr. Maria Shaikh Stack Representation

Engr. Maria ShaikhRECURSIONA programming technique in which a function calls itself.One of the most effective techniques in programming.Consider the numbers 1, 3, 6, 10, 15.What is so peculiar about them?The nth term in the series is obtained by adding n to the previous number.Recursion can be used to find the nth term Engr. Maria Shaikh6TOWERS OF HANOIHanoi is the capital of Vietnam. It is also a logical puzzle game that can be solved very effectively with recursion. The setup of Hanoi is simple. We have three pegs. On each of these pegs is a series of disks decreasing in size from the bottom of the peg to the top of the peg.Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in 1883. He was inspired by a legend that tells of a Hindu temple where the puzzle was presented to young priests. At the beginning of time, the priests were given three poles and a stack of 64 gold disks, each disk a little smaller than the one beneath it. Their assignment was to transfer all 64 disks from one of the three poles to another, with two important constraints. They could only move one disk at a time, and they could never place a larger disk on top of a smaller one. The priests worked very efficiently, day and night, moving one disk every second. When they finished their work, the legend said, the temple would crumble into dust and the world would vanish.

Engr. Maria Shaikh7TOWERS OF HANOIThe game starts by having few discs stacked in increasing order of size. The number of discs can vary, but there are only three pegs. The goal is to move all these disks to another peg following these rules: -Only one disk can be moved at a time. -Only the topmost disk of any given peg can be moved to another peg.

8Engr. Maria ShaikhThe Towers of HanoiA larger disk cannot be placed on top a smaller disk. Only one disk could be moved at a time.A larger disk must never be stacked above a smaller one.One and only one auxillary needle could be used for the intermediate storage of disks.

Engr. Maria ShaikhFigure 6-11

The Towers of HanoiCase 1Engr. Maria ShaikhFigure 6-11

The Towers of HanoiMove one disk to auxiliary needle.Move one disk to destination needle.Move one disk from auxiliary to destination needle.

Move two disks from source to auxiliary needle. (Step-3)Move one disk from source to destination needle. (Step-4)Move two disks from auxiliary to destination needle. (Step-7)The Towers of Hanoi

Move n-1 disks from source to auxiliary. General CaseMove one disk from source to destination. Base CaseMove n-1 disks form auxiliary to destination. General Case

Call Towers( n-1, source, auxiliary, destination)Move one disk from source to destinationCall Towers(n-1, auxilary, destination, source).

Engr. Maria ShaikhTower of HanoiHow to solve the 4 pegs

Engr. Maria Shaikh14AlgorithmTOWER (N, BEG, AUX, END)This procedures gives a recursive solution to the Tower of Hanoi problem for N disks. If N = 1, then: (a) Write: BEG END (b) Return [End of If Structure][ Move N-1 disks from peg BEG to peg AUX] Call TOWERS (N -1, BEG, END, AUX)3. Write: BEG END[Move N -1 disks from peg AUX to peg END] Call TOWER (N -1, AUX, BEG, END)Return

Engr. Maria Shaikh15Tower of HanoiSolutionTo get a better understanding for the general algorithm used to solve the Tower of Hanoi, try to solve the puzzle with a small amount of disks, 3 or 4, and once you master that , you can solve the same puzzle with more discs with the following algorithm.No of steps can be find out by a formula 2n 1Where n are number of disks.Engr. Maria Shaikh16Tower of HanoiRecursive Solution for the Tower of Hanoi with algorithm

Lets call the three peg Src(Source), Aux(Auxiliary) and st(Destination).

Move the top N 1 disks from the Source to Auxiliary towerMove the Nth disk from Source to Destination towerMove 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. So once you master solving Tower of Hanoi with three disks, you can solve it with any number of disks with the above algorithm.Engr. Maria Shaikh17Tower of HanoiThis actually serves as the definition of the function Solve. The function is recursive in that it calls itself repeatedly with decreasing values of N until a terminating condition (in our case N = 0) has been met. To me the sheer simplicity of the solution is breathtaking. For N =3 it translates into

1. Move from Src to Dst 2. Move from Src to Aux 3. Move from Dst to Aux 4. Move from Src to Dst 5. Move from Aux to Src 6. Move from Aux to Dst 7. Move from Src to Dst Of course "Move" means moving the top most disk.Engr. Maria Shaikh18Tower of HanoiFor N = 4 we get the following sequence

1. Move from Src to Aux 14. Move from Src to Dst 2. Move from Src to Dst 15. Move from Aux 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 Engr. Maria Shaikh19END OF SLIDE SET 4Engr. Maria Shaikh20