learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Algorithm Selection

Select an algorithm and generate steps to begin visualization

Bidirectional Dijkstra Visualizer

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

Time: O((V + E) log V)
Space: O(V)
Use Case: Route planners and navigation, point-to-point shortest paths in road networks, the base of contraction hierarchies
Showing the example graph for this algorithm.
Algorithm Execution

Select an algorithm and generate steps to begin visualization

About Bidirectional Dijkstra

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.

How it works

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.

Applications

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.

Pseudocode

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 arc

The 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.

Worked example, step by step

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.

  1. Turns 1 and 2. Forward settles F (0) and labels A 2, G 1, K 3. Backward settles O (0) and labels J 2, N 3, T 3.
  2. Turns 3 to 6. Forward settles G (1), backward J (2), forward A (2), backward N (3). N labels I 4 and S 4 from the target side.
  3. First meeting. Forward settles H (2) and relaxes H–I. I is labelled from both sides, so the path F, G, H, I, N, O of length 2 + 1 + 4 = 7 is a candidate: mu = 7.
  4. Stop. Backward settles T (3). The smallest keys are now 3 and 4, and 3 + 4 = 7 is not below mu, so no shorter path can exist.

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.

Complexity, and where it comes from

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.

When to use Bidirectional Dijkstra, and when not to

Other shortest path methods for one start and one target.

AlternativePrefer it whenCost
DijkstraOne 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 BFSThe graph is unweighted. The same meeting idea with plain queues and a simpler stopping test.O(V + E)

Common pitfalls

  • Stopping at the first common vertex. The first vertex settled by both searches need not lie on the shortest path. Keep mu over every arc that links the two searches and stop only when min(Qf) + min(Qb) ≥ mu.
  • Forgetting to reverse the edges. On a directed graph the backward search must follow arcs into each vertex, not out of it. Following forward arcs from the target computes distances from t instead of to t.
  • Negative weights. Like Dijkstra, both searches assume that a settled distance is final, which fails with negative edges. Use Bellman–Ford or Johnson's reweighting first.

Frequently asked questions

How much faster is it than Dijkstra?
On road networks it settles roughly half as many vertices, a factor of about 2. On networks where neighbourhoods grow very fast, such as social graphs, the factor can reach the hundreds. On this page it settles 8 vertices instead of 20.
Which side should move next?
Any order is correct with the right stopping test. Taking the side with the smaller key, as here, keeps both radii equal. Taking the side with the smaller queue balances the work, which helps when one end lies in a dense area.
How do route planners go further?
They precompute. Contraction hierarchies add shortcut edges and run a bidirectional search that only climbs the hierarchy, settling a few hundred vertices on a continental road network. ALT adds landmark lower bounds to a bidirectional A*.

Related algorithms: Dijkstra's Algorithm, Bidirectional BFS, A* Search Algorithm, Yen's K Shortest Paths, Δ-Stepping (Parallel Shortest Paths)

Built by Mohammed Islam HadjoudjWork with me

An example graph: you can change it, or start blank.

Zoom Controls

31%
Nodes: 20
Edges: 31