FACTORIAL(n)
- if n = 0 then
- return 1
- else
- return n*Factorial(n-1)
T(n) = T(n-1)+1 if n > 0 = 1 if n = 0
T(n) = T(n-1) + 1 Now put n-1 into the original equation T(n-1) = T(n-1-1) + 1 = T(n-2) +1 Substitute this for T(n-1) in the working equn. so we substitute this back into the original T(n) = T(n-2)+1 + 1 and simplify T(n) = T(n-2) + 2 Do this again to see the pattern Put n-2 back into the equation T(n-2) = T(n-2 -1) + 1 = T(n-3) + 1 And back into the work we have been doing T(n) = T(n-3) +1 + 2 = T(n-3) + 3 So if you see a pattern go on, if not repeat. At step k, T(n) = T(n-k) + k Let k = n, selected so that T(n-k) is the base case T(n) = T(n-n) + n = 1 + n = n +1
BINARY_SEARCH(A,key, start, stop)
- ifstart> stop then
- return -1
- mid ← (start+stop)/2
- if A[mid] = key then
- return mid
- elseif A[mid] > key then
- return BINARY_SEARCH(A, key, start, mid-1)
- else
- return BINARY_SEARCH(A, key, mid+1,stop)
T(n) = T(n/2) + 1 if n > 0 = 1 if n = 0 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) + 3At step k T(n) = T(n/2k) + k WLOG assume n = 2k lg(n) = lg(2k) = k lg(2) = k T(n) = T(2k/2k)+ lg(n) = T(1) + lg(n) But T(1) will make one more call and be T(0) so T(n) = lg(n) + 1
TOWER_OF_HANOI(n,source, dest,spare)T(n) = T(n-1) + 1 + T(n-1) if n > 1 = 2T(n-1) + 1 = 1 if n = 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 = 22 T(n-2) + 2 + 1 T(n-2) = 2T(n-2-1)+1 = 2T(n-3) + 1 = 22[2T(n-3)+1] + 2 + 1 = 23T(n-3)+22 + 21 + 20 = 23T(n-3) + Σi=022i = 23T(n-3) + 23-1 At step k T(n) = 2kT(n-k) + 2k-1 Let k = n-1 T(n) = 2n-1T(n-(n-1)) + 2n-1-1+1-1 = 2n-1T(1)) + 2n-1-1 = 2n-1 + 2n-1-1 = 2(2n-1 + 2n-1)-1 = 2n -1 ∈ O(2n)
- if n > 1
- Move n-1 disks from source to spare
- Move last disk from source to dest
- Move n-1 disks from spare to dest
- else
- Move the disk from the source to dest
- return