Algorithmcore
Huntington-Hill method
Also called method of equal proportions.
A divisor method that rounds each quota at the geometric mean of consecutive integers. The US House of Representatives uses it.
- Time
- O(n + h log n) worst
- Space
- O(n) extra
- Where
- h = the number of seats; n = the number of states
- Model
- Word RAM
- Properties
- comparison baseddeterministiciterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Joseph A. Hill, Edward V. Huntington, 1911
The Huntington-Hill method apportions seats among states in proportion to their populations, and gives every state at least one seat. It is the divisor method that rounds each quotient at the geometric mean of the two integers around it. In the highest averages form, each seat after the first goes to the state with the largest priority . The US House of Representatives has used it since 1941.
In plain words
Every state first gets one seat. The other seats are then given one at a time. For each state, compute a priority number: its population divided by the square root of its seats times its seats plus one. The next seat goes to the state with the highest priority, and that state's priority then drops. This is the same as finding a number of people per seat such that, when each population is divided by it and rounded, the seats add up to the house size. The rounding point between two whole numbers is not the midpoint but a little below it, which helps small states a little.
The problem
The input is the populations , integers with sum , and the number of seats . The output is integers with . The quota of state is . See the apportionment problem for the setting and the other methods.
In divisor form, the method finds a divisor and gives state the number rounded at the geometric mean: a number with rounds up when . Since , every state with gets at least one seat. The highest averages form gives one seat to each state and then each further seat to the state with the largest , ties to the smallest index. The two forms give the same apportionments.
The algorithm
The algorithm is the one of the D'Hondt method, with a different start and other divisors. To avoid square roots, a priority queue holds the squared priorities , and two keys and are compared by the integers and .
HUNTINGTON-HILL(p, h) ▷ p = (p_1, ..., p_n) populations, h seats- 1
if h < n then fail ▷ every state needs one seat - 2
a_i ← 1 for each i - 3
Q ← a max-priority queue of the states, with keys p_i^2 / 2 - 4
repeat h - n times - 5
i ← extract from Q the largest key, the smallest index among ties - 6
a_i ← a_i + 1 - 7
insert i into Q with key p_i^2 / (a_i (a_i + 1)) - 8
return a
Each pass of lines L4 to L7 gives one seat. Since the priorities are not negative, the order of their squares is the order of the priorities.
Example
Four states have populations 100000, 80000, 30000 and 20000, and seats are to be filled. Each state first gets one seat. The table lists the priorities , rounded for display; the numbers in bold get seats 5 to 8, in the order given in brackets.
| seats so far | state 1 | state 2 | state 3 | state 4 |
|---|---|---|---|---|
| 1 () | 70711 (5) | 56569 (6) | 21213 | 14142 |
| 2 () | 40825 (7) | 32660 (8) | 12247 | 8165 |
| 3 () | 28868 | 23094 | 8660 | 5774 |
The result is , the same as the Sainte-Lague method and Hamilton's method give. In divisor form, works: and lie between and , so they round to 3, and and lie below , so they round to 1. With the method gives , while the Sainte-Lague method gives and the D'Hondt method gives .
Correctness
The proof shows that the heap picks the same state as the greedy rule, and that the squared keys of the seats never increase. So the largest squared key left at the end, , is at most , and for every strictly between them, puts each strictly between and . The proof also shows Huntington's pairwise test, house and population monotonicity, and checks the quota violation below.
Proof: Correctness and properties of the Huntington-Hill methodfull
Let be integers with , and let . The quota of state is . The greedy rule gives every state one seat and then gives the other seats one at a time; each goes to the state with the largest priority , where is its current number of seats, the smallest index among ties.
Claim 1 (exact comparison). The method of the page gives the same seats as the greedy rule, with no square roots.
Proof. Priorities are nonnegative, so exactly when . The heap compares the squares and by the integers and , and breaks ties by the smaller index. After line L3 it holds the key of each state for , and lines L5 to L7 replace the key of the winner by its key for the new . So line L5 removes the state that the greedy rule picks.
Claim 2 (divisor form). Let and (or if no ). Then . If , then for every with and , each is rounded at the geometric mean: , where the left bound is 0 for . Conversely, every vector with sum that comes from such a rounding for some satisfies the same inequalities, so the output is the only Huntington-Hill apportionment when .
Proof. The squared keys of the seats given after the first never increase, because a seat changes only the key of its winner, to a smaller value. So every such key is at least the largest key at the end, which is . The last seat of a state with had the key , so . If , then for every and when ; take square roots. The uniqueness argument is the one of the D'Hondt method with and in place of and : if , some and some , and then and , which contradicts .
Claim 3 (Huntington's pairwise test). For any two states with , moving one seat from to does not lower the relative difference of their representations. Here state has seats per person, and the relative difference of is .
Proof. By Claim 2, , that is,
Let , , and . First, : otherwise , and multiplying contradicts (1). So after the move the ratio is . If , the ratio before is . If , the ratio before is , and is , which is (1).
Claim 4 (house and population monotone). The seats for are those for plus one seat. If and , then and cannot both hold, unless one of the two apportionments has .
Proof. The first part holds because the greedy rule for repeats the steps for . For the second, take for and for . If and , then and , and
so all are equal and for .
Claim 5 (quota fails). With and , the method gives , while , so state 1 gets less than its lower quota.
Proof. After one seat each, the squared keys are , , and . The four other seats go to the keys , and of state 1, and of state 2. The largest key left is and the smallest key used for a second or later seat is , so and there is no tie.
Complexity
With a binary heap, line L3 takes time and each of the passes takes , so the time is and the extra space is . The keys and have bits, and each comparison multiplies two of them. Cheng and Eppstein (2014) give an time algorithm for every divisor method, independent of .
Properties
- One seat for every state. Rounding at gives every state with a positive population at least one seat. The US Constitution requires this for the House.
- Huntington's pairwise test. No transfer of one seat between two states lowers the relative difference of their seats per person, with . The same holds for the relative difference of their people per seat. Huntington showed that this test picks out the method, which he called the method of equal proportions.
- House and population monotone. One more seat never takes a seat away, so there is no Alabama paradox. A state that grows does not lose a seat to a state that shrinks, except through a tie.
- Quota can fail. With populations and , the method gives , while the quota of state 1 is ; there is no tie. By the Balinski-Young theorem, every population monotone method fails quota for some input with 4 or more states.
- A small bias. The geometric mean is below the midpoint, so small states round up a little more often than under the Sainte-Lague method. Balinski and Young show that the method favors small states on average, though much less than Adams's method.
Variants and improvements
- Priority list. The US Census Bureau computes the priorities for for all states, sorts them, and gives seats 51 to 435 down the list. This is the same algorithm with a sort in place of the heap, and it gives the apportionment for every house size at once.
- Other divisor methods. The D'Hondt method rounds down, Adams's method rounds up, the Sainte-Lague method rounds at the midpoint , and Dean's method at the harmonic mean . Huntington compared all five with his pairwise tests.
- Linear time. For many states and seats, the method of Cheng and Eppstein finds a divisor in time.
When to use it
Use the Huntington-Hill method when every state must get at least one seat and the result must be free of the Alabama and population paradoxes, as for the US House. If small and large states must be treated alike on average, the Sainte-Lague method is the better choice; if a law must keep every state within its quota, use Hamilton's method and accept its paradoxes.
Pitfalls
- Square roots in floating point. The priorities are irrational in general, and rounding errors can break or invent ties. Compare the squares with integer cross products.
- Fewer seats than states. The method gives every state one seat first, so it needs . A state with population 0 still gets a seat.
- Not the same as rounding at one half. Rounding at the arithmetic mean is the Sainte-Lague method. The two agree in the example, but with they do not.
- Ties. If the last seat goes to one of two equal keys, the result depends on the tie rule. State the rule.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/huntington-hill-method/. The authors are listed in prov/Y00758.jsonl.
import heapq
from typing import Sequence
class _Key:
"""The priority s / d of state i in a min-heap: a larger quotient comes first, then a smaller index.
Here s = p^2 and d = a (a + 1), so s / d is the square of p / sqrt(a (a + 1)). Quotients
are compared by cross multiplication, so no square root and no division is needed.
"""
__slots__ = ("s", "d", "i")
def __init__(self, s: int, d: int, i: int) -> None:
self.s, self.d, self.i = s, d, i
def __lt__(self, other: "_Key") -> bool:
x, y = self.s * other.d, other.s * self.d
if x != y:
return x > y
return self.i < other.i
def huntington_hill_method(populations: Sequence[int], h: int) -> list[int]:
"""Apportion h seats by the Huntington-Hill method (method of equal proportions).
populations are nonnegative integers with a positive sum, and h >= n. Every state first
gets one seat. Each of the other h - n seats goes to the state with the largest priority
p / sqrt(a (a + 1)), where a is its number of seats so far, the smallest index among
ties. The priorities are compared through their squares p^2 / (a (a + 1)).
"""
n = len(populations)
if any(p < 0 for p in populations) or sum(populations) <= 0:
raise ValueError("need populations >= 0 and a positive total")
if h < n: # @line L1
raise ValueError("Huntington-Hill gives every state a seat, so it needs h >= n")
a = [1] * n # @line L2
heap = [_Key(p * p, 2, i) for i, p in enumerate(populations)] # @line L3
heapq.heapify(heap)
for _ in range(h - n): # @line L4
i = heapq.heappop(heap).i # @line L5
a[i] += 1 # @line L6
heapq.heappush(heap, _Key(populations[i] ** 2, a[i] * (a[i] + 1), i)) # @line L7
return a # @line L8
test_huntington_hill_method.py 157 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/huntington-hill-method/. The authors are listed in prov/Y00758.jsonl.
import random
import unittest
from fractions import Fraction
from huntington_hill_method import huntington_hill_method
def compositions_at_least_one(h, n):
"""Yield every list of n integers >= 1 with sum h."""
if n == 1:
if h >= 1:
yield [h]
return
for x in range(1, h - n + 2):
for rest in compositions_at_least_one(h - x, n - 1):
yield [x] + rest
def by_scan(pops, h):
"""A second implementation: a linear scan for each seat, on the squared priorities."""
a = [1] * len(pops)
for _ in range(h - len(pops)):
best = max(range(len(pops)), key=lambda i: (Fraction(pops[i] ** 2, a[i] * (a[i] + 1)), -i))
a[best] += 1
return a
def gap(pops, a):
"""Return (low, high) on squared priorities: the largest p^2 / (a (a + 1)) and the smallest p^2 / ((a - 1) a).
a is a Huntington-Hill apportionment exactly when low <= high; it is the only one when low < high.
"""
low = max(Fraction(p * p, x * (x + 1)) for p, x in zip(pops, a))
high = min((Fraction(p * p, (x - 1) * x) for p, x in zip(pops, a) if x >= 2), default=None)
return low, high
def is_hh(pops, a):
low, high = gap(pops, a)
return high is None or low <= high
def tie_free(pops, a):
low, high = gap(pops, a)
return high is None or low < high
def round_geometric(x2):
"""Round x >= 0 given by its square x2: the a >= 1 with (a - 1) a <= x2 < a (a + 1)."""
a = 1
while a * (a + 1) <= x2:
a += 1
return a
def quotas(pops, h):
return [Fraction(h * p, sum(pops)) for p in pops]
def floor(x):
return x.numerator // x.denominator
class TestCheckValues(unittest.TestCase):
V = [100000, 80000, 30000, 20000]
def test_eight_seats(self):
self.assertEqual(huntington_hill_method(self.V, 8), [3, 3, 1, 1])
def test_four_seats(self):
self.assertEqual(huntington_hill_method(self.V, 4), [1, 1, 1, 1])
def test_lower_quota_violation(self):
p, h = [18, 7, 2, 1], 8
a = huntington_hill_method(p, h)
self.assertEqual(a, [4, 2, 1, 1])
self.assertEqual(quotas(p, h)[0], Fraction(36, 7))
self.assertTrue(tie_free(p, a))
def test_errors(self):
with self.assertRaises(ValueError):
huntington_hill_method([5, 3, 2], 2)
with self.assertRaises(ValueError):
huntington_hill_method([0, 0], 3)
with self.assertRaises(ValueError):
huntington_hill_method([4, -1], 3)
class TestProperties(unittest.TestCase):
def test_against_scan_and_divisor_form(self):
rng = random.Random(51)
for _ in range(1500):
n = rng.randint(1, 7)
p = [rng.randint(0, 500) for _ in range(n)]
if sum(p) == 0:
continue
h = rng.randint(n, n + 30)
a = huntington_hill_method(p, h)
self.assertEqual(sum(a), h)
self.assertEqual(a, by_scan(p, h))
self.assertTrue(is_hh(p, a))
low, high = gap(p, a)
if high is not None and low < high:
m2 = (low + high) / 2
self.assertEqual([round_geometric(Fraction(x * x) / m2) for x in p], a)
def test_brute_force(self):
rng = random.Random(52)
for _ in range(250):
n = rng.randint(1, 4)
p = [rng.randint(1, 60) for _ in range(n)]
h = rng.randint(n, n + 8)
a = huntington_hill_method(p, h)
hh = [b for b in compositions_at_least_one(h, n) if is_hh(p, b)]
self.assertIn(a, hh)
if tie_free(p, a):
self.assertEqual(hh, [a])
def test_no_transfer_lowers_the_relative_difference(self):
rng = random.Random(53)
for _ in range(400):
n = rng.randint(2, 5)
p = [rng.randint(1, 200) for _ in range(n)]
h = rng.randint(n, n + 20)
a = huntington_hill_method(p, h)
for i in range(n):
for j in range(n):
if i == j or a[i] < 2:
continue
r = [Fraction(a[i], p[i]), Fraction(a[j], p[j])]
s = [Fraction(a[i] - 1, p[i]), Fraction(a[j] + 1, p[j])]
self.assertGreaterEqual(max(s) / min(s), max(r) / min(r))
def test_house_and_population_monotone(self):
rng = random.Random(54)
for _ in range(600):
n = rng.randint(2, 5)
p = [rng.randint(1, 100) for _ in range(n)]
h = rng.randint(n, n + 15)
a = huntington_hill_method(p, h)
b = huntington_hill_method(p, h + 1)
self.assertTrue(all(y >= x for x, y in zip(a, b)))
p2 = [max(1, v + rng.randint(-30, 30)) for v in p]
a2 = huntington_hill_method(p2, h)
if not (tie_free(p, a) and tie_free(p2, a2)):
continue
for i in range(n):
for j in range(n):
if i != j and p2[i] >= p[i] and p2[j] <= p[j]:
self.assertFalse(a2[i] < a[i] and a2[j] > a[j])
if __name__ == "__main__":
unittest.main()
History
Joseph A. Hill, a statistician at the Census Bureau, proposed the method in 1911. Edward V. Huntington, a mathematician at Harvard, built its theory in the 1920s: he compared the divisor methods by pairwise tests and showed that the test of relative difference gives this method. Walter Willcox argued for Webster's method of major fractions, and a committee of the National Academy of Sciences sided with Huntington in 1929. Congress adopted the method in 1941 for the census of 1940, and has used it with a House of 435 seats since then. In 1992 the US Supreme Court upheld it in United States Department of Commerce v. Montana, a case Montana brought after it lost its second seat.
References
- Michel L. Balinski and H. Peyton Young, Fair Representation: Meeting the Ideal of One Man, One Vote, 2nd edition, Brookings Institution Press, 2001, Chapter 6 (The Controversy over Bias) and Appendix A (The Theory of Apportionment)
- Edward V. Huntington, The apportionment of representatives in Congress, Transactions of the American Mathematical Society 30(1) (1928), 85-110, the pairwise tests of relative difference and the method of equal proportions
- Zhanpeng Cheng and David Eppstein, Linear-time algorithms for proportional apportionment, Proc. 25th International Symposium on Algorithms and Computation (ISAAC 2014), LNCS 8889, 581-592, divisor methods in O(n) time, independent of the number of seats
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag Y00758 Edit this page History Report an error Markdown