Proofs: Uniform-cost search
These are the human proofs of Uniform-cost search, at full length.
Uniform-cost search returns a cheapest path, and it stops when costs are bounded below full
This proof shows: correctness, termination.
We use the pseudocode of the page. All action costs are non-negative. For a state , is the least cost of a path from to , and when cannot be reached. A pop is live if the node passes line L6, and a state is expanded when a live pop of one of its nodes reaches line L8. The cost of the path PATH is , because line L11 sets of a child to of its parent plus the cost of the action. So for each node.
Facts about the table. (R1) For each state , the nodes stored as reached, one after the other, have strictly decreasing values of , by the strict test of line L10. Each of them was pushed when it was stored (lines L11 and L12), or it is the root (lines L2 and L3). (R2) A popped node is live if and only if it is the node stored in reached at the time of the pop. If it is, then $n.g = $ reached. If it is not, a later node with a smaller replaced it, and line L6 skips . (R3) Each node is pushed once and popped at most once.
Lemma 1. At each live pop of a node , for each state with that was not expanded before this pop.
Proof. By induction on the live pops. Fix a live pop of and assume the lemma for all earlier live pops. Then each state expanded earlier was expanded at a live pop of a node with , since was not expanded before that pop. With we get .
Let be a state with that was not expanded before the pop of . Take a path of cost . Each prefix of is a cheapest path to its end, because a cheaper path to followed by the rest of would be cheaper than . So the prefix up to costs .
Case 1: was not expanded before the pop of . Then this is the first live pop. Each node pushed so far is the root, because nodes are pushed only during expansions. So is the root, .
Case 2: was expanded. The state was not. Let be the least index such that was not expanded before the pop of . Then , and was expanded, at a live pop with . During that expansion, line L9 computed for the child . After lines L10 to L12, reached, and by (R1) it stays so. It cannot be less than , because it is the cost of a path. So at the pop of , the node $r = $ reached has .
The node is still in the frontier just before the pop of , or . It was pushed, by (R1). If it had been popped earlier, it was the stored node at that time too, because a stored node is only replaced, never restored. So that pop was live by (R2), and would have been expanded, which is false. The node is popped as a minimum, so , where the last step uses that the costs of after are non-negative. If itself, the same inequality holds.
Corollary 2. Each state is expanded at most once, and at each live pop, .
Proof. Let be the node of the first live pop for a state . If was not expanded before, Lemma 1 with gives , and holds for each node, so . After this pop, no node can replace in reached, because by (R1) it would need , and no path is that cheap. So stays the stored node. By (R3) is not popped again, and by (R2) a later pop of another node for is stale. So has exactly one live pop.
Theorem 3 (soundness and optimality). If the search returns PATH and at line L7, then PATH is a path from to a goal with cost .
Proof. PATH follows parent pointers from , and each parent link is an action of SUCCESSORS, so it is a path from to , which is a goal by line L7, and its cost is . Let be any goal with . If was expanded before this pop, line L7 would have returned at that earlier live pop, since is a goal. So was not expanded before, and Lemma 1 gives . Hence . Since is the cost of a path to a goal, .
Theorem 4 (failure is correct). If the search returns failure at line L13, no goal can be reached.
Proof. Suppose that a goal is reachable, and take a path . The loop stops only when the frontier is empty. Then each node that was pushed was popped. For each state in reached, the node stored at the end was pushed by (R1) and popped, and at its pop it was the stored node, so the pop was live by (R2) and the state was expanded. The state is in reached. If is in reached, it was expanded, so was put in reached at that expansion (line L10 adds a state that is not in the table). By induction is in reached and was expanded. Line L7 runs before line L8, so the search returned at that live pop instead of failure.
Theorem 5 (termination). (a) If the branching factor is finite, each action cost is at least , and a goal is reachable, the search stops after finitely many steps and returns a path of cost . (b) If the space of reachable states is finite, the search stops for all non-negative costs.
Proof. Each node that is pushed is a distinct path from : a child is pushed only at the expansion of its parent node, a node is popped once, and so two pushed nodes with the same path would have two pushed parents with the same path, down to the root, which is pushed once.
(a) Let be a goal with ; it exists by the fact that a finite branching factor and costs at least make the minimum attained (see completeness and optimality of search, Fact 1). As long as no goal has been expanded, the proof of Lemma 1, case 2, gives a node in the frontier with (in case 1 the root has ). So each pop, live or stale, before the search returns has . A path with has at most actions, and with a finite branching factor there are finitely many such paths. So the number of pops before the return is finite, and each pop does finitely many steps. The loop cannot stop with failure, by Theorem 4. So it returns, and by Theorem 3 the cost is .
(b) Suppose a pushed node has a path that visits a state twice, at positions . When the prefix up to position was pushed or was stored, reached became at most the cost of that prefix, and by (R1) it never increased. The prefix up to costs at least as much, because the costs between and are non-negative. So the strict test of line L10 refused the prefix up to , and it was never pushed, a contradiction because each prefix of a pushed node was pushed. Hence each pushed node is a path without a repeated state. A finite space has finitely many such paths, so the loop runs finitely often.