Lesson 2 of 21 in this unit
Use an informed search.
Compare uniform-cost search and A* on a weighted map, audit estimates and reopen a location when a cheaper route arrives.
By the end: Trace g + h priorities, distinguish admissibility from consistency, and compare legal route cost with an independent optimum under a bounded search budget.
Prerequisites and path options
Why learn this? Use a heuristic to reduce search work while checking the assumptions needed for the result you claim.
Start with something concrete.
Two roads leave the delivery depot. The shorter first road might lead to an expensive detour. How can you compare complete trips before driving them?
Try the interactive modelA route reaches G using only legal roads. What does that establish?
Change one thing. Watch what follows.
Loading this browser experiment…
Understand what changed.
A shorter explanation, with the original worked examples available below. Both explain the same experiment and its limits.
The idea in everyday terms
A route with fewer roads is not always cheaper. Uniform-cost search prioritizes cost already paid. A* also uses a heuristic: an estimate of the remaining cost. A good estimate can focus work, but its assumptions matter.
What to watch in the experiment
Follow the frontier and compare legal routes using their total edge costs. Distinguish finding a goal from proving the cheapest route under the chosen algorithm and heuristic. An independent route checker verifies legality without supplying the search’s answer.
What this does not establish
An overoptimistic stopping claim or unsuitable heuristic can lose the desired guarantee. Budget exhaustion is not proof of impossibility, and this fixed map is development practice.
Open the full worked examples, calculations and limitations
This is the same reference used in Technical mode. Expand it whenever you need the exact numbers, assumptions or further explanation.
Compare complete trips
A delivery must travel from depot S to destination G. Each one-way road has a positive travel cost. The goal is the lowest total cost, not the smallest number of roads. A route with four inexpensive roads can beat one with three expensive roads.
The lab’s original map makes this concrete. S → A → C → G costs 2 + 2 + 2 = 6 units. S → B → D → C → G costs 1 + 2 + 0.5 + 2 = 5.5. Both routes are legal. An independent road checker can accept both while a cost comparison prefers the second. The positions in the drawing are for readability; they do not represent geographical distances or justify straight-line estimates.
If you enter here midway, recall three terms. A state is the current location. A successor is a location reached by one legal outgoing road. The frontier holds discovered locations whose next processing step is still pending. Search inspects alternatives before choosing a route; it is not the delivery itself driving down every inspected road.
Start with the cost you actually know
Uniform-cost search selects the frontier location with the smallest discovered cost from S. This accumulated cost is g(n), where n names a location. If g(B) = 1 and the road B → D costs 2, the candidate cost to D is 1 + 2 = 3.
This operation is called relaxing an edge: compare the candidate with the best cost stored for its destination. Store the candidate and its parent only when it is cheaper. If a location has not been discovered, any finite candidate is an improvement. Keeping one best record per location avoids storing every possible route in the frontier.
After expanding S, the uniform-cost frontier contains B at 1, X at 1 and A at 2. The fixed tie order chooses B before X. Predict this choice before pressing the step button. X is a dead end, but discovering that still requires selecting and expanding it. Inspecting no outgoing edges costs zero edge reads, yet it counts as an expansion.
Breadth-first search, introduced earlier, orders by the number of moves. It finds minimum-cost routes when each move has the same cost. Here the costs differ, so the number of moves and the total cost answer different questions.
Add an estimate of the remaining work
A heuristic h(n) estimates the cost still needed from n to G. A* orders the frontier by accumulated cost plus this estimate:
Read this as: the priority f at location n is the cost already discovered g plus the estimated remaining cost h. All three use the same travel-cost units. The estimate affects which location is considered next; it is never charged as an actual road cost.
For B under the consistent estimate, g = 1 and h = 4, giving f = 5. For A, g = 2 and h = 4, giving f = 6. A* examines B first. After B, D has g = 3 and h = 2, so f = 5. You can follow every addition in the frontier table. Uniform-cost search is the special case h = 0.
The lab supplies explicit lookup tables for h. They are teaching inputs, not learned predictions. More realistic heuristics can come from a simplified problem that removes constraints; a cheaper relaxed problem can provide a lower bound. Calculating an elaborate heuristic also has a cost. Fewer expanded locations alone does not prove a lower wall-clock time.
Two related checks on a heuristic
A heuristic is admissible when it never overestimates the true cheapest remaining cost. For this lesson all estimates are nonnegative and h(G) = 0. In the open map, the exact remaining cost from D is 0.5 + 2 = 2.5, so h(D) = 2 is admissible there. The unreachable X has no finite route cost; a finite estimate of 6 still does not overestimate infinity. Do not turn “unreachable” into zero.
Consistency is a local condition on every directed road:
Here u is the road’s starting location, v its destination, and c(u,v) its travel cost. Read it as: your remaining-cost estimate at u must not exceed the cost of that road plus your estimate at v. For D → C, the consistent table gives 2 ≤ 0.5 + 2. The audit lists any edge that violates this condition.
With h(G) = 0, applying these inequalities along a route makes consistency imply a lower bound on its cost. Admissibility alone does not require the estimates to fit smoothly together along every edge. The lab checks both properties against the entire tiny map instead of inferring them from one successful run.
The exact remaining costs come from a separate all-pairs dynamic-programming calculation. The search routine never reads those answers. They are affordable here because the graph has only seven locations; access to this teaching oracle is not an assumption that realistic search problems reveal their optimal solution in advance.
Why reopening matters
Choose the admissible-but-inconsistent table. Its estimate at A and C is zero, while B has h = 4.5 and D has h = 2.5. None exceeds the true remaining cost. However, D → C violates consistency because 2.5 is greater than 0.5 + 0.
Search first expands A at g = 2, then C at g = 4, discovering G at cost 6. Later it selects B and D. That route reaches C at g = 3.5. C has already been expanded, but its old route was more expensive. Reopening puts C back into the frontier with the improved cost, allowing G to improve from 6 to 5.5.
For finite graphs with nonnegative costs and an admissible heuristic with zero at the goal, A* can recover an optimal route by retaining best costs, reopening strict improvements, and stopping when the goal is selected. A graph-search variant that permanently ignores previously expanded locations needs a stronger condition such as consistency. The implementation choice is part of the guarantee; the name “A*” by itself does not tell you how repeat visits are handled.
A legal but more expensive answer
Now choose the overestimating table. It assigns B and D an estimate of 9. This makes the cheap branch look unattractive. A* selects A, C and then G, returning cost 6 before considering the cost-5.5 branch. The independent move checker accepts the returned route: all its roads exist.
The failure is about optimality, not feasibility. Overestimation removes this optimality guarantee; it does not imply every run must fail or every returned move is illegal. A learned heuristic would need the same distinction. Good predictions on a few maps do not establish a lower bound on all unseen maps.
The stopping rule matters too. G can be discovered through an expensive route before the frontier has ruled out cheaper ones. This lab stops when G is selected as the lowest-priority frontier entry, not when a road to G is first encountered. Even that stopping rule cannot repair an arbitrary overestimate.
Budget, evidence and a small project
An expansion selects a non-goal location and inspects its outgoing roads. The lab separately counts expansions, outgoing-edge records inspected, heuristic lookups, reopened locations and peak frontier size. Selecting the goal does not expand its successors, so it is allowed when the expansion budget has just been consumed. The route checker and exact oracle perform additional work outside the search counts. Sorting, rendering and other runtime overhead are also excluded.
At budget 1, the open map stops with alternatives still in the frontier. It has not established that G is unreachable. On the map with every incoming goal road closed, a sufficiently large budget lets the frontier empty. That establishes no route in this finite, completely specified graph. Neither result says anything about unmodelled real roads or a different environment.
For a guided comparison, capture the four priority rules on the same open map and explain two frontier selections. For an implementation project, reproduce the priority queue, improvement and reopening rules, then test a new weighted graph against a separately written exhaustive route enumerator. Include a legal but suboptimal result and a budget cutoff. For a decision brief, recommend a stopping rule and explain when you would return a route, request more computation or report uncertainty.
Exports contain up to four temporary runs and a bounded note. These are public practice experiments, not an independently assessed qualification. The lesson’s prediction and application questions provide feedback; human project review and learner pilots remain pending. Continue to the existing planning lessons through the course navigation, or return directly to any prerequisite without a completion lock.
An admissible but inconsistent estimate expands C at g = 4. Later a route reaches C at g = 3.5. What should this A* implementation do?
Take this idea with you.
A* prioritizes discovered cost plus estimated remaining cost. Estimates can reduce work, but their guarantees depend on admissibility, reopening and correct stopping rules. Feasibility, optimality and budget exhaustion are different claims.
Explore related topics and the topic map
How this topic connects
Explore this topic on the interactive mapUse these connections to review a foundation or choose where to go next. You can open any topic without completing the others.
What it builds on
- Search and procedures
Choose when to use exact search and recognize the difference between running out of time and proving there is no solution.
- Compute budgets
Compare model and hardware choices against a limited budget without confusing estimates with measured performance.
Compare uniform-cost search and A* on a weighted map, audit estimates and reopen a location when a cheaper route arrives.
What it enables
- Solve a constraint problem
Turn scheduling or assignment rules into explicit constraints and distinguish partial consistency from a complete solution.
- Reason about an opponent
Choose actions with another decision-maker’s possible responses in mind.