Apex › Storage & databases

How galaxies simulate gravity fast (Barnes-Hut)

Simulating gravity among n bodies is O(n²) — every star pulls on every other. Barnes-Hut cuts it to O(n log n) using one fact: a distant clump of stars pulls almost exactly like a single body at its center of mass. A quadtree summarizes each region’s mass and center of mass; to find a body’s force you walk the tree, lumping any node far enough that its width ÷ distance is below θ, and opening the rest. Verified against the exact sum: ~2% force error at θ=0.5. Turn the θ dial and watch the approximation boxes grow.

More in Storage & databases