Algorithms | Analysis of Algorithms | Question 3
The recurrence relation capturing the optimal time of the Tower of Hanoi problem with n discs is. (GATE CS 2012)
T(n) = 2T(n – 2) + 2
T(n) = 2T(n – 1) + n
T(n) = 2T(n/2) + 1
T(n) = 2T(n – 1) + 1
Following are the steps to follow to solve Tower of Hanoi problem recursively.
Let the three pegs be A, B and C. The goal is to move n pegs from A to C.
To move n discs from peg A to peg C:
move n-1 discs from A to B. This leaves disc n alone on peg A
move disc n from A to C
move n-1 discs from B to C so they sit on disc n
The recurrence function T(n) for the time complexity of the above recursive solution can be written as follows.
T(n) = 2T(n-1) + 1
Quiz of this Question
Please comment below if you find anything wrong in the above post
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!