mindstair

Home / Courses / Discrete Mathematics / Unit 9

Discrete Mathematics Unit 9: Algorithms & complexity (intro)

Big-O notation describes how an algorithm's running time grows as input size grows, ignoring constants.

Covers: Big-O, basic algorithm analysis

Study guide

Algorithm analysis measures how running time or memory grows with input size n. Big-O notation describes an upper bound on growth: f(n) is O(g(n)) if there are constants C and k with f(n) ≤ C·g(n) for all n > k. Constants and lower-order terms don't matter, so 3n² + 5n + 7 is O(n²). Big-Omega gives a lower bound, and Big-Theta means both bounds match.

Common growth rates, from slowest to fastest: constant O(1), logarithmic O(log n), linear O(n), linearithmic O(n log n), quadratic O(n²), cubic O(n³), exponential O(2ⁿ), and factorial O(n!). Exponential and factorial algorithms become impractical even for modest n, while any polynomial is eventually dwarfed by 2ⁿ.

Basic analysis counts operations. A single loop over n items is O(n); two nested loops over n items are O(n²); a loop that halves the problem each time is O(log n). Linear search is O(n), while binary search on a sorted list is O(log n) because it halves the search range each step. Simple sorts like bubble sort and insertion sort are O(n²) in the worst case, while merge sort is O(n log n).

Worst-case analysis gives guarantees, while average-case analysis describes typical behavior. Recursive algorithms are analyzed with recurrence relations, like T(n) = 2T(n/2) + n for merge sort, which solves to O(n log n).

f is o(g) if f(n)/g(n) → 0, so f grows strictly slower than g, and f is ω(g) if the ratio → ∞. f ~ g means the ratio → 1. Little-o implies big-O, but not the other way around: n/100 is O(n) but not o(n). Stirling's formula says n! ~ √(2πn)(n/e)ⁿ. It shows that n! grows faster than any cⁿ but is o(nⁿ). It also gives log(n!) = Θ(n log n).

For a decreasing positive function f, ∫₁ⁿ⁺¹ f(x) dx ≤ f(1) + f(2) + … + f(n) ≤ f(1) + ∫₁ⁿ f(x) dx. For example, the harmonic sum Hₙ = 1 + 1/2 + … + 1/n is ~ ln n.

Key ideas

Formulas and rules

Growth order: 1 < log n < n < n log n < n² < 2ⁿ < n!

Common mistakes

Words to know

Big-O simplification
Keep the fastest-growing term and drop constants: 3n² + 5n + 7 is O(n²).
Running time of binary search
O(log n)
Exponential vs. polynomial growth
2ⁿ eventually outgrows any polynomial like n¹⁰⁰.
Typical cost of two nested loops over n items
O(n²)
Big-O notation
f = O(g): for large n, f(n) ≤ C·g(n).
Common growth rates
1 < log n < n < n log n < n² < 2ⁿ < n!
Algorithm
A finite sequence of precise steps for solving a problem.
Big-Omega (Ω)
A lower bound on growth.
Big-Theta (Θ)
Matching upper and lower bounds.
Linear search
O(n): checks items one by one.
Merge sort
O(n log n) divide-and-conquer sort.
Worst-case analysis
The maximum number of operations over all inputs of size n.
Merge sort recurrence
T(n) = 2T(n/2) + n → O(n log n)

Sample practice questions

17 more questions like these, from level 1 to 5, are in the practice set.

Question 1 (level 1)

Simplify O(5n + 100).

  1. O(n)
  2. O(n²)
  3. O(5n)
  4. O(1)
Show the answer and steps

Answer: A) O(n)

  1. Drop constants and lower-order terms.
  2. 5n + 100 → O(n).
Question 2 (level 1)

Simplify O(3n⁴ + 9n + 118).

  1. O(3n⁴)
  2. O(n⁵)
  3. O(1)
  4. O(n⁴)
Show the answer and steps

Answer: D) O(n⁴)

  1. Keep the fastest-growing term and drop constants.
  2. O(n⁴).
Question 3 (level 3)

Which grows fastest?

  1. n log n
  2. n!
  3. n³
  4. 2ⁿ
Show the answer and steps

Answer: B) n!

  1. n! eventually outgrows 2ⁿ, n³ and n log n.
  2. n! grows fastest.