CPSC 221

Reasoning About Algorithms

Today

Announcements

  • Labs start next week
  • HW1 available Monday Sep 14, due Sep 21
  • Registration for EX1 will be soon!


Where we’re going

  • The value of an algorithm
  • The abstraction of running time
  • What it takes to describe an algorithm precisely

Algorithm analysis

Analysis of an algorithm gives insight into

  • whether it is correct
  • how long it runs — time complexity
  • how much memory it uses — space complexity

All with the aim of making comparisons between different approaches to solving the same problem analytically, not experimentally.

Where we stopped

If an algorithm is not correct, is its value 0?


No — and the exceptions are important and worth considering:

  • approximation — provably close, much faster
  • heuristics — usually good, no promise
  • randomized — correct with high probability
  • restricted inputs — correct on the data you actually have

Measuring the value of algorithms

Two algorithms solve the same problem. Which is better?

  • Use different metrics — time, space most common
  • Express running time as a (positive) function of input size:

\[T(n): \text{time to complete the operations on data of size } n\]

 

 

  • The form of that function depends on the structure of the algorithm

Running times

Suppose a computer executes \(10^{12}\) operations per sec.

operations n=10 n=100 n=1000 n=10,000 n=\(10^{12}\)
\(n\) \(10^{-11}\) s \(10^{-10}\) s \(10^{-9}\) s \(10^{-8}\) s 1 s
\(n \log n\) \(10^{-11}\) s \(10^{-9}\) s \(10^{-8}\) s \(10^{-7}\) s 40 s
\(n^2\) \(10^{-10}\) s \(10^{-8}\) s \(10^{-6}\) s \(10^{-4}\) s \(10^{12}\) s
\(n^3\) \(10^{-9}\) s \(10^{-6}\) s \(10^{-3}\) s 1 s \(10^{24}\) s
\(2^n\) \(10^{-9}\) s \(10^{18}\) s \(10^{289}\) s

Which of these are you willing to wait for?

Algorithmic analysis — example 1

int ____________ (vector<int> arr, int q) {
    for (int i = 0; i < arr.size(); i++)
        if (arr[i] == q)
            return i;
    return -1;
}

C++ mysteries?

Algorithmic analysis — example 1

int ____________ (vector<int> arr, int q) {
    for (int i = 0; i < arr.size(); i++)
        if (arr[i] == q)
            return i;
    return -1;
}
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 does it do? Describe it using a complete sentence. Give it a good name.

Example 1, continued

Form groups of size 3. Look at the group papers.

How many written statements can you see in your group?

Example 1, continued

Read your statement out loud, simultaneously on the count of 3.

Example 1, continued

Share your statements, and discuss them.

Write a final description for your group.

Example 1, continued

int ____________ (vector<int> arr, int q) {
    for (int i = 0; i < arr.size(); i++)
        if (arr[i] == q)
            return i;
    return -1;
}

What just happened?

Algorithmic analysis — running time

int idxFirst (vector<int> arr, int q) {
    for (int i = 0; i < arr.size(); i++)
        if (arr[i] == q)
            return i;
    return -1;
}

How long does it take?