ContractionHierarchy#

class graph_tool.structure.ContractionHierarchy(g, weights=None, witness_limit=500, max_core_degree=32, parallel=True)[git][source]#

Bases: object

Contraction hierarchy of a graph, for fast shortest-path queries between pairs of vertices.

Parameters:
gGraph

Graph to be used, which can be a filtered, reversed, or undirected view.

weightsEdgePropertyMap (optional, default: None)

Edge weights, which must be non-negative. If not given, every edge has unit weight.

witness_limitint (optional, default: 500)

Maximum number of vertices settled by each witness search during the contraction. Smaller values make the preprocessing faster but may add unnecessary shortcuts, which make the queries slower.

max_core_degreefloat (optional, default: 32)

The contraction stops when the average degree (out-degree, if the graph is directed) of the graph of the remaining vertices exceeds this value, and these vertices form the core of the hierarchy (see below).

parallelbool (optional, default: True)

If True, the vertices are contracted in rounds, in each of which an independent set of vertices of locally minimal importance is contracted in parallel. Otherwise, the vertices are contracted one at a time, from a priority queue with lazy updates.

Notes

A contraction hierarchy [geisberger-contraction-2008] is built by removing (“contracting”) the vertices one at a time, in increasing order of importance, and by adding a shortcut edge between two neighbors of the contracted vertex whenever the path through it may be the only shortest path between them. Whether a shortcut is needed is decided by a local Dijkstra search for a path of at most the same length that avoids the vertex (a “witness”), limited by witness_limit. The importance of a vertex is its edge difference, i.e. the number of shortcuts its contraction adds minus the number of edges it removes, plus the number of its neighbors already contracted.

The shortest path between any two vertices can then be found by a bidirectional Dijkstra search in which both sides only follow the edges and shortcuts that lead to more important vertices. This search is used by shortest_distance() and shortest_path() when the hierarchy is passed as their parameter ch, and the shortcuts are replaced by the vertices and edges of the graph they bypass. If the graph is directed, the search from the source follows the out-edges, and the one from the target the in-edges. Self-loops are ignored, and of parallel edges only the one of smallest weight is kept.

The cost of the witness searches grows quickly with the degrees of the remaining vertices, which increase as shortcuts are added. Therefore, the contraction stops when the average degree of the remaining graph exceeds max_core_degree, and the remaining vertices, which keep all the edges and shortcuts between them, form the core of the hierarchy, in which the searches follow all edges. The number of these vertices is returned by core_size(). A smaller value of max_core_degree makes the preprocessing faster but leaves a larger core, which makes the queries slower, since the searches in the core are not restricted to more important vertices.

The hierarchy does not follow changes of the graph or of the weights, and must be built again after any of them. Before each query, it is verified that the graph has the same numbers of vertices and edges and the same view (directedness, reversal, and filters), and that the weights are the same, otherwise a ValueError is raised.

The speedup depends strongly on the structure of the graph. On road networks, which are nearly planar and have a natural hierarchy of importance, the contraction adds about one shortcut per edge, the preprocessing is fast, and each query visits only a few hundred vertices, so that the queries become orders of magnitude faster than the bidirectional search without the hierarchy. Social and other small-world networks, with small diameters and broad degree distributions, lack such a hierarchy: the contraction of the neighbors of the hubs adds many shortcuts, the preprocessing is slow, a large core remains, and the queries can be slower than the bidirectional search without the hierarchy, which is therefore not recommended for these networks.

The preprocessing has no useful worst-case bound; for graphs with a bounded number of shortcuts per vertex it runs in time \(O(V \log V)\), plus the witness searches. Each query is a bidirectional Dijkstra search on the upward part of the hierarchy, and the unpacking of the path takes time proportional to its number of edges.

With parallel=True, the witness searches of each round and the updates of the priorities are distributed among the threads. The independent sets yield somewhat more shortcuts, and slightly slower queries, than the contraction one vertex at a time, which is serial and therefore slower when several threads are available.

If a list of targets is passed to shortest_distance(), one query is run for each of them, in parallel. For a single source and many targets (or all vertices), a single Dijkstra search without the hierarchy will be faster.

Parallel implementation.

If enabled during compilation, this algorithm will run in parallel using OpenMP. See the parallel algorithms section for information about how to control several aspects of parallelization.

References

[geisberger-contraction-2008]

Robert Geisberger, Peter Sanders, Dominik Schultes, and Daniel Delling, “Contraction hierarchies: Faster and simpler hierarchical routing in road networks”, Experimental Algorithms (WEA 2008), Lecture Notes in Computer Science 5038, 319-333 (2008), DOI: 10.1007/978-3-540-68552-4_24 [sci-hub, @tor]

Examples

The hierarchy is built once for a graph and its weights:

>>> g = gt.lattice([100, 100])
>>> w = g.new_ep("double", vals=numpy.random.random(g.num_edges()))
>>> ch = gt.ContractionHierarchy(g, weights=w)
>>> print(ch.num_shortcuts(), ch.core_size())
27386 0

and then used for any number of queries, which give the same results as the searches without it:

>>> print(gt.shortest_distance(g, 0, 9999, weights=w, ch=ch))
47.32801673...
>>> print(gt.shortest_distance(g, 0, 9999, weights=w))
47.32801673...
>>> vlist, elist = gt.shortest_path(g, 0, 9999, weights=w, ch=ch)
>>> print(len(vlist), w.a[[g.edge_index[e] for e in elist]].sum())
207 47.32801673...

Methods

core_size()

Return the number of vertices in the core of the hierarchy, which were not contracted.

get_rank()

Return a vertex property map with the rank of each vertex in the hierarchy, i.e. the position of the vertex in the contraction order.

num_shortcuts()

Return the number of shortcuts in the hierarchy.

core_size()[git][source]#

Return the number of vertices in the core of the hierarchy, which were not contracted.

get_rank()[git][source]#

Return a vertex property map with the rank of each vertex in the hierarchy, i.e. the position of the vertex in the contraction order.

num_shortcuts()[git][source]#

Return the number of shortcuts in the hierarchy.