AP Computer Science Principles · Algorithms and programming
Algorithmic efficiency
1Measuring efficiency
An algorithm's efficiency is how much time (number of steps) and memory it uses as the input size grows. We usually count steps, not seconds, because seconds depend on the computer.
- Linear search checks up to every element: about $n$ steps for $n$ items.
- Binary search (sorted data only) halves the range each step: about $\log_2 n$ steps.
for 1,000,000 sorted items, linear search may need 1,000,000 comparisons; binary search needs about 20.
Check yourself
Binary search can be used only when the data is:
Show the answer
Sorted
It relies on order.
2Reasonable and unreasonable time
- Algorithms whose steps grow like a polynomial ($n$, $n^2$, $n^3$) run in reasonable time.
- Algorithms whose steps grow exponentially ($2^n$) or factorially ($n!$) run in unreasonable time: even fast computers cannot finish for moderately large $n$.
for $n = 50$: $n^2 = 2{,}500$ steps, but $2^n \approx 10^{15}$ steps.
Exponential steps (2ⁿ) shoot up far faster than polynomial steps (n, n²)
Check yourself
Which growth rate is unreasonable?
Show the answer
2ⁿ
Exponential.
3Heuristics
When exact solutions take unreasonable time, programmers may use a heuristic: an approach that finds a good enough solution quickly, though not always the best.
the travelling salesperson problem (shortest route visiting every city once) has $n!$ possible routes. A heuristic like "always go to the nearest unvisited city" finds a reasonable route fast.
Check yourself
Why use a heuristic?
Show the answer
To find a good solution in reasonable time when the best is too slow to find
Good enough, fast.
4Decidable and undecidable problems
- A decision problem has a yes/no answer ("Is this number prime?").
- A problem is decidable if an algorithm can always produce a correct answer for every input.
- An undecidable problem has no algorithm that works for all inputs. The classic example is the halting problem: no algorithm can decide, for every program and input, whether the program will eventually stop.
Some instances of undecidable problems can still be solved; it is the general case that cannot.
Check yourself
The halting problem is an example of:
Show the answer
An undecidable problem
No general algorithm.
5Going deeper: reasonable and unreasonable time, heuristics and problems no algorithm can solve
A problem is a general task (sort any list); an instance is one specific input (sort this list). An algorithm is a finite set of steps that solves the problem.
Efficiency estimates the resources an algorithm needs, usually the number of steps, as a function of the size of the input (n). What matters is how the count grows as n grows.
Two kinds of problem
- A decision problem has a yes-or-no answer: "Is there a route shorter than 100 km?"
- An optimization problem asks for the best solution: "What is the shortest route?"
Reasonable time: algorithms whose step count is a polynomial in n or better: constant, logarithmic, linear (n), quadratic (n²), cubic (n³).
Unreasonable time: step counts that grow exponentially (2ⁿ) or factorially (n!). They become impossible for quite small inputs.
How fast they grow
- n = 10: n² = 100; 2ⁿ = 1024; n! = 3.6 million.
- n = 20: n² = 400; 2ⁿ ≈ 1 million; n! ≈ 2.4 × 10¹⁸.
- n = 30: n² = 900; 2ⁿ ≈ 1 billion.
- n = 60: n² = 3600; 2ⁿ ≈ 10¹⁸: at a billion steps a second, over 30 years.
Adding one item to the input doubles the work of a 2ⁿ algorithm. A faster computer hardly helps.
Comparing two searches
- Linear search checks items one by one: up to n steps. Works on any list.
- Binary search halves the remaining range each time: about log₂ n steps. Needs sorted data.
- For 1000 items: up to 1000 against about 10. For a million: a million against about 20.
- Doubling the list adds 1 step to binary search but doubles linear search.
Worked example (counting steps): a procedure compares every item in a list of n names with every other item, looking for duplicates.
- Each of n items is compared with about n others.
- About n² comparisons.
- Polynomial: reasonable time. For 1000 names, about a million steps: quick.
Worked example (classify): an algorithm tries every possible subset of n items.
- Each item is either in or out: 2ⁿ subsets.
- Exponential: unreasonable time. For 100 items, 2¹⁰⁰ subsets: far beyond any computer.
Different algorithms for one problem may differ greatly in efficiency; and different correct algorithms can be compared by counting steps or by timing them on inputs of increasing size.
Heuristics: when the best solution cannot be found in reasonable time, use an approach that finds a good-enough solution quickly. It is not guaranteed to be optimal.
Case study (the traveling salesperson problem): find the shortest route visiting each of n cities once and returning home. Checking every route means about (n − 1)! ÷ 2 possibilities: 12 cities give about 20 million; 20 cities about 6 × 10¹⁶. A heuristic, "always go next to the nearest unvisited city," gives a reasonable route almost instantly, though usually not the shortest. Delivery companies rely on such methods every day.
Undecidable problems: some problems cannot be solved by any algorithm, however much time is allowed.
- A decidable problem is a decision problem for which an algorithm can be written that gives a correct yes-or-no answer for every input.
- An undecidable problem has no such algorithm. An algorithm may work for some inputs, but none works for all.
The halting problem is the classic example: "Given any program and its input, will the program eventually stop, or run forever?" Alan Turing proved in 1936 that no algorithm can answer this correctly in every case.
Do not confuse the two limits
- Unreasonable time: an algorithm exists, but it is too slow to be practical.
- Undecidable: no algorithm exists at all.
Formal and informal analysis: efficiency can be worked out mathematically (reasoning about loops) or measured empirically (timing runs). Exam questions ask you to reason informally: how many times does the loop run for a list of n items?
Common mistakes
- Calling n² "unreasonable" because it is slower than n.
- Saying a heuristic gives the best answer.
- Using binary search on unsorted data.
- Saying an undecidable problem "takes too long."
- Thinking a faster computer makes exponential algorithms practical.
Check yourself
Which describes an undecidable problem?
Show the answer
No algorithm can give a correct answer for every possible input
Undecidability means no algorithm works in all cases, however long it runs.
6For top marks
- Compare algorithms using numbers: $n$ vs $\log_2 n$, $n^2$ vs $2^n$.
- Know that binary search requires sorted data, and that sorting first has its own cost.
- Define heuristic as "good enough, not guaranteed optimal".
- Distinguish unreasonable (solvable but too slow) from undecidable (no algorithm exists for all cases).
"Checking all $2^{30} \approx 1$ billion subsets of 30 items is unreasonable; a heuristic that adds the most valuable items first gives a quick, near-best answer."
Check yourself
An algorithm takes 100 steps for n = 10 and 10,000 steps for n = 100. It most likely grows as:
Show the answer
n²
n grew 10 times; steps grew 100 = 10² times.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera