ArticleslgStudy

computer science

Contraction hierarchies

Contraction hierarchies is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Contraction hierarchies rather than just read about it. In short: In computer science, the method of contraction hierarchies is a speed-up technique for finding the shortest path in a graph. The most intuitive applications are car-navigation systems: a user wants to drive from A to B using the quickest possible route.

Contraction hierarchies — main illustration
Contraction hierarchies — illustration

Key takeaways

  • Contraction hierarchies belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Contraction hierarchies to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Contraction hierarchies from memory before moving on to harder problems.

Reference excerpt

In computer science, the method of contraction hierarchies is a speed-up technique for finding the shortest path in a graph. The most intuitive applications are car-navigation systems: a user wants to drive from A to B using the quickest possible route. The metric optimized here is the travel time. Intersections are represented by vertices, the road sections connecting them by edges. The edge weights represent the time it takes to drive along this segment of the road. A path from A to B is a sequence of edges (road sections); the shortest path is the one with the minimal sum of edge weights among all possible paths. The shortest path in a graph can be computed using Dijkstra's algorithm but, given that road networks consist of tens of millions of vertices, this is impractical. Contraction hierarchies is a speed-up method optimized to exploit properties of graphs representing road networks. The speed-up is achieved by creating shortcuts in a preprocessing phase which are then used during a shortest-path query to skip over "unimportant" vertices. This is based on the observation that road networks are highly hierarchical. Some intersections, for example highway junctions, are "more important" and higher up in the hierarchy than for example a junction leading into a dead end. Shortcuts can be used to save the precomputed distance between two important junctions such that the algorithm doesn't have to consider the full path between these junctions at query time. Contraction hierarchies do not know about which roads humans consider "important" (e.g. highways), but they are provided with the graph as input and are able to assign importance to vertices using heuristics. Contraction hierarchies are not only applied to speed-up algorithms in car-navigation systems but also in web-based route planners, traffic simulation, and logistics optimization. Implementations of the algorithm are publicly available as open source software.

Algorithm The contraction hierarchies (CH) algorithm is a two-phase approach to the shortest path problem consisting of a preprocessing phase and a query phase. As road networks change rather infrequently, more time (seconds to hours) can be used to once precompute some calculations before queries are to be answered. Using this precomputed data, many queries can be answered taking very little time (microseconds) each. CHs rely on shortcuts to achieve this speedup. A shortcut connects two vertices u and v not adjacent in the original graph. Its edge weight is the sum of the edge weights on the shortest u-v path. Consider two large cities connected by a highway. Between these two cities, there is a multitude of junctions leading to small villages and suburbs. Most drivers want to get from one city to the other – maybe as part of a larger route – and not take one of the exits on the way. In the graph representing this road layout, each intersection is represented by a node and edges are created between neighboring intersections. To calculate the distance between these two cities, the algorithm has to traverse all the edges along the way, adding up their length. Precomputing this distance once and storing it in an additional edge created between the two large cities will save calculations each time this highway has to be evaluated in a query. This additional edge is called a "shortcut" and has no counterpart in the real world. The contraction hierarchies algorithm has no knowledge about road types but is able to determine which shortcuts have to be created using the graph alone as input.

Preprocessing phase The CH algorithm relies on shortcuts created in the preprocessing phase to reduce the search space – that is the number of vertices CH has to look at, at query time. To achieve this, iterative vertex contractions are performed. When contracting a vertex v it is temporarily removed from the graph G, and a shortcut is created between each pair {u,w} of neighboring vertices if the shortest path from u to w contains v. The process of determining if the shortest path between u and w contains v is called witness search. It can be performed for example by computing a path from u to w using a forward search using only not yet contracted nodes.

… excerpt ends here. Continue reading the full article.

Illustrations

Contraction hierarchies: The original graph is the line (a,b,c,d,e,f)(solid). Dashed edges represent shortcuts, grey arrows show which two edges are combined to form the respective shortcut. Vertices have been drawn to represent the node order in which the vertices are being contracted, bottom-to-top. Contracting vertex c introduces a shortcut between b and d with dist(b,d)=dist(b,c)+dist(c,d). Contractions of the vertices e and d introduce one shortcut respectively. Contractions of a, b and f do not introduce any shortcuts and are therefore not shown.
The original graph is the line (a,b,c,d,e,f)(solid). Dashed edges represent shortcuts, grey arrows show which two edges are combined to form the respective shortcut. Vertices have been drawn to represent the node order in which the vertices are being contracted, bottom-to-top. Contracting vertex c introduces a shortcut between b and d with dist(b,d)=dist(b,c)+dist(c,d). Contractions of the vertices e and d introduce one shortcut respectively. Contractions of a, b and f do not introduce any shortcuts and are therefore not shown.
Contraction hierarchies: When computing the shortest path from s to t, forward (orange) and backward (blue) search only need to follow edges going upwards in the hierarchy. The found path marked in red and uses one shortcut (dashed).
When computing the shortest path from s to t, forward (orange) and backward (blue) search only need to follow edges going upwards in the hierarchy. The found path marked in red and uses one shortcut (dashed).

Worked examples

Example 1 — a first encounter with Contraction hierarchies

Start with the simplest possible case. Write down what Contraction hierarchies claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Contraction hierarchies before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Contraction hierarchies ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Contraction hierarchies

In research
Contraction hierarchies appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Contraction hierarchies in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Contraction hierarchies is common in secondary-school and first-year university syllabi. It links to neighbouring topics Graph algorithms, Routing algorithms, Search algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Contraction hierarchies outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Contraction hierarchies” →

How to study Contraction hierarchies in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Contraction hierarchies means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Contraction hierarchies out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Contraction hierarchies in simple terms?

In computer science, the method of contraction hierarchies is a speed-up technique for finding the shortest path in a graph. The most intuitive applications are car-navigation systems: a user wants to drive from A to B using the quickest possible route.

Why does Contraction hierarchies matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Contraction hierarchies?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Contraction hierarchies.

Tags

  • Graph algorithms
  • Routing algorithms
  • Search algorithms

Keep exploring