How bidirectional BFS meets in the middle of a path

Short answer: Bidirectional breadth-first search grows one frontier from the source and one from the target on an unweighted graph. A shortest path has some meeting vertex, and the two searches together expand about O(b^{d/2}) vertices instead of O(b^d) when every vertex has about b unused edges and the shortest path has d edges. The recorded answer is the best sum of a forward distance and a backward distance at a meeting vertex.


What the problem actually asks

A one-sided BFS toward a distant target explores a ball of radius d. Searching from both ends explores two balls of radius about d/2, which is dramatically smaller at large branching factors. The path is the concatenation of a forward shortest path and a backward shortest path at a vertex both searches have reached.

The first collision is not automatically the minimum sum unless the searches expand in an order that respects distance. A forward vertex at distance i and a backward vertex at distance j meet at total i+j. A collision at a large i and a tiny j can be worse than a later collision at two moderate distances. The stopping rule has to account for that.


What the algorithm guarantees

On an unweighted undirected graph, or a directed graph when the backward search walks reversed edges, the minimum of distF[v]+distB[v] over vertices reached by both sides is the shortest-path length. Expanding the side whose next vertex has the smaller distance, and closing the search when the sum of the two sides’ next distances is at least the best meeting already found, returns that minimum after O(b^{d/2}) expansions in the uniform-branching model. Extra memory is two distance arrays and two queues.

Weighted graphs need bidirectional Dijkstra, with the same meeting logic on tentative distances and with nonnegative weights. Plain bidirectional BFS on unequal weights returns a minimum-hop meeting, which may be an expensive path.


The working steps

  1. Initialize a forward queue with the source at distance 0 and a backward queue with the target at distance 0. If source equals target, the distance is 0.
  2. Always expand one vertex from the side whose front distance is smaller.
  3. When an edge from the expanded vertex lands on a vertex the other side has already reached, consider dist_this + 1 + dist_other as a candidate answer and keep the minimum.
  4. Stop when either queue is empty or the sum of the two front distances is at least the best candidate. Further expansions cannot beat it on unit weights.
  5. Recover the path by walking forward parents to the meeting vertex and backward parents to the target.

The square-root improvement is for unit weights and a single target. It is not a faster all-pairs algorithm, and it is not correct for unequal weights unless the expansion order is Dijkstra’s. Both searches must see the same graph, with the backward search using reversed arcs when the graph is directed.


Approaches that look simpler and fail

Approach Why it fails when the first collision is returned immediately
Return the first vertex generated by both sides That vertex may sit at distances whose sum is not minimal. Another meeting vertex, still inside the current layers, can be shorter.
Run both searches to completion with no stop rule The answer becomes correct and the search has expanded far more than the meeting required. The sum-of-fronts test is the stop.
Walk backward along outgoing edges in a directed graph The reverse path has to follow incoming edges. Otherwise the backward distances are distances in the wrong graph.
Use it for weighted road networks Unit-weight meetings minimize hops. Bidirectional Dijkstra minimizes weight, with a heap on each side.

One-source unweighted distances belong to ordinary BFS. Weighted bidirectional search is bidirectional Dijkstra. A memory-tight unweighted search that does not store both frontiers is iterative deepening.


A working example

This Python fragment searches from both ends. The answer is the best sum of a forward distance and a backward distance, which on this graph is the length of the shortest path. It prints 2.

from collections import deque
adj = {0: [1, 4], 1: [0, 2], 2: [1, 3], 3: [2, 4], 4: [0, 3]}

def bfs(src):
    dist = {src: 0}
    q = deque([src])
    order = [src]
    while q:
        u = q.popleft()
        for v in adj[u]:
            if v not in dist:
                dist[v] = dist[u] + 1
                q.append(v)
                order.append(v)
    return dist

df, db = bfs(0), bfs(3)
best = min(df[v] + db[v] for v in df if v in db)
assert best == 2
print(best)

Where it is used

Bidirectional breadth-first search is how an unweighted route or a puzzle solver searches from the start and the goal at once. Social-distance queries on an unweighted graph use the same meeting point. A weighted road still wants Dijkstra or A*.


When you can drop this algorithm

Drop bidirectional BFS when there is no single target, or when the edges are weighted. Keep it for one unweighted path in a large graph with a high branching factor, where two balls of radius d/2 fit and one ball of radius d does not.


Why this stays published

We publish this so a search for bidirectional BFS returns the meeting-sum rule and the reason the first shared vertex is not enough. Bidirectional BFS finds an unweighted shortest path by meeting in the middle, and the answer is the best sum of forward and backward distances rather than the first collision.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *