UNIHIVE

Study Hub UNIPORT MEG 202

MEG 202 · Basic Software Engineering

Computability, complexity and correctness of algorithms

What can be computed, big-O notation, complexity classes, and how to show an algorithm is correct.

Our notes

2 min read

Computability

Can every well-defined problem be solved by an algorithm? No.

  • Turing machine (Alan Turing, 1936): a simple abstract machine (a tape, a head, a set of states and rules) that can carry out any algorithm. The Church-Turing thesis: anything computable by an effective procedure is computable by a Turing machine.
  • The halting problem: there is no general algorithm that can decide, for every program and input, whether the program will eventually stop. Turing proved this by contradiction: if such a decider existed, one could build a program that halts exactly when the decider says it does not.
  • Practical meaning: no tool can automatically find every infinite loop or prove every program correct.

Complexity

How the resources (time, memory) an algorithm needs grow with input size n.
Big-O notation gives the growth rate for large n, ignoring constants:

OrderNameExample
O(1)constantarray access by index
O(log n)logarithmicbinary search
O(n)linearlinear search, summing an array
O(n log n)n log nmerge sort
O(n²)quadraticbubble sort, comparing all pairs
O(2ⁿ)exponentialnaive Fibonacci, trying all subsets

Counting steps: a single loop over n items is O(n); a loop inside a loop over n is O(n²); halving the range each step is O(log n).
Example: an algorithm takes 3n² + 5n + 20 steps: it is O(n²): for n = 1,000 the n² term gives 3,000,000 of the 3,005,020 steps.
Best, average and worst cases can differ (quicksort: n log n average, n² worst).

Tractable and intractable problems

  • P: problems solvable in polynomial time (n, n², n³...): considered tractable.
  • NP: problems whose solutions can be checked in polynomial time. Many scheduling, routing (the travelling salesman problem) and packing problems are NP-complete: no fast algorithm is known, and whether P = NP is a famous open question. Engineers use heuristics and approximations for them.

Correctness

An algorithm is correct if it gives the right output for every valid input and terminates.

  • Testing can show the presence of bugs, not their absence (Edsger Dijkstra). Test normal cases, boundary cases (empty list, one item, the largest values) and invalid inputs.
  • Formal reasoning: preconditions (what must be true before), postconditions (what will be true after), and loop invariants.

Example: summing an array: s ← 0; for i = 0 to n − 1: s ← s + a[i].
Invariant: before each iteration, s = a[0] + ... + a[i − 1].

  • True initially (i = 0, s = 0, the empty sum).
  • Maintained: adding a[i] makes it true for i + 1.
  • At the end (i = n): s = the sum of all elements. ✓

Termination: i increases each step and stops at n.

Exams usually ask

  • Explain the halting problem and why it matters.
  • Give the big-O complexity of given code fragments.
  • Use a loop invariant to argue an algorithm is correct.

Common mistake

Thinking a faster computer makes an exponential algorithm practical. Doubling speed adds only about one to the largest n an O(2ⁿ) algorithm can handle in the same time.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All MEG 202 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.