Unit 8 · Lesson 4

The Limits of Algorithms

Are there problems that no algorithm can ever solve in a reasonable amount of time?

FRQ

From Last Lesson

What is the difference between a reasonable and an unreasonable time algorithm?

Activity — Explore Paths

How many paths can you find?

You live in house 1 and want to visit all three friends, then return home. You can only visit each house once.

Click your house to start a path.
Unique Paths Found: 0
Activity

Here are three of those paths.

1 2 3 4
1 → 2 → 3 → 4 → 1
Best: ?
1 2 3 4
1 → 2 → 4 → 3 → 1
Best: ?
1 2 3 4
1 → 3 → 2 → 4 → 1
Best: ?

Think: What information would you need to figure out which of these paths is the best?

Activity

Distance makes the difference.

Here are the same three paths, now with distances labeled on each leg.

1 1 1 1 1 2 3 4
1 → 2 → 3 → 4 → 1
Total Distance: 4 ✓ shortest
1 2 1 2 1 2 3 4
1 → 2 → 4 → 3 → 1
Total Distance: 6
2 1 2 1 1 2 3 4
1 → 3 → 2 → 4 → 1
Total Distance: 6

The algorithm must check every possible path and compare distances to guarantee finding the shortest one.

What if you had a lot more friends to visit?

With 4 houses, there were 6 paths to check. That was manageable.

But what happens as the number of houses grows? How many paths would you need to check if you had 10 friends? 20? 100?

Think About It

Each time you add one more house, how does the number of possible paths change? As you add more, does the number grow slowly or quickly?

The Traveling Salesman Problem

A famous optimization problem.

A traveling salesman must visit a set of cities exactly once and return home. The goal is to find the shortest possible route. Unlike our activity, the salesman can start from any city.

The numbers grow quickly!

With just 10 cities there are over 3.6 million paths to check.

With 20 cities it is over 2.4 quintillion. Checking 1 billion possible paths a second would take over 76 years.

Cities (n)Paths to Check
11
22
36
424
5120
6720
75,040
840,320
9362,880
103,628,800
Factorial — n!

Where does the number of paths come from?

At each step, count how many unvisited cities you can choose from. Multiply all those counts together.

Pick a starting city: n options

Next city (haven't visited it yet): n−1 options

Next city: n−2 options

…continuing until only 1 city remains

n × (n−1) × (n−2) × … × 1 = n!

Example: 4 cities, any start

4 cities → pick start, then 3 unvisited, then 2, then 1:

4 × 3 × 2 × 1 = 24

7 cities → pick start, then 6, 5, 4, 3, 2, 1:

7 × 6 × 5 × 4 × 3 × 2 × 1 = 5,040

10 cities:

10 × 9 × 8 × … × 1 = 3,628,800

That is a lot of possible paths for only 10 cities, and the numbers only get more extreme from there.

Types of Problems

Not all problems are the same kind.

A problem is any task that may or may not be solved with an algorithm. Sorting a list is a problem. Sorting the specific list (2, 3, 1, 7) is an instance of that problem.

Important types of problems in computer science:

Decision Problems

The answer is simply yes or no.

"Is there a path that visits all cities in under 100 km?"

Optimization Problems

The answer is the best possible value or solution.

"What is the most valuable set of items that fits in a suitcase?"

What type is the traveling salesman problem?

The Traveling Salesman Problem

It can be solved, but not in reasonable time.

The Traveling Salesman Problem can be solved with an algorithm that checks every possible option. The problem is not that we lack an algorithm: the problem is that the algorithm runs in unreasonable time.

As the number of nodes (locations to visit) increases, the number of paths grows factorially. Even the most powerful computers cannot finish for large inputs.

Key Takeaway

The Traveling Salesman Problem is an optimization problem that requires unreasonable time to solve exactly. Most practical instances of the problem cannot be solved with a guaranteed-optimal answer in a useful amount of time.

The Solution

Heuristics

A heuristic provides a "good enough" solution to a problem when an exact solution is impractical or impossible.

Rather than spending unreasonable time finding the perfect answer, a heuristic finds an answer that is close enough to be useful in a fraction of the time.

Activity — Traveling Salesman

Try to find the shortest path.

Do This

Move down to open the Traveling Salesman activity. Start on Level 1 and try to find the best path to visit all nodes and return home. Write down a plan or heuristic for how you choose which node to visit next.

Your heuristic may not always find the absolute best path, but it should consistently find a good one.

Note: A heuristic is a strategy or rule of thumb, not a guaranteed-optimal algorithm. Write yours as a simple rule you can apply at each step.

Move Down to Open Activity
Activity — Traveling Salesman · Level 1

The Traveling Salesman activity will open here when launched by your instructor.

Open Activity
Activity — Test Your Heuristic

Put your heuristic to the test.

Do This

Move down to open the Traveling Salesman activity. Apply your heuristic on at least three different levels. For each level, record:

  • The distance your heuristic found.
  • The best distance you can find by trying other paths (brute force).
  • Whether you think your heuristic should be updated.
Move Down to Open Activity
Activity — Traveling Salesman · Test Your Heuristic

The Traveling Salesman activity will open here when launched by your instructor.

Open Activity
FRQ — Reflection

Reflect on your heuristic.

How did you create your heuristic? Did you change it after testing it out on different levels?

Think About

What rule did you follow at each step? Did your rule always work, or did it sometimes lead you to a longer path?

Updating a Heuristic

A good heuristic can be refined through testing. If you noticed it consistently failing in certain situations, what rule might fix it?

Sample Heuristic

Nearest Neighbor

The Rule

At each node, travel to the next closest unvisited node. Repeat until all nodes are visited, then return home.

This strategy is fast to apply and often produces a short path. It is a classic example of a greedy heuristic: always make the locally best choice.

Limitation: Nearest neighbor can sometimes miss a shorter overall path because a locally close node leads to a very long leg later. It is "good enough" but not guaranteed optimal.

Move Down to Try It

Example: apply nearest neighbor

H A B C D E

Start at H. Each step, go to the nearest unvisited node.

Activity — Try Nearest Neighbor

The Traveling Salesman activity will open here when launched by your instructor.

Open Activity
Takeaways

What We Learned About TSP

Optimization Problem

The Traveling Salesman Problem is an optimization problem: we are trying to find the best path, not just any path.

Unreasonable Time

There is no known algorithm that solves the TSP in reasonable time. The exact solution requires checking every possible path, which grows factorially and becomes impractical very quickly.

Heuristics to the Rescue

We use a heuristic to find a solution that is "good enough" for most instances of the problem, in a fraction of the time an exact solution would require.

Types of Problems

There is a third category.

Decision Problems

The answer is yes or no.

"Is there a path shorter than 100 km?"

Optimization Problems

The answer is the best possible value.

"What is the shortest path?"

Undecidable Problems

No algorithm can ever guarantee a correct yes-or-no answer for all instances.

"Will this program eventually stop running?"

Undecidable problems are not just hard: it has been mathematically proven that no algorithm exists that can solve them correctly in all cases.

Video — The Halting Problem

An Undecidable Problem

Watch for: what makes the Halting Problem provably impossible to solve for all programs.

Takeaways

Some problems are provably unsolvable.

There are problems we have mathematically proven that no computer will ever be able to solve for all inputs. The Halting Problem is the most famous example.

The Halting Problem
Given any program and any input, determine whether the program will eventually stop or run forever. Alan Turing proved in 1936 that no algorithm can solve this for all possible programs. It is the classic undecidable problem.
Why It Matters
The existence of undecidable problems tells us there are hard theoretical limits to what computers can do, regardless of how fast or powerful they become. Speed is not the issue: the problem is unsolvable in principle.
Wrap Up — Key Vocabulary
Decision Problem
A problem whose answer is simply yes or no. For example: "Is there a route visiting all cities in under 100 km?"
Optimization Problem
A problem whose answer is the best possible value among all candidates. For example: "What is the shortest route visiting all cities?"
Undecidable Problem
A problem for which no algorithm can be constructed that always gives a correct yes-or-no answer for every possible input. These are not just hard problems: they are provably beyond the reach of any algorithm that could ever exist.
The Traveling Salesman Problem
An optimization problem asking for the shortest route visiting a set of nodes exactly once and returning to the start. Solving it exactly requires checking all possible paths, which grows factorially with the number of nodes.
Heuristic
A strategy that provides a "good enough" solution to a problem when an exact solution is impractical or impossible. Heuristics trade the guarantee of optimality for speed, finding solutions that are close to the best in far less time. The nearest-neighbor rule is a classic example.