Knowledge / Computer Science and Engineering / Time and space complexity

Time and space complexity

Computer Science and Engineering (B.E./B.Tech) · Algorithms · free study notes for exams, placements and interviews

In short

Time and space complexity measure how the running time and memory of an algorithm grow as the input size n grows, written in asymptotic notation (O, Ω, Θ) that ignores machine speed and constant factors. It is the single most examined idea in algorithms: every GATE CSE paper carries questions on loop counting, recurrences and asymptotic comparison, and every coding round at Amazon, Microsoft, Google, Flipkart, Zoho, Adobe, Oracle, Samsung R&D and the service majors (TCS NQT, Infosys, Wipro) asks "what is the complexity of your solution?" before accepting it. On Indian judges (CodeChef, HackerEarth, HackerRank) complexity decides whether code passes within the 1-second limit or gets TLE. It is also the language in which tractability is defined — the classes P, NP and NP-complete.

Explained

What is measured, and why asymptotically

Asymptotic notations and their properties

Best, worst, average case — and amortized

Analysing iterative code — counting rules

Analysing recursive code — recurrences

Space complexity

Complexity classes and intractability

Key points

Formulas and examples

f(n) = O(g(n)) ⟺ ∃ c > 0, n₀ > 0 such that 0 ≤ f(n) ≤ c·g(n) for all n ≥ n₀
f(n) = Ω(g(n)) ⟺ ∃ c > 0, n₀ > 0 such that 0 ≤ c·g(n) ≤ f(n) for all n ≥ n₀
f(n) = Θ(g(n)) ⟺ ∃ c₁, c₂ > 0, n₀ such that c₁·g(n) ≤ f(n) ≤ c₂·g(n) for all n ≥ n₀
f(n) = o(g(n)) ⟺ lim(n→∞) f(n)/g(n) = 0; f(n) = ω(g(n)) ⟺ lim(n→∞) f(n)/g(n) = ∞
Σ(i=1..n) i = n(n+1)/2; Σ(i=1..n) i² = n(n+1)(2n+1)/6; Σ(i=0..n) 2ⁱ = 2n+1 − 1
Σ(i=0..n) rⁱ = (rn+1 − 1)/(r − 1), r ≠ 1; infinite sum = 1/(1−r) for |r| < 1
Harmonic: H(n) = Σ(i=1..n) 1/i ≈ ln n + γ, γ ≈ 0.5772 ⇒ H(n) = Θ(log n)
Stirling: n! ≈ √(2πn)·(n/e)ⁿ ⇒ log(n!) = Θ(n log n)
Master theorem: T(n) = a·T(n/b) + f(n); critical exponent = log_b a; compare f(n) with nlog_b a
Amortized cost per operation = (total cost of a worst-case sequence of m operations) / m
Potential method: amortized cost ĉᵢ = cᵢ + Φᵢ − Φᵢ₋₁, with Φ ≥ 0 and Φ₀ = 0
Space(algorithm) = input size + auxiliary space; recursion space = (max depth) × (frame size)

Symbols: n = input size, c, c₁, c₂ = positive constants, n₀ = threshold beyond which the bound holds, a = number of subproblems, b = shrink factor, f(n) = work outside the recursive calls, γ = Euler–Mascheroni constant. Units are *operations* (time) and *words or bytes* (space) — not seconds, because seconds are machine-dependent.

Example 1 — prove a bound from the definition. Show 3n² + 10n + 5 = Θ(n²).

Example 2 — loop counting.

int c = 0;
for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j += i)
        c++;

Example 3 — recurrence by recursion tree and by master theorem. T(n) = 2T(n/2) + n, T(1) = 1, n = 8.

Example 4 — master theorem, all three cases.

Recurrencea, bnlog_b af(n)CaseT(n)
T(n) = 4T(n/2) + n4, 2n²n1Θ(n²)
T(n) = 2T(n/2) + n2, 2nn2Θ(n log n)
T(n) = 2T(n/2) + n log n2, 2nn log n2, k=1Θ(n log²n)
T(n) = 3T(n/4) + n²3, 4n0.792n²3Θ(n²)
T(n) = T(n/2) + 11, 2n⁰ = 112Θ(log n)
T(n) = 7T(n/2) + n²7, 2n2.807n²1Θ(n2.807)
T(n) = T(n−1) + n———substitutionΘ(n²)

(log₄3 = 0.7925, log₂7 = 2.8074.)

Example 5 — amortized cost of a doubling dynamic array. Capacity starts at 1 and doubles when full; a copy costs 1 per element.

Example 6 — choosing an algorithm from n (≈10⁸ operations per second).

Complexityn = 10⁴n = 10⁵n = 10⁶Largest n in ~1 s
O(log n)~13~17~20astronomically large
O(√n)1003161,000~10¹⁶
O(n)10⁴10⁵10⁶~10⁸
O(n log n)1.3×10⁵1.7×10⁶2×10⁷~5×10⁶
O(n²)10⁸ (~1 s)10¹⁰ (~100 s)10¹²~10⁴
O(n³)10¹²——~460
O(2ⁿ)———~26
O(n!)———~11

So for n = 10⁵ an O(n²) solution needs about 100 s and gets TLE, while O(n log n) needs 1.7×10⁶ operations ≈ 0.017 s. Binary search on n = 10⁶ takes ⌈log₂10⁶⌉ = 20 comparisons against 10⁶ for linear search.

Where it is used

Interview questions

Common mistakes

Related topics

Sorting algorithms · Searching and binary search · Recursion and backtracking · Divide and conquer · Dynamic programming · DSA interview patterns

Open in the Knowledge reader →Find jobs →