Advertisement
timothy235

sicp-1-2-3-orders-of-growth

Feb 15th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 1.47 KB | None | 0 0
  1. #lang racket
  2.  
  3. ;; 1.14
  4.  
  5. ;; claim:  The time complexity of cc(n, d) is O(n ^ d) where n is the amount and d
  6. ;; is the number of denominations.
  7.  
  8. ;; Here is a proof sketch that goes by induction on d.
  9.  
  10. ;; Write n = q * d + r with 0 <= r < d.
  11.  
  12. ;; Keep applying the recursive definition of cc(x, d) = cc(x, d - 1) + cc(x - d, d)
  13. ;; to rewrite cc(n, d) as a sum of terms involving only d - 1 as follows:
  14.  
  15. ;; cc(n, d) = cc(n, d - 1) + cc(n - d, d)
  16.          ;; = cc(n, d - 1) + cc(n - d, d - 1) + cc(n - 2 * d, d)
  17.          ;; = ...
  18.          ;; = cc(n, d - 1) + ... + cc(n - (q - 1) * d, d - 1) + cc(n - q * d, d)
  19.          ;; = cc(n, d - 1) + ... + cc(n - (q - 1) * d, d - 1) + cc(n - q * d, d - 1)
  20.  
  21. ;; where the last step uses the fact that cc(n - q * d, d) = cc(n - q * d, d - 1)
  22. ;; because n - q * d = r < d.
  23.  
  24. ;; The terms of this sum are decreasing, the first term cc(n, d - 1) = O(n ^ (d - 1))
  25. ;; by induction, and there are theta(n) terms.  So the sum itself must be O(n ^ d).
  26.  
  27. ;; The space complexity is the depth of the recursive tree which is q and so O(n).
  28.  
  29. ;; 1.15
  30.  
  31. ;; The sine procedure makes a single recursive call.  So the recursive tree is a
  32. ;; single branch.  So the space complexity, which is the depth of the tree, will
  33. ;; be the same as the time complexity, which is the number of nodes in the tree.
  34. ;; This complexity is O(log a) because the recursion bottoms out after repeatedly
  35. ;; cutting a down by 1/3 until it is less than 0.1, which is log3(10 * a) steps.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement