Algorithm Search⌘K

Algorithmcore

Selfridge-Conway procedure

Also called Selfridge-Conway discrete procedure.

A finite protocol that gives an envy-free division of a cake among three agents with at most five cuts. Each agent gets at most two intervals.

Time
Θ(1) worst
Space
Θ(1) extra
Where
n = the number of agents (here 3)
Properties
deterministicofflineiterative

The Selfridge-Conway procedure is a finite protocol that divides a cake among three agents so that the division is envy-free: no agent values another agent's piece more than its own. It uses at most five cuts, and each agent gets at most two intervals. It differs from the three other envy-free protocols of this area as follows: the Stromquist moving knife gives connected pieces to three agents but needs continuously moving knives, the Brams-Taylor procedure works for any number of agents but has no bound on its steps, and the Aziz-Mackenzie protocol works for any number of agents with a bound that is a tower of exponentials. In the Robertson-Webb query model, the procedure makes at most 5 cut queries and 12 eval queries.

In plain words

Three friends share a cake. The first friend cuts it into three pieces that look equal to her. The second friend looks at the pieces. If two pieces tie for best in her eyes, nothing changes. If one piece looks bigger than all others, she trims a thin slice from it until it ties with her second best piece. The slice is put aside for now.

The third friend chooses first, then the second friend, then the first friend. The second friend must take the trimmed piece if it is still there. So the first friend always gets an untrimmed piece.

Now the slice is shared. The friend who did not get the trimmed piece cuts the slice into three equal parts in her eyes. The friend with the trimmed piece chooses first, then the first friend, then the cutter of the slice.

The problem

The cake is [0,1][0, 1]. Agents 1, 2 and 3 have nonnegative, additive and nonatomic valuations V1,V2,V3V_1, V_2, V_3 with Vi([0,1])=1V_i([0, 1]) = 1. The protocol must output pieces X1,X2,X3X_1, X_2, X_3, each a finite union of intervals, that are disjoint and cover the cake, with Vi(Xi)≥Vi(Xj)V_i(X_i) \ge V_i(X_j) for all ii and jj. Pieces need not be connected. An envy-free division is also proportional, because 3Vi(Xi)≥Vi(X1)+Vi(X2)+Vi(X3)=13 V_i(X_i) \ge V_i(X_1) + V_i(X_2) + V_i(X_3) = 1. The protocol learns the valuations only by the queries Evali(a,b)=Vi([a,b])\mathrm{Eval}_i(a, b) = V_i([a, b]) and Cuti(a,t)\mathrm{Cut}_i(a, t), the least x≥ax \ge a with Vi([a,x])=tV_i([a, x]) = t.

The algorithm

Agent 1 is the cutter, agent 2 the trimmer and agent 3 the first chooser. "Takes the piece it values most" means one eval query per piece that is left; ties go to the leftmost piece.

  1. SELFRIDGE-CONWAY(agents 1, 2, 3)
  2. 1c ← CUT_1(0, 1/3); d ← CUT_1(c, 1/3); the pieces are [0, c], [c, d], [d, 1]
  3. 2P ← the piece agent 2 values most; Q ← the piece agent 2 values second ▷ three evals
  4. 3if V_2(P) > V_2(Q) then
  5. 4[a, b] ← P; x ← CUT_2(a, V_2(Q)); P ← [a, x]; L ← [x, b] ▷ trim P, set L aside
  6. 5else L ← nothing ▷ no trim needed
  7. 6agent 3 takes the piece it values most
  8. 7if P is still there then agent 2 takes P else agent 2 takes its better piece of the two left
  9. 8agent 1 takes the last piece
  10. 9if L is nothing then return
  11. 10T ← the agent of {2, 3} that holds P; U ← the other one
  12. 11U cuts L into three parts that U values at V_U(L)/3 each ▷ one eval, two cuts
  13. 12T takes the part it values most, then agent 1 takes its better part, U takes the last

Stage one is lines L1 to L8 and divides the cake minus the trimmings LL. Stage two is lines L10 to L12 and divides LL. Agent 2 needs no query in line L7, because it knows its values from line L2.

Example

Take six equal segments of [0,1][0, 1] with piecewise-constant densities. Agent 1 is uniform. Agent 2 has densities proportional to (3,1,1,1,2,4)(3, 1, 1, 1, 2, 4), and agent 3 to (1,2,3,3,2,1)(1, 2, 3, 3, 2, 1).

step what happens values
L1 agent 1 cuts at 1/31/3 and 2/32/3 A=[0,1/3]A = [0, 1/3], B=[1/3,2/3]B = [1/3, 2/3], C=[2/3,1]C = [2/3, 1]
L2 agent 2 values A,B,CA, B, C 1/31/3, 1/61/6, 1/21/2, so P=CP = C and Q=AQ = A
L4 agent 2 trims CC to value 1/31/3 P=[2/3,11/12]P = [2/3, 11/12], L=[11/12,1]L = [11/12, 1]
L6 agent 3 values A,B,PA, B, P 1/41/4, 1/21/2, 5/245/24, so it takes BB
L7 PP is still there agent 2 takes PP
L8 the last piece agent 1 takes AA
L10 TT = agent 2, UU = agent 3
L11 agent 3 values LL at 1/241/24 and cuts at 17/1817/18 and 35/3635/36 three parts of value 1/721/72 to agent 3
L12 agent 2 takes [11/12,17/18][11/12, 17/18], agent 1 takes [17/18,35/36][17/18, 35/36] agent 3 gets [35/36,1][35/36, 1]

The final values, with row ii giving ViV_i of each agent's piece:

X1X_1 X2X_2 X3X_3
V1V_1 13/36 5/18 13/36
V2V_2 7/18 7/18 2/9
V3V_3 19/72 2/9 37/72

Each row has its largest entry on the diagonal, so the division is envy-free. Agent 1 ties with agent 3, and agent 2 ties with agent 1. The run made 5 cut queries and 12 eval queries. With agent 2 as (1,1,2,2,3,3)(1, 1, 2, 2, 3, 3) and agent 3 as (1,1,1,2,4,3)(1, 1, 1, 2, 4, 3) instead, agent 3 takes the trimmed piece [2/3,8/9][2/3, 8/9], agent 2 takes [1/3,2/3][1/3, 2/3], and then agent 2 cuts the trimmings.

Correctness

The proof checks each agent against each other agent, stage by stage, and adds the two stages by additivity.

  • Agents 2 and 3 in stage one. Agent 3 chooses first. Agent 2 values the trimmed piece equal to its second best piece and at least as much as the third piece, so whichever piece it gets in line L7 is a best piece for it.
  • Agent 1 in stage one. Agent 2 or agent 3 holds the trimmed piece, so agent 1 gets an untrimmed piece of value 1/31/3, which is at least the value of any stage one piece to agent 1.
  • Stage two. TT chooses first, so it envies nobody's part. UU made three equal parts, so it envies nobody's part. Agent 1 chooses before UU. Against TT, agent 1 uses a stronger fact: the trimmed piece and all of LL together are worth exactly 1/31/3 to agent 1, because they make up one of agent 1's equal pieces. So TT's whole piece is worth at most 1/31/3 to agent 1, which is what agent 1 already has from stage one.

The full proof, with the existence of each cut and the query count, is in proofs/correctness.md.

Proof: The Selfridge-Conway procedure is envy-free and uses at most five cutsfull

Setting. The cake is [0,1][0, 1]. Agents 1, 2 and 3 have nonnegative, additive and nonatomic valuations V1,V2,V3V_1, V_2, V_3 with Vi([0,1])=1V_i([0, 1]) = 1. Single points have value 0, so two intervals that meet at one end point do not overlap in value. For a∈[0,1]a \in [0, 1] and 0≤t≤Vi([a,1])0 \le t \le V_i([a, 1]), the query Cuti(a,t)\mathrm{Cut}_i(a, t) returns the least x≥ax \ge a with Vi([a,x])=tV_i([a, x]) = t; such an xx exists because x↦Vi([a,x])x \mapsto V_i([a, x]) is continuous and nondecreasing. We refer to the lines L1 to L12 of the page. Let A,B,CA, B, C be the three pieces of line L1, let PP and QQ be the best and second best piece of agent 2 at line L2, and let RR be the third piece.

Lemma 1 (the cuts exist). Every cut query of the procedure has an answer.

Proof. Line L1 asks Cut1(0,1/3)\mathrm{Cut}_1(0, 1/3) and then Cut1(c,1/3)\mathrm{Cut}_1(c, 1/3), where V1([c,1])=2/3≥1/3V_1([c, 1]) = 2/3 \ge 1/3. Line L4 asks Cut2(a,V2(Q))\mathrm{Cut}_2(a, V_2(Q)) with P=[a,b]P = [a, b], and V2(Q)<V2(P)=V2([a,b])V_2(Q) < V_2(P) = V_2([a, b]). Line L11 asks CutU(a′,w)\mathrm{Cut}_U(a', w) and then CutU(y,w)\mathrm{Cut}_U(y, w) with L=[a′,b′]L = [a', b'] and w=VU(L)/3w = V_U(L)/3; here VU([a′,b′])=3wV_U([a', b']) = 3w and VU([y,b′])=2wV_U([y, b']) = 2w. □\square

Lemma 2 (stage one). After line L8, the three pieces P′,Q,RP', Q, R (with P′=PP' = P when there is no trim) form, together with the trimmings LL, a partition of [0,1][0, 1]. The values satisfy:

  1. V1(Q)=V1(R)=1/3≥V1(P′)V_1(Q) = V_1(R) = 1/3 \ge V_1(P'), and V1(P′)+V1(L)=1/3V_1(P') + V_1(L) = 1/3;
  2. V2(P′)=V2(Q)≥V2(R)V_2(P') = V_2(Q) \ge V_2(R);
  3. agent 3 holds a piece that it values at least as much as each of the other two pieces of stage one;
  4. agent 1 holds QQ or RR, never the trimmed piece P′P' when a trim happened.

Proof. Line L1 makes three pieces of value 1/31/3 each to agent 1. Line L4 replaces P=[a,b]P = [a, b] by P′=[a,x]P' = [a, x] and L=[x,b]L = [x, b], so V1(P′)+V1(L)=V1(P)=1/3V_1(P') + V_1(L) = V_1(P) = 1/3, and QQ and RR keep the value 1/31/3. This gives item 1. Item 2: with a trim, V2(P′)=V2(Q)V_2(P') = V_2(Q) by line L4, and V2(Q)≥V2(R)V_2(Q) \ge V_2(R) by the choice of QQ; without a trim, V2(P)=V2(Q)≥V2(R)V_2(P) = V_2(Q) \ge V_2(R) by line L3. Item 3 holds because agent 3 chooses first, in line L6, from all three pieces. For item 4: if agent 3 did not take P′P', line L7 gives P′P' to agent 2. If agent 3 took P′P', then P′P' is gone. In both cases P′P' is not left for agent 1 in line L8. □\square

Lemma 3 (agent 2 in stage one). Agent 2 holds a piece that it values at least as much as each of the other two pieces of stage one.

Proof. If agent 2 takes P′P' in line L7, its value is V2(P′)=V2(Q)≥V2(R)V_2(P') = V_2(Q) \ge V_2(R) by Lemma 2, so it is a best piece. If agent 3 took P′P', agent 2 takes the better of QQ and RR by its own values, and that value is V2(Q)=V2(P′)V_2(Q) = V_2(P'). □\square

Theorem 1 (envy-free). The final division is envy-free: Vi(Xi)≥Vi(Xj)V_i(X_i) \ge V_i(X_j) for all i,j∈{1,2,3}i, j \in \{1, 2, 3\}.

Proof. Without a trim, line L9 returns after stage one. Each XiX_i is one of A,B,CA, B, C. Agents 2 and 3 do not envy by Lemma 2 item 3 and Lemma 3, and agent 1 values all three pieces at 1/31/3.

With a trim, let TT be the agent of {2,3}\{2, 3\} that holds P′P' and UU the other one (line L10). Line L11 cuts LL into L1,L2,L3L_1, L_2, L_3 with VU(Lk)=VU(L)/3V_U(L_k) = V_U(L)/3. In line L12, TT takes a part first, agent 1 takes a part second, and UU takes the last part. Write SiS_i for the stage one piece and ZiZ_i for the part of LL of agent ii, so Xi=Si∪ZiX_i = S_i \cup Z_i and Vi(Xi)=Vi(Si)+Vi(Zi)V_i(X_i) = V_i(S_i) + V_i(Z_i) by additivity.

  • Agent TT: VT(ST)≥VT(Sj)V_T(S_T) \ge V_T(S_j) for each jj by Lemma 2 item 3 or Lemma 3, and VT(ZT)≥VT(Zj)V_T(Z_T) \ge V_T(Z_j) because TT chooses first among all three parts.
  • Agent UU: VU(SU)≥VU(Sj)V_U(S_U) \ge V_U(S_j) by the same lemmas, and VU(Zj)=VU(L)/3V_U(Z_j) = V_U(L)/3 for every jj because UU made the parts.
  • Agent 1: against UU, V1(S1)=1/3≥V1(SU)V_1(S_1) = 1/3 \ge V_1(S_U) by Lemma 2 items 1 and 4, and V1(Z1)≥V1(ZU)V_1(Z_1) \ge V_1(Z_U) because agent 1 chooses before UU. Against TT, V1(XT)=V1(P′)+V1(ZT)≤V1(P′)+V1(L)=1/3=V1(S1)≤V1(X1)V_1(X_T) = V_1(P') + V_1(Z_T) \le V_1(P') + V_1(L) = 1/3 = V_1(S_1) \le V_1(X_1), by Lemma 2 item 1 and monotonicity.

Adding the two inequalities for TT and for UU gives Vi(Xi)≥Vi(Xj)V_i(X_i) \ge V_i(X_j) in each case. □\square

Theorem 2 (partition and pieces). The pieces X1,X2,X3X_1, X_2, X_3 are disjoint up to end points and cover [0,1][0, 1]. Each XiX_i is a union of at most two intervals.

Proof. Stage one splits [0,1][0, 1] into P′,Q,RP', Q, R and LL (Lemma 2), and line L11 splits LL into three intervals. Each agent gets one interval in stage one and at most one interval in stage two. □\square

Theorem 3 (cost). The procedure makes at most 5 cut queries and at most 12 eval queries.

Proof. Cuts: two in line L1, at most one in line L4 and at most two in line L11. Evals: three by agent 2 in line L2 and three by agent 3 in line L6; agent 2 needs no new query in line L7, because it knows its values from line L2 and V2(P′)=V2(Q)V_2(P') = V_2(Q). In stage two, UU makes one eval in line L11, TT makes three and agent 1 makes two in line L12. The total is at most 3+3+1+3+2=123 + 3 + 1 + 3 + 2 = 12. □\square

Complexity

The number of agents is fixed at three, so the cost is a constant.

  • At most 5 cut queries: two in line L1, one in line L4 and two in line L11. Without a trim, only 2.
  • At most 12 eval queries: three each in lines L2 and L6, one in line L11, and three plus two in line L12.
  • The time outside the queries is Θ(1)\Theta(1), and the output has at most six intervals.

Properties

  • Envy-free and proportional. Envy-freeness implies proportionality for additive valuations.
  • Not connected. Agents 2 and 3 can each get two intervals, and so can agent 1. With connected pieces, no finite protocol in the Robertson-Webb model can be envy-free for three agents, by Stromquist's impossibility theorem.
  • Safe for an honest agent. Each agent that answers by its own valuation gets an envy-free share, whatever the other agents do. The proof for agent ii uses only agent ii's own answers and the order of choices.
  • Not equitable and not Pareto optimal in general. In the example, the agents get 13/3613/36, 7/187/18 and 37/7237/72.

Variants and improvements

  • More agents. Brams and Taylor found the first finite envy-free protocol for every number of agents, with no bound on its steps. Aziz and Mackenzie gave a bounded protocol for four agents and then for any number of agents.
  • Connected pieces. The Stromquist moving knife gives each of three agents one interval, at the price of continuous knives.
  • Chores. For a burden that each agent wants to minimize, the roles of choosing change; see chore division.
  • Proportional only. If proportionality is enough, divide and choose style protocols such as the Dubins-Spanier moving knife need fewer queries per agent and give connected pieces.

When to use it

Use the Selfridge-Conway procedure when exactly three agents must share a divisible good without envy, and each agent accepts a piece made of two intervals. It is short enough to run by hand. For connected pieces, use a moving-knife method; for more agents, the known envy-free protocols are much larger.

Pitfalls

  • Letting agent 2 skip the trimmed piece. If agent 3 leaves the trimmed piece and agent 2 may choose freely, agent 1 can be left with it. Agent 1 values it at less than 1/31/3 and then envies.
  • The wrong cutter in stage two. The agent without the trimmed piece cuts the trimmings, and the holder of the trimmed piece chooses first. If the holder cut, the other agent would choose before agent 1, and agent 1 could envy it, because agent 1's extra margin holds only against the holder of the trimmed piece.
  • Agent 1 choosing last in stage two. Agent 1 must choose before UU; agent 1 has no margin against UU from stage one.
  • Stopping after stage one. Stage one alone leaves the trimmings unassigned. The partial division is envy-free, but it is not a division of the whole cake.
  • Trimming on a tie. When agent 2's two best pieces tie, there is nothing to trim. A trim of zero value gives empty trimmings and the same result with one more query.

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.

agtcake.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Shared helper of the algorithmic game theory pages of https://algorithm.tamnd.com/. The authors are listed in prov/.
"""Piecewise-constant cake valuations and a Robertson-Webb query oracle.

The cake is the interval [0, 1]. A valuation is a list of k nonnegative
Fraction densities d[0], ..., d[k - 1]. Density d[j] holds on the segment
[j/k, (j + 1)/k], so that segment has value d[j] / k. A valuation is
normalized when the value of the whole cake is 1, that is, sum(d) = k.

All numbers are Fraction, so every value and every cut point is exact.
This module holds no cake cutting protocol.
"""

from fractions import Fraction
from random import Random
from typing import Optional, Sequence

Valuation = list[Fraction]
Interval = tuple[Fraction, Fraction]

ZERO = Fraction(0)
ONE = Fraction(1)


def normalize(dens: Sequence) -> Valuation:
    """Scale nonnegative densities so that the whole cake has value 1."""
    d = [Fraction(x) for x in dens]
    if not d:
        raise ValueError("a valuation needs at least one segment")
    if any(x < 0 for x in d):
        raise ValueError("densities must be nonnegative")
    total = sum(d)
    if total == 0:
        raise ValueError("densities must not all be zero")
    k = len(d)
    return [x * k / total for x in d]


def _check_point(x: Fraction) -> None:
    if x < 0 or x > 1:
        raise ValueError("a point of the cake must be in [0, 1]")


def value(v: Valuation, a, b) -> Fraction:
    """Return the value of the interval [a, b] under v. Needs 0 <= a <= b <= 1."""
    a, b = Fraction(a), Fraction(b)
    _check_point(a)
    _check_point(b)
    if a > b:
        raise ValueError("value(v, a, b) needs a <= b")
    k = len(v)
    total = ZERO
    j = min(int(a * k), k - 1)
    while j < k:
        lo = Fraction(j, k)
        if lo >= b:
            break
        hi = Fraction(j + 1, k)
        left = a if a > lo else lo
        right = b if b < hi else hi
        if right > left:
            total += v[j] * (right - left)
        j += 1
    return total


def cut(v: Valuation, a, t) -> Optional[Fraction]:
    """Return the least x >= a with value(v, a, x) = t, or None if there is none."""
    a, t = Fraction(a), Fraction(t)
    _check_point(a)
    if t < 0:
        raise ValueError("cut(v, a, t) needs t >= 0")
    if t == 0:
        return a
    k = len(v)
    pos = a
    rest = t
    j = min(int(a * k), k - 1)
    while j < k:
        hi = Fraction(j + 1, k)
        d = v[j]
        if d > 0 and hi > pos:
            here = d * (hi - pos)
            if rest <= here:
                return pos + rest / d
            rest -= here
        if hi > pos:
            pos = hi
        j += 1
    return None


def piece_value(v: Valuation, piece: Sequence[Interval]) -> Fraction:
    """Return the value of a piece, given as a list of intervals (a, b)."""
    return sum((value(v, a, b) for a, b in piece), ZERO)


def random_valuation(rng: Random, k: int, hi: int) -> Valuation:
    """Return a normalized valuation with k segments and integer densities in 0..hi."""
    if k < 1 or hi < 1:
        raise ValueError("random_valuation needs k >= 1 and hi >= 1")
    while True:
        dens = [rng.randint(0, hi) for _ in range(k)]
        if any(dens):
            return normalize(dens)


class Oracle:
    """Answer Robertson-Webb queries for a list of valuations and count them.

    eval_query(i, a, b) returns V_i([a, b]). cut_query(i, a, t) returns the
    least x with V_i([a, x]) = t, or None. The lists evals and cuts hold the
    number of queries of each kind for each agent.
    """

    def __init__(self, valuations: Sequence[Valuation]) -> None:
        self.valuations = [list(v) for v in valuations]
        n = len(self.valuations)
        self.evals = [0] * n
        self.cuts = [0] * n

    @property
    def n(self) -> int:
        return len(self.valuations)

    def eval_query(self, i: int, a, b) -> Fraction:
        self.evals[i] += 1
        return value(self.valuations[i], a, b)

    def cut_query(self, i: int, a, t) -> Optional[Fraction]:
        self.cuts[i] += 1
        return cut(self.valuations[i], a, t)

    def total_evals(self) -> int:
        return sum(self.evals)

    def total_cuts(self) -> int:
        return sum(self.cuts)

    def total(self) -> int:
        return self.total_evals() + self.total_cuts()
selfridge_conway_procedure.py 73 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/selfridge-conway-procedure/. The authors are listed in prov/9GXB9K.jsonl.

from fractions import Fraction

from agtcake import Oracle

Interval = tuple[Fraction, Fraction]
Piece = list[Interval]


def _best(oracle: Oracle, i: int, pieces: list[Interval]) -> int:
    """Return the index of the piece agent i values most (ties: the smallest index).

    One eval query per piece.
    """
    vals = [oracle.eval_query(i, a, b) for a, b in pieces]
    top = max(vals)
    return vals.index(top)


def selfridge_conway(oracle: Oracle) -> dict[int, Piece]:
    """The Selfridge-Conway procedure for three agents.

    Agents 0, 1 and 2 of the code are agents 1, 2 and 3 of the page: agent 0
    cuts, agent 1 trims and agent 2 chooses first. The result maps each agent
    to its piece, a list of at most two intervals.
    """
    if oracle.n != 3:
        raise ValueError("the Selfridge-Conway procedure is for three agents")
    third = Fraction(1, 3)
    c = oracle.cut_query(0, 0, third)  # @line L1
    d = oracle.cut_query(0, c, third)
    pieces: list[Interval] = [(Fraction(0), c), (c, d), (d, Fraction(1))]
    vals = [oracle.eval_query(1, a, b) for a, b in pieces]  # @line L2
    order = sorted(range(3), key=lambda j: (-vals[j], j))
    p, q = order[0], order[1]
    trimmings: Interval | None
    if vals[p] > vals[q]:  # @line L3
        a, b = pieces[p]
        x = oracle.cut_query(1, a, vals[q])  # @line L4
        pieces[p] = (a, x)
        trimmings = (x, b)
    else:
        trimmings = None  # @line L5
    alloc: dict[int, Piece] = {}
    left = [0, 1, 2]
    j3 = left[_best(oracle, 2, [pieces[j] for j in left])]  # @line L6
    alloc[2] = [pieces[j3]]
    left.remove(j3)
    if p in left:  # @line L7
        j2 = p
    elif vals[left[0]] >= vals[left[1]]:
        j2 = left[0]  # agent 1 knows these values from line L2
    else:
        j2 = left[1]
    alloc[1] = [pieces[j2]]
    left.remove(j2)
    alloc[0] = [pieces[left[0]]]  # @line L8
    if trimmings is None:  # @line L9
        return alloc
    t = 2 if j3 == p else 1  # @line L10
    u = 3 - t
    a, b = trimmings
    w = oracle.eval_query(u, a, b) / 3  # @line L11
    y = oracle.cut_query(u, a, w)
    z = oracle.cut_query(u, y, w)
    parts: list[Interval] = [(a, y), (y, z), (z, b)]
    for agent in (t, 0):  # @line L12
        k = _best(oracle, agent, parts)
        alloc[agent].append(parts.pop(k))
    alloc[u].append(parts[0])
    return alloc
test_selfridge_conway_procedure.py 96 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/selfridge-conway-procedure/. The authors are listed in prov/9GXB9K.jsonl.

import random
from fractions import Fraction

from agtcake import Oracle, normalize, piece_value, random_valuation
from selfridge_conway_procedure import selfridge_conway

F = Fraction


def check_partition(alloc) -> None:
    """The pieces are disjoint, cover [0, 1], and each has at most two intervals."""
    assert sorted(alloc) == [0, 1, 2]
    assert all(1 <= len(alloc[i]) <= 2 for i in range(3))
    ivs = sorted(iv for i in range(3) for iv in alloc[i])
    assert ivs[0][0] == 0 and ivs[-1][1] == 1
    for (a1, b1), (a2, b2) in zip(ivs, ivs[1:]):
        assert a1 <= b1 == a2 <= b2
    assert ivs[-1][0] <= ivs[-1][1]


def check_envy_free(vals, alloc) -> None:
    for i in range(3):
        own = piece_value(vals[i], alloc[i])
        for j in range(3):
            assert own >= piece_value(vals[i], alloc[j])


def run(vals):
    o = Oracle(vals)
    alloc = selfridge_conway(o)
    check_partition(alloc)
    check_envy_free(vals, alloc)
    assert o.total_cuts() <= 5
    assert o.total_evals() <= 12
    return o, alloc


def test_page_example() -> None:
    vals = [normalize([1] * 6), normalize([3, 1, 1, 1, 2, 4]), normalize([1, 2, 3, 3, 2, 1])]
    o, alloc = run(vals)
    assert alloc[0] == [(F(0), F(1, 3)), (F(17, 18), F(35, 36))]
    assert alloc[1] == [(F(2, 3), F(11, 12)), (F(11, 12), F(17, 18))]
    assert alloc[2] == [(F(1, 3), F(2, 3)), (F(35, 36), F(1))]
    table = [[piece_value(vals[i], alloc[j]) for j in range(3)] for i in range(3)]
    assert table == [
        [F(13, 36), F(5, 18), F(13, 36)],
        [F(7, 18), F(7, 18), F(2, 9)],
        [F(19, 72), F(2, 9), F(37, 72)],
    ]
    assert o.cuts == [2, 1, 2] and o.evals == [2, 6, 4]


def test_third_agent_takes_trimmed_piece() -> None:
    vals = [normalize([1] * 6), normalize([1, 1, 2, 2, 3, 3]), normalize([1, 1, 1, 2, 4, 3])]
    o, alloc = run(vals)
    # [2/3, 1] is trimmed to [2/3, 8/9]; the first chooser (agent 2 of the code)
    # takes it, so agent 1 of the code takes its second best piece [1/3, 2/3].
    assert alloc[2][0] == (F(2, 3), F(8, 9))
    assert alloc[1][0] == (F(1, 3), F(2, 3))
    assert alloc[0][0] == (F(0), F(1, 3))
    assert o.total_cuts() == 5 and o.cuts == [2, 3, 0]


def test_identical_valuations_need_no_trim() -> None:
    rng = random.Random(7)
    for _ in range(30):
        v = random_valuation(rng, rng.randint(1, 8), 5)
        o, alloc = run([v, v, v])
        assert o.total_cuts() == 2
        assert all(piece_value(v, alloc[i]) == F(1, 3) for i in range(3))


def test_random_valuations() -> None:
    rng = random.Random(2024)
    for _ in range(300):
        k = rng.randint(1, 8)
        vals = [random_valuation(rng, k, 6) for _ in range(3)]
        o, alloc = run(vals)
        # the cutter gets at least a third, as one of its own equal pieces
        assert piece_value(vals[0], alloc[0]) >= F(1, 3)


def test_trim_matches_second_best() -> None:
    rng = random.Random(11)
    for _ in range(200):
        vals = [random_valuation(rng, 8, 4) for _ in range(3)]
        o, alloc = run(vals)
        if o.total_cuts() == 5:
            # the agent with the trimmed piece holds it as its first interval,
            # and agent 1 of the code values it as its second best of the three.
            first = [alloc[i][0] for i in range(3)]
            v1 = sorted((piece_value(vals[1], [iv]) for iv in first), reverse=True)
            assert v1[0] == v1[1]

History

John Selfridge found the procedure around 1960 and told it to Richard Guy, who passed it on. John Horton Conway found it independently later. Neither of them published it, and Brams and Taylor describe it in their 1996 book. It was the first finite envy-free procedure for three agents. Brams and Taylor extended the trimming idea to any number of agents in 1995, and Aziz and Mackenzie gave the first bounded protocol for any number of agents in 2016.

References

  1. Steven J. Brams and Alan D. Taylor, Fair Division, From Cake-Cutting to Dispute Resolution, Cambridge University Press, 1996, the Selfridge-Conway procedure and its history, pp. 116-120
  2. Jack Robertson and William Webb, Cake-Cutting Algorithms, Be Fair If You Can, A K Peters, 1998, the envy-free procedure for three players, pp. 13-14
  3. Steven J. Brams and Alan D. Taylor, An envy-free cake division protocol, American Mathematical Monthly 102(1) (1995), pp. 9-18 (the extension of the trimming idea to any number of agents)
  4. Felix Brandt, Vincent Conitzer, Ulle Endriss, Jerome Lang and Ariel D. Procaccia (eds.), Handbook of Computational Social Choice, Cambridge University Press, 2016, Chapter 13, Ariel D. Procaccia, Cake Cutting Algorithms (pp. 311-330)

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.

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
  • History with a source
  • a scene preset for the example