Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Towers of Hanoi

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

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)

Initial setup of Towers of Hanoi with n = 6

n=3
TOWER (1, A, B, C)

AC AB CB AC

TOWER (2, A, C, B)

TOWER (1, C, A, B) TOWER (3, A, B, C) TOWER (1, B, C, A)

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)

TOWER (1, A, C, B) TOWER (2, A, B, C)

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

TOWER (1, A, C, B) TOWER (4, A, B, C)

AC
TOWER (1, B, A, C) TOWER (2, B, C, A)

BA

TOWER (1, C, B, A) TOWER (3, B, A, C)

BC
TOWER (1, A, C, B)

TOWER (2, A, B, C)

AC

TOWER (1, B, A, C)

Amortized Analysis (Robert Tarjan 1985 )


Amortization is an important analysis tool for understanding the running time of algorithms that have steps with widely varying performance It turns out that in some situations a single operation can be expensive, but the total time for an entire sequence of n such operations is always significantly better than the worst-case efficiency of that single operation multiplied by n Rather than focusing on each operation separately, amortization considers the interactions between all the operations by studying the running time of a series of these operations so this technique provides more accurate analysis in such situations

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

The Accounting Trick


Suppose counter state : 0 1 0 1 1 1 (23) [i = 4]

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

Intelligent Guess Work Technique

Intelligent Guess Work


Stages of Intelligent Guess Work are: Calculate the first few values of the recurrence Look for regularity Guess a suitable general form Prove by mathematical induction that this form is correct

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:

Intelligent Guess Work


Values of the recurrence on the first few powers of 2 are: n T(n) i.e., T(2) = 3 x T1 + 2 = 3 x 1 + 2 = 3 + 2 = 5 T(4) = 3 x T2 + 4 = 3 x 5 + 4 = 15 + 4 = 19 T(8) = 3 x T4 + 8 = 3 x 19 + 8 = 57 + 8 = 65 T(16) = 3 x T8 + 16 = 3 x 65 + 16 = 195 + 16 = 211 and so on 1 1 2 5 4 19 8 65 16 211 32 665

Intelligent Guess Work


Instead of writing T(2) = 5, it is more useful to write T(2) = 3 x 1 + 2 T(4) = 3 x T(2) + 4 = 3 x (3 x 1 + 2) + 4 = 32 x 1 + 3 x 2 + 4 Resultantly n T(n) 1 1 2 3x1+2 22 32 x 1 + 3 x 2 + 22 23 33 x 1 + 32 x 2 + 3 x 22 + 23 24 34 x 1 + 33 x 2 + 32 x 22 + 3 x 23 + 24 25 35 x 1 + 34 x 2 + 33 x 22 + 32 x 23 + 3 x 24 + 25

Intelligent Guess Work


The pattern is now obvious. T(2k) = 3k20 + 3k-1 21 + 3k-2 22 + . . . 12k-1 + 30 2k

=
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

Intelligent Guess Work


Equation can further be guessed with a little more intuition and values can be tabulated as T(n) +- in for small values of i. n T(n)-2n T(n)-n T(n) T(n) + n T(n) + 2n 1 -1 0 1 2 3 2 1 3 5 7 9 4 11 15 19 23 27 8 49 57 65 73 81 16 179 195 211 227 243 32 601 633 665 697 729

Intelligent Guess Work


T(n) + 2n is an exact power of 3. It is difficult to solve recurrence if n is not a power of 2. Recurrences for which automatic techniques become powerless, intelligent guess work approach can always be used as a last resort even though it takes more time.

You might also like