Towers of Hanoi
Towers of Hanoi
Towers of Hanoi
It provides analysis of recursive algorithm If rods = 3 rings = 64 of different sizes All rings are arranged in decreasing size on rod 1 (the largest at the bottom and the smallest at the top) Transfer all the rings to the 3rd rod Move one ring in one move No ring is even placed on top of another smaller one on any rod Time required to finish this work for 64 rings is 500000 million years if one ring is moved in one second
TOWER (N, BEG, AUX, END. If N=1 then BEG "" END If N>1 then Tower (N-1, BEG, END, AUX) write BEG "" END Tower (N-1, AUX, BEG, END) Let t(n) denote the number of times it is executed on a call of N. t(n) = 0 if N =0 2t(n-1) + 1 if N>0 Number of moves = 2n 1 Time taken to solve the problem with n rings = (2n)
n=3
TOWER (1, A, B, C)
AC AB CB AC
TOWER (2, A, C, B)
BA BC AC
TOWER (2, B, A, C)
TOWER (1, A, B, C)
n=3
(a) Initial
(1) A C
(2) A B
(3) C B
(4) A C
(5) B A
(6) B C
(3) A C
n=4
TOWER (3, A, C, B)
AB AC BC AB CA CB AB AC BC BA CA BC AB AC BC
AC
TOWER (1, B, A, C)
AB
TOWER (1, C, B, A)
TOWER (2, C, A, B)
CB
AC
TOWER (1, B, A, C) TOWER (2, B, C, A)
BA
BC
TOWER (1, A, C, B)
TOWER (2, A, B, C)
AC
TOWER (1, B, A, C)
Amortized Analysis
Time taken by each call is different depending upon nature of call so average-case analysis does not reflect true time of an algorithm Amortized constant time is taken for each call on P The total time bounded for the sequence of n calls on the process is above cn where c is constant time depending only on the implementation Example of updation of a binary counter Array [0,1,1,0,1,1] represents 27 as counter can count up to 2m 1 Algorithm for adding 1 to the counter
Procedure count (c{1..m}) {This procedure assumes m > 1 and c[j] {0,1} for each 1 < j < m} jm+1 repeat jj1 c [j] 1 c[j] until c[j] = 1 or j = 1
Binary Count
Operation of the Algorithm Array value c First time round the loop: Repeat j=6 c c[6] 0 until c[j] = 1 or j = 1 (False)
0
1
1
2
1
3
0
4
1
5
1
6
0
1
1
2
1
3
0
4
1
5
0
6
Binary Count
Second time round the loop Repeat j=5 c 0 1 1 0 0 0 c[5] 0 1 2 3 4 5 6 until c[j] = 1 or j = 1 (False) Third Time round the loop Repeat j=4 0 1 1 1 0 0 c[4] 1 1 2 3 4 5 6 until c[j] = 1 or j = 1 (True) This number (i.e., 011100) represents the value 28 in binary.
Algorithms
worst-case
occurs
when
c[j]=1 for all j, in which case it goes round the loop m times Thus, n calls a count starting from allzero array take total time in O(nm) Amortized established approach analysis by the results can be trick accounting
The Accounting Trick A virtual bank account is set up which initially contains zero tokens Each time P is called, an allowance of T tokens (guess of upper bound) is deposited in the account. One token is spent each time the barometer instruction is executed Account will never become overdrawn ---- golden rule Analysis of binary counter - For each call count, two tokens are allocated in amortized sense - One token is spent each time count goes round its loop
1st loop 0 1 0 1 1 0 2nd loop 0 1 0 1 0 0 3rd loop 0 1 0 0 0 0 4th loop 0 1 1 0 0 0 (24) Tokens in the account = 4 x 2 4 = 8 4 = 4
Consider this recurrence: T(n) = 0 if n = 0 3T(n2) + n otherwise n 2 is hard to analyse, it is replaced by n/2 and n is restricted to an exact power of 2 Values of the recurrence on the first few powers of 2 are:
=
i =0 k
k i
2 =3
i k +1
i =0
(2 / 3)
= 3 (1 (2 / 3) =3
k +1
) /(1 2 / 3)
k +1
Regularity can be observed General form is 3k+1 2k+1 It can be proved by mathematical induction