包含n个光盘的递归关系捕获了河内塔问题的最佳时间。 (GATE CS 2012)
(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答案: (D)
说明:以下是递归解决河内塔问题的步骤。
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
上述递归解的时间复杂度的递归函数T(n)可以写成如下。
T(n)= 2T(n-1)+1