Algorithmcore
Sainte-Lague method
Also called Webster method, major fractions method.
Assigns seats one at a time to the party with the largest votes divided by 2s + 1, where s is its current number of seats.
- 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
- Daniel Webster, Andre Sainte Lague, 1832
The Sainte-Lague 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 , where is its number of seats so far. It is the same as Webster's method, the divisor method that rounds every quotient to the nearest integer. Unlike the D'Hondt method, it favors neither large nor small parties, and it has no Alabama or population paradox, but in rare cases it gives a party less than its lower quota or 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 3, by 5, by 7, and so on, using only odd numbers. Give the seats to the largest numbers in the whole table, one seat for each number. This is the same as finding a number of votes per seat such that, when each party's votes are divided by it and rounded in the usual way, the seats add up to the house size. The usual rounding treats large and small parties alike, so neither group gains on average.
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.
Webster's method in divisor form is: find a divisor such that the numbers , each rounded to the nearest integer, sum to , and give party that rounded number. A number between and rounds up when it is above , the arithmetic mean. The Sainte-Lague rule in highest averages form gives each seat to the party with the largest , or equivalently , 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 other divisors. A priority queue holds one key per party, and two keys and are compared by the integers and .
SAINTE-LAGUE(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 / (2 a_i + 1) - 7
return a
Each pass of lines L3 to L6 gives one seat. Only line L6 differs from the D'Hondt method: it divides by , not by .
Example
Four parties have 100000, 80000, 30000 and 20000 votes, and seats are to be filled. The table lists the quotients , rounded for display; 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 (4) | 20000 (7) |
| 3 | 33333 (3) | 26667 (5) | 10000 | 6667 |
| 5 | 20000 (6) | 16000 (8) | 6000 | 4000 |
| 7 | 14286 | 11429 | 4286 | 2857 |
The result is , the same as Hamilton's method and the Huntington-Hill method give, while the D'Hondt method gives . Seats 6 and 7 tie at 20000; both go to their parties, so the tie does not matter for , but for it gives the seat to party 1. In divisor form, works: , , and round to . With , the method gives , and the Huntington-Hill method gives .
Correctness
The proof shows that the keys of the seats never increase, so the largest key left at the end, , is at most the smallest key that won a seat, . For every strictly between them, puts each strictly between and , so the seats are the rounded quotients. The proof also shows least squares, house monotonicity and population monotonicity, and checks the quota violation below.
Proof: Correctness and properties of the Sainte-Lague 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. For a real that is not of the form , let be the nearest integer.
Claim 1 (the heap). The method of the page gives the same seats as the greedy rule.
Proof. The heap compares with by against , with , and breaks ties by the smaller index. It holds one key per party, : this holds after line L2, where , and lines L4 to L6 remove the key of the winner, add its seat and insert its new key for the new . So line L4 removes the key that the greedy rule picks.
Claim 2 (the keys do not increase). The keys of the seats, in the order they are given, do not increase, and each is at least for the final seats .
Proof. A seat changes only the key of its winner, to a smaller value. So the largest key never increases, and is the largest key at the end.
Claim 3 (divisor form). Let (or if ). Then . If , then for every with and , for every : the method rounds at one half, which is Webster's method. Conversely, a vector with and for some satisfies the same inequalities, so the output is the only Webster apportionment when .
Proof. The last seat of party had the key , which is at least by Claim 2. So . If , then gives , and for , gives . So lies strictly between and , and . The converse and the uniqueness follow as for the D'Hondt method, with and in place of and : if , some and some , and then and , which contradicts .
Claim 4 (least squares). If every , the output minimizes over all integer vectors with . Equivalently, it minimizes , the spread of the seats per voter around the average weighted by the votes.
Proof. , a sum of marginal costs that increase in . As for the D'Hondt method, the sum of any marginal costs taken as prefixes is at least the sum of the smallest ones, and the greedy rule takes the smallest marginal cost left, which is the largest key . For the second form, , which differs from by a constant.
Claim 5 (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
so all are equal and for .
Claim 6 (quota fails). With and , the method gives , while , so party 1 gets less than its lower quota.
Proof. The keys of party 1 for end with and . Those of party 2 are ; of party 3, ; of party 4, . The keys at least are the 13 of party 1 from down to , the 3 keys of party 2, the 2 keys of party 3, and the key of party 4: 19 keys in all, which are the 19 seats. The largest key left is and the smallest key used is , so and there is no tie.
Complexity
As for the D'Hondt method, a binary heap gives time and extra space, a linear scan gives , and the method of Cheng and Eppstein (2014) gives time for any divisor method. The comparisons use products of numbers of bits.
Properties
- Least squares. The result minimizes , which equals : it makes the seats per voter as equal as possible in this weighted sense. The cost of the next seat of party is , which grows with , and the method always takes the cheapest one. This is how Sainte-Lague (1910) derived it.
- No bias. Balinski and Young show that, among divisor methods, Webster's method is the one that favors neither large nor small states on average.
- House and population monotone. One more seat never takes a seat away, and a party that gains votes does not lose a seat to a party that loses votes, except through a tie.
- Quota can fail. With votes and , the method gives , while the quota of party 1 is ; there is no tie. By the Balinski-Young theorem, every population monotone method fails quota for some input with 4 or more parties. With 3 parties, Webster's method stays within quota.
- Small parties. The first divisor 1 makes the first seat cheap, so a party with about half a quota can win a seat.
Variants and improvements
- Modified Sainte-Lague. The first divisor is raised from 1 to a larger value, such as 1.4 in Norway, so that a party needs more votes for its first seat.
- Sainte-Lague/Schepers. The rounding form with a divisor found by search, used in Germany. It gives the same seats as the highest averages form, up to ties.
- Other divisor methods. The D'Hondt method rounds down, Adams's method rounds up, the Huntington-Hill method rounds at the geometric mean , and Dean's method at the harmonic mean .
- Biproportional apportionment. Seats are apportioned to parties and districts at once, with Webster rounding and one divisor per row and per column.
When to use it
Use the Sainte-Lague method for party lists or for states when large and small units must be treated alike, and the paradoxes of Hamilton's method are not acceptable. Balinski and Young recommend Webster's method for the US House. If a law must guarantee that no party gets less than its lower quota, use the D'Hondt method; if it must guarantee both quotas, use Hamilton's method and accept the paradoxes.
Pitfalls
- Dividing by in floating point. Use the odd divisors and integer cross products, so that ties are seen as ties.
- Ties. In the example, seats 6 and 7 tie at 20000. If only one seat were left, the tie rule would decide it. State the rule.
- Not the same as Hamilton's method. The two often agree, as in the example, but not always: with votes and , Hamilton's method stays within quota and Sainte-Lague does not.
- Modified first divisor. Laws that use 1.4 or 1.2 as the first divisor are not Webster's method. Check which one the law means.
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/sainte-lague-method/. The authors are listed in prov/YHWSF5.jsonl.
import heapq
from typing import Sequence
class _Key:
"""The priority p / d of party 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 sainte_lague_method(votes: Sequence[int], h: int) -> list[int]:
"""Apportion h seats by the Sainte-Lague method (Webster'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] / (2 a[i] + 1), where a[i] is its number of seats so
far, the smallest index among ties. This is the same as dividing by a[i] + 1/2.
"""
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], 2 * a[i] + 1, i)) # @line L6
return a # @line L7
test_sainte_lague_method.py 145 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/sainte-lague-method/. The authors are listed in prov/YHWSF5.jsonl.
import random
import unittest
from fractions import Fraction
from sainte_lague_method import sainte_lague_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, dividing by a + 1/2."""
a = [0] * len(votes)
for _ in range(h):
best = max(range(len(votes)), key=lambda i: (Fraction(votes[i]) / (a[i] + Fraction(1, 2)), -i))
a[best] += 1
return a
def gap(votes, a):
"""Return (low, high): the largest votes / (2 seats + 1) and the smallest votes / (2 seats - 1).
a is a Sainte-Lague apportionment exactly when low <= high; it is the only one when low < high.
"""
low = max(Fraction(v, 2 * x + 1) for v, x in zip(votes, a))
high = min((Fraction(v, 2 * x - 1) for v, x in zip(votes, a) if x > 0), default=None)
return low, high
def is_webster(votes, a):
low, high = gap(votes, a)
return high is None or low <= high
def tie_free(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
def within_quota(votes, h, a):
return all(floor(q) <= x <= -floor(-q) for x, q in zip(a, quotas(votes, h)))
class TestCheckValues(unittest.TestCase):
V = [100000, 80000, 30000, 20000]
def test_eight_seats(self):
self.assertEqual(sainte_lague_method(self.V, 8), [3, 3, 1, 1])
def test_four_seats(self):
self.assertEqual(sainte_lague_method(self.V, 4), [2, 1, 1, 0])
def test_lower_quota_violation(self):
p, h = [26, 5, 3, 1], 19
a = sainte_lague_method(p, h)
self.assertEqual(a, [13, 3, 2, 1])
self.assertEqual(quotas(p, h)[0], Fraction(494, 35))
self.assertTrue(tie_free(p, a))
def test_errors(self):
for p, h in (([0, 0], 3), ([1, -1, 2], 2), ([1, 2], -1)):
with self.assertRaises(ValueError):
sainte_lague_method(p, h)
class TestProperties(unittest.TestCase):
def test_against_scan_and_divisor_form(self):
rng = random.Random(41)
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 = sainte_lague_method(p, h)
self.assertEqual(sum(a), h)
self.assertEqual(a, by_scan(p, h))
self.assertTrue(is_webster(p, a))
low, high = gap(p, a)
if high is not None and low < high and low > 0:
lam = low + high
self.assertEqual([floor(Fraction(v) / lam + Fraction(1, 2)) for v in p], a)
def test_least_squares_by_brute_force(self):
rng = random.Random(42)
for _ in range(250):
n = rng.randint(1, 4)
p = [rng.randint(1, 60) for _ in range(n)]
h = rng.randint(0, 9)
a = sainte_lague_method(p, h)
webster = [b for b in compositions(h, n) if is_webster(p, b)]
self.assertIn(a, webster)
if tie_free(p, a):
self.assertEqual(webster, [a])
cost = lambda b: sum(Fraction(x * x, v) for x, v in zip(b, p))
self.assertEqual(cost(a), min(cost(b) for b in compositions(h, n)))
def test_three_parties_stay_within_quota(self):
rng = random.Random(43)
for _ in range(3000):
p = [rng.randint(1, 300) for _ in range(3)]
h = rng.randint(1, 40)
a = sainte_lague_method(p, h)
if tie_free(p, a):
self.assertTrue(within_quota(p, h, a))
def test_house_and_population_monotone(self):
rng = random.Random(44)
for _ in range(600):
n = rng.randint(2, 5)
p = [rng.randint(1, 100) for _ in range(n)]
h = rng.randint(1, 15)
a = sainte_lague_method(p, h)
b = sainte_lague_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 = sainte_lague_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
Daniel Webster proposed rounding at one half for the US House in 1832. Congress used his method for the census of 1840, and again for those of 1910 and 1930, when Walter Willcox argued for it as the method of major fractions. André Sainte-Laguë derived the same rule in 1910 as the solution of a least squares problem. Germany has used it for the Bundestag since 2009, and New Zealand uses it for its list seats. Balinski and Young (1982) showed that it is the unbiased divisor method and argued for its use in the US.
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 4 (The Method of Webster), Chapter 9 (Eliminating Bias) and Appendix A (The Theory of Apportionment)
- André Sainte-Laguë, La représentation proportionnelle et la méthode des moindres carrés, Annales scientifiques de l'École Normale Supérieure, 3e série, 27 (1910), 529-542, the rule as the minimum of a sum of squares
- 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 YHWSF5 Edit this page History Report an error Markdown