Guest
Sign in to keep your graphs and progress.
Select an algorithm and generate steps to begin visualization
Two half-size searches instead of one big one
Runs Dijkstra from the start and, along reversed edges, from the target, taking turns, and stops once the two smallest queue keys add up to the best path found
Select an algorithm and generate steps to begin visualization
Bidirectional Dijkstra finds the shortest path between one start and one target by running two Dijkstra searches at once: a forward search from the start and a backward search from the target along reversed edges. The searches take turns, and once they have both labelled a vertex, a candidate path exists. The trick is knowing when to stop: the first vertex settled by both searches is not always on the shortest path. The correct rule keeps the best path found so far, mu, and stops when the smallest keys of the two queues add up to at least mu. On a road network a search grows like a disc, so two discs of half the radius cover about half the area of one large disc, which is where the speed-up comes from.
Each turn settles the next vertex of one search, the one with the smaller key, and relaxes its arcs: forward along the edges, backward against them. Whenever an arc reaches a vertex that the other search has already labelled, the path through that arc is a candidate, and mu keeps the shortest one. The visualizer colours the forward search blue and the backward search orange, writes both distances in the badges and draws the best meeting edge. On the 5 × 4 grid on this page the searches from F and O meet on the edge H–I at length 7, and the next settled vertices show that the queue keys add up to 3 + 4 = 7, which is at least mu, so the search stops. The path F, G, H, I, N, O is shortest, and only 8 vertices were settled, against 20 for a single Dijkstra from F.
Every route planner answers point-to-point queries, so bidirectional search is everywhere in navigation. It is the base of the fast road network methods: contraction hierarchies run a bidirectional Dijkstra that only goes up a hierarchy of shortcuts, and ALT and arc flags add guidance on top of it. It answers travel time queries in logistics, network latency queries in routing, and word ladder style puzzles where the target is known. On graphs with no geometry, such as social networks, the same idea finds the degree of separation between two people. It only pays off when the target is known in advance, so for one source and all targets plain Dijkstra stays the right choice.
Two Dijkstra searches take turns. The stopping test compares the queue keys with the best path found so far.
BidirectionalDijkstra(G, s, t):
df(s) = 0, db(t) = 0, every other distance = infinity
Qf = {s}, Qb = {t}, mu = infinity
while Qf and Qb are not empty:
if min(Qf) + min(Qb) >= mu: stop // mu is now the answer
take the side with the smaller key and settle its vertex u
for each arc (u, v) of that side (reversed for the backward side):
relax v with d(u) + w(u, v)
if the other side has labelled v:
mu = min(mu, d(u) + w(u, v) + d_other(v))
return mu and the path through the best meeting arcThe stopping test is what makes it correct. A path shorter than mu would have to pass a vertex that neither search has settled, so it would be at least min(Qf) + min(Qb) long. Stopping at the first vertex settled by both sides looks natural but can return a path that is too long.
The weighted grid loaded on this page, from F to O.
Example graph: A 5 × 4 grid with columns A to E, F to J, K to O and P to T, edges between horizontal and vertical neighbours, weights 1 to 4. 20 vertices, 31 edges.
Shortest path F → G → H → I → N → O, length 7. The two searches settled 8 vertices, while one Dijkstra from F would settle all 20 before reaching O.
Time: O((V + E) log V) with a binary heap · Space: O(V)
Each side is an ordinary Dijkstra, so the worst case equals one Dijkstra. The gain is in the typical case. On a road network the settled region of a search is roughly a disc of radius r with about r² vertices, and two discs of radius r / 2 hold about half as many. Where balls grow exponentially, as in social networks, the gain is much larger, because 2·b^(r/2) is far smaller than b^r.
Other shortest path methods for one start and one target.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Dijkstra | One source and many or all targets, or the target is not known in advance. | O((V + E) log V) |
| A* | Coordinates give a lower bound on the remaining distance. A* steers one search toward the target, and both ideas combine in bidirectional A* and ALT. | O((V + E) log V), fewer vertices in practice |
| Bidirectional BFS | The graph is unweighted. The same meeting idea with plain queues and a simpler stopping test. | O(V + E) |
Related algorithms: Dijkstra's Algorithm, Bidirectional BFS, A* Search Algorithm, Yen's K Shortest Paths, Δ-Stepping (Parallel Shortest Paths)
Built by Mohammed Islam Hadjoudj · Work with me