Skip to lesson content
navan✳THE BUILD LABDSA
0/66 complete
BUILD LAB / DSA/ Algorithm design patterns
EXPERIMENT 56 / 66

Fast to check is not fast to find.

Separate verification, exact search, heuristics, and unsupported impossibility claims.

BY NAVAN SAMI◷ 7 MIN READSTRETCHCONCEPT

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.

THE THING THAT MUST STAY TRUE

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.

the_strategy.txt
CHECK certificate shape
CHECK every vertex occurs exactly once before the return
CHECK every claimed edge

03 Make it concrete

The implementation uses Python 3.11+ with only the standard library. Run it locally, or download the independent DSA workbench.

hard_problems.py
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"]))
↳ EXPECTED OUTPUT
True
False
  1. 1

    The proposed route is a certificate. Checking its length, identities, and edges is much easier than enumerating all candidate tours.

  2. 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. 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

TIME

Expected O(V) certificate check with set adjacency

SPACE

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

A SMALL MISSION

Validate a proposed route’s edge conditions independently of its vertex-uniqueness condition.

Reveal one possible solution +
one_possible_solution.py
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

QUICK SIGNAL CHECK

What does NP mean in this context?

ANOTHER PATTERN IN YOUR TOOLKIT.

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.

Built with curiosity. By Navan Sami. ↗Patterns over panic. One step at a time.