Sub topic 4
Combining Both: f(n) = g(n) + h(n)
This chapter assumes you know the two strategies from the previous chapters. Uniform-cost search expands the frontier node with the lowest path cost so far. Greedy best-first search expands the node that looks closest to the goal. It also assumes you are comfortable with priority queues and weighted graphs.
Each strategy has one strength and one weakness. Uniform-cost search is optimal, but it expands nodes in every direction, including away from the goal. Greedy search heads straight for the goal, but it ignores how much it has already paid, so it can commit to an expensive route. The evaluation function used by A* combines the two numbers so that each one covers the other's weakness.
The Three Quantities
For a node n on the frontier:
| Symbol | Meaning | Known or estimated? |
|---|---|---|
| g(n) | Cost of the best path found so far from the start to n | Known exactly for that path |
| h(n) | Heuristic estimate of the cheapest cost from n to the goal | Estimated |
| f(n) = g(n) + h(n) | Estimated cost of the cheapest complete solution that passes through n | Estimated |
The value f(n) answers one question: "If I commit to going through n, about how much will the whole trip cost?" A* always expands the frontier node with the smallest f.
Both Earlier Algorithms Are Special Cases
The formula contains both earlier algorithms:
- Set h(n) = 0 for every node. Then f(n) = g(n), and A* behaves exactly like uniform-cost search.
- Ignore g(n). Then the priority is h(n) alone, and you get greedy best-first search.
This means one implementation covers all three algorithms, and only the priority changes. It also explains how A* behaves. A larger, more accurate h pulls the search toward the goal. A smaller h makes it spread out more cautiously, like uniform-cost search.
Seeing the Orderings Differ
The script below takes a frontier of three nodes and ranks them under each priority rule.
frontier = {
"A": {"g": 2, "h": 6},
"B": {"g": 7, "h": 1},
"C": {"g": 4, "h": 3},
}
rules = {
"uniform-cost (g)": lambda v: v["g"],
"greedy (h)": lambda v: v["h"],
"A* (g + h)": lambda v: v["g"] + v["h"],
}
for name, key in rules.items():
order = sorted(frontier, key=lambda n: key(frontier[n]))
scores = [key(frontier[n]) for n in order]
print(f"{name:18} -> {list(zip(order, scores))}")Output:
uniform-cost (g) -> [('A', 2), ('C', 4), ('B', 7)]
greedy (h) -> [('B', 1), ('C', 3), ('A', 6)]
A* (g + h) -> [('C', 7), ('A', 8), ('B', 8)]Uniform-cost search picks A because it is cheap so far, even though A looks far from the goal. Greedy search picks B because it looks close, even though reaching it cost 7. A* picks C, which has the lowest estimated total.
Common Errors
- Mixing units. g and h must measure the same thing. If edge costs are seconds of travel time and h is a distance in meters, their sum is meaningless.
- Using a stale g. g(n) belongs to the best path found so far. When you find a cheaper path to n, you must update g(n), and therefore f(n).
- Treating f as a guarantee. f is an estimate. Whether ranking by f produces an optimal path depends on properties of h that later chapters cover.
Exercise
A frontier holds P (g = 3, h = 5), Q (g = 6, h = 1), and R (g = 1, h = 9). Which node does A* expand first? Which node would uniform-cost search expand first, and which would greedy best-first search expand first?
Show answer
The f values are P = 8, Q = 7, and R = 10, so A* expands Q. Uniform-cost search picks the lowest g, which is R (1). Greedy search picks the lowest h, which is Q (1).
Exercise
You run A* with h(n) = 0 for every node. Which algorithm are you running, and what would change if h were replaced by a better estimate?
Show answer
You are running uniform-cost search (Dijkstra's algorithm), because f(n) = g(n). A better estimate pulls expansion toward the goal, so the search usually expands fewer nodes.