MEG 202 · Basic Software Engineering
Our notes
2 min readComputability
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:
| Order | Name | Example |
|---|---|---|
| O(1) | constant | array access by index |
| O(log n) | logarithmic | binary search |
| O(n) | linear | linear search, summing an array |
| O(n log n) | n log n | merge sort |
| O(n²) | quadratic | bubble sort, comparing all pairs |
| O(2ⁿ) | exponential | naive 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
-
Best pick Introduction to Computer Science, 3.6 Computer Science Theory
(opens openstax.org in a new tab)
OpenStax, Introduction to Computer Science, 3.6 Computer Science Theory, CC BY-NC-SA 4.0 · licence
-
Introduction to Computer Science, 3.3 Formal Properties of Algorithms
(opens openstax.org in a new tab)
OpenStax, Introduction to Computer Science, 3.3 Formal Properties of Algorithms, CC BY-NC-SA 4.0 · licence
-
Computability theory
(opens en.wikipedia.org in a new tab)
Wikipedia, Computability theory, CC BY-SA 4.0 · licence
-
Computational complexity theory
(opens en.wikipedia.org in a new tab)
Wikipedia, Computational complexity theory, CC BY-SA 4.0 · licence
-
Correctness (computer science)
(opens en.wikipedia.org in a new tab)
Wikipedia, Correctness (computer science), CC BY-SA 4.0 · licence
-
NOUN CIT 310: Algorithms and Complexity Analysis
NOUN course book (PDF, about 4.0 MB). Complexity and correctness of algorithms.
(opens nou.edu.ng in a new tab)
-
OpenStax, Introduction to Computer Science (whole free textbook)
The whole book, free to read online or download as a PDF (the PDF is large: Wi-Fi).
(opens openstax.org in a new tab)
OpenStax, OpenStax, Introduction to Computer Science (whole free textbook), CC BY-NC-SA 4.0 · licence
Watch
-
Computability, Complexity, and Automata Theory
13 min · ≈ 59 MB · about ₦33
(opens youtube.com in a new tab)
-
Algorithms Explained: Computational Complexity
21 min · ≈ 95 MB · about ₦53
(opens youtube.com in a new tab)
-
Lecture 23: Computational Complexity
51 min · ≈ 230 MB · about ₦129
(opens youtube.com in a new tab)
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.