Barnes–Hut algorithm

Note · 12 November 2026 · Computer science › Algorithms

A galaxy, a protein in water and a drawing of a social network share one computation. Every point pulls or pushes every other point. A simulation of any of them needs the sum of all those forces, at every time step. This is the N-body problem.

The figure runs two discs of stars into each other, with gravity computed by the Barnes–Hut algorithm. Switch to the direct sum and the time per force evaluation jumps. Raise N to 5000 and the animation stalls. Go back to Barnes–Hut and it runs again. This note explains where that difference comes from.

Open the figure full-size.

The direct sum

The force on body ii is a sum over all the other bodies. With the gravitational constant set to 1, it is 𝐅i=mi∑j≠imj𝐱j−𝐱i(|𝐱j−𝐱i|2+ε2)3/2. \mathbf F_i = m_i\sum_{j\neq i} m_j\,\frac{\mathbf x_j-\mathbf x_i}{\left(|\mathbf x_j-\mathbf x_i|^2+\varepsilon^2\right)^{3/2}} . The small softening length ε\varepsilon keeps the force finite when two bodies come very close.

Each dotted line in the figure is one term. One body needs N−1N-1 terms, and all bodies together need N(N−1)/2N(N-1)/2 pairs. Twelve points give 66 pairs, and 120 points give 7140. A million stars give 5×10115\times10^{11} pairs per time step, and a simulation needs thousands of steps.

How can we do better?

The idea: a far group acts like one body

Before 1985, simulations either paid for every pair or solved the Poisson equation for the potential on a grid with fast Fourier transforms. The grid is fast, but its spacing sets the resolution. Galaxies and molecules cluster, so most of a grid is empty while the dense regions are under-resolved.

Appel grouped nearby bodies into a hierarchy of clusters and let a distant cluster act as a single body (Appel 1985). His tree followed the bodies, so its shape was irregular and its error was hard to predict. In 1986 the astronomers Josh Barnes and Piet Hut replaced it with a regular subdivision of space and one simple rule (Barnes and Hut 1986).

The key idea is to replace a group of distant bodies with their total mass at their centre of mass. That costs a small error, and it brings the work down from N2N^2 to NlogNN\log N.

The method needs a map of space that groups the bodies. In two dimensions this is a quadtree: a square is split into four equal quadrants, and each quadrant again, as far as needed. In three dimensions an octree splits a cube into eight. The algorithm has three steps:

  1. build the quadtree;
  2. compute the mass and the centre of mass of every cell;
  3. estimate the force on each body by walking the tree.
Timeline of hierarchical N-body methods

Step 1: build the quadtree

The tree starts as one root cell that covers the whole domain. The first point simply goes into the root. A second point would share the cell, so the root splits into four children and each point moves into its own quadrant. Every later insertion walks down from the root to the leaf that contains it, and splits that leaf if it is occupied. After 17 points the tree is already 5 levels deep near the clusters. With all 120 points it has 207 cells and depth 8.

Step 2: compute the centres of mass

Every cell cc needs its total mass and its centre of mass: Mc=∑k∈childrenMk,𝐗c=1Mc∑k∈childrenMk𝐗k. M_c=\sum_{k\in\text{children}} M_k,\qquad \mathbf X_c=\frac{1}{M_c}\sum_{k\in\text{children}} M_k\,\mathbf X_k . A parent needs its children first, so the pass starts at the deepest leaves. It then merges level by level and ends at the root, which holds all the mass at the overall centre of mass. Each cell is visited once, so this step costs O(N)O(N).

Step 3: estimate the force

To find the force at a point, the walk starts at the root. A cell of width ss whose centre of mass is at distance dd is far enough when sd<θ. \frac{s}{d}<\theta . The walk then uses the cell’s total mass at its centre of mass and does not look inside. Otherwise it opens the cell and repeats the test on the children. A leaf that is still too close is summed point by point. The opening angle θ\theta is the one parameter of the method.

At θ = 0 no cell is ever far enough, and the walk becomes the direct sum: 120 terms and no error. At θ = 0.5 the probe uses 21 cells and 21 single points, and the force error is 0.7 %. At θ = 1 only 18 terms are left, and the error at this probe jumps to 33 %. Try it yourself:

The force on every body is NN such walks, one per body. That is the step that makes the method O(NlogN)O(N\log N).

Why the cost is N log N

How large is the error?

Performance analysis

The figure compares the direct sum with Barnes–Hut at three values of θ, in the same JavaScript code. The points are uniform random in the unit square, the leaf size is 8, and the softening is 0.3 times the mean spacing. The error of one body is the length of its force error, divided by the mean force over all bodies. The button measures again on your own machine, up to N = 4000.

N direct θ = 0.5 θ = 1 θ = 1.5 mean error at θ = 0.5 / 1 / 1.5
1000 125 ms 35 ms 13 ms 7 ms 0.7 % / 4.8 % / 19 %
4000 1.7 s 164 ms 51 ms 27 ms 1.0 % / 5.4 % / 22 %
16000 31.9 s 1.4 s 0.37 s 0.18 s 1.1 % / 5.9 % / 26 %

What it leaves open

Code

Further reading

References:

Appel, Andrew W. 1985. “An Efficient Program for Many-Body Simulation.” SIAM Journal on Scientific and Statistical Computing 6 (1): 85–103. https://doi.org/10.1137/0906008.
Barnes, Josh, and Piet Hut. 1986. “A Hierarchical O(N Log N) Force-Calculation Algorithm.” Nature 324 (6096): 446–49. https://doi.org/10.1038/324446a0.
Greengard, L, and V Rokhlin. 1987. “A Fast Algorithm for Particle Simulations.” Journal of Computational Physics 73 (2): 325–48. https://doi.org/10.1016/0021-9991(87)90140-9.
Heer, Jeffrey. 2017. “The Barnes-Hut Approximation: Efficient Computation of N-Body Forces.” Jheer.github.io. https://jheer.github.io/barnes-hut/.