Artificial Intelligence and Machine Learning · Lecture 04 of 16

Search algorithms · Part 2

Heuristics and informed search

Use estimates to guide exploration, then examine when the resulting search is complete or finds a cheapest route.

01Heuristics and greedy search
02A* and the conditions behind its guarantees
03Branch-and-bound and choosing a strategy
Search · Heuristics and bounds
03 · Informed search

Give the search a useful estimate

The frontier still holds possible paths.
A heuristic changes which one looks promising.

h(n) ≈ remaining cost from n to a goal
n is the current state; h(n) is its estimated least cost still to pay.
03 · Informed search

A heuristic estimates the work still ahead

hvG : V → ℝ
vG is the fixed goal vertex; V the set of states; ℝ real-valued costs.
The subscript fixes the goal; h(n) evaluates the estimate at state n.
  • h(n) estimates the cheapest remaining cost.
  • It must use information available at n.
  • It should be cheaper to compute than solving the problem.
  • A relaxed problem often gives a useful lower bound.

G is the graph, V its states, and E its legal directed edges.
In G = (V, E, ωV, ωE), ωV = h assigns estimates to states; ωE assigns actual costs to edges. Unit edge costs use ωE = 1.

Straight-line distance is a lower bound for route distance, using the same units.

Search · Heuristics and bounds

A heuristic must estimate the same quantity as g

g is cost already paid; h estimates cost still ahead. Distance, time and money are different objectives.

If costs are kilometres

Straight-line distance to the nearest valid goal is a lower bound on route distance.

If costs are minutes

Kilometres are not minutes.
Convert using a valid bound on travel speed.

htime = straight-line distance / maximum possible speedhtime estimates remaining travel time.
12 km ÷ 60 km/hour = 0.2 hours = 12 minutes, assuming no speed exceeds 60 km/hour.

Use the same units in g + h. The assumptions behind the conversion are part of the guarantee.

Search · Heuristics and bounds

Make the problem easier to get a lower bound

Grid: horizontal or vertical unit-cost moves. Δx and Δy are horizontal and vertical displacement in grid squares; |…| takes the absolute value.

The direct two-step route crosses a blocked cell; a legal route around it takes four stepsblockedSGIgnore the obstacle: 2 · obey it: 4

Original problem

Avoid the blocked square.
Best legal route: 4 moves.

Relaxed problem

Ignore obstacles.
Manhattan distance: |Δx| + |Δy| = 2.

h = 2 is the relaxed estimate; h* = 4 is the true least remaining cost. Removing constraints while preserving costs gives h ≤ h*.

03 · Informed search

Greedy best-first: follow the smallest estimate

A global frontier priority
p ← arg minq ∈ F h(end(q))
  • F is the frontier; q a waiting path; end(q) its last state.
  • h estimates remaining cost. arg min chooses a path; ← assigns it to p.
  • Prefer the endpoint that looks closest.
  • Ignore the cost already paid.
Fast-looking can be expensive

A tiny remaining estimate says nothing about how much the route has already cost.

Greedy search has no general optimality guarantee.

Search · Heuristics and bounds

Even a safe heuristic can mislead greedy search

A, B, C and G label states; G is the goal. h estimates remaining cost. B looks closer than C, but its complete route costs more.

Greedy chooses B with heuristic zero and returns cost ten; the route through C costs fivecost 1cost 9cost 4cost 1AstartBh = 0Ch = 1Gh = 0

Greedy · compare h only

Choose B (h = 0), then G (h = 0).
Returned cost: 1 + 9 = 10.

The cheaper alternative

A → C → G costs 4 + 1 = 5.
Both h(B) = 0 and h(C) = 1 are admissible.

Admissibility does not make greedy optimal. Its selection rule still ignores the cost already paid.

Interactive lab · Informed search

Greedy best-first · use h alone

After E: F has h = 2; its new rivals K and L have h = 8 and 7. Which waiting path is selected?
h estimates remaining cost; returned solution costs sum the supplied edge weights.

03 · Informed search

Heuristic DFS: order siblings, then go deep

Local ordering
h(end(p₁)) ≥ … ≥ h(end(pₘ))
  • p₁, …, pₘ are the m new paths; i indexes them; end(pᵢ) is the endpoint. Keep a LIFO frontier.
  • Push p₁, …, pₘ in this order: largest h first.
  • Pop pₘ next: smallest h is on top.
  • Explore the chosen subtree before older alternatives.
Compare with greedy search

Greedy: choose the best endpoint across the entire frontier.

Heuristic DFS: order the newest siblings, then follow the stack.

It retains DFS’s memory advantage and its lack of a general optimality guarantee.

Search · Heuristics and bounds

Same waiting paths, different next choice

Suppose one older path is waiting (h = 2). Expanding the current path adds two new child paths (h = 7 and h = 8).
h estimates remaining cost: smaller looks better.

Greedy: compare all waiting paths

Whole frontier, shown from smallest h to largest

Older pathh = 2New childh = 7New childh = 8

Next: the older path (h = 2).

It has the smallest h across the whole frontier.
New and older paths compete together.

Heuristic DFS: take the stack’s top

Stack order: top (next) on the left → bottom on the right

New childh = 7New childh = 8Older pathh = 2

Next: the new child with h = 7.

Push the h = 8 child first, then the h = 7 child.
Both sit above the older path, despite its lower h.

The difference: greedy uses h for every selection; heuristic DFS uses h to order new siblings, then follows the stack.

Interactive lab · Informed search

Heuristic DFS · local priority, deep commitment

Apply the rule to this tree: after E, why are L and K tried before the older F?
h orders new siblings; returned solution costs sum the supplied edge weights.

03 · Informed search

A*: combine the cost paid and the cost ahead

g(p)

Actual cost so far

+
h(end(p))

Estimated remaining cost

=
f(p)

Estimated total cost

p ← arg minq ∈ F f(q)
p is a path; end(p) its last state; F the frontier; q a waiting path.
arg min chooses the minimum-score path; ← assigns that choice to p.

Select the frontier path with the smallest f. With h = 0, A* becomes lowest-cost-first.

03 · Informed search

The first A* decision

g is this path’s cost; h its endpoint’s remaining estimate; f = g + h its total score.
Initially f(⟨A⟩) = 0 + 5 = 5. After expanding A:

Pathg · paidh · estimatef = g + h
A → B123 ← select
A → C246
A → D437

After B: E has f = 3 + 1 = 4; F has f = 7 + 2 = 9; G has f = 4 + 9 = 13.

Search · Heuristics and bounds

A* can leave a branch without finishing it

Course tree: expand A, then B, then E. Compare all waiting paths again.

Endpoint / pathg · paidh · remainingf = g + h
C · A → C246 ← next
D · A → D437
F · A → B → F729
K · A → B → E → K4812
L · A → B → E → L5712
G · A → B → G4913

E’s new children do not receive priority just because they are new. A* selects the smallest total estimate.

Interactive lab · Informed search

A* · inspect g, h and f together

Why does A* return to C before F? Toggle h = 0 and compare the order.
g: path cost · h: remaining estimate · f = g + h: total estimate. Returned costs use the supplied edge weights.

Search · Heuristic requirements

Two properties to check for A* heuristics

h estimates the cheapest cost still needed to reach a goal. At a goal, use h = 0.
These two properties answer different questions about that estimate.

Admissibility

Does the estimate ever guess too high?

It must never exceed the true cheapest remaining cost. Guessing too low is allowed.

If the cheapest remaining cost is 6:
h = 4 is safe; h = 8 is too high.

Why it matters: supports finding a cheapest solution, provided A* keeps better routes available.

Consistency

Do neighboring estimates agree?

After a move, the estimate must not drop by more than the cost of that move.

If a move costs 1:
3 → 2 passes; 5 → 2 drops too much.

Why it matters: with correct cost updates, graph A* only needs to expand each state once.

Consistency is stronger: with h = 0 at goals, consistency also gives admissibility.
An admissible heuristic can be inconsistent; graph A* may then need to expand a state again.

These are properties of h. Selecting the smallest score, testing goals on selection, and keeping cheaper routes are rules of the algorithm.

Search · Heuristics and guarantees

Admissible means “never overestimate”

h is estimated remaining cost; h* (“h star”) is the true cheapest remaining cost.

The rule

0 ≤ h ≤ h*

At every state.
At a goal: h = 0.

Which estimates are admissible when the true remaining cost is 6? Bars show estimates 4, 6 and 8 against a dashed line at the true remaining cost of 6. Four is a safe underestimate, six is exact and safe, and eight is too high. True cost: h* = 6 h = 4 Safe h = 6 Exact · safe h = 8 Too high 02468

The true remaining cost is 6. Estimates of 4 and 6 are admissible because they do not exceed 6; 8 is too high.
Why it matters: admissibility keeps estimates optimistic, so A* does not overlook a cheaper solution.

Search · Heuristics and guarantees

Why the first selected goal is cheapest

f = g + h estimates total cost: g is cost paid; h estimates cost still ahead.

A cheaper possible route gets selected before a goal costing 10 A found goal has total cost and score 10. Suppose another solution costs 8. A waiting path on that route has paid cost 3 and true remaining cost 5. With an admissible estimate h at most 5, its score f is at most 8, so A* selects it before the goal costing 10. A solid line is a discovered route; the dashed line is the hypothetical continuation. Beneath the nodes, the admissible heuristic bounds are h≤8 at the start, h≤5 at the waiting path endpoint, and h=0 at both goals. Start Found goal: cost 10 Goal f = 10 g = 3 paid SELECT FIRST Path f = 3 + h ≤ 8 5 truly remainsnot yet explored Goal Suppose cost 8 h ≤ 8h = 0h ≤ 5h = 0

A goal costing 10 has been found, but a waiting path could lead to a solution costing 8.
Its estimated total is at most 8, so A* explores it before selecting the more expensive goal.

Assume admissible h and no better route discarded. Stop when a goal is selected, not just found.

Search · Heuristics and guarantees

A tighter estimate can save unnecessary exploration

Two paths are waiting: a goal costing 7 and an unfinished path with g = 3 already paid.
Its true cheapest remaining cost is 6, so the cheapest completion through that path costs 9.

Low estimate: h = 2

h estimates remaining cost; f = g + h estimates total cost.

Waiting pathf
Unfinished path3 + 2 = 5 ← next
Goal7

Explore the unfinished path first.
Its estimate makes it look cheaper than 7, although it can only finish at 9 or more.

Tighter estimate: h = 5

Still safe: 5 does not exceed the true remaining cost of 6.

Waiting pathf
Unfinished path3 + 5 = 8
Goal7 ← next

Select the goal costing 7 and stop.
A* avoids expanding the unfinished path in this example.

Tighter means closer to the true remaining cost, while still never overestimating. It makes fewer expensive routes look cheap.

Computing h also takes time. A better estimate can save search work without guaranteeing a faster run in every problem.

Search · Heuristics and guarantees

Consistency: check how the estimate changes in one move

If a move costs 1, the estimated remaining cost may drop by at most 1.
Check this rule on every directed edge, from the state before the move (u) to the state after it (v).

Passes this edge

uh(u) = 3
move costs 1→
vh(v) = 2

The estimate falls from 3 to 2: a drop of 1, matching the move cost.

3 ≤ 1 + 2 · consistent on this edge

Fails this edge

uh(u) = 5
move costs 1→
vh(v) = 2

The estimate falls from 5 to 2: a drop of 3, although the move costs only 1.

5 > 1 + 2 · inconsistent on this edge

h(u) ≤ c(u,v) + h(v)h estimates remaining cost; c(u,v) is the cost of the move u → v.
Read it as: “the estimate here ≤ one move’s cost + the estimate after that move.”

Consistency is stronger than admissibility: passing every edge, with h(goal) = 0, guarantees no overestimation. An admissible heuristic can still fail an edge check.

Search · Heuristics and guarantees

With consistency, the total estimate f cannot go down

g is cost paid, h estimates remaining cost, and f = g + h estimates total cost.
Follow one path for a move costing 1: g rises from 4 to 5. What happens to h and f?

Consistent on this edge

QuantityBeforeAfter
g · cost paid45
h · remaining estimate32
f = g + h77

g rises by 1; h falls by 1.
They cancel out: f stays at 7.

Inconsistent on this edge

QuantityBeforeAfter
g · cost paid45
h · remaining estimate52
f = g + h97

g rises by 1; h falls by 3.
The drop is too large: f falls from 9 to 7.

Why f cannot fall: consistency limits the drop in h to the move cost; g increases by that same cost. Their sum must stay the same or increase.

Why this helps graph A*: with a consistent h and correct cost updates, the first selected route to a state has its cheapest g. After expanding the state, mark it closed (already expanded): it will not need to be reopened.

Search · Worked example

Same state, cheaper route: update the record

g is cost paid from the start; h estimates remaining cost. Two routes to the same state can have different g.

Two routes to the same state X have costs 10 and 4Two complete arrivals from the start end at the same state X. The old route costs 10; the new route costs 4. Keep the new route and update the best cost.SStartXSame stateOld route: g = 10New route: g = 4

Best known cost: 10 → 4

If X is still waiting

Update its cost and remember the cheaper route.

If X was already expanded

With an inconsistent h, reopen it: expand it again using the cheaper route.

Reaching X before does not make every new arrival useless. Keep the cheaper route, so its lower cost can improve the solution.

These rules assume nonnegative edge costs. Negative costs need different reasoning.

Search · Worked example

Reopen a state when a cheaper route arrives

g is cost paid; h estimates remaining cost; f = g + h is the total estimate.
Here h is admissible but inconsistent.

Reopening X improves the goal from cost 6 to 5Start A to X costs 3, A to B costs 1, B to X costs 1 and X to goal G costs 3. Estimates are A 0, B 4, X 0, G 0. X is first expanded at g 3, then reopened at g 2 after B finds a cheaper route.3113Ah = 0 · startBh = 4Xh = 0Gh = 0 · goal

Reopen = expand X again

1 · X is selected first

X has f = 3; B has f = 5.
Discover goal G at cost 6.

2 · B finds a cheaper route to X

Cost to X: 3 → 2.

3 · Reopen X

Goal cost improves: 6 → 5.

Refusing to expand X again leaves the cost-6 solution. Reopening lets the cheaper route reach the goal, giving cost 5.

The edge B → X fails consistency: h(B) = 4 > 1 + h(X) = 1.

Search · Worked example

Negative costs can change which route is cheapest

UCS (uniform-cost search) selects the smallest g, the cost already paid. A negative edge can make that cost fall later.

One negative edge can break the stopping rule

One negative edge defeats ordinary UCS stoppingA is the start and G the goal. The direct route costs 2. A to B costs 5 and B to G costs minus 10, so the route through B costs minus 5. UCS selects the direct goal at 2 before B at 5.25−10AStartBGGoalVia B: 5 − 10 = −5

A negative loop can remove any minimum

A repeatable negative loop has no finite optimumFrom start A go to X at cost zero, loop at X for a net cost of minus 1, then reach goal G at cost 2. Every loop lowers the goal cost: 2, 1, 0, minus 1 and so on.02Loop costs −1AStartXGGoalGoal cost: 2 → 1 → 0 → −1 → …

UCS selects the direct goal at 2 before B at 5, but the route via B costs −5. A repeatable negative loop on the way to a goal lowers cost forever: no cheapest solution exists.

Avoiding cycles does not restore ordinary UCS or A* guarantees for negative edges.

03 · Informed search

Branch-and-bound: improve, then prune

  1. B is the pruning bound; initially B = ∞ (infinity: no finite bound).
  2. Explore paths depth first.
  3. p is a path; end(p) its endpoint.
    Prune if its lower bound g(p) + h(end(p)) ≥ B.
  4. At a better goal, save its path and set B = g(p).
  5. Continue until no candidate remains.
What makes pruning safe
  • The estimate must be a lower bound.
  • The incumbent is the best solution found so far.
  • The first solution is not necessarily the best.
  • DFS saves frontier space; safe pruning alone does not guarantee termination.
Search · Read the mathematics

The bound changes; the depth-first loop continues

Use nonnegative costs and 0 ≤ h(v) ≤ h*(v); h* is true least remaining cost.
F: frontier stack · p: popped path · vₛ: start · vG: goal · v: successor · E: legal edges.

F ← stack(⟨vₛ⟩); B ← ∞; best ← none
while F ≠ ∅:
    p ← pop(F)
    if g(p) + h(end(p)) ≥ B: continue
    if end(p) = vG:
        best ← p; B ← g(p)
    else:
        for v with (end(p), v) ∈ E:
            if v is not in p: push(F, p ⊕ v)
return best

Reject an unpromising path

g: path cost; h: remaining estimate.
g + h ≥ B: skip this path.
continue starts the next loop iteration.

Save evidence of improvement

best holds a complete route.
B holds its cost.
A goal updates both; it does not stop the search.

pop removes the latest path; push adds one. p ⊕ v appends v; ∈ means “in”; ← assigns; ∅ means empty.
Check path cycles; return best when the stack is empty. ∞ means infinity; none means no saved route.

Search · Heuristics and bounds

A better solution tightens the pruning bound

Three-exit lab: rightmost-first DFS. g is path cost, h estimated remainder, and B the current incumbent’s cost.

Eventg + hCurrent bound BDecision
Reach G3 through E12 + 0 = 12∞ → 12Save the first solution.
Select C2 + 4 = 612Explore: 6 < 12.
Select D4 + 2 = 612Explore: 6 < 12.
Reach G26 + 0 = 612 → 6Save the improved solution.
Select B1 + 7 = 86Prune: 8 ≥ 6.

The incumbent is a complete solution; g + h is a lower bound for an unfinished path. Comparing them makes pruning safe.

Interactive lab · Informed search

Branch-and-bound · watch the incumbent improve

Rightmost-first DFS improves the saved cost from 12 to 6, then prunes state B.
g: path cost · h: estimated remainder · f = g + h: lower bound. The pruning bound is the saved route’s cost.

03 · Informed search

The frontier rule explains the algorithm

AlgorithmContainer / orderInformation used
BFS / DFSQueue / stackInsertion order
IDSRepeated depth-limited DFSDepth bound
Lowest-cost-firstMinimum gCost already paid
Greedy best-firstMinimum hEstimated remaining cost
Heuristic DFSStack; order new siblings by hLocal heuristic guidance
A*Minimum g + hEstimated total cost
Branch-and-boundDFS; prune against an incumbentLower bounds and best solution
Putting it together

Choose from the problem’s requirements

Fewest steps · equal positive costs

Try BFS when its frontier fits in memory.
Try IDS when depth is unknown and memory is tight.

Weighted costs · no useful h

Use lowest-cost-first with nonnegative costs and correct duplicates. Check its termination assumptions.

Weighted costs · useful lower bound

Use A* when you can afford its frontier.
Check admissibility, goal tests on selection, and reopening when needed.

Memory tight · optimization needed

Consider depth-first branch-and-bound.
Safe lower bounds, early saved solutions and termination conditions matter.

Check your understanding

Predict first, then use the labs

  1. Set goal B in the course tree. Why does DFS do more work than BFS?
  2. In the three-exit lab, can BFS return a path costing 9 when a path costing 6 exists?
  3. After A and B are expanded, why does A* choose E before F?
  4. In branch-and-bound, what happens if the initial strict bound is 6?

Name the selection or pruning rule. For questions 1–2, use Part 1’s BFS, DFS and exit-comparison labs; 3–4 use this part’s A* and branch-and-bound labs.

Check your understanding

Explain the behavior using the frontier

01 · Stack order

DFS explores D and the C subtree before B. BFS selects B immediately after A.

02 · Edge count ≠ total cost

BFS finds A → B → G1: two edges, cost 9. A → C → D → G2 has three edges, cost 6.

03 · Compare total estimates

E has f = 3 + 1 = 4.
F has f = 7 + 2 = 9.
E wins the next comparison.

04 · Strict bound

The lab searches only for costs below 6. It finds none; this does not mean that no solution exists.

Lecture 04 · Takeaways

Search is controlled exploration

  • Model the right states, actions and costs.
  • Keep track of paths waiting on the frontier.
  • Select using order, cost, heuristic — or a combination.
  • Match guarantees to their assumptions.
  • Use the demos to explain what happens next.

Next time you see a search algorithm, ask: what does it keep, and what does it select?

Sources & further reading

Continue exploring

Primary course resource

AIML 2025/26 · Lesson 01 — Search

Content sequence, six original interactive examples, course-tree topology, edge costs and heuristic values.

Textbook reference

Poole & Mackworth · Artificial Intelligence: Foundations of Computational Agents, 3rd ed. · Chapter 3

Search strategies, heuristic conditions and pruning.

Adaptation notes, complete slide notes and source mapping are included in the notes folder.
The interactive slides run locally; the gallery and PDF are static review copies.

Lecture 04 · Wrap-up

Before you leave this part

  • Explain what a heuristic estimates.
  • Compare greedy search with the cost-plus-estimate rule in A*.
  • Use a valid bound to explain why a branch can be pruned.

Choose one idea and explain it in your own words. Use an example or an interactive experiment to support your explanation.

Continue learning
AIML · Lecture 04 · Search algorithms · Part 2
Course home