liveBook · Manning

Node contraction

As explained in Optimization Algorithms: AI techniques for design, planning, and control problems

Graph contraction is the transformation of a graph that removes selected nodes and adds shortcut edges to preserve relevant path properties.

Node contraction removes a node from a graph while adding shortcuts where needed to preserve shortest paths. In road routing, contraction is a preprocessing step: a later search can skip many less important intersections without losing the shortest route. section 4.3.4

To contract a node v:

  • Consider each incoming edge u → v and outgoing edge v → w. section 4.3.4
  • If the shortest path from u to w needs v, add a shortcut u → w with the combined weight of the two edges. A witness path that is already as short avoids the need for that shortcut. section 4.3.4
  • Remove v from the graph being contracted. Retain enough information about shortcuts to recover the original route later. section 4.3.4 section 4.4.4

A small road network

Suppose u → v costs 2 and v → w costs 3. If the path through v is the only shortest route from u to w, contracting v adds a shortcut u → w with cost 5. If another route from u to w costs 4, it is a witness path, so that shortcut is unnecessary. section 4.3.4

Figure 4.24 Witness path—there is another path from P to w that is shorter, so no shortcut is needed when contracting v.

The witness-path diagram illustrates why an alternative route can make a shortcut unnecessary. section 4.3.4

A contraction hierarchy ranks nodes by importance and contracts them from least to most important. Major intersections or bridges may be important because shortest routes pass through them. One ranking heuristic is edge difference: the number of required shortcuts minus the node’s degree. section 4.3.4 section 4.4.4

Listing 4.10 U of T routing using CH
def edge_differences(G, sp):
    ed = {}
    degrees = dict(G.degree)
    for node in G.nodes:
        req_edges = 0
        neighbours = list(G.neighbors(node))
  
        if len(neighbours)==0: ed[node] = - degrees[node]     #1
  
        for u, v in G.in_edges(node):
            for v, w in G.out_edges(node):
                if u == w: continue                           #2
                if v in sp[u][w]:
                    req_edges += 1
        
        ed[node] = req_edges - degrees[node]                  #3
  
    return dict(sorted(ed.items(), key=lambda x: x, reverse=True))

This Python listing calculates edge differences for a road-network graph. section 4.4.4

After preprocessing, a modified bidirectional Dijkstra search follows increasingly important edges. The resulting route can contain shortcuts; unpacking each shortcut through its stored midpoint restores the original road segments. Shortcuts can themselves contain shortcuts, so unpacking may be recursive. section 4.3.4 section 4.4.4

def unpack(G, u,v):
    u = int(u)
    v = int(v)
    if "midpoint" in G[u][v][0]:
        midpoint = G[u][v][0]["midpoint"]
        return unpack(G,u,midpoint) + unpack(G,midpoint, v)     #1
    return [u]
  
route = []
for u,v in zip(solution.result[:-1], solution.result[1:]):      #2
    route.extend(unpack(G,u,v))
route += [solution.result[-1]]
print(route)

The recursive unpacking code turns shortcut edges back into a route through original nodes. section 4.4.4

Contraction trades preprocessing work for faster queries. In one 404-node road network, preprocessing took about 24 seconds; the hierarchy query explored 164 nodes, compared with 282 for ordinary bidirectional Dijkstra, while returning the same route. Query times alone do not include that preprocessing cost. section 4.4.4

How Optimization Algorithms: AI techniques for design, planning, and control problems covers it

How deep it goes
Explains shortcut creation, witness paths, node ranking, hierarchy queries, and route unpacking, with Python listings.
Examples
Illustrates contraction with shortcut and witness-path diagrams and compares routing searches on a 404-node road network.
What you can do after
Practice/perform node contraction, rank nodes by edge difference, and unpack shortcut routes.
How it connects
Connects contraction hierarchies with shortest paths, bidirectional Dijkstra search, witness paths, and edge difference.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Optimization Algorithms: AI techniques for design, planning, and control problems. The section links point to the parts of the book it draws on.