Chapter 9Complexity Analysisslides.en.pdf:205

Time/Space Complexity

Algorithms → functions from inputs to work

Def 0.3Complexity
Concept

Definition 0.3. An algorithm α\alpha that terminates in time t(n)t(n) on
all inputs of size nn has running time T(α):=tT(\alpha) := t.

If SNNS \subseteq \mathbb{N} \to \mathbb{N} is a set of functions, we say
α\alpha has time complexity in SS (write T(α)ST(\alpha) \in S or
T(α)=ST(\alpha) = S) iff tSt \in S. Similarly for space complexity: s(n)s(n)
is the memory used on inputs of size nn.

Worst-case vs. average-case complexity. Most textbooks (and this course)
focus on worst-case: t(n)=maxt(n) = \max over all inputs of size nn. Average
case replaces the max with the expected value over some distribution.

Size measure matters. "Size" of the input is a choice. For a number, nn is
the number itself. For a list of length nn, the size is nn. For a graph with
VV vertices and EE edges, the size is usually V+EV + E. There is no canonical
choice — the analysis must say which.

Examples:
- Array lookup: T(n)=1T(n) = 1 — constant. Lookup doesn't depend on array size.
- Linear search: T(n)=nT(n) = n — linear. Worst case: target not present.
- Matrix multiply (naive): T(n)=n3T(n) = n^3 — cubic.
- Subsets of an nn-set: T(n)=2nT(n) = 2^n — exponential.

Time vs. space. You can often trade one for the other. Hash tables use
more memory but enable O(1)O(1) lookup. Quicksort uses O(logn)O(\log n) stack space
but O(n2)O(n^2) worst-case time.

Practice — score 100% to advance
Multiple choice
Q1
What does T(α)T(\alpha) denote?
Q2
What is worst-case complexity?
Q3
What is space complexity?
Q4
Why is 'size of input' ambiguous?
Match definitions
Match each concept on the left to its definition on the right.
Loading…