📘 CodingMarble Learn

Computational Complexity: Easy, Hard and Impossible Problems

Complexity measures how the number of steps grows as the input size n grows. Polynomial algorithms (n, n², n³) are tractable; exponential (2ⁿ) and factorial (n!) ones become hopeless very fast, so such problems are intractable. Some problems are easy to check but hard to solve (NP). Some, like the halting problem, cannot be solved by any algorithm at all.

🎬 Step-by-step story

  1. Four kinds of algorithm work on n = 10 items. Bar height shows the number of steps (each level up is 10 times more).
  2. Now n = 20. n² is only 400. But 2ⁿ is over a million and n! is enormous. Exponential growth runs away.
  3. Six cities: find the shortest round trip. Trying every route means 60 trips. With 20 cities, it is 6 × 10¹⁶ trips.
  4. If someone gives you a route, checking its length is quick: just add 6 distances. Hard to solve, easy to check.
  5. The halting problem: no machine can look at every program and always say if it will stop or loop forever.
  6. Your turn: move n and see how long a fast computer would take for each kind of algorithm.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why not just buy a faster computer?

For 2ⁿ, a 1000-times-faster computer lets you add only about 10 items. The bars still explode.

Why count steps instead of seconds?

Steps do not depend on the computer. Bar heights compare kinds of growth fairly.

Why is the route problem so hard?

Every new city multiplies the number of routes. 6 cities give 60 trips, 20 cities give 6 × 10¹⁶.

How can checking be easy if solving is hard?

To check, you just add the n road lengths of the given route. To solve, you must compare a huge number of routes.

Is the halting problem just very slow?

No. It is impossible: any machine H can be tricked by a program built to do the opposite.

At what n does 2ⁿ become impractical?

Move the slider: past about n = 50, even 10⁹ steps per second takes days or years.

Measuring complexity with Big-O

We do not time algorithms in seconds, because computers differ. We count steps and ask how the count grows with the input size n. Big-O keeps only the fastest-growing part: 3n² + 5n + 7 is O(n²).

Big-ONameExample
O(1)constantread the first item of a list
O(log n)logarithmicbinary search
O(n)linearlinear search
O(n log n)linearithmicmerge sort
O(n²)quadraticbubble sort
O(2ⁿ)exponentialtrying every subset
O(n!)factorialtrying every order (route)

We also talk about space complexity: how much memory grows with n.

Polynomial vs exponential: tractable and intractable

A problem is tractable if some algorithm solves it in polynomial time, O(nᵏ) for a fixed k. It is intractable if every known algorithm needs more than polynomial time, such as O(2ⁿ) or O(n!).

Why does it matter? Doubling n makes n² four times bigger. But adding just 1 to n doubles 2ⁿ. At 10⁹ steps per second, n = 60 with 2ⁿ steps takes about 36 years. A faster computer barely helps.

For intractable problems we use heuristics: rules of thumb that give a good-enough answer quickly (for example, 'always go to the nearest unvisited city').

Classic hard problems, P and NP

P is the class of problems that can be solved in polynomial time. NP is the class whose answers can be checked in polynomial time. Checking a given route is fast; finding the best one seems slow. Whether P = NP is one of the biggest open questions in computer science.

Computable and non-computable: the halting problem

A problem is computable if some algorithm always gives the right answer in a finite number of steps. Some problems are non-computable: no algorithm can ever solve them for every input.

The halting problem asks: given any program and its input, will it stop or run forever? Alan Turing showed (1936) that no general machine H can answer this. Idea of the proof: suppose H exists. Build a program X that asks H about itself and then does the opposite (loops if H says 'stops', stops if H says 'loops'). Then H is wrong about X. So H cannot exist.

Lesson: some limits are not about speed. Even an infinitely fast computer cannot solve a non-computable problem.

Try it: feel the explosion

Write the letters A, B, C. List every order (ABC, ACB, …): 6 orders. Add D: 24 orders. Add E: 120. Each new letter multiplies the work. That is n! growth, the same reason route-finding gets hard.

Key formulas and definitions

Worked examples

1. Give the Big-O of 5n³ + 2n² + 100.

Keep the fastest-growing term and drop the constant: O(n³).

2. An O(n²) algorithm takes 1 s for n = 1000. About how long for n = 3000?

n is 3 times bigger, so time is 3² = 9 times: about 9 s.

3. An O(2ⁿ) algorithm takes 1 s for n = 30. About how long for n = 40?

10 more items → 2¹⁰ = 1024 times longer: about 1024 s ≈ 17 minutes.

4. How many different round trips for 5 cities (brute force)?

(5 − 1)!/2 = 24/2 = 12 trips.

5. At 10⁹ steps per second, how long do 2⁵⁰ steps take?

2⁵⁰ ≈ 1.13 × 10¹⁵. Time ≈ 1.13 × 10⁶ s ≈ 13 days.

6. Is 'sort a list' tractable? Is 'find the best exam timetable for 500 students' tractable?

Sorting is O(n log n), polynomial, so tractable. The best timetable (graph colouring) has no known polynomial algorithm, so it is treated as intractable; schools use heuristics.

Common mistakes

Practice quiz

1. Which grows fastest for large n?
2. A problem with a polynomial-time algorithm is:
3. The halting problem is:
4. Round trips for 4 cities by brute force:
5. NP problems are ones whose solutions can be:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is computational complexity?

The study of how the time or memory an algorithm needs grows as the input size grows, usually written in Big-O notation.

What is the difference between tractable and intractable?

Tractable problems have polynomial-time algorithms. Intractable ones have no known polynomial-time algorithm, so they become too slow for large inputs.

What is the halting problem in simple words?

It asks whether we can write one program that tells, for any program, if it will finish or run forever. Turing proved no such program can exist.

Where this is taught

NetherlandsHAVO 5 (eindexamenjaar)Elective theme: Algorithms, computability and logic
NetherlandsVWO 6 (eindexamenjaar)Elective theme: Algorithms, computability and logic
England (GCSE, A level)Year 134.4 Theory of computation (A-level)
Germany (Bavaria)Jahrgangsstufe 13Algorithms, complexity and computability

Learn first

Related lessons

All Computer Science lessons