AlgoViz

Hilbert curves and key locality

medium 4 min

A space-filling curve assigns an order to cells so a spatial region can often be read as a small set of key ranges.

Video lesson · Chapter 1 of 5

A square map becomes a line of keys

0:00 / 3:36

Neighbours on a map, far apart in key order

1:07Reel

Theory

Read or listen first. About 1 min.

What it is

To assign a key on a Hilbert curve, visit spatial cells in a recursive order. Nearby positions along the traversal tend to represent nearby areas of the map.

The two ends of a Hilbert traversal touch

2314One-dimensional order follows the blue line.
At this level the curve visits four quadrants in a U, in key order 1, 2, 3, 4. Consecutive keys share an edge.

Why it exists

A point also has neighbours across edges the traversal does not directly cross. A geographic query can therefore need several separated intervals of keys.

Words to know

Spatial locality
The extent to which locations near one another stay close in an index or traversal.

How it works

Step through the idea, or press Play.

Step 1 of 3

Visit cells. Trace four quadrants in a U-shaped order.

Understand it

Worked example

Hilbert curves and key locality in practice

Consider four quadrants visited in a U. The first and last touch along an edge but are far apart in key order. A nearest-neighbor lookup therefore cannot rely on the next or previous key alone.

Related case

Find every charger across a cell boundary

A driver near the edge of a grid cell requests chargers within 800 metres. The closest charger sits across the street in another cell.

Across the street, across the cell
Chargers on both sides of the streetABCOne cell misses charger B

Fetch intersecting cells and check candidates.

Sources