FACTORIAL(n)
- if n = 0
- return 1
- else
- return FACTORIAL(n-1) *n
T(n) = T(n-1) + 1 T(n-1) = T((n-1)-1) + 1 = T(n-2) + 1 = [T(n-2) +1] + 1 = T(n-2) + 2 T(n-2) = T((n-2)-1) + 1 = T(n-3) + 1 = [T(n-3)+1] + 2 = T(n-3) + 3 At step i, T(n) = T(n-i) + i Let k = n (Selected so that T(n-k) = T(0) the base case) T(k) = T(n-k) + k = T(n-n) + n = T(0) + n = 1 + n ∈ O(n)
TOWER(T(n,1),s,d,a)
- TOWER(T(n-1,1), s, a, d) // move sub tower to aux peg
- Move T[n] from s to d
- TOWER(T(n-1,1),a,d,s) // move the sub tower from aux to destination
T(1) = 1 T(n) = T(n-1) + 1 + T(n-1) = 2T(n-1) + 1 T(n) = 2T(n-1) + 1 T(n-1) = 2T(n-1-1) + 1 = 2T(n-2) + 1 = 2[2T(n-2)+1] + 1 = 22T(n-2) + 21 + 20 T(n-2) = 2T(n-2-1)+1 = 2T(n-3)+1 = 22[2T(n-3)+1] + 21 + 20 = 23T(n-3) + 22 + 21 + 20 = 23T(n-3) + Σj=02 2j At step i T(n) = 2iT(n-i) + Σj=0i-1 2j Let k = n-1 T(k) = T(n-(n-1)) = T(1) T(n) = 2kT(1) + Σj=0k-1 2j We know (from the back) That Σj=0n 2j = 2n+1-1 (Think about it, all of the bits are set in an n bit number.) so T(n) = 2kT(1) + 2k-1+1 -1 = 2k + 2k -1 = 2×2k-1 = 2k+1-1 = 2n-1+1-1 = 2n-1 ∈ O(2n)
264-1 = 18,446,744,073,709,551,615 seconds 18,446,744,073,709,551,615/(60*60*24) = 213,503,982,334,601 days 213,503,982,334,601/365.25 = 584,542,046,090 years
BIN_REC(n)
- if n = 1
- return n
- else
- return BIN_REC(⌊n/2⌋)+1
T(1) = 1 T(n) = T(n/2) + 1 n > 1 T(n) = T(n/2) + 1 T(n/2) = T(n/2/2) + 1 = T(n/22)+1 = T(n/22) + 1 + 1 = T(n/22) + 2 T(n/22) = T(n/22/2) + 1 = T(n/23)+1 = T(n/23) + 1 + 2 = T(n/23) + 3 At step i T(n) = T(n/2i) + i WLOG assume n = 2k, or k = lg(n) (2k = n) T(n) = T(n/2k) = T(1) T(n) = T(1) + k = ln(n) + 1 ∈ O(ln(n))