2-D Prefix Sums & Inclusion–Exclusion
Summed-area tables, rectangle queries and higher dimensions.
Viola and Jones's face detector (2001) ran in real time by testing simple rectangle features at every position and scale of every frame. Each feature, whatever its size, cost a handful of memory lookups, thanks to a table that Frank Crow had introduced to computer graphics in 1984. The same four-corner formula, generalized to the -dimensional hypercube, sums a function over all subsets of every set in .
What you’ll learn
- Build a summed-area table in and prove the four-term rectangle formula by inclusion–exclusion.
- Generalize to dimensions: signed corners per query, but only one-dimensional passes to build the table.
- Treat sums over subsets as prefix sums on the hypercube : compute the zeta transform in and invert it by Möbius inversion.
- Explain what subtraction buys, and why dynamic prefix sums need time per operation in the cell-probe model.
Summed-area tables
Write the rectangle's indicator as a product of two 1-D ranges, and each range as a difference of two prefixes:
Multiply out: four signed "top-left blocks", each of which is one entry of :
Weight by and sum over the grid. The block is counted once; the strips above it and left of it are subtracted once each; the corner block above and to the left is subtracted twice and added back once. That is inclusion–exclusion.
In the grid above (rows and columns numbered from 0), the table has , , and . What is the sum of the rectangle with rows and columns , inclusive?
Show the answer
37
, which matches . Forgetting to add back gives 34, the corner block subtracted twice.
d dimensions: 2ᵈ corners, d passes
The proof never used that there were two dimensions. A box is a product of ranges, each a difference of two prefixes, so its sum expands into signed corners. For let the corner take in coordinate when and when :
where counts the ones in . For this is ; for , the four-term formula.
Building by the inclusion–exclusion recurrence costs terms per cell. There is a better way: the -dimensional prefix-sum operator is a product of commuting one-dimensional ones, . So run an ordinary prefix sum along axis 1, then along axis 2, and so on. After the pass along axis , each cell holds the sum over the box that is a prefix in axes and a single point in the others.
| Dimension | Lookups per box query | Build by recurrence | Build by axis passes |
|---|---|---|---|
| 1 | 2 | ||
| 2 | 4 | ||
| 3 | 8 | ||
The hypercube: sums over subsets
A subset of is a point of the hypercube , an -dimensional array of side 2, and means in every coordinate. So the sum over subsets, the zeta transform
is exactly a -dimensional prefix sum with . Enumerating every pair costs (the Optimal lesson Subsets as Bitmasks proves it); one prefix pass per axis costs additions. Its inverse, the difference along every axis, is Möbius inversion:
def zeta(f, n): # F[X] = sum of f[Y] over all Y subset of X
F = f[:]
for i in range(n): # one axis of the hypercube per bit
for X in range(1 << n):
if X >> i & 1:
F[X] += F[X ^ (1 << i)]
return F
def mobius(F, n): # the inverse: a difference along every axis
f = F[:]
for i in range(n):
for X in range(1 << n):
if X >> i & 1:
f[X] -= f[X ^ (1 << i)]
return fEach loop over bit is a 1-D prefix sum (or difference) along axis . Both run in time, in place.
For , how many additions does the bit-by-bit zeta transform perform?
Show the answer
A.
Each of the 20 passes adds into the masks that contain its bit: additions, about 330 times fewer than enumerating all pairs .
Classical inclusion–exclusion is Möbius inversion on this lattice. The integers in divisible by none of the primes number , over all subsets ; for and the primes that is .
The same transforms multiply set functions: the OR-convolution satisfies pointwise, so it costs . Disjoint unions need one more idea, which Subsets as Bitmasks develops into the subset convolution.
What subtraction buys, and dynamic lower bounds
Everything so far relied on subtraction: the static range sum in dimensions takes preprocessing and per query. Without an inverse, as for minimum and maximum, the picture changes. In one dimension a sparse table answers range minima in after preprocessing, because tolerates overlapping ranges; general associative operations need other structures (see Sparse Tables & Square-Root Decomposition).
Updates change the picture again. With point updates mixed with prefix-sum queries, a static table costs per update, while a Fenwick tree (Fenwick, 1994) does both operations in , and in in dimensions. Can anyone do better?
The cell-probe model charges only for memory accesses: computation is free, and any word-RAM data structure is a cell-probe data structure. So the bound holds for every algorithm, hashing and bit tricks included. The proof idea is information transfer. Lay a random sequence of operations along a balanced binary tree over time. At each node, the queries in the right half must learn about bits of each update in the left half, so they must read cells per operation that were written during the left half. Each read is charged to one node only, the lowest common ancestor of the times the cell was written and read, and each of the levels contributes per operation.
Key takeaways
- A summed-area table of top-left block sums answers any rectangle with four lookups, , by inclusion–exclusion.
- In dimensions a box query needs signed corners, but the table is built with one-dimensional passes, , because the prefix-sum operator factors by axis.
- Sums over subsets are prefix sums on the hypercube: the zeta transform takes additions instead of , and Möbius inversion, the same loops with subtraction, undoes it.
- Static range sums are thanks to subtraction; dynamic prefix sums cost per operation, a cell-probe lower bound (Pătraşcu–Demaine) that Fenwick trees meet.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A 3-D prefix table is built over a array. How many entries of does one box-sum query combine in general?
- 3
- 4
- 6
- 8
Show the answer
D. 8
A box is a product of three ranges, each a difference of two prefixes, so its sum expands into signed corners.
Why build a -dimensional prefix table by one-dimensional passes rather than by the inclusion–exclusion recurrence?
- The recurrence gives wrong answers for
- Passes cost additions instead of , because the -dimensional prefix sum factors into commuting 1-D prefix sums
- Passes make each query cost lookups instead of
- Passes avoid the need for a zero border
Show the answer
B. Passes cost additions instead of , because the -dimensional prefix sum factors into commuting 1-D prefix sums
, and each factor is one linear pass. The table is the same either way; only the build cost differs. Queries still need corners.
You computed , the sums over subsets, and need back. What does the inverse transform do?
- Run the same bit-by-bit loops with subtraction instead of addition
- Divide each by
- Run the zeta transform a second time
- Enumerate supersets instead of subsets
Show the answer
A. Run the same bit-by-bit loops with subtraction instead of addition
Each pass of the zeta transform is a 1-D prefix sum along one axis; its inverse is the 1-D difference along that axis, . Together the passes compute .
Which statement about maintaining prefix sums under point updates is true?
- Hashing gives time for both updates and queries
- The lower bound holds only for comparison-based algorithms
- In the cell-probe model some operation needs amortized time, so Fenwick trees are optimal up to constants
- The best possible time per operation is
Show the answer
C. In the cell-probe model some operation needs amortized time, so Fenwick trees are optimal up to constants
Pătraşcu and Demaine proved amortized per operation, even for randomized structures, in a model where computation is free. A Fenwick tree achieves for both.
Further reading
- F. C. Crow, "Summed-area tables for texture mapping," Computer Graphics (Proceedings of SIGGRAPH '84) 18(3) (1984), 207–212.
- P. Viola & M. Jones, "Rapid object detection using a boosted cascade of simple features," Proceedings of CVPR (2001).
- G.-C. Rota, "On the foundations of combinatorial theory I. Theory of Möbius functions," Zeitschrift für Wahrscheinlichkeitstheorie und verwandte Gebiete 2 (1964), 340–368.
- M. Pătraşcu & E. D. Demaine, "Logarithmic lower bounds in the cell-probe model," SIAM Journal on Computing 35(4) (2006), 932–963.
- K. G. Larsen, "The cell probe complexity of dynamic range counting," Proceedings of STOC (2012).