Algorithmintro
D'Hondt method
Also called Jefferson method, greatest divisors method.
Assigns seats one at a time to the party with the largest votes divided by one plus its seats. It favours large parties.
- Time
- O(n + h log n) worst
- Space
- O(n) extra
- Where
- h = the number of seats; n = the number of parties
- Model
- Word RAM
- Properties
- comparison baseddeterministiciterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Thomas Jefferson, Victor D'Hondt, 1792
The D'Hondt method apportions seats among parties or states in proportion to their votes. It gives the seats one at a time, each to the party with the largest quotient of its votes divided by one more than its seats so far. It is the same as Jefferson's method, the divisor method that rounds every quota down. It has none of the paradoxes of Hamilton's method, and it never gives a party less than its lower quota, but it can give a large party more than its upper quota.
In plain words
Write each party's vote total at the top of a column. Below it write the total divided by 2, by 3, by 4, and so on. Now give the seats to the largest numbers in the whole table, one seat for each number, until all seats are gone. A party with twice the votes of another gets about twice the seats. Small parties are cut a little: the method rounds down, and the rounding hurts a party with few seats the most.
The problem
The input is the votes , integers with sum , and the number of seats . The output is integers with . The quota of party is . See the apportionment problem for the setting and the other methods.
Jefferson's method in divisor form is: find a divisor such that , and give party the seats . The D'Hondt rule in highest averages form gives each seat to the party with the largest , ties to the smallest index. The two forms give the same apportionments.
The algorithm
A priority queue holds one key per party, its next quotient. A binary heap compares two keys and by the integers and , with no division.
D'HONDT(p, h) ▷ p = (p_1, ..., p_n) votes, h seats- 1
a_i ← 0 for each i - 2
Q ← a max-priority queue of the parties, with keys p_i / 1 - 3
repeat h times - 4
i ← extract from Q the largest key, the smallest index among ties - 5
a_i ← a_i + 1 - 6
insert i into Q with key p_i / (a_i + 1) - 7
return a
Each pass of lines L3 to L6 gives one seat and puts the party back with its next quotient.
Example
Four parties have 100000, 80000, 30000 and 20000 votes, and seats are to be filled. The table lists the quotients ; the numbers in bold get the seats, in the order given in brackets.
| divisor | party 1 | party 2 | party 3 | party 4 |
|---|---|---|---|---|
| 1 | 100000 (1) | 80000 (2) | 30000 (6) | 20000 |
| 2 | 50000 (3) | 40000 (4) | 15000 | 10000 |
| 3 | 33333 (5) | 26667 (7) | 10000 | 6667 |
| 4 | 25000 (8) | 20000 | 7500 | 5000 |
| 5 | 20000 | 16000 | 6000 | 4000 |
The result is . The quotas are , , and , so party 1 gets its upper quota and party 4 its lower quota 0. Hamilton's method, the Sainte-Lague method and the Huntington-Hill method all give . Any divisor with gives the same result in divisor form: for , , , and . The ninth seat is a three-way tie at 20000, between parties 1, 2 and 4; the smallest index gives it to party 1.
Correctness
The proof shows that the keys of the seats, in the order they are given, never increase. Let be the largest key left at the end and the smallest key that won a seat. Then , and every with gives for every party. So the highest averages rule computes Jefferson's divisor method, and its result is unique when . The proof also shows lower quota, the least-cost form below, house monotonicity and population monotonicity.
Proof: Correctness and properties of the D'Hondt methodfull
Let be integers with , and let . The quota of party is . The greedy rule gives seats one at a time; each goes to the party with the largest key , where is its current number of seats, the smallest index among ties.
Claim 1 (the heap). The method of the page gives the same seats as the greedy rule.
Proof. The order used by the heap compares and by the products and , which is exact since , and breaks ties by the smaller index. It is a strict total order on the keys. At each pass of line L3 the heap holds exactly one key per party, : this holds after line L2, and lines L4 to L6 remove the key of and insert its new one. So line L4 removes the key that the greedy rule picks.
Claim 2 (the keys do not increase). Let be the keys of the seats in the order they are given. Then , where for the final seats .
Proof. Each is the largest key at time . Giving the seat changes only the key of the winner, from to the smaller value . So the largest key never increases, and is the largest key at the end.
Claim 3 (divisor form). Let (or if ). Then , and for every with , for every . Conversely, if some gives , then satisfies . So the output of the method is an apportionment of Jefferson's divisor method, and it is the only one when .
Proof. The seats of party had the keys , so is one of the , and by Claim 2 it is at least . So . If , then gives , and, for , gives ; for this is trivial. So . The converse is the definition of the floor: . If , any other vector of the divisor form with sum has its own interval , and forces some and some , so and , which contradicts .
Claim 4 (lower quota). for every .
Proof. Suppose , so . Then . For every party with , Claim 3 gives , so . Then , a contradiction.
Claim 5 (upper quota fails). With and , the method gives , but .
Proof. The keys of party 1 are , of party 2 they are , and parties 3 and 4 have . In decreasing order the first seven keys are , and only one of them belongs to party 2. Here , so there is no tie.
Claim 6 (least cost). If every , the output minimizes over all integer vectors with .
Proof. with the marginal costs , which increase in . So is a sum of marginal costs, taken as a prefix from each party, and it is at least the sum of the smallest marginal costs over all pairs . Because each party's costs increase, the smallest costs form a prefix of each party, so some reaches this bound. The greedy rule takes at each step the smallest cost left, which is the largest key , so it takes the smallest costs.
Claim 7 (no paradoxes). The method is house monotone: the seats for are those for plus one seat. And it is population monotone: if the populations change from to (and the house from to ), with and , then it cannot happen that and , unless both apportionments come from a tie ().
Proof. The greedy rule for seats repeats the first steps of the rule for seats. For the second part, take for and for , so that for all , and the same for . Suppose and . Then , and , so
So all are equal, and gives , a tie.
Complexity
Building the heap takes time, and each of the seats costs , so the total is , with extra space. A linear scan for the largest key instead of the heap gives . Cheng and Eppstein (2014) solve every divisor method in time, independent of : they start from an estimate of near , which leaves only seats to place, and they place them by selection. The comparisons use products of numbers of bits.
Properties
- Lower quota. Every party gets at least . Jefferson's method is the only divisor method with this property (Balinski and Young 1975).
- Upper quota fails. With votes and , the method gives , while the quota of party 1 is . There is no tie in this example.
- House monotone. One more seat never takes a seat from anyone, so there is no Alabama paradox.
- Population monotone. If party gains votes and party loses votes, does not lose a seat to , except through a tie.
- Bias to large parties. Rounding down favors the parties with many seats. In the example, the largest party gets its upper quota, and the smallest gets nothing.
- Least cost. The result minimizes , because the cost of the next seat of party , , grows with , and the method always takes the cheapest next seat.
Variants and improvements
- Other divisors. The Sainte-Lague method divides by and rounds at one half. The Huntington-Hill method rounds at the geometric mean . Adams's method rounds up, the mirror of Jefferson's, and favors small parties.
- Thresholds. Many laws give no seats to lists below a share of the votes, such as 3 or 5 percent, and run the method on the others.
- Biproportional apportionment. Seats are apportioned to parties and to districts at the same time, with one divisor per party and one per district.
- A quota with a cap. Balinski and Young's quota method gives seats one at a time like D'Hondt, but only to parties still below their upper quota.
When to use it
Use the D'Hondt method for party lists when stable majorities matter more than exact proportionality for small parties, or when the law asks for it. It is simple to compute and to check, it has no paradoxes, and it never denies a party its lower quota. If small parties must be treated as fairly as large ones, the Sainte-Lague method is unbiased. If every party must stay within quota, use Hamilton's method, at the price of its paradoxes.
Pitfalls
- Floating point. Quotients such as and are not exact in floating point, and ties like the three-way tie at 20000 in the example can be decided by rounding errors. Compare with .
- Ties. The law must say how to break a tie for the last seat. This page gives it to the smallest index; real laws often use the larger vote total or a lot.
- Divisors from 1. The divisors are . With divisors (the Imperiali method) or (a modified Sainte-Lague), the result differs.
- Zero votes. A party with no votes has key 0. It can get a seat only if every other key is 0, which needs , an invalid input.
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/dhondt-method/. The authors are listed in prov/0Y9DP6.jsonl.
import heapq
from typing import Sequence
class _Key:
"""The priority p / d of state i in a min-heap: a larger quotient comes first, then a smaller index.
Quotients are compared by cross multiplication, never by division.
"""
__slots__ = ("p", "d", "i")
def __init__(self, p: int, d: int, i: int) -> None:
self.p, self.d, self.i = p, d, i
def __lt__(self, other: "_Key") -> bool:
x, y = self.p * other.d, other.p * self.d
if x != y:
return x > y
return self.i < other.i
def dhondt_method(votes: Sequence[int], h: int) -> list[int]:
"""Apportion h seats by the D'Hondt method (Jefferson's method).
votes are nonnegative integers with a positive sum. Each of the h seats goes to the party
with the largest quotient votes[i] / (a[i] + 1), where a[i] is its number of seats so far,
the smallest index among ties. A heap holds one quotient per party.
"""
if h < 0 or any(v < 0 for v in votes) or sum(votes) <= 0:
raise ValueError("need h >= 0, votes >= 0 and a positive total")
n = len(votes)
a = [0] * n # @line L1
heap = [_Key(v, 1, i) for i, v in enumerate(votes)] # @line L2
heapq.heapify(heap)
for _ in range(h): # @line L3
i = heapq.heappop(heap).i # @line L4
a[i] += 1 # @line L5
heapq.heappush(heap, _Key(votes[i], a[i] + 1, i)) # @line L6
return a # @line L7
test_dhondt_method.py 139 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/dhondt-method/. The authors are listed in prov/0Y9DP6.jsonl.
import random
import unittest
from fractions import Fraction
from dhondt_method import dhondt_method
def compositions(h, n):
"""Yield every list of n nonnegative integers with sum h."""
if n == 1:
yield [h]
return
for x in range(h + 1):
for rest in compositions(h - x, n - 1):
yield [x] + rest
def by_scan(votes, h):
"""A second implementation: a linear scan for each seat, with Fraction quotients."""
a = [0] * len(votes)
for _ in range(h):
best = max(range(len(votes)), key=lambda i: (Fraction(votes[i], a[i] + 1), -i))
a[best] += 1
return a
def gap(votes, a):
"""Return (low, high): the largest votes / (seats + 1) and the smallest votes / seats.
a is a D'Hondt apportionment exactly when low <= high; it is the only one when low < high.
"""
low = max(Fraction(v, x + 1) for v, x in zip(votes, a))
high = min((Fraction(v, x) for v, x in zip(votes, a) if x > 0), default=None)
return low, high
def is_jefferson(votes, a):
low, high = gap(votes, a)
return high is None or low <= high
def quotas(votes, h):
return [Fraction(h * v, sum(votes)) for v in votes]
def floor(x):
return x.numerator // x.denominator
class TestCheckValues(unittest.TestCase):
V = [100000, 80000, 30000, 20000]
def test_eight_seats(self):
self.assertEqual(dhondt_method(self.V, 8), [4, 3, 1, 0])
def test_sequence(self):
got = [dhondt_method(self.V, h) for h in range(1, 8)]
self.assertEqual(got, [[1, 0, 0, 0], [1, 1, 0, 0], [2, 1, 0, 0], [2, 2, 0, 0], [3, 2, 0, 0], [3, 2, 1, 0], [3, 3, 1, 0]])
def test_three_way_tie(self):
a = dhondt_method(self.V, 8)
tied = [Fraction(v, x + 1) for v, x in zip(self.V, a)]
self.assertEqual(tied.count(Fraction(20000)), 3)
self.assertEqual(dhondt_method(self.V, 9), [5, 3, 1, 0])
def test_upper_quota_violation(self):
p, h = [8, 2, 1, 1], 7
self.assertEqual(dhondt_method(p, h), [6, 1, 0, 0])
self.assertEqual(quotas(p, h)[0], Fraction(14, 3))
low, high = gap(p, [6, 1, 0, 0])
self.assertLess(low, high)
def test_errors(self):
for p, h in (([0, 0], 3), ([1, -1, 2], 2), ([1, 2], -1)):
with self.assertRaises(ValueError):
dhondt_method(p, h)
class TestProperties(unittest.TestCase):
def test_against_scan_and_divisor_form(self):
rng = random.Random(31)
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(0, 30)
a = dhondt_method(p, h)
self.assertEqual(sum(a), h)
self.assertEqual(a, by_scan(p, h))
self.assertTrue(is_jefferson(p, a))
low, high = gap(p, a)
if high is not None and low < high and low > 0:
lam = (low + high) / 2
self.assertEqual([floor(Fraction(v) / lam) for v in p], a)
for x, q in zip(a, quotas(p, h)):
self.assertGreaterEqual(x, floor(q))
def test_brute_force(self):
rng = random.Random(32)
for _ in range(250):
n = rng.randint(1, 4)
p = [rng.randint(1, 60) for _ in range(n)]
h = rng.randint(0, 9)
a = dhondt_method(p, h)
divisor = [b for b in compositions(h, n) if is_jefferson(p, b)]
self.assertIn(a, divisor)
low, high = gap(p, a)
if high is None or low < high:
self.assertEqual(divisor, [a])
cost = lambda b: sum(Fraction(x * (x + 1), v) for x, v in zip(b, p))
self.assertEqual(cost(a), min(cost(b) for b in compositions(h, n)))
def test_house_and_population_monotone(self):
rng = random.Random(33)
for _ in range(600):
n = rng.randint(2, 5)
p = [rng.randint(1, 100) for _ in range(n)]
h = rng.randint(1, 15)
a = dhondt_method(p, h)
b = dhondt_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 = dhondt_method(p2, h)
l1, h1 = gap(p, a)
l2, h2 = gap(p2, a2)
if (h1 is not None and l1 == h1) or (h2 is not None and l2 == h2):
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
Thomas Jefferson proposed the method for the US House after the census of 1790. After Washington vetoed Hamilton's method in 1792, Congress adopted Jefferson's, and it was used until the census of 1830. Its bias to large states led to its end: after the census of 1830 it gave New York 40 seats, while its quota was about 38.6. Victor D'Hondt, a Belgian lawyer, proposed the same rule for party lists in the 1870s and 1880s, in the highest averages form, and Belgium adopted it. It is now one of the most used rules for party-list elections, for example in Spain and Portugal, and Balinski and Young (1975, 1982) showed that it is the only divisor method that satisfies lower quota.
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 3 (The Methods of Jefferson and Hamilton), Chapter 10 (Staying within the Quota) and Appendix A (The Theory of Apportionment)
- M. L. Balinski and H. P. Young, The quota method of apportionment, American Mathematical Monthly 82(7) (1975), 701-730, Jefferson's method is the only divisor method that satisfies lower quota
- 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
Where this leads
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 0Y9DP6 Edit this page History Report an error Markdown