Docsity

CS 161 — Algorithm Complexity and Big-O Notation, Exams of Nursing

Toggle sidebar
bg1
CS 161 — Algorithm Complexity and
Big-O Notation
Table of Contents
1. What Is Algorithm Complexity?
2. Worst, Best, and Average Cases
3. Big-O Notation
4. Big-Omega Notation
5. Big-Theta Notation
6. Simplifying Runtime Expressions
7. Analyzing Loops
8. Common Complexity Classes
9. Common Mistakes
10. Official CS 161 References
What Is Algorithm Complexity?
Algorithm complexity measures how the resources used by an algorithm—usually time or
space—grow as the input size grows.
Let:
T(n)=running time on an input of size nT(n) = \text{running time on an input of size }
nT(n)=running time on an input of size n
CS 161 focuses on asymptotic analysis because exact operation counts depend on programming
language, compiler, hardware, and implementation details. Instead, we study how T(n)T(n)T(n)
scales as nnn becomes large.web.stanford+2
Example
Suppose an algorithm takes:
T(n)=3n2+100n+7T(n) = 3n^2 + 100n + 7T(n)=3n2+100n+7
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS 161 — Algorithm Complexity and Big-O Notation and more Exams Nursing in PDF only on Docsity!

CS 161 — Algorithm Complexity and

Big-O Notation

Table of Contents

  1. What Is Algorithm Complexity?
  2. Worst, Best, and Average Cases
  3. Big-O Notation
  4. Big-Omega Notation
  5. Big-Theta Notation
  6. Simplifying Runtime Expressions
  7. Analyzing Loops
  8. Common Complexity Classes
  9. Common Mistakes
  10. Official CS 161 References

What Is Algorithm Complexity?

Algorithm complexity measures how the resources used by an algorithm—usually time or space—grow as the input size grows.

Let:

T(n)=running time on an input of size nT(n) = \text{running time on an input of size } nT(n)=running time on an input of size n

CS 161 focuses on asymptotic analysis because exact operation counts depend on programming language, compiler, hardware, and implementation details. Instead, we study how T(n)T(n)T(n) scales as nnn becomes large.web.stanford+

Example

Suppose an algorithm takes:

T(n)=3n2+100n+7T(n) = 3n^2 + 100n + 7T(n)=3n2+100n+

For large nnn, the n2n^2n2 term dominates. Therefore:

T(n)=Θ(n2)T(n) = \Theta(n^2)T(n)=Θ(n2)

The constants 3, 100, and 7 do not affect the asymptotic growth rate.stanford-cs161.github

Worst, Best, and Average Cases

These describe which input is being analyzed.

Case Meaning Example

Worst case Maximum runtime over all inputs of size nnn Linear search on an absent target

Best case Minimum runtime over all inputs of size nnn Linear search finds the target immediately

Average case Expected runtime over an input distribution Random target position

Expected case

Expected runtime under the algorithm’s random choices Randomized QuickSort

A key CS 161 distinction is that Big-O is not the same as worst case , and Big-Omega is not the same as best case. Big-O and Big-Omega describe bounds on functions; worst and best case describe which input is considered.web.stanford

For example, an algorithm may have:

Tworst(n)=Θ(n2)T_{\text{worst}}(n) = \Theta(n^2)Tworst(n)=Θ(n2)

but:

Tbest(n)=Θ(n)T_{\text{best}}(n) = \Theta(n)Tbest(n)=Θ(n)

Stanford’s notes use this distinction to prevent the common confusion between complexity notation and input-case analysis.web.stanford

Big-O Notation

Intuition

f(n)=O(g(n))f(n) = O(g(n))f(n)=O(g(n)) means f(n)f(n)f(n) grows no faster than g(n)g(n)g(n), up to constant factors, for sufficiently large nnn.web.stanford+

Intuition

f(n)=Ω(g(n))f(n) = \Omega(g(n))f(n)=Ω(g(n)) means f(n)f(n)f(n) grows at least as fast as g(n)g(n)g(n), up to constant factors, for sufficiently large nnn.web.stanford+

Formal definition

f(n)=Ω(g(n))f(n) = \Omega(g(n))f(n)=Ω(g(n))

if there exist constants c>0c > 0c>0 and n0>0n_0 > 0n0>0 such that for all n≥n0n \ge n_0n≥n0:

0≤c⋅g(n)≤f(n)0 \le c \cdot g(n) \le f(n)0≤c⋅g(n)≤f(n)

web.stanford+

Examples

n2=Ω(n)n^2 = \Omega(n)n2=Ω(n)

because n2n^2n2 grows at least as fast as nnn.

Also:

n2=Ω(n2)n^2 = \Omega(n^2)n2=Ω(n2)

because a function is always at least as large as itself up to constant factors.

Stanford’s notes express the relationship as:

f(n)=Ω(g(n))⟺g(n)=O(f(n))f(n) = \Omega(g(n)) \quad \Longleftrightarrow \quad g(n) = O(f(n))f(n)=Ω(g(n))⟺g(n)=O(f(n))

stanford-cs161.github

Big-Theta Notation

Intuition

f(n)=Θ(g(n))f(n) = \Theta(g(n))f(n)=Θ(g(n)) means f(n)f(n)f(n) and g(n)g(n)g(n) grow at the same asymptotic rate , up to constant factors.web.stanford+

Formal definition

f(n)=Θ(g(n))f(n) = \Theta(g(n))f(n)=Θ(g(n))

if and only if:

f(n)=O(g(n))andf(n)=Ω(g(n))f(n) = O(g(n)) \quad \text{and} \quad f(n) = \Omega(g(n))f(n)=O(g(n))andf(n)=Ω(g(n))

Equivalently, there exist constants c1,c2>0c_1, c_2 > 0c1,c2>0 and n0>0n_0 > 0n0>0 such that for all n≥n0n \ge n_0n≥n0:

0≤c1g(n)≤f(n)≤c2g(n)0 \le c_1 g(n) \le f(n) \le c_2 g(n)0≤c1g(n)≤f(n)≤c2g(n)

web.stanford+

Examples

3n2+100n+7=Θ(n2)3n^2 + 100n + 7 = \Theta(n^2)3n2+100n+7=Θ(n2) 50n=Θ(n)50n = \Theta(n)50n=Θ(n) 5nlogn+n=Θ(nlogn)5n \log n + n = \Theta(n \log n)5nlogn+n=Θ(nlogn)

Stanford’s notes emphasize that Θ\ThetaΘ captures the exact asymptotic growth rate, ignoring lower-order terms and leading constants.web.stanford+

Simplifying Runtime Expressions

To find a tight asymptotic bound:

  1. Expand the runtime expression.
  2. Keep only the fastest-growing term.
  3. Drop constant coefficients.

Examples

5n2+100n+3=Θ(n2)5n^2 + 100n + 3 = \Theta(n^2)5n2+100n+3=Θ(n2) 2nlogn+10n=Θ(nlogn)2n \log n + 10n = \Theta(n \log n)2nlogn+10n=Θ(nlogn) 7n3+2n=Θ(2n)7n^3 + 2^n = \Theta(2^n)7n3+2n=Θ(2n) 100=Θ(1)100 = \Theta(1)100=Θ(1)

Stanford’s Big-O handout states that asymptotic notation cares only about the highest-growing term and ignores leading constants.stanford-cs161.github

j = j * 2

Runtime:

Θ(logn) \Theta(\log n)Θ(logn)

because jjj takes values:

1,2,4,8,…,2k1, 2, 4, 8, \ldots, 2^k1,2,4,8,…,2k

Linear outer loop, logarithmic inner loop

text for i = 1 to n: j = 1 while j <= n: constant work j = j * 2

Runtime:

Θ(nlogn) \Theta(n \log n)Θ(nlogn)

Loop with shrinking input

text while n > 1: constant work n = n / 2

Runtime:

Θ(logn) \Theta(\log n)Θ(logn)

Common Complexity Classes

From slowest to fastest growth:

Θ(1)<Θ(logn)<Θ(n)<Θ(nlogn)<Θ(n2)<Θ(n3)<Θ(2n) \Theta(1) < \Theta(\log n) < \Theta(n) < \Theta(n \log n) < \Theta(n^2) < \Theta(n^3) < \Theta(2^n)Θ(1)<Θ(logn)<Θ(n)<Θ(nlogn)<Θ(n2)<Θ(n3)<Θ(2n)

Complexity Typical algorithm or operation

Θ(1)\Theta(1)Θ(1) Array access, arithmetic on fixed-size integers

Θ(logn) \Theta(\log n)Θ(logn) Binary search

Θ(n)\Theta(n)Θ(n) Linear search

Complexity Typical algorithm or operation

Θ(nlogn) \Theta(n \log n)Θ(nlogn) MergeSort

Θ(n2)\Theta(n^2)Θ(n2) InsertionSort worst case

Θ(n3)\Theta(n^3)Θ(n3) Floyd-Warshall

Θ(2n)\Theta(2^n)Θ(2n) Exhaustive subset enumeration

Stanford’s CS 161 materials use MergeSort and InsertionSort as standard examples of O(nlogn)O(n \log n)O(nlogn) and O(n2)O(n^2)O(n2) algorithms, respectively.web.stanford+

Common Mistakes

Mistake 1: Confusing Big-O with worst case

Big-O describes an upper bound on a function. Worst case describes the input that maximizes runtime.

An algorithm’s worst-case runtime may be Θ(n2)\Theta(n^2)Θ(n2), while its best-case runtime may be Θ(n)\Theta(n)Θ(n).web.stanford

Mistake 2: Treating O(f(n))O(f(n))O(f(n)) as a tight bound

OOO is only an upper bound.

For example:

n=O(n2)n = O(n^2)n=O(n2)

is true, but it is not tight. The tight bound is:

n=Θ(n)n = \Theta(n)n=Θ(n)

Mistake 3: Keeping lower-order terms

Do not write:

3n2+100n+7=Θ(3n2+100n+7)3n^2 + 100n + 7 = \Theta(3n^2 + 100n + 7)3n2+100n+7=Θ(3n2+100n+7)

Although technically true, it is not simplified. The standard answer is:

Θ(n2)\Theta(n^2)Θ(n2)