Fast to check is not fast to find.
Separate verification, exact search, heuristics, and unsupported impossibility claims.
01 Model the idea
Complexity classes describe decision problems under a computational model. P contains problems solvable in polynomial time; NP contains problems whose yes certificates can be verified in polynomial time. NP does not mean nonpolynomial, and P versus NP remains unresolved. NP-hardness describes a reduction-based relationship, not proof that every small instance is difficult or that no useful heuristic exists.
A valid certificate satisfies every stated requirement, not only its final cost.
02 State the strategy
Read the steps independently of syntax. Explain what each step preserves before translating it into code.
CHECK certificate shape
CHECK every vertex occurs exactly once before the return
CHECK every claimed edge03 Make it concrete
The implementation uses Python 3.11+ with only the standard library. Run it locally, or download the independent DSA workbench.
def valid_cycle(graph, route):
return (len(route) == len(graph) + 1 and route[0] == route[-1]
and set(route[:-1]) == set(graph)
and all(v in graph[u] for u, v in zip(route, route[1:])))
graph = {"A": {"B", "C"}, "B": {"A", "C"}, "C": {"A", "B"}}
print(valid_cycle(graph, ["A", "B", "C", "A"]))
print(valid_cycle(graph, ["A", "B", "A", "A"]))True False
- 1
The proposed route is a certificate. Checking its length, identities, and edges is much easier than enumerating all candidate tours.
- 2
This example assumes a nonempty graph whose adjacency containers are sets. Expected constant-time membership makes verification expected linear in the route size.
- 3
When exact search grows too quickly, change the problem size, exploit structure, use a justified approximation, or communicate a heuristic result honestly. A timeout does not establish an impossibility theorem.
04 Count the cost
Expected O(V) certificate check with set adjacency
O(V) identity-check sets
Bounds refer to the shown implementation and exclude printed output unless stated. n is input size; V and E are graph vertices and edges.
05 Your next move
Validate a proposed route’s edge conditions independently of its vertex-uniqueness condition.
Reveal one possible solution +
graph = {"A": {"B"}, "B": {"C"}, "C": {"A"}}
route = ["A", "B", "C", "A"]
print(all(v in graph[u] for u, v in zip(route, route[1:])))06 Check your reasoning
What does NP mean in this context?
Understand it. Then own it.
Can you explain the invariant, trace an edge case, and defend the cost model? Log the experiment when the reasoning makes sense.