Algorithm Search⌘K

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
Properties
comparison baseddeterministiciterative
Origin
Daniel Webster, Andre Sainte Lague, 1832

The Sainte-Lague method apportions hh 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 2s+12s + 1, where ss 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 p1,…,pn≥0p_1, \ldots, p_n \ge 0, integers with sum P>0P > 0, and the number of seats hh. The output is integers ai≥0a_i \ge 0 with ∑iai=h\sum_i a_i = h. The quota of party ii is qi=hpi/Pq_i = h p_i / P. See the apportionment problem for the setting and the other methods.

Webster's method in divisor form is: find a divisor λ>0\lambda > 0 such that the numbers pi/λp_i / \lambda, each rounded to the nearest integer, sum to hh, and give party ii that rounded number. A number between aa and a+1a + 1 rounds up when it is above a+1/2a + 1/2, the arithmetic mean. The Sainte-Lague rule in highest averages form gives each seat to the party with the largest pi/(ai+1/2)p_i/(a_i + 1/2), or equivalently pi/(2ai+1)p_i/(2 a_i + 1), 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 pi/dip_i/d_i and pj/djp_j/d_j are compared by the integers pidjp_i d_j and pjdip_j d_i.

  1. SAINTE-LAGUE(p, h) ▷ p = (p_1, ..., p_n) votes, h seats
  2. 1a_i ← 0 for each i
  3. 2Q ← a max-priority queue of the parties, with keys p_i / 1
  4. 3repeat h times
  5. 4i ← extract from Q the largest key, the smallest index among ties
  6. 5a_i ← a_i + 1
  7. 6insert i into Q with key p_i / (2 a_i + 1)
  8. 7return a

Each pass of lines L3 to L6 gives one seat. Only line L6 differs from the D'Hondt method: it divides by 2ai+12 a_i + 1, not by ai+1a_i + 1.

Example

Four parties have 100000, 80000, 30000 and 20000 votes, and h=8h = 8 seats are to be filled. The table lists the quotients pi/dp_i / d, 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 (3,3,1,1)(3, 3, 1, 1), the same as Hamilton's method and the Huntington-Hill method give, while the D'Hondt method gives (4,3,1,0)(4, 3, 1, 0). Seats 6 and 7 tie at 20000; both go to their parties, so the tie does not matter for h=8h = 8, but for h=6h = 6 it gives the seat to party 1. In divisor form, λ=30000\lambda = 30000 works: 100000/30000=3.33100000/30000 = 3.33, 80000/30000=2.6780000/30000 = 2.67, 30000/30000=130000/30000 = 1 and 20000/30000=0.6720000/30000 = 0.67 round to 3,3,1,13, 3, 1, 1. With h=4h = 4, the method gives (2,1,1,0)(2, 1, 1, 0), and the Huntington-Hill method gives (1,1,1,1)(1, 1, 1, 1).

Correctness

The proof shows that the keys of the seats never increase, so the largest key left at the end, ℓ\ell, is at most the smallest key that won a seat, u=min⁡ipi/(2ai−1)u = \min_i p_i/(2 a_i - 1). For every μ\mu strictly between them, λ=2μ\lambda = 2\mu puts each pi/λp_i/\lambda strictly between ai−1/2a_i - 1/2 and ai+1/2a_i + 1/2, 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 p1,…,pn≥0p_1, \ldots, p_n \ge 0 be integers with P=∑ipi>0P = \sum_i p_i > 0, and let h≥0h \ge 0. The quota of party ii is qi=hpi/Pq_i = h p_i / P. The greedy rule gives hh seats one at a time; each goes to the party with the largest key pi/(2ai+1)p_i/(2 a_i + 1), where aia_i is its current number of seats, the smallest index among ties. For a real x≥0x \ge 0 that is not of the form k+1/2k + 1/2, let [x][x] 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 pi/dip_i/d_i with pj/djp_j/d_j by pidjp_i d_j against pjdip_j d_i, with di,dj≥1d_i, d_j \ge 1, and breaks ties by the smaller index. It holds one key per party, pi/(2ai+1)p_i/(2 a_i + 1): this holds after line L2, where ai=0a_i = 0, and lines L4 to L6 remove the key of the winner, add its seat and insert its new key pi/(2ai+1)p_i/(2 a_i + 1) for the new aia_i. 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 ℓ=max⁡ipi/(2ai+1)\ell = \max_i p_i/(2 a_i + 1) for the final seats aa.

Proof. A seat changes only the key of its winner, to a smaller value. So the largest key never increases, and ℓ\ell is the largest key at the end.

Claim 3 (divisor form). Let u=min⁡i:ai≥1pi/(2ai−1)u = \min_{i : a_i \ge 1} p_i/(2 a_i - 1) (or u=∞u = \infty if h=0h = 0). Then ℓ≤u\ell \le u. If ℓ<u\ell < u, then for every μ\mu with ℓ<μ<u\ell < \mu < u and λ=2μ\lambda = 2 \mu, ai=[pi/λ]a_i = [p_i/\lambda] for every ii: the method rounds pi/λp_i/\lambda at one half, which is Webster's method. Conversely, a vector bb with ∑ibi=h\sum_i b_i = h and bi=[pi/λ]b_i = [p_i/\lambda] for some λ\lambda satisfies the same inequalities, so the output is the only Webster apportionment when ℓ<u\ell < u.

Proof. The last seat of party ii had the key pi/(2ai−1)p_i/(2 a_i - 1), which is at least ℓ\ell by Claim 2. So u≥ℓu \ge \ell. If ℓ<μ<u\ell < \mu < u, then pi/(2ai+1)<μp_i/(2 a_i + 1) < \mu gives pi/λ<ai+1/2p_i/\lambda < a_i + 1/2, and for ai≥1a_i \ge 1, μ<pi/(2ai−1)\mu < p_i/(2 a_i - 1) gives pi/λ>ai−1/2p_i/\lambda > a_i - 1/2. So pi/λp_i/\lambda lies strictly between ai−1/2a_i - 1/2 and ai+1/2a_i + 1/2, and ai=[pi/λ]a_i = [p_i/\lambda]. The converse and the uniqueness follow as for the D'Hondt method, with 2b+12 b + 1 and 2b−12 b - 1 in place of b+1b + 1 and bb: if b≠ab \ne a, some bi>aib_i > a_i and some bj<ajb_j < a_j, and then λb/2≤pi/(2bi−1)≤pi/(2ai+1)≤ℓ\lambda_b / 2 \le p_i/(2 b_i - 1) \le p_i/(2 a_i + 1) \le \ell and λb/2≥pj/(2bj+1)≥pj/(2aj−1)≥u\lambda_b / 2 \ge p_j/(2 b_j + 1) \ge p_j/(2 a_j - 1) \ge u, which contradicts ℓ<u\ell < u.

Claim 4 (least squares). If every pi>0p_i > 0, the output minimizes G(b)=∑ibi2/piG(b) = \sum_i b_i^2/p_i over all integer vectors b≥0b \ge 0 with ∑ibi=h\sum_i b_i = h. Equivalently, it minimizes ∑ipi(bi/pi−h/P)2\sum_i p_i (b_i/p_i - h/P)^2, the spread of the seats per voter around the average weighted by the votes.

Proof. G(b)=∑i∑x=0bi−1(2x+1)/piG(b) = \sum_i \sum_{x = 0}^{b_i - 1} (2x + 1)/p_i, a sum of marginal costs (2x+1)/pi(2x + 1)/p_i that increase in xx. As for the D'Hondt method, the sum of any hh marginal costs taken as prefixes is at least the sum of the hh smallest ones, and the greedy rule takes the smallest marginal cost left, which is the largest key pi/(2ai+1)p_i/(2 a_i + 1). For the second form, ∑ipi(bi/pi−h/P)2=G(b)−2(h/P)∑ibi+h2/P=G(b)−h2/P\sum_i p_i (b_i/p_i - h/P)^2 = G(b) - 2 (h/P) \sum_i b_i + h^2/P = G(b) - h^2/P, which differs from GG by a constant.

Claim 5 (house and population monotone). The seats for h+1h + 1 are those for hh plus one seat. If pi′≥pip'_i \ge p_i and pj′≤pjp'_j \le p_j, then ai′<aia'_i < a_i and aj′>aja'_j > a_j cannot both hold, unless one of the two apportionments has ℓ=u\ell = u.

Proof. The first part holds because the greedy rule for h+1h + 1 repeats the steps for hh. For the second, take μ∈[ℓ,u]\mu \in [\ell, u] for pp and μ′∈[ℓ′,u′]\mu' \in [\ell', u'] for p′p'. If ai′<aia'_i < a_i and aj′>aja'_j > a_j, then

μ′≥pi′2ai′+1≥pi2ai−1≥μ≥pj2aj+1≥pj′2aj′−1≥μ′,\mu' \ge \frac{p'_i}{2 a'_i + 1} \ge \frac{p_i}{2 a_i - 1} \ge \mu \ge \frac{p_j}{2 a_j + 1} \ge \frac{p'_j}{2 a'_j - 1} \ge \mu',

so all are equal and u≤ℓu \le \ell for pp.

Claim 6 (quota fails). With p=(26,5,3,1)p = (26, 5, 3, 1) and h=19h = 19, the method gives (13,3,2,1)(13, 3, 2, 1), while q1=494/35≈14.11q_1 = 494/35 \approx 14.11, so party 1 gets less than its lower quota.

Proof. The keys 26/(2x+1)26/(2x + 1) of party 1 for x=0,…,13x = 0, \ldots, 13 end with 26/25=1.0426/25 = 1.04 and 26/27≈0.96326/27 \approx 0.963. Those of party 2 are 5,5/3,1,5/7≈0.7145, 5/3, 1, 5/7 \approx 0.714; of party 3, 3,1,3/5=0.63, 1, 3/5 = 0.6; of party 4, 1,1/31, 1/3. The keys at least 11 are the 13 of party 1 from 2626 down to 26/2526/25, the 3 keys 5,5/3,15, 5/3, 1 of party 2, the 2 keys 3,13, 1 of party 3, and the key 11 of party 4: 19 keys in all, which are the 19 seats. The largest key left is 26/2726/27 and the smallest key used is 11, so ℓ<u\ell < u and there is no tie.

Complexity

As for the D'Hondt method, a binary heap gives O(n+hlog⁡n)O(n + h \log n) time and O(n)O(n) extra space, a linear scan gives O(nh)O(n h), and the method of Cheng and Eppstein (2014) gives O(n)O(n) time for any divisor method. The comparisons use products of numbers of O(log⁡P+log⁡h)O(\log P + \log h) bits.

Properties

  • Least squares. The result minimizes ∑iai2/pi\sum_i a_i^2/p_i, which equals ∑ipi(ai/pi−h/P)2+h2/P\sum_i p_i (a_i/p_i - h/P)^2 + h^2/P: it makes the seats per voter as equal as possible in this weighted sense. The cost of the next seat of party ii is (2ai+1)/pi(2 a_i + 1)/p_i, which grows with aia_i, 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 (26,5,3,1)(26, 5, 3, 1) and h=19h = 19, the method gives (13,3,2,1)(13, 3, 2, 1), while the quota of party 1 is 494/35≈14.11494/35 \approx 14.11; 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 a(a+1)\sqrt{a(a + 1)}, and Dean's method at the harmonic mean 2a(a+1)/(2a+1)2a(a + 1)/(2a + 1).
  • 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 a+1/2a + 1/2 in floating point. Use the odd divisors 2a+12a + 1 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 (26,5,3,1)(26, 5, 3, 1) and h=19h = 19, 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.

sainte_lague_method.pyI0RawGitHub
# 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

  1. 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)
  2. 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
  3. 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.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example