Proofs: Bidirectional search
These are the human proofs of Bidirectional search, at full length.
Bidirectional breadth-first search with complete layers returns a shortest path full
This proof shows: correctness, termination.
We use the pseudocode of the page, with unit action costs. Write for the least number of actions from to , for the least number from to (each is when there is no path), and . Write and for the key sets of parent_f and parent_b. We assume ; otherwise line L1 returns the path with 0 actions.
Invariant 1 (exact layers). Each time line L4 tests the loop condition, there are integers with
and for each other than , is a state with and a successor of . The same holds for the backward side with predecessors.
Proof. At the start, , and . Assume the invariant and let the loop expand the forward side without returning. Lines L7 to L12 add to exactly the successors of states with that were not in . A successor of a state with has , and if then . Each state with has a predecessor on a shortest path with , so it is found. So after line L13, and is the new layer, with the parents as claimed. The backward case is the same with predecessors.
Invariant 2 (disjoint tables). Until the search returns, .
Proof. At the start . A state enters or only at line L10, and line L11 returns at once if it is already in the other table.
Lemma 3 (no meeting means a long path). If with and as in Invariant 1, then .
Proof. Suppose , and let be a shortest path. Let . Then and , because when and when . So , a contradiction.
Theorem 4 (a returned path is shortest). If the search returns a path, it goes from to and has exactly actions.
Proof. Let the return happen at line L11, while the forward side expands its layer , at the state . Then has just entered with , and , so . JOIN follows the parents in from back to , a path of actions by Invariant 1, and the parents in from to , a path of actions. Together they form a path from to with at most actions. Before this layer, the tables were disjoint by Invariant 2, so by Lemma 3. The path has at least actions, so it has exactly . The backward case is the same with the roles of the sides exchanged.
Theorem 5 (failure and termination). If can be reached from , the search returns a path after at most layer expansions. If it returns failure, cannot be reached from . If the graph is finite, the search stops.
Proof. Each expansion that does not return adds one to or to , so after such expansions . By Invariant 2 and Lemma 3, at each test of line L4, so when is finite at most expansions end without a return. A frontier that is not empty contains states at distance from (or to ), and when is reachable from neither frontier is empty: a shortest path from to has a state at each distance , and for the state at distance lies in , and likewise for the backward side. So the loop keeps expanding until it returns, within expansions, and by Theorem 4 with a shortest path.
If the search returns failure at line L14, one frontier is empty, say . By Invariant 1 no state has , so no state has , and is the set of all states reachable from . If were reachable, then , while always, against Invariant 2. The backward case is the same.
If the graph is finite, each expansion that does not return adds a non-empty set of new states to or to , or leaves a frontier empty, which ends the loop. The tables are finite, so the loop stops.