How Google Maps Works
Explore the core algorithms and data structures Google Maps uses to calculate fast routes, from road graphs and weighted edges to contraction hierarchies and…
In depth
Google Maps transforms the complex physical world into a solvable mathematical problem to provide instant routing. It achieves this by modeling roads as a network and employing advanced algorithms to navigate it efficiently.
Road Graphs: Nodes and Edges
At its foundation, Google Maps represents the entire road network as a graph. Every intersection is a "node," and the roads connecting these intersections are "edges." This graph structure allows the system to define relationships and pathways between locations.
Weighted Edges for Travel Time
Not all roads are equal in terms of travel time. Each edge in the road graph is assigned a "weight," which represents the time it typically takes to traverse that segment. A highway, even if physically longer, will have a lower weight (faster travel time) than a congested local street, reflecting real-world conditions.
Contraction Hierarchies for Scalability
A standard graph search algorithm like Dijkstra's, when applied to a global road network with billions of nodes and edges, would be computationally prohibitive. To overcome this, Google Maps uses a technique called Contraction Hierarchies. This method precomputes and simplifies the map by creating layers. It prioritizes major roads and highways, effectively creating shortcuts that bypass the need to consider every local street for long-distance routes.
Hierarchical Route Calculation
When calculating a route, the algorithm starts at your origin and quickly finds the fastest path to exit the local road layer and ascend to a higher layer of major roads. Once on these major roads, it uses the precomputed shortcuts, efficiently traversing long distances without evaluating every minor intersection. As the destination approaches, the search descends back into the local road layer to guide you through the final few turns.
FUNCTION FindRoute(start_node, end_node):
path = []
current_node = start_node
// Ascend to higher hierarchy (major roads)
WHILE current_node IS NOT on_major_road:
current_node = FindFastestExitToMajorRoad(current_node)
path.ADD(current_node)
// Traverse major roads using precomputed shortcuts
WHILE current_node IS NOT near_destination_major_road:
current_node = UseShortcutToNextMajorHub(current_node)
path.ADD(current_node)
// Descend to local hierarchy (local roads)
WHILE current_node IS NOT end_node:
current_node = FindFastestLocalPathToDestination(current_node)
path.ADD(current_node)
RETURN pathReal-time Traffic Updates
The physical world is dynamic. Google Maps continuously incorporates real-time traffic data, primarily from anonymous speed information sent by millions of active phones. If a segment of road experiences a slowdown, its corresponding edge weight in the graph is instantly increased. This allows the routing engine to recalculate and offer alternative paths to avoid congestion, ensuring the fastest possible route at any given moment.
Key takeaways
- Roads are modeled as a graph where intersections are nodes and roads are weighted edges representing travel time.
- Contraction Hierarchies simplify the global map into layers, enabling efficient long-distance routing.
- The routing algorithm prioritizes major roads and uses precomputed shortcuts for speed.
- Real-time traffic data, sourced from anonymous user pings, dynamically updates edge weights.
- This allows Google Maps to recalculate routes instantly to avoid congestion.
Got a different question? SeaThru generates a fresh video for any topic where systems talk or data structures move.
Ask your own question →