Link
Iterative Algorithm Analysis
Modeling the running time of iterative code.
Kevin Lin, with thanks to many others.
1

Comprehending. Understanding the implementation details of a program.
Modeling. Counting the number of steps in terms of N, the size of the input.
Case Analysis. How certain conditions affect the program execution.
Asymptotic Analysis. Describing what happens for very large N, as N→∞.
Formalizing. Summarizing the final result in precise English or math notation.
Runtime Analysis Process
2
boolean dup1(int[] A)
Consider every pair
Array contains a duplicate at front
Array contains no duplicate items
Constant time
Quadratic time
Best: Θ(1)
Worst: Θ(N2)
Overall: Ω(1) and O(N2)
Worst case
Best case

Repeat After Me…
There is no magic shortcut for these problems (except in a few well-behaved cases).
Know these two summations since they’re common patterns.



Strategies.
Find the exact count of steps.
Write out examples.
Use a geometric argument–visualizations!
3
Numerical Linear Algebra (Lloyed N. Trefethen, David Bau, III/SIAM)
Real world programs are often messy and difficult to model.

?: What’s different between these two summations?




?: How did we apply these strategies to analyze printParty?

4
Runtime: f0
static void f0(int N) {
    if (N < 10000)
        for (int i = 0; i < N * N; i += 1)
            System.out.println("hello");
    else
        System.out.println(N * N * N);
}
Q
?: What happens when N is less than 10000? What happens when N is greater than 10000?



?: What is the asymptotic variable in this problem?




Q1: Give the order of growth of the runtime in Θ notation as a function of N. Your answer should be simple with no unnecessary leading constants or summations.

5
Runtime: f0
A

6
Runtime: f1
public static void f1(int N) {
    for (int i = 1; i < N; i *= i)
        System.out.println("hello");
    for (int i = 1; i < N; i *= 2)
        System.out.println("hiya");
    for (int i = 1; i < N; i += 1)
        System.out.println("hi");
}
Q
Q1: Give the order of growth of the runtime in Θ notation as a function of N. Your answer should be simple with no unnecessary leading constants or summations.

7
Runtime: f1
A

8
Runtime: f2
static void f2(int N) {
    for (int i = 0; i < N; i += 1) {
        // Math.pow takes constant time
        int big = Math.pow(2, i + 1) - 1;
        for (int j = 0; j < big; j += 1)
            System.out.println("hello");
    }
}
Q
Q1: Give the order of growth of the runtime in Θ notation as a function of N. Your answer should be simple with no unnecessary leading constants or summations.

9
Runtime: f2
A

10
Runtime: f3
static void f3(int N) {
    for (int x = 0; x < N; x += 1) {
        int i = N / 2;
        while (i != x)
            if (i > x)
                i -= 1;
            else
                i += 1;
    } }
Q
Q1: Give the order of growth of the runtime in Θ notation as a function of N. Your answer should be simple with no unnecessary leading constants or summations.

11
Runtime: f3
A

12
Big-Theta Notation
Q
Suppose we have an algorithm with a runtime that is in Θ(N2 log N) in all cases.
For each statement about the algorithm runtime, determine if it is definitely true, definitely false, or not enough information to know for sure.

O(N2 log N)
Ω(N2 log N)
O(N3)
Θ(N2 log4 N)
Q1: For each statement about the algorithm runtime, determine if it is definitely true, definitely false, or not enough information to know for sure.

13
Big-Theta Notation
A

14
Big-O Notation
Q
Suppose we have an algorithm with a runtime that is O(N3) in all cases.
For each statement about the algorithm runtime, determine if it is definitely true, definitely false, or not enough information to know for sure.

There exists some inputs for which the runtime is Θ(N2)
There exists some inputs for which the runtime is Θ(N3)
There exists some inputs for which the runtime is Θ(N4)
The worst case runtime is O(N3)
The worst case runtime has order of growth N3
Q1: For each statement about the algorithm runtime, determine if it is definitely true, definitely false, or not enough information to know for sure.

15
Big-O Notation
A