Algorithm Search⌘K

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 ds(v)d_s(v) for the least number of actions from ss to vv, dt(v)d_t(v) for the least number from vv to tt (each is ∞\infty when there is no path), and d=ds(t)d = d_s(t). Write FF and BB for the key sets of parent_f and parent_b. We assume s≠ts \ne t; otherwise line L1 returns the path [s][s] with 0 actions.

Invariant 1 (exact layers). Each time line L4 tests the loop condition, there are integers a,β≥0a, \beta \ge 0 with

F={v:ds(v)≤a},frontf={v:ds(v)=a},B={v:dt(v)≤β},frontb={v:dt(v)=β},F = \{ v : d_s(v) \le a \}, \quad \mathrm{front}_f = \{ v : d_s(v) = a \}, \quad B = \{ v : d_t(v) \le \beta \}, \quad \mathrm{front}_b = \{ v : d_t(v) = \beta \},

and for each v∈Fv \in F other than ss, parentf[v]\mathrm{parent}_f[v] is a state uu with ds(u)=ds(v)−1d_s(u) = d_s(v) - 1 and vv a successor of uu. The same holds for the backward side with predecessors.

Proof. At the start, a=β=0a = \beta = 0, F=frontf={s}F = \mathrm{front}_f = \{s\} and B=frontb={t}B = \mathrm{front}_b = \{t\}. Assume the invariant and let the loop expand the forward side without returning. Lines L7 to L12 add to FF exactly the successors of states with ds=ad_s = a that were not in FF. A successor vv of a state with ds=ad_s = a has ds(v)≤a+1d_s(v) \le a + 1, and if v∉Fv \notin F then ds(v)=a+1d_s(v) = a + 1. Each state with ds(v)=a+1d_s(v) = a + 1 has a predecessor on a shortest path with ds=ad_s = a, so it is found. So after line L13, F={v:ds(v)≤a+1}F = \{ v : d_s(v) \le a + 1 \} and frontf\mathrm{front}_f is the new layer, with the parents as claimed. The backward case is the same with predecessors. □\square

Invariant 2 (disjoint tables). Until the search returns, F∩B=∅F \cap B = \emptyset.

Proof. At the start F∩B={s}∩{t}=∅F \cap B = \{s\} \cap \{t\} = \emptyset. A state enters FF or BB only at line L10, and line L11 returns at once if it is already in the other table. □\square

Lemma 3 (no meeting means a long path). If F∩B=∅F \cap B = \emptyset with aa and β\beta as in Invariant 1, then d≥a+β+1d \ge a + \beta + 1.

Proof. Suppose d≤a+βd \le a + \beta, and let s=x0,x1,…,xd=ts = x_0, x_1, \ldots, x_d = t be a shortest path. Let i=min⁡(a,d)i = \min(a, d). Then ds(xi)=i≤ad_s(x_i) = i \le a and dt(xi)=d−i≤βd_t(x_i) = d - i \le \beta, because d−a≤βd - a \le \beta when i=ai = a and d−i=0d - i = 0 when i=di = d. So xi∈F∩Bx_i \in F \cap B, a contradiction. □\square

Theorem 4 (a returned path is shortest). If the search returns a path, it goes from ss to tt and has exactly dd actions.

Proof. Let the return happen at line L11, while the forward side expands its layer aa, at the state vv. Then vv has just entered FF with ds(v)=a+1d_s(v) = a + 1, and v∈Bv \in B, so dt(v)≤βd_t(v) \le \beta. JOIN follows the parents in FF from vv back to ss, a path of ds(v)d_s(v) actions by Invariant 1, and the parents in BB from vv to tt, a path of dt(v)d_t(v) actions. Together they form a path from ss to tt with at most a+1+βa + 1 + \beta actions. Before this layer, the tables were disjoint by Invariant 2, so d≥a+β+1d \ge a + \beta + 1 by Lemma 3. The path has at least dd actions, so it has exactly dd. The backward case is the same with the roles of the sides exchanged. □\square

Theorem 5 (failure and termination). If tt can be reached from ss, the search returns a path after at most dd layer expansions. If it returns failure, tt cannot be reached from ss. If the graph is finite, the search stops.

Proof. Each expansion that does not return adds one to aa or to β\beta, so after kk such expansions a+β=ka + \beta = k. By Invariant 2 and Lemma 3, k+1≤dk + 1 \le d at each test of line L4, so when dd is finite at most d−1d - 1 expansions end without a return. A frontier that is not empty contains states at distance aa from ss (or β\beta to tt), and when tt is reachable from ss neither frontier is empty: a shortest path from ss to tt has a state at each distance i≤di \le d, and for a<da < d the state at distance aa lies in frontf\mathrm{front}_f, and likewise for the backward side. So the loop keeps expanding until it returns, within dd expansions, and by Theorem 4 with a shortest path.

If the search returns failure at line L14, one frontier is empty, say frontf\mathrm{front}_f. By Invariant 1 no state has ds=ad_s = a, so no state has ds>ad_s > a, and FF is the set of all states reachable from ss. If tt were reachable, then t∈Ft \in F, while t∈Bt \in B 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 FF or to BB, or leaves a frontier empty, which ends the loop. The tables are finite, so the loop stops. ■\blacksquare

Back to Bidirectional search