Algorithmic Complexity

Context: FIT1008_MOC, FIT2004_MOC · the measurement foundation — input size, the RAM cost model, time complexity, per-operation cost, best/worst/average cases FIT2004 emphasis: distinguish total from auxiliary space (extra beyond the input — in-place auxiliary); quote the tightest () bound available. Input size is often bit-length — for a number , , not .

Quick Revision

  • 🎯 Objective: measure the resource (time/space) as a function of input size ➔ the question is scalability as .
  • 📦 Core Components: input size (often bit-length!) | RAM unit-cost steps | time complexity | case (best/worst/avg/amortised).
  • ⚡ Key Constraint: unqualified “complexity” = worst case; the RAM unit-cost assumption breaks for variable-size keys ().

📝 Core

1. The Resource Question

  • What it measures ➔ how cost grows with input size — time (elementary ops) or space (peak memory).
  • Function of ➔ stated as , not a single number, because we care how it scales.
  • Time–space trade-off ➔ extra memory buys speed (memoisation, hash tables) and vice-versa.

2. Input Size (“Big” Defined)

  • Type-dependent ➔ collection’s element count | string’s chars | graph’s and .
  • Bit-length trap ➔ a number has size , not ; halving to takes steps, i.e. .
  • Pseudo-polynomial ➔ a loop running times is in true size (Knapsack , still NP-hard).
  • A BOUNDED parameter is a constant ➔ if the spec caps a parameter (, ), it contributes and vanishes from the bound — the cap makes it independent of input size, not merely small.
  • Which symbols are free ➔ before quoting a bound, list which parameters can grow without limit; a bound may only be expressed in those. The output size can be one ➔ Output-Sensitive Complexity.

3. Running Time & RAM Model

  • Abstraction ➔ Random-Access Machine — each elementary op = 1 unit, random access.
  • Portability ➔ step count is machine-independent ⟹ "" portable, “3 ms” not.
  • Declare the unit-cost assumption ➔ ” is ” holds only for machine-word operands; on values whose bit-length grows with an addition costs ➔ iterative Fibonacci is word ops but bit ops, since occupies bits ➔ Fibonacci Sequence. The model also breaks for external-memory effects.

4. Time Complexity & Step Cost

  • Counting rules ➔ statement | sequence sums | if = test + branch | loop = body × iters | recursion = recurrence.
  • CompEq factor ➔ a step is only for fixed-size keys; length- strings ⟹ .
  • swap is ➔ 3 copies; choose a sort minimising the expensive op.

5. Best / Worst / Average Case

  • Definitions ➔ at fixed : , , , with .
  • Best ≠ worst only on short-circuit ➔ requires an if/break/early return, else best=worst (selection sort).
  • Average ≠ amortised ➔ average needs a distribution; amortised is a worst-case-sequence guarantee with no probability.

6. Space: Total vs Auxiliary (FIT2004 quoting standard)

  • The lecture’s split ➔ space complexity input space auxiliary space, reported separately because only the second is the algorithm’s choice. Total space is always for an -element input, so it never discriminates.
  • Auxiliary space ➔ any memory beyond the input — the number quoted in a complexity table; in-place auxiliary.
  • The three iterative reference cases ➔ find_min(arr) keeps one variable ⟹ input , auxiliary , in-place · build_list(n) takes a number and allocates slots ⟹ input , auxiliary (no recursion, still — output counts) · iterative binary_search ⟹ auxiliary .
  • Recursion stack counts ➔ auxiliary max live frame chain , never ; balanced recursion gives frames against for peeling one element — the same split that decides time in Divide and Conquer ➔ Analysing Recursive Algorithms (Time and Auxiliary Space). Shrinking frames of size sum to , not , by the bound in Geometric Series.
  • Time auxiliary space — always ➔ memory must be allocated and written before it counts, and each cell costs step ⟹ an algorithm quoting auxiliary cannot be in time.
  • Tightest bound ➔ quote when best worst; reserve for a genuine upper-bound-only claim.

⚙️ Core Implementation

🔹 Step-counting & the input-size gotcha

🔹 Cost of a step & best/worst short-circuit

⚖️ Core Decision Matrix

(Best / Average / Worst time, with the worst-case trigger.)

AlgorithmBestAverageWorstTrigger of worst
Bubble Sort IIreverse-sorted
Selection Sortalways (no short-circuit)
Insertion Sortreverse-sorted
Quick Sortmin/max pivot
Hash Tableall keys collide

When It Flips: quote worst for guarantees (real-time/adversarial), average for typical (quicksort, hashing), best rarely. Average ≠ amortised — average assumes a distribution (fails on skewed inputs); amortised is a hard worst-case-sequence guarantee.

📊 Exam Execution Trace

Applied Exercise

Problem: Show why an Knapsack DP is pseudo-polynomial, not polynomial.

Final Extracted Output: polynomial in the value but exponential in its bit-length ⟹ pseudo-polynomial (why Knapsack stays NP-hard).

✍️ Practice

🧠 Active Recall