CPSC 221

Correctness

Today

Announcements

  • Examlet 1 is next week — do the practice problems
  • HW1 is out, due Monday Sep 21
  • Friday’s class is a video, posted by Thursday morning
    • recording at 4:30p today, if you’d like to attend (zoom)!


Where we’re going

  • Runtime, final words
  • Warm-up
  • Intro to correctness
  • Loop invariants

Asymptotic notation

For positive constants \(c\) and \(n_0\):

\(T(n) \in O(f(n))\) if \(\exists\, c, n_0\) such that \(T(n) \le c\,f(n)\) for all \(n \ge n_0\)
\(T(n) \in \Omega(f(n))\) if \(\exists\, c, n_0\) such that \(T(n) \ge c\,f(n)\) for all \(n \ge n_0\)
\(T(n) \in \Theta(f(n))\) if \(T(n) \in O(f(n))\) and \(T(n) \in \Omega(f(n))\)
\(T(n) \in o(f(n))\) if for any \(c\), \(\exists\, n_0\) such that \(T(n) < c\,f(n)\) for all \(n \ge n_0\)
\(T(n) \in \omega(f(n))\) if for any \(c\), \(\exists\, n_0\) such that \(T(n) > c\,f(n)\) for all \(n \ge n_0\)

Warm-up

\[\text{Thm: } \log n! \in \Theta(n \log n)\]

Proof:

Intro to algorithm correctness

int ____________ (vector<int>& C, int a) {
    int retloc = a;
    for (int i = a+1; i < C.size(); i++)
        if (C[i] < C[retloc]) {
            retloc = i;
        }
    return retloc;
}

Good name — what does it do?

Running time?

Does it work?

Correctness: prelims

int ____________ (vector<int>& C, int a) {
    int retloc = a;
    for (int i = a+1; i < C.size(); i++)
        if (C[i] < C[retloc]) {
            retloc = i;
        }
    return retloc;
}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
32 11 73 21 29 86 81 92 57 61 64 15 79 44 7 45

What are the possible values of \(i\)?

Stop the algorithm at some iteration \(i\) — what do we know about the variables?

What are we trying to prove?

int idxOfMin (vector<int>& C, int a) {
    int retloc = a;
    for (int i = a+1; i < C.size(); i++)
        if (C[i] < C[retloc]) {
            retloc = i;
        }
    return retloc;
}

Thm. idxOfMin(C, a) returns the index of the minimum value in C[a..n-1].

The invariant

We want, at exit: retloc is the index of the min of C[a..n-1].


So carry that claim through the loop:

Loop invariant. At the start of iteration \(i\), retloc is the index of the minimum value in C[a..___].

Proof Strategy

Loop invariant. At the start of iteration \(i\), retloc is the index of the minimum value in C[a..___].

Proving the Theorem requires three things:

Initialization the invariant is true before the first iteration
Restoration if true at the start of iteration \(i\), it is true at the start of \(i+1\)
Termination the loop ends — and invariant + exit condition give the theorem


What does this smell like?

Initialization and restoration

int idxOfMin (vector<int>& C, int a) {
    int retloc = a;
    for (int i = a+1; i < C.size(); i++)
        if (C[i] < C[retloc]) {
            retloc = i;
        }
    return retloc;
}

Initialization. \(i = a{+}1\):

Restoration. Assume invariant holds at the top of \(i\), and then run the body:

Termination

int idxOfMin (vector<int>& C, int a) {
    int retloc = a;
    for (int i = a+1; i < C.size(); i++)
        if (C[i] < C[retloc]) {
            retloc = i;
        }
    return retloc;
}

Does the loop stop?

What is the state when it does?

The recipe

1. Recognize the Claim the postcondition you are trying to reach
2. Find an invariant strong enough to give that at exit, and still true after one pass
3. Initialization true before the first iteration
4. Restoration start of \(i\) \(\Rightarrow\) start of \(i{+}1\)
5. Termination the loop ends, and invariant gives result


Here the invariant was the theorem with \(n\) replaced by \(i\). That is the easy case — often it has to say more than the theorem.


Proving correctness is only one use of invariants — they are also how you design a loop in the first place.