Proofs: Best-first search
These are the human proofs of Best-first search, at full length.
Best-first search is sound and stops on finite spaces, and with f = g + h for an admissible h it returns a cheapest path full
This proof shows: correctness, termination.
We use the pseudocode of the page, with action costs . A node is live while it is the node stored in reached for its state. Line L6 skips each popped node that is not live. A node is expanded when it passes line L6 and line L7 and reaches line L8.
Fact 1 (each node is a path). Each node is the end of a chain of parent pointers that spells a path of the state space, with the sum of the costs of its actions and . This holds for the root of line L1, and line L9 adds one action of cost to the path of the parent, to and to the depth.
Fact 2 (stored costs decrease). For each state , the value only decreases strictly over time, because line L11 overwrites the entry only after line L10 found a strictly smaller . A state never leaves reached.
Fact 3 (live nodes). Each node in the chain of parents of a pushed node was pushed, at line L2 or line L12. A live node is in the frontier or was expanded: it was pushed when it became live, and a live node that is popped is either returned at line L7 or expanded.
Theorem 1 (soundness). If the search returns , then is a path from to a goal state and is its cost.
Proof. The search returns only at line L7, with the path of a node whose state passes IS-GOAL, and . By Fact 1, is a path from to with cost .
Lemma 2 (no repeated state). Each node pushed at line L12 is a path without a repeated state. Two pushed nodes never have the same path.
Proof. Suppose a pushed node has the path with for some . Let and be the nodes of the prefixes that end at positions and . By Fact 3 both were pushed, and was pushed before was created, since descends from . From the push of on, by Fact 2. The cycle from position to position has cost at least , so when was created. Then the test at line L10 failed, and was not pushed, a contradiction. If two pushed nodes had the same path, they would have the same , and the second would fail the test at line L10 in the same way.
Theorem 3 (finite spaces, any ). If finitely many states can be reached from , the search stops for every evaluation function . If it returns failure, then no goal state can be reached from .
Proof. A finite space has finitely many paths from without a repeated state. By Lemma 2, each of them is pushed at most once and no other path is pushed. So the number of pushes is finite. Each iteration pops a node, so the loop stops.
Suppose the search returns failure at line L13, so the frontier is empty. By Fact 3, the live node of each state in reached was expanded. Let be a path to a goal state . We show by induction that each is in reached. The state is in reached from line L3. If is in reached, its live node was expanded, so line L10 was run for a child with state , and after it was in reached, which by Fact 2 it never leaves. So is in reached, and its live node was expanded. But a live node of a goal state is returned at line L7 and never expanded. This contradiction shows that no goal is reachable.
Lemma 4 (a good node in the frontier). Let , where is admissible: for each state , with the least cost of a path from to a goal. Let be a path from to a goal state with cost . At the start of each iteration of line L4, if the search has not yet returned, the frontier holds a live node with .
Proof. Write for the cost of the prefix . Call covered when is in reached and . By Fact 2, a covered state stays covered. The state is covered, since its root node has . Let be the largest index such that are all covered, and let be the live node of , so .
If and was expanded, then line L10 was run for a child with state and , and afterwards . Then is covered, against the choice of . If , then is a live node of a goal state, and it was not expanded, by line L7. So in both cases was not expanded, and by Fact 3 it is in the frontier. The rest of is a path from to a goal with cost , so , and
Theorem 5 (optimality for ). Let with admissible. If the search returns , then , the infimum of the costs of the paths from to a goal.
Proof. Let the search return the node at line L7. By Theorem 1, is the cost of a path to a goal, so . Since is at a goal, , so . Take any path from to a goal, with cost . In the iteration that pops , Lemma 4 gives a node in the frontier with , and POP-MIN returned , so . This holds for each such , so . Hence .
With , Theorem 5 is the optimality of uniform-cost search.
Theorem 6 (infinite spaces for ). Let with admissible, let each state have finitely many successors, let each action cost be at least , and let some goal be reachable from . Then the search stops and returns a path of cost .
Proof. Fix a path to a goal, with cost . By Lemma 4, until the search returns, the frontier is not empty, and POP-MIN returns a node with . Each node has , so each popped node has depth at most . With finite branching there are finitely many paths from of at most this depth, and by Lemma 2 each is pushed at most once. So only finitely many pops are possible, and the loop cannot end at line L13, because the frontier is not empty. So the search returns, and by Theorem 5 the cost is .
Remark (other ). Theorems 1 and 3 hold for every . Theorems 5 and 6 need with admissible. For , greedy best-first search, they fail. Let have two children: a state with , whose child is a goal, and a state that starts an infinite chain with no goal and . All costs are 1. Each has , so the search pops and never pops . It runs for ever, while a goal is reachable.