Information Technology is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Time and space complexity, asymptotic notation

Time and space complexity explain how an algorithm's running time and memory use grow with input size using asymptotic notations (Big-O, Theta, Omega) so engineers can choose and prove efficient algorithms.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two sentences: When engineers design software, they must predict how an algorithm's running time and memory use grow as input size increases to ensure scalability and acceptable performance. Asymptotic analysis (time and space complexity) gives machine-independent, formal measures to compare algorithms and choose the best for large inputs.

Key ideas

Start simple and define terms before using them.

  • Algorithm: a finite sequence of well-defined steps that transforms input into output. (We measure resources used when running it.)
  • Input size (n): a numeric measure of the problem instance size (for example, number of elements in an array). Define n before analysis.
  • Cost / Running time T(n): the number of elementary operations an algorithm performs on an input of size n. We count a basic operation (assignment, comparison, arithmetic) as one unit unless stated otherwise, under the standard word-RAM model (unit cost for operations on O(log n)-bit words).
  • Space S(n): the amount of memory an algorithm requires for input size n; includes auxiliary space (extra memory beyond input) and sometimes total space (including input). Use bytes or words as units.

Asymptotic notation: formal definitions universities expect.

  • Big-O (upper bound): f(n) = O(g(n)) iff there exist positive constants c and n0 such that for all n ≥ n0, f(n) ≤ c·g(n). This gives an asymptotic upper bound.
  • Big-Omega (lower bound): f(n) = Ω(g(n)) iff there exist positive constants c and n0 such that for all n ≥ n0, f(n) ≥ c·g(n).
  • Big-Theta (tight bound): f(n) = Θ(g(n)) iff f(n) = O(g(n)) and f(n) = Ω(g(n)); equivalently there exist positive c1, c2, n0 with c1·g(n) ≤ f(n) ≤ c2·g(n) for all n ≥ n0.
  • little-o: f(n) = o(g(n)) iff for every ε>0, there exists n0 such that for n≥n0, f(n) ≤ ε·g(n). This means f grows strictly slower than g.
  • little-omega: f(n) = ω(g(n)) iff for every c > 0, there exists n0 such that for all n ≥ n0, f(n) ≥ c·g(n); equivalently, lim_{n→∞} f(n)/g(n) = ∞ (assuming g(n) > 0 eventually).
  • Limit test (useful): if limit as n→∞ of f(n)/g(n) = c where 0 < c < ∞, then f(n) = Θ(g(n)). If limit = 0 then f = o(g); if ∞ then f grows faster than g.

Properties and common laws (state and use them):

  • Transitivity: if f = O(g) and g = O(h) then f = O(h).
  • Addition: f + g = Θ(max(f,g)) when f and g are positive functions.
  • Multiplication by constant: c·f(n) = Θ(f(n)) for any constant c>0.
  • Change of logarithm base: for any constants a, b > 1, log_a n = (log_b n)/(log_b a) = Θ(log n); thus the base of the logarithm does not matter in asymptotic classes.
  • Polynomial vs logarithm vs exponential: for any a > 0 and k > 0, (log n)^a = o(n^k); and for any k > 0 and c > 1, n^k = o(c^n) (polynomials grow slower than exponentials).

Step-count method (how to analyze code, step-by-step):

  1. Identify the basic operation to count (usually inside inner loop or a comparison).
  2. Express the number of times that operation runs as a function of n (a sum or recurrence).
  3. Simplify the sum or solve the recurrence.
  4. Drop lower-order terms and constant factors to get the asymptotic class.

Recurrences and Master Theorem (short form): for divide-and-conquer recurrences T(n) = a·T(n/b) + f(n), compare f(n) with n^{log_b a} to get:

  • If f(n) = O(n^{log_b a - ε}) for some ε>0 then T(n) = Θ(n^{log_b a}).
  • If f(n) = Θ(n^{log_b a}·log^k n) for some k ≥ 0, then T(n) = Θ(n^{log_b a}·log^{k+1} n).
  • If f(n) = Ω(n^{log_b a + ε}) and the regularity condition holds (i.e., there exists a constant c < 1 and n0 such that for all n ≥ n0, a·f(n/b) ≤ c·f(n)), then T(n) = Θ(f(n)). (Explain the assumptions: a ≥ 1, b > 1, n large and f positive.)

Diagrams to draw in exams:

  • Plot a simple log-log chart: x-axis labelled n (log scale), y-axis labelled time (log scale). Draw curves labelled O(log n), O(n), O(n log n), O(n^2), O(2^n) with increasing steepness; label each curve.
  • Recurrence tree for merge sort: draw a root labelled cost cn, two children each labelled c(n/2), four children c(n/4), show height log_2 n and annotate sum of internal levels = cn·(log_2 n); leaves contribute Θ(n), so total = n·(log_2 n + 1) = Θ(n log n). Label leaves with c and show total leaf cost = Θ(n).

Common complexity classes to memorize: O(1), O(log n), O(n), O(n log n), O(n^2), O(n^3), O(2^n), O(n!). Always state whether you mean worst-case, average-case or best-case.

Formulas

f(n) = O(g(n)) iff ∃ c > 0, n0 > 0 such that ∀ n ≥ n0, f(n) ≤ c·g(n) — f and g are nonnegative functions of n; no SI unit (pure counts). Applies to asymptotic upper bounds.

f(n) = Ω(g(n)) iff ∃ c > 0, n0 > 0 such that ∀ n ≥ n0, f(n) ≥ c·g(n) — lower bound.

f(n) = Θ(g(n)) iff ∃ c1>0, c2>0, n0>0 such that ∀ n ≥ n0, c1·g(n) ≤ f(n) ≤ c2·g(n) — tight bound.

T(n) = a·T(n/b) + f(n) — recurrence for divide and conquer; a = number of subproblems, b = factor the input size reduces by, f(n) = cost outside recursion.

If limit_{n→∞} f(n)/g(n) = c (0 < c < ∞) then f(n) = Θ(g(n)) — the limit test.

Sum of first n integers: ∑_{i=1}^{n} i = n(n+1)/2 — useful for nested loop sums. Geometric series: ∑_{i=0}^{k} r^i = (r^{k+1} - 1)/(r - 1) for r ≠ 1; in particular, ∑_{i=0}^{⌊log_b n⌋} a^i = Θ(n^{log_b a}) for a > 1, b > 1. Harmonic sum: ∑_{i=1}^{n} 1/i = Θ(log n).

Space lines: S_total(n) = S_input(n) + S_aux(n) — total memory = input memory + auxiliary memory. Units: bytes or words.

Worked examples

Example 1 — nested loops (step-count method). Question: Analyse the time complexity of: for (i = 1; i <= n; ++i) { for (j = 1; j <= i; ++j) { sum++; } } Solution steps:

  1. Choose basic operation: increment sum (executed inside inner loop).
  2. Count how many times it runs: for i = 1 to n, inner loop runs i times, so total = ∑_{i=1}^{n} i.
  3. Use formula: ∑_{i=1}^{n} i = n(n+1)/2.
  4. Simplify asymptotically: n(n+1)/2 = (n^2 + n)/2 = (1/2) n^2 + (1/2) n = Θ(n^2). Final answer: Time complexity = Θ(n^2).

Example 2 — recurrence / merge sort. Question: Solve T(n) = 2 T(n/2) + n with T(1) = c. Model answer (short proof):

  1. Apply Master Theorem: a = 2, b = 2 so n^{log_b a} = n^{log_2 2} = n.
  2. f(n) = n so f(n) = Θ(n^{log_b a}). This is case 2 (k = 0), so T(n) = Θ(n · log n).
  3. Optionally show recurrence tree: level 0 cost = n, level 1 cost = 2·(n/2)=n, ... height = log_2 n, total = n·(log_2 n + 1) = Θ(n log n). Final answer: T(n) = Θ(n log n).

Example 3 — space for recursion. Question: What is auxiliary space of a recursive factorial function: int fact(int n) { if (n==0) return 1; return n * fact(n-1); } Model answer:

  1. Each recursive call uses a constant amount of stack frame (local variables, return address). Let k be that constant number of words.
  2. Depth of recursion for input n is n+1 calls (from n down to 0).
  3. Total auxiliary space = k·(n+1) = Θ(n). Final answer: Auxiliary space = Θ(n) (stack space).

Common mistakes

  • Misapplying Master Theorem: ensure recurrence fits form a·T(n/b) + f(n) and that a and b are constants; check regularity condition for case 3.
  • Counting operations incorrectly in nested variable loops (e.g., inner loop bound depends on i) — write the sum explicitly.
  • Treating recursion depth as time rather than space (stack depth affects space, not time directly).

For your exam

Kinds of questions you will see: definitions (state Big-O, Big-Theta), short notes (differences between O, Θ, Ω), derivations (prove transitivity or show limit test), numericals (count operations of loops, sums), recurrence problems (solve by Master theorem or recurrence tree), and diagrams (draw recursion trees or plots of growth rates). What a full-marks answer includes:

  • State definitions precisely (quantifiers, constants, n0).
  • Show assumptions (what you count as basic operation, whether n is integer and large).
  • For loop/algorithm analysis: show step-count expression (sum or recurrence), simplify algebraically and conclude with asymptotic class with justification.
  • For recurrences: mention which Master case or show recurrence tree with level costs and sum.
  • When asked, give space analysis separately and state whether counting auxiliary or total space.

Quick check

  1. Give the formal definition of f(n) = O(g(n)).
  2. Determine asymptotic class of T(n) = 3n + 7.
  3. Using limit test, decide if n^2 and n log n are Θ of each other. Answers: 1) f(n)=O(g(n)) iff ∃ c>0,n0 s.t. ∀ n≥n0 f(n) ≤ c·g(n). 2) T(n)=Θ(n). 3) n^2 is not Θ(n log n): limit n^2/(n log n) = n/log n → ∞, so n^2 grows faster; they are not Θ of each other.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?