8
TOWERS OF HANOI

towers of hanoi

  • Upload
    student

  • View
    169

  • Download
    3

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: towers of hanoi

TOWERS OF HANOI

Page 2: towers of hanoi

• Move: A B• For, N=1, the only move is AC• For, N=2, the moves are AB, AC, BC• For, N=3 ?

Page 3: towers of hanoi
Page 4: towers of hanoi
Page 5: towers of hanoi

• For N=N: following steps are used:

1.Move top n-1 disks from A to B2.Move top disk from A to C3.Move top n-1 disks from B to C (recursion by changing the B and C)

Page 6: towers of hanoi

Algorithm

• General Notation of Algorithm is: TOWER (N, BEG, AUX, END)• EXAMPLE MOVE: TOWER(1, BEG, AUX, END) I.E, BEGEND• Steps for N disks:• 1. TOWER(N-1, BEG,END,AUX)• 2 TOWER(1, BEG,AUX,END)• 3 TOWER(N-1, AUX,BEG,END) --- (recursion)

• Example : TOWER(4,A,B,C)

Page 7: towers of hanoi
Page 8: towers of hanoi

• TOWER(N,BEG,AUX,END)• 1 If N=1, then• (a) Write : BEGEND• (b) Return• 2 [Move N-1 disks from BEG to AUX]• Call TOWER(N-1,BEG,END,AUX)• 3 Write: BEGEND• 4 [Move N-1 disks from AUX to END]• Call TOWER(N-1,AUX,BEG,END)• 5 Return