Data Structures19 sections · 729 units
Open in Course

Coordinate Compression

Shrinking the alphabet

When values range up to 10910^9 but there are only nn distinct values, compress:

function compress(arr)
    sorted := sort(unique(arr))
    mapping := map from value to compressed index
    for i from 0 to length(sorted) - 1
        mapping[sorted[i]] := i

    for i from 0 to length(arr) - 1
        arr[i] := mapping[arr[i]]

Now alphabet size σ=n\sigma = n instead of 10910^9.

This reduces wavelet tree depth from 3030 to logn17\log n \approx 17.

Always compress when values are sparse. The queries work the same; just remember to translate results back if needed.