Contraction Hierarchies
Contraction Hierarchies (CH) is an exact routing algorithm that transforms a road network graph into a multi-layer hierarchical structure via node contraction. By iteratively removing less important vertices and adding 'shortcut' edges to preserve shortest path distances, CH reduces point-to-point shortest path queries from linear complexity relative to graph size down to microseconds.
HOW-TO IMPLEMENT & CONFIGURE
- Modern C++17 compiler (GCC 9+, Clang 10+, MSVC 2019+)
- CMake 3.15 or higher
- Standard Template Library (STL) vector, priority_queue, and adjacency list representations
Architectural Setup & Graph Representation
Initialize memory-efficient adjacency arrays for directed, weighted graphs using Compressed Sparse Row (CSR) format or vector of vectors with edge weights and identification attributes.
struct Edge {
int target;
int weight;
bool is_shortcut;
};
using Graph = std::vector<std::vector<Edge>>;
Core Algorithm: Node Contraction & Shortcut Injection
Implement the contraction phase. For each node, compute its importance metric (e.g., edge difference, shortcut count, level), simulate contraction, and insert necessary shortcuts if witness searches fail.
#include <vector>
#include <queue>
#include <limits>
const int INF = std::numeric_limits<int>::max();
void contract_node(int u, Graph& g, std::vector<int>& ranks, int rank_counter) {
ranks[u] = rank_counter;
// Collect incoming neighbors v -> u and outgoing u -> w
// Run local witness searches to verify if shortcuts (v, w) are required
}
Verification & Bidirectional Query Engine
Build a modified bidirectional Dijkstra search operating strictly on upward edges (from lower rank to higher rank) to compute point-to-point shortest paths.
int ch_query(int source, int target, const Graph& g, const std::vector<int>& ranks) {
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<>> forward_pq;
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<>> backward_pq;
// Standard CH bidirectional search implementation traversing only edges to higher ranks
return -1; // Return minimal distance
}
Production Hardening & Unpacking Paths
Ensure proper recovery of actual node paths from shortcut edges during query expansion. Handle memory alignment and pre-sort adjacency lists for CPU cache efficiency.
struct PathNode {
int node_id;
int original_edge_reference;
};
// Implement recursive shortcut unpacking to yield full spatial route coordinates.
Mathematical Foundations & Node Ordering
Contraction Hierarchies relies on defining a total ordering of nodes V = {v_1, v_2, …, v_n} based on an importance heuristic. The contraction of a node u involves removing u from the graph and adding a directed shortcut edge (v, w) with weight w(v, u) + w(u, w) if and only if u lies on the unique shortest path between v and w. To avoid combinatorial explosion of shortcuts, a witness search is performed: Dijkstra's algorithm is run from v to w restricted to a maximum distance and excluding u. If the witness search finds a path from v to w shorter than or equal to the potential shortcut weight, the shortcut is omitted.
Bidirectional Search Mechanics
Queries on a contracted graph leverage a hierarchical bi-directed search. The forward search starts at the source node s and only traverses outgoing edges to nodes with strictly higher contraction ranks. Simultaneously, the backward search starts at the target node t and only traverses incoming edges originating from nodes with higher contraction ranks. The stopping condition occurs when the tentative shortest path distance is less than or equal to the sum of the minimum distance keys at the top of both priority queues, intersecting at nodes of maximum rank.
Complexity Analysis
Preprocessing time complexity is O(n log n + m) on typical road networks when using lazy priority updates for node ordering heuristics. Space complexity is O(n + m + s), where s represents the number of generated shortcuts, typically bounded by 0.2m to 2.0m depending on network topology. Query time complexity drops drastically from O(m log n) down to O(k log k) where k is the dramatically smaller number of settled nodes in the hierarchical overlay graph.
📚 STANDARDS & ACADEMIC RFC CITATIONS
- Geisberger, R., Sanders, P., Schultes, D., & Delling, C. (2008). Contraction Hierarchies: Faster and Simpler Hierarchical Routing in Road Networks. Experimental Algorithms, 319-333.
- Sanders, P., & Schultes, D. (2005). Engineering Fast Route Planning Algorithms. Operations Research/Computer Science Interfaces Series.
