Skip to the workspace
Pebellslearn by building
Hard Problems & Adversarial Search  ›  Lesson

Hard to Find, Easy to Check

P, NP & Verification 30 minFinding the cheapest tour needs every permutation; checking one finished tour needs none of that

dsa / Hard to Find, Easy to Check

Explore each scene at your own pace.

01 · Count what brute force actually tries

Count what brute force actually tries

Hard to Find, Easy to Check

Definition
P is the class of problems solvable in time that grows as a polynomial of the input size, and NP is the class whose proposed solutions can be checked in polynomial time, even when no fast way to find a solution is known.
In plain words
Finding the cheapest tour through many stops may mean trying a staggering number of orders. Checking a tour someone hands you is quick: add up the distances and confirm every stop appears once. Finding is hard; checking is easy.

In short: every algorithm this course has taught so far runs in time that is a polynomial in its input size — that's the class P. Finding the cheapest tour through a handful of stops isn't known to have any such algorithm: the best general method is still brute force over every ordering, and the number of orderings explodes factorially. But checking a tour someone else hands you — does it visit every stop once, does it cost what they claim — takes barely any work at all. That gap, between finding a solution and checking one, is what the class NP names, and it's not a philosophical point: you can measure it on your own laptop.

Two groups of bars at n=4, n=8 and n=12: permutations_tried in red climbs to 39,916,800 at n=12, while verify_checks in green barely rises off the baseline to 13

Count what brute force actually tries

Meridian's bid desk gets a tender with a handful of stops and needs the cheapest loop that visits every one of them and returns to the start. There's no shortcut known: try every ordering, score each one, keep the best.

step through it
1import itertools
2 
3def bid_desk_tsp(cities, edges):
4 dist = {}
5 for a, b, cost in edges:
6 dist[(a, b)] = cost
7 dist[(b, a)] = cost
8 start, rest = cities[0], cities[1:]
9 best_cost, tried = None, 0
10 for perm in itertools.permutations(rest):
11 route = [start] + list(perm)
12 cost = sum(dist[(route[i], route[i + 1])] for i in range(len(route) - 1))
13 cost += dist[(route[-1], start)]
14 tried += 1
15 if best_cost is None or cost < best_cost:
16 best_cost = cost
17 return best_cost, tried
18 
19cities = ["central", "harbor", "mill", "north"]
20edges = [["central", "harbor", 4], ["central", "mill", 7], ["central", "north", 9],
21 ["harbor", "mill", 2], ["harbor", "north", 6], ["mill", "north", 3]]
22print(bid_desk_tsp(cities, edges))

Four stops means 3! = 6 orderings — nothing. The start city never moves, so what grows is (n - 1)!, the orderings of everyone else. This is never about how fast your machine is: doubling the clock speed buys you maybe one more stop before the wait becomes a problem again, because the count itself is growing factorially, not the time per step.

What does this print?
import math

def permutations_to_try(n):
    return math.factorial(n - 1) if n >= 1 else 1

print([permutations_to_try(n) for n in (4, 8, 12)])
Scene 1 of 5
Project context and further resources
You're building a piece ofMeridian — the bid desk
This piece — bid_desk_tsp(): Finds the cheapest route through every stop on a tender by brute force, and shows why checking a submitted route is nothing like as expensive.
Scenario A tender lands on Meridian's bid desk with a dozen stops that have to be served on one route. Finding the cheapest route means trying orderings; checking whether a route a bidder has already proposed is any good means nothing of the sort.
bid_desk_tsp.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
bid_desk_tsp(cities, edges) → dictThe cheapest full loop through every city, how much it costs, and how many permutations brute force had to try.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.