GATE / CS ARCHIVE

GATE CSE 2012 / QUESTION 16

Towers of Hanoi recurrence

Algorithms · Asymptotic Analysis & Recurrences · Full GATE CSE 2012 paper

GATE 2012, Q16 1 mark · MCQ
Report issue

Asymptotic Analysis & Recurrences

Question 16, GATE 2012: The recurrence relation capturing the optimal execution time of the Towers of Hanoi problem with
        n discs is
        (A) T(n) = 2T(n − 2) + 2                         (B) T(n) = 2T(n − 1) + n
        (C) T(n) = 2T(n/2) + 1                           (D) T(n) = 2T(n − 1) + 1
Tap to enlarge
Your answer
Answer & solution

Official answer: D

To move n discs, first move the top n−1 discs to the spare peg, move the largest disc once, then move those n−1 discs onto it. These two subproblems cannot be avoided in an optimal solution. Thus T(n)=2T(n−1)+1 with T(0)=0, yielding 2n−1 moves. Hence D.

Check: The largest disc cannot move until all n−1 smaller discs are off it, and afterward those smaller discs must be transferred onto it. With T(1)=1, the recurrence yields T(2)=3 and T(3)=7, consistent with 2n−1.

Original paper, page 4 · Answer key