Link

Algorithm Analysis II Study Guide

Runtime Analysis. Understanding the runtime of code involves deep thought. It amounts to asking: “How long does it take to do stuff?”, where stuff can be any conceivable computational process whatsoever. It simply cannot be done mechanically, at least for non-trivial problems. As an example, a pair of nested for loops does NOT automatically result in Theta(N2) runtime.

Cost Model. As an anchor for your thinking, recall the idea of a “cost model” from last lecture. Pick an operation and count them. You want the one whose count has the highest order of growth as a function of the input size.

Important Summations. This is not a math class so we’ll be a bit sloppy, but the two key sums that you should know are that:

  • 1 + 2 + 3 + … + N is in Theta(N2)
  • 1 + 2 + 4 + 8 + … + N is in Theta(N)

Practice. The only way to learn this is through plenty of practice. Make sure to work through the problems in lecture and below when you have some time.

  1. Prove to yourself that Theta(log2 N) = Theta(log3 N).
  2. Give the runtime of each method in Big-Theta notation. Your answer should be a function of N that is as simple as possible with no unnecessary leading constants or lower order terms.
    public static void g1(int N) {
        for (int i = 0; i < N * N * N; i += 1) {
            for (int j = 0; j < N * N * N; j += 1) {
                System.out.print("fyhe");
            }
        }
    }
    
    public static void g2(int N) {
        for (int i = 0; i < N; i += 1) {
            int numJ = Math.pow(2, i + 1) - 1; // constant time
            for (int j = 0; j < numJ; j += 1) {
                System.out.print("fhet");
            }
        }
    }
    
    public static void g3(int N) {
        for (int i = 2; i < N; i *= i) {}
        for (int i = 2; i < N; i += 1) {}
    }
    
  3. Suppose we have an algorithm with a runtime that is Theta(N2 log N) in all cases. Are these statements about the runtime definitely true, definitely false, or is there not enough information?
    • O(N2 log N)
    • Omega(N2 log N)
    • O(N3)
    • Theta(N2 log4 N)
  4. Suppose we have an algorithm with a runtime that is O(N3) in all cases.Are these statements about the runtime definitely true, definitely false, or is there not enough information?
    • There exists some inputs for which the runtime is Theta(N2)
    • There exists some inputs for which the runtime is Theta(N3)
    • There exists some inputs for which the runtime is Theta(N4)
    • The worst case runtime is O(N3)
    • The worst case runtime has order of growth N3
  5. Q2 from CS 61BL 16su MT1
  6. Q4b from CS 61BL 17su MT1
  7. Q5 from CS 61B 15sp MT2
  8. Q9 from CS 61B 16sp MT2
  9. Q4a, Q4b from CS 61B 17sp MT2
  10. Q5 from CS 61BL 17su MT2
  11. Q8 from CS 61B 18sp MT2
  12. Q4 from CS 61BL 18su MT2
  13. Q10a from CS 61B 19sp MT2
  14. Q5 from CS 61BL 19su MT1