Data Structures and Algorithms (H) Lecture 2 Notes
Runtime and Asymptotic Notation
In the previous lecture, we showed a naive way to analyze the runtime of algorithms. But this naive approach is very heavy and can be particularly troublesome when comparing algorithms that have complex runtime expressions. To simplify this, we introduce the asymptotic notation. A high-level intuition of the asymptotic notation is that it captures, and only captures the growth speed of functions.
The running time of every instance is sandwiched between the best case and the worst case running time. The worst case is often more important because:
- it guarantees that the algorithm will never take longer time.
- for some algorithms, the worst case is quite frequent.
- often the average case is as bad as the base case.
Where the average case is defined as the performance on “average” input. e.g. for sorting it means that we assume that every possible permutation is equally likely.
Some key observations:
- the biggest-order term (e.g.
vs. ) dominates the runtime as grows. - and how the runtime scales with
is more important than constant factors (for sufficiently large ). - additive smaller order terms become irrelevant for large
. - we care about large
because small problems are always easy to solve.
Asymptotic Notation:
Definition:
For a given non-negative function
Intuitively,
Example
- Use
if we only want to express an upper bound. - Use
if we only want to express a lower bound.
The formal definition of these two new notations are similar:
Definition:
For a given non-negative function
Tip
We also have
An overview of asymptotic notation:
| Notation | Meaning | Analogy |
|---|---|---|
Caution
Always remember that
By using the asymptotic notation, we can now describe the runtime of
insertion sort as:
| Mathematical expression | Name |
|---|---|
| logarithmic time | |
| linear time | |
| quadratic time | |
| cubic time | |
| polynomial time | |
| exponential time |
Tip
- Every polynomial of
grows strictly slower than every polynomial of . - Every polynomial of
grows strictly slower than every exponential function.
How to find
- It is often helpful to divide by
, e.g. Then try sandwiching the constant term. - Remember that
. - Also remember that inequalities need to hold for all
. - No need to invest time to find the best possible constants.
Practical rules to make runtime analysis simple
For two non-negative functions
- Slower functions can be ignored:
- Asymptotic times can be multiplied:
Asymptotic notation: comparing sets
Is
Such statement is true if no matter how the anonymous functions are chosen on the left of the equal sign, there is a way to choose the anonymous functions on the right of the equal sign to make the equation valid.
Example
is true because . is false, e.g. but .
- Title: Data Structures and Algorithms (H) Lecture 2 Notes
- Author: Zhige Chen
- Created at : 2025-09-18 21:06:52
- Updated at : 2025-10-10 13:21:26
- Link: https://nofe1248.github.io/2025/09/18/dsaa-lec-2/
- License: This work is licensed under CC BY-NC-SA 4.0.