Chapter 9Complexity Analysisslides.en.pdf:208

Program Complexity

Compute T(α) by structural induction

Def 0.8Program complexity
Concept

Computing the complexity of a program requires structural induction on its
syntax. The rules mirror the structure of the language:

Base cases:
- Constants and variables: O(1)O(1) — they evaluate in one step.

Arithmetic operations: +,,,/+, -, *, /O(1)O(1) per operation (one machine
instruction).

Function application: f(x1,,xk)f(x_1, \dots, x_k) — cost of evaluating each
argument plus the function body.

Sequential composition α1;α2;;αk\alpha_1; \alpha_2; \dots; \alpha_k:

C(α)=max(C(α1),,C(αk))+cC(\alpha) = \max(C(\alpha_1), \dots, C(\alpha_k)) + c

The total is dominated by the slowest part.

Branching if P then alpha1 else alpha2:

C=C(P)+max(C(α1),C(α2))C = C(P) + \max(C(\alpha_1), C(\alpha_2))

Loops — for for i := 1 to n do alpha:

C=nC(α)+O(n)C = n \cdot C(\alpha) + O(n)
(the O(n)O(n) accounts for the loop overhead).

Recursion f(x)=f(smaller)f(x) = \dots f(\text{smaller}) \dots:
Set up a recurrence T(n)T(n) and solve it. Examples:
- Linear recursion T(n)=T(n1)+1=O(n)T(n) = T(n-1) + 1 = O(n).
- Binary recursion (divide & conquer) T(n)=2T(n/2)+n=O(nlogn)T(n) = 2 T(n/2) + n = O(n \log n).

Examples:
- Linear search on a list of nn: T(n)=O(n)T(n) = O(n).
- Binary search: T(n)=T(n/2)+1=O(logn)T(n) = T(n/2) + 1 = O(\log n).
- Selection sort: T(n)=n+(n1)++1=O(n2)T(n) = n + (n-1) + \dots + 1 = O(n^2).
- Merge sort: T(n)=2T(n/2)+n=O(nlogn)T(n) = 2T(n/2) + n = O(n \log n).

Nested summation counting

To compute the time complexity of nested loops, count iterations.

Pattern 1: Single loop.

for i = 1 to n: O(1) work

Total work: i=1n1=n\sum_{i=1}^{n} 1 = n. Complexity: O(n)O(n).

Pattern 2: Nested loops.

for i = 1 to n: for j = 1 to n: O(1) work

Total work: i=1nj=1n1=i=1nn=n2\sum_{i=1}^{n} \sum_{j=1}^{n} 1 = \sum_{i=1}^{n} n = n^2. Complexity: O(n2)O(n^2).

Pattern 3: Triangular nested loops.

for i = 1 to n: for j = 1 to i: O(1) work

Total work: i=1nj=1i1=i=1ni=n(n+1)2\sum_{i=1}^{n} \sum_{j=1}^{i} 1 = \sum_{i=1}^{n} i = \frac{n(n+1)}{2}. Complexity: O(n2)O(n^2) (the leading term dominates).

Pattern 4: Loop with shrinking inner bound.

for i = 1 to n: for j = 1 to n-i: O(1) work

Total work: i=1n(ni)=k=0n1k=(n1)n2\sum_{i=1}^{n} (n - i) = \sum_{k=0}^{n-1} k = \frac{(n-1)n}{2}. Complexity: O(n2)O(n^2).

Pattern 5: Logarithmic inner loop.

for i = 1 to n: j = 1 while j <= n: j = j * 2 O(1) work

Inner loop runs O(logn)O(\log n) times. Total: nO(logn)=O(nlogn)n \cdot O(\log n) = O(n \log n).

Reading. Each pattern reduces to a summation; identify the closed form; take the leading term. The summation i=1ni=n(n+1)2\sum_{i=1}^{n} i = \frac{n(n+1)}{2} is the most common.

Worked example
Step 0 of 2
Practice — score 100% to advance
Multiple choice
Q1
Linear search on a list of nn elements has complexity…
Q2
Binary search has complexity…
Q3
Sequential composition C(α1;α2;α3)=C(\alpha_1; \alpha_2; \alpha_3) = ___.
Q4
A for loop running nn times with body O(k)O(k) has total cost…
Q5
To analyze recursion, you set up a ___ and solve it.
Q6
The pattern 'for i = 1 to n, for j = 1 to i' has total iterations:
Fill in the blank
Q1
For 'for i = 1 to n, j *= 2 inside', the inner loop runs ___ times.
Loading…