← Back to course
Intro to CS 9-10 / Week 07 / Thursday
4/6
Week 07 · Search, Speed and Limits

Thursday

Reasonable, unreasonable, undecidable
// Binary search, counting steps, heuristics and problems no algorithm can solve
⏱ about 20 min

Thursday: Reasonable, Unreasonable, Undecidable

Beacon has a new job: plan the supply rover's route through the greenhouse domes.

Today the rover leaves the dock and visits three domes: A for tomatoes, B for beans and C for herbs.

"Easy," Comet says. "Beacon can try every order and pick the shortest."

Wren counts on his fingers. "Three domes give six orders. But mission control wants ten domes next year."

Nova projects a number that keeps growing until it fills the wall. "Ten domes give 3,628,800 orders," she says.

Comet's mouth falls open. "Maybe we need a smarter rule."

"A good rule," Wren says, "even if it is not always the best one."

How fast do the orders grow?

To count the orders, multiply. Three domes give 3 times 2 times 1, which is 6 orders.

Each extra dome multiplies the count again. This kind of growth is called factorial.

Domes to visitPossible orders
36
424
5120
6720
840,320
103,628,800

Algorithms with polynomial efficiency, like constant, linear, square or cube, run in a reasonable amount of time.

Algorithms with exponential or factorial efficiency are examples of algorithms that run in an unreasonable amount of time.

Some problems cannot be solved in a reasonable amount of time, because no efficient algorithm exists for them. Then people look for approximate solutions.

REASONABLE OR UNREASONABLE?
  • Read the question.
  • Tap your answer.
How many orders are there for 4 domes?
Linear search grows at the same rate as the list. Is that reasonable or unreasonable time?
Trying every order of the domes grows factorially. Is that reasonable or unreasonable time?

A heuristic for the rover

A heuristic is an approach that gives a solution that is not guaranteed to be the best. People use one when finding the best is impractical.

Wren suggests a heuristic for the rover: always drive to the nearest dome you have not visited yet.

Here are the drive times in minutes. The rover does not need to return to the dock.

DriveMinutes
Dock to dome A1
Dock to dome B2
Dock to dome C5
Dome A to dome B3
Dome A to dome C4
Dome B to dome C8
RUN THE NEAREST-FIRST HEURISTIC
  • Read the question.
  • Tap your answer.
From the dock, which dome is nearest?
From dome A, which unvisited dome is nearer: B or C?
The heuristic route is dock, A, B, C. How many minutes in all?
Now try dock, B, A, C. How many minutes is that route?
Did the nearest-first heuristic find the best route?

With three domes, Beacon can check all six orders. With ten, the heuristic gives a good route fast, even if it is not always the best.

Try it
On paper, list all six orders the rover could visit domes A, B and C, starting at the dock.
Add up the minutes for each order. Check that the best is 9 minutes and that no order is shorter.
On paper, draw a map of the dock and the three domes. Label each road with its minutes, then trace the heuristic route and the best route in two colors.

Problems no algorithm can always solve

A decidable problem is a decision problem for which an algorithm can give a correct output for every input. "Is the number even?" is one.

An undecidable problem is one for which no algorithm can be built that always gives a correct yes or no answer.

Some instances of an undecidable problem may still be solvable. But no algorithm can solve all of its instances.

StatementTrue or false?
"Is the number even?" is a decidable problem.?
A heuristic always finds the best solution.?
An undecidable problem has no algorithm that is always correct for every input.?
If a problem is undecidable, none of its instances can ever be solved.?
WHY THIS EXERCISEKnowing the limits of algorithms helps a developer choose between an exact answer, a heuristic, or a different question.
What is the name for an approach that finds a good solution, but not always the best one? Type one word.
WHY THIS EXERCISEHeuristics are how developers handle problems too big to check every possibility.

Big ideas today. Tomorrow you will find these algorithms far beyond the greenhouse.

← Wednesday