Video in progress

In a logistics platform, a route must be returned within 50 ms on a graph with millions of road nodes. If Dijkstra isn't enough, which approaches do you consider?

On a city-scale graph, plain Dijkstra looks at hundreds of thousands of nodes and is slow. A* uses a heuristic to steer the search toward the goal. The real speedup comes from precomputation: Contraction Hierarchies or similar methods bring a query down to a millisecond, but they make fast-changing weights like traffic harder.

📖 Answer

I compare the options by query speed, precomputation cost and weight updates.

Plain Dijkstra has no precomputation and works with any weights. But it spreads from the source in a circle: if the target is 20 km away, it looks at everything within a 20 km radius.

Bidirectional Dijkstra, with two small circles, visits about half as many nodes. It's a cheap improvement.

In A*, the priority is g(u) + h(u), where h is a lower bound to the target. Haversine distance / maximum speed is an admissible heuristic, so the result stays optimal. With a weak heuristic, the gain is 2-5x.

ALT (A*, landmarks, triangle inequality): by precomputing distances to up to 16 landmarks, you get a much stronger lower bound. Moderate complexity, good speedup.

Contraction Hierarchies. We order nodes by "importance", remove the less important ones and add shortcut edges in their place. A query is a bidirectional search that goes only "upward" and looks at a few hundred nodes. Even at continental scale it takes milliseconds. The cost: precomputation takes minutes to hours, and it has to be redone when weights change. OSRM and GraphHopper rely on this method.

Customizable CH or multi-level partitioning computes the topology once and recomputes the weights (traffic) quickly, in a few seconds. If you need real-time traffic, this is the direction.

This is how I usually decide:

  1. We don't write it ourselves. GraphHopper (Java, Apache 2.0) or OSRM is battle-tested and gives CH/LM ready-made.
  2. I find out how often traffic updates: once a day → CH is enough; every 5 minutes → a customizable variant or A* with LM.
  3. "Shortest" isn't enough: turn restrictions, height/weight limits for trucks, time windows. These go into the graph model and complicate some precomputation methods.
  4. Often you need a matrix, not a single route (100 couriers × 300 addresses); this is input data for a VRP problem. For a matrix, dedicated "one-to-many" queries are much cheaper than route-by-route queries.

Memory: the European road graph with CH is tens of GB. How to scale this service and how to update it (blue-green, switching once the new graph is loaded) matters as much as the algorithm.