Saturday, October 18, 2008

unwind recursion formula

Though recursion formulas are defined based on previous ones, like Fibonacci numbers, F(n) = F(n-1) + F(n-2), for natural numbers n > 2, the formula can be expressed only depending on current variables, which is the closed form for F(n).
To get the closed form, we need to unwind the formula given recursively. What we do is use the formula continuously until we hit the base case, whose value is given explicitly. For example, in time complexity for binary search, T(n) = 3 for n = 1, and T(n) = 3 + T(n/2) for other natural numbers. We unwind T(n) until we get to T(1). So T(n) = 3 + T(n/2) = 3 + 3 + T(n/4) = ... = 3lgn + T(1) = 3lgn + 3. Thus, we get the closed form for time complexity of binary search.
A special case occurs in Fibonacci numbers. Each element of Fibonacci numbers seems like r^n. Because F(n) = F(n-1) + F(n-2), so r^2 = r^1 + r^0. we can get two roots r1, r2 from the equation and for any a, b, ar1 + br2 also satifies the equation. As F(n) = n for n < 2, ar1 + br2 = 0 and ar1 + br2 = 1. Then we get a and b. Thus, we get the closed form for F(n).

No comments: