Algorithmintro
Divide and choose
Also called I cut you choose.
One agent cuts the cake into two pieces it values equally, and the other takes the piece it prefers. The result is envy-free.
- Solves
- Cake cutting problem
- Time
- Θ(1) worst
- Space
- Θ(1) extra
- Where
- n = the number of agents
- Properties
- deterministicofflineiterative
- Evidence
- Proof sketchCode in 1 languages
Divide and choose is the protocol that splits a cake between two agents: one agent cuts the cake into two pieces that it values equally, and the other agent takes the piece it prefers. Each agent gets at least half of the cake by its own measure, so the division is proportional, and neither agent prefers the other piece, so it is envy-free. In the Robertson-Webb query model it uses one cut query and one eval query. The cryptographic cut-and-choose technique (one party makes many copies of an object and the other opens all but one to check them) takes its name from this protocol, but it is a method of verification, not a method of division.
In plain words
Two children share a cake with chocolate on one side and fruit on the other. They like the parts in different ways. The first child cuts the cake into two pieces. The first child does not know which piece it will get, so it cuts where both pieces look equally good to it. The second child then takes the piece that it likes more.
The first child gets exactly half by its own view, whichever piece is left. The second child gets at least half by its own view, because it chose the better of two pieces. Neither child wants to swap. Each child can make sure of this result by itself: the cutter by cutting honestly, the chooser by choosing honestly. Neither child needs to trust the other.
The problem
The cake is the interval . Agent has a valuation : a nonnegative, additive and nonatomic measure on with . Nonatomic means that a single point has value 0, so it does not matter which piece holds a cut point. The output is a partition of into pieces and , here two intervals.
The goal is a proportional division, for , and envy-freeness, . With two agents and , the two goals are the same: , so holds exactly when .
The protocol does not see and . It asks queries: returns a point with , and returns .
The algorithm
Agent 1 is the cutter and agent 2 is the chooser.
DIVIDE-AND-CHOOSE() ▷ agent 1 cuts, agent 2 chooses- 1
x ← CUT_1(0, 1/2) ▷ now V_1([0, x]) = 1/2 - 2
if EVAL_2(0, x) ≥ 1/2 then - 3
return X_1 = [x, 1], X_2 = [0, x] ▷ the chooser takes the left piece - 4
else return X_1 = [0, x], X_2 = [x, 1] ▷ the chooser takes the right piece
Line L1 is the cut. Line L2 asks the chooser for the value of the left piece. Because , the test is the same as , so the chooser takes the piece it values more. On a tie it takes the left piece; any fixed rule for ties gives the same guarantees.
Example
Split the cake into four equal segments . The cutter has densities on them, so it values the four segments at . The chooser has the uniform density .
| step | query | answer |
|---|---|---|
| L1 | , because | |
| L2 | , less than | |
| L4 | , |
The cutter gets . The chooser gets . Both pieces meet the guarantee, but the chooser does much better than half. This is typical: the cutter gets exactly when it cuts honestly, and the chooser gets .
Correctness
Claim. If agent 1 cuts as in line L1 and agent 2 chooses as in line L2, then and . So the division is proportional and envy-free.
Line L1 gives , and additivity gives , so agent 1 gets in both branches. In line L3, . In line L4, , so . Envy-freeness follows from the equivalence of the two goals for two agents. The cut query always has an answer: is continuous (the measure is nonatomic) and goes from 0 to 1, so it takes the value . The full proof is in proofs/correctness.md.
Proof: Divide and choose is proportional and envy-freefull
Setting. The cake is . Agents 1 and 2 have valuations and , each a nonnegative, additive and nonatomic measure with . Agent 1 cuts and agent 2 chooses, as in lines L1 to L4.
Lemma 1 (the cut exists). There is a point with .
Proof. Let . Then because is nonatomic, and . For , additivity gives , and this tends to 0 as , because a nonatomic measure of a shrinking interval goes to the measure of a point, which is 0. So is continuous and nondecreasing, and the intermediate value theorem gives with . The least such exists because the set is closed and nonempty.
So line L1 always has an answer. Fix this . Because is nonatomic, , so for each agent .
Lemma 2 (two goals are one for two agents). Let be a partition of , up to a finite set of points. Then holds if and only if .
Proof. Additivity and the nonatomic property give . So holds exactly when .
Theorem. The output of lines L1 to L4 has and . Hence it is proportional and envy-free.
Proof. Agent 1. In line L3, and . In line L4, and by line L1. So in both branches.
Agent 2. Let , the answer of line L2. If , line L3 gives and . If , line L4 gives and .
Both agents get at least , which is proportionality for . Lemma 2 turns this into envy-freeness.
Each guarantee needs only the agent's own behavior. The argument for agent 1 uses only line L1, the cut by agent 1. It holds for any rule the chooser uses, because both pieces are worth to agent 1. The argument for agent 2 uses only line L2, the choice by agent 2. It holds for any point , because for every . So an agent that follows the protocol gets at least even if the other agent does not follow it.
Query count. Line L1 makes one cut query and line L2 makes one eval query. No other line makes a query. So the protocol makes exactly two queries in the Robertson-Webb model.
Complexity
The protocol makes exactly one cut query (line L1) and one eval query (line L2), so it uses queries in the Robertson-Webb model in every case. Each agent answers one question. If the valuations are piecewise constant on segments, as in the implementation, each query takes arithmetic operations on rationals.
Properties
- Each agent is safe alone. The cutter gets if it cuts at its own half point, whatever the chooser does. The chooser gets at least if it takes the piece it prefers, whatever the cut is. So the guarantee needs no assumption about the other agent.
- Connected pieces. Each agent gets one interval, with one cut.
- Not equitable. In the example the cutter gets and the chooser gets . The chooser has the advantage, because the cutter commits first.
- Not Pareto optimal. Let the cutter be uniform and let the chooser have density . The cutter cuts at , and the chooser values both halves at , so each agent gets . The division and gives the cutter and the chooser . See Pareto optimality.
- Not strategyproof for the cutter. A cutter that knows the chooser's measure can gain by a cut that is not at its half point. In the example, a cut at makes the uniform chooser take , and the cutter gets . Honest cutting is still the safe choice when the cutter does not know the chooser's measure.
Variants and improvements
- More agents, proportional. The last diminisher and the Dubins-Spanier moving knife use queries for agents. The Even-Paz algorithm uses queries, which is optimal by the Edmonds-Pruhs lower bound.
- More agents, envy-free. For three agents, the Selfridge-Conway procedure gives an envy-free division with at most five cuts. For any number of agents there are the Brams-Taylor procedure and the bounded Aziz-Mackenzie protocol.
- Moving knives for two agents. Austin's moving-knife procedure gives each of two agents a piece that both value at exactly . It needs continuous moves, so it is not a finite protocol in the Robertson-Webb model.
- Goods and points. For two agents who split several divisible goods by points, the adjusted winner procedure is envy-free, equitable and Pareto optimal.
When to use it
Use divide and choose for two agents when you want a simple rule that each agent can check by itself. It needs only one cut and one comparison. If the agents also want equal satisfaction or an efficient result, use the adjusted winner procedure on a list of goods, or an exact two-person moving knife if continuous moves are possible. For more than two agents, use one of the -agent protocols above; divide and choose alone does not extend.
Pitfalls
- Cutting at the wrong value. Line L1 must cut at value for the cutter. A cut at the midpoint of the interval gives the cutter , which can be 0 (in the example, ).
- Comparing with the wrong total. The test of line L2 compares with only because . With unnormalized valuations, compare with instead.
- Two cut points. A cut query can have many answers when the density is 0 on a stretch. Any answer gives the cutter ; the implementation returns the least one. Do not assume the answer is unique.
- Envy-free for two is not envy-free for more. Running divide and choose repeatedly on pieces among three agents does not give envy-freeness; that needs a procedure such as Selfridge-Conway.
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
# 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()
divide_and_choose.py 24 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/divide-and-choose/. The authors are listed in prov/EWY1RW.jsonl.
from fractions import Fraction
from agtcake import Oracle
Piece = list[tuple[Fraction, Fraction]]
HALF = Fraction(1, 2)
def divide_and_choose(oracle: Oracle, cutter: int = 0, chooser: int = 1) -> dict[int, Piece]:
"""Divide [0, 1] between two agents. The cutter cuts, the chooser chooses.
Returns a dict from each agent to its piece. The chooser takes the left
piece when it values the two pieces equally.
"""
x = oracle.cut_query(cutter, 0, HALF) # @line L1
left: Piece = [(Fraction(0), x)]
right: Piece = [(x, Fraction(1))]
if oracle.eval_query(chooser, 0, x) >= HALF: # @line L2
return {chooser: left, cutter: right} # @line L3
return {cutter: left, chooser: right} # @line L4
test_divide_and_choose.py 143 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/divide-and-choose/. The authors are listed in prov/EWY1RW.jsonl.
import random
from fractions import Fraction
import pytest
from agtcake import Oracle, cut, normalize, piece_value, random_valuation, value
from divide_and_choose import divide_and_choose
F = Fraction
# ---------------------------------------------------------------- the helper
def test_normalize() -> None:
assert normalize([1, 1, 1, 1]) == [1, 1, 1, 1]
assert normalize([3, 1, 0, 0]) == [3, 1, 0, 0]
assert normalize([2, 0]) == [2, 0]
assert normalize([1, 2, 3]) == [F(1, 2), F(1), F(3, 2)]
for bad in ([], [0, 0], [1, -1]):
with pytest.raises(ValueError):
normalize(bad)
def test_value_by_hand() -> None:
v = normalize([3, 1, 0, 0])
assert value(v, 0, 1) == 1
assert value(v, 0, F(1, 4)) == F(3, 4)
assert value(v, F(1, 8), F(3, 8)) == F(3, 8) + F(1, 8)
assert value(v, F(1, 2), 1) == 0
assert value(v, F(1, 3), F(1, 3)) == 0
with pytest.raises(ValueError):
value(v, F(1, 2), F(1, 4))
def test_cut_by_hand() -> None:
v = normalize([3, 1, 0, 0])
assert cut(v, 0, F(1, 2)) == F(1, 6)
assert cut(v, 0, F(3, 4)) == F(1, 4)
assert cut(v, 0, 1) == F(1, 2) # the least point, not 1
assert cut(v, F(1, 2), F(1, 100)) is None
assert cut(v, F(1, 2), 0) == F(1, 2)
w = normalize([1, 0, 1])
assert cut(w, 0, F(1, 2)) == F(1, 3) # the least x, at the start of the zero segment
assert cut(w, 0, F(1, 2) + F(1, 10)) == F(2, 3) + F(1, 15)
def test_cut_and_value_invert_each_other() -> None:
rng = random.Random(20240101)
for _ in range(300):
k = rng.randint(1, 8)
v = random_valuation(rng, k, 5)
assert sum(v) == k and value(v, 0, 1) == 1
a = F(rng.randint(0, 24), 24)
rest = value(v, a, 1)
t = rest * F(rng.randint(0, 12), 12)
x = cut(v, a, t)
assert x is not None and a <= x <= 1
assert value(v, a, x) == t
# x is the least such point: a little to the left the value is smaller
if x > a:
assert value(v, a, x - (x - a) / 1000) < t
assert cut(v, a, rest + F(1, 1000)) is None
b = a + (1 - a) * F(rng.randint(0, 10), 10)
y = cut(v, a, value(v, a, b))
assert y is not None and y <= b and value(v, y, b) == 0
def test_piece_value_and_additivity() -> None:
rng = random.Random(7)
for _ in range(100):
v = random_valuation(rng, rng.randint(1, 8), 4)
pts = sorted(F(rng.randint(0, 30), 30) for _ in range(3))
a, b, c = pts
assert value(v, a, c) == value(v, a, b) + value(v, b, c)
assert piece_value(v, [(0, a), (b, 1)]) == 1 - value(v, a, b)
def test_oracle_counts() -> None:
o = Oracle([normalize([1, 1]), normalize([1, 3])])
assert o.n == 2
assert o.eval_query(1, 0, F(1, 2)) == F(1, 4)
assert o.cut_query(1, 0, F(1, 4)) == F(1, 2)
assert o.cut_query(0, 0, F(1, 2)) == F(1, 2)
assert o.evals == [0, 1] and o.cuts == [1, 1]
assert o.total_evals() == 1 and o.total_cuts() == 2 and o.total() == 3
# ---------------------------------------------------------------- divide and choose
def check(vals, alloc) -> None:
"""Both pieces cover the cake, the cutter gets 1/2, and nobody envies."""
a, b = alloc[0], alloc[1]
ends = sorted(a + b)
assert ends[0][0] == 0 and ends[-1][1] == 1 and ends[0][1] == ends[1][0]
assert piece_value(vals[0], a) == F(1, 2)
for i in (0, 1):
own = piece_value(vals[i], alloc[i])
other = piece_value(vals[i], alloc[1 - i])
assert own >= other
assert own >= F(1, 2)
# brute force over the two pieces: the chooser has a best one
assert piece_value(vals[1], b) == max(piece_value(vals[1], a), piece_value(vals[1], b))
def test_example() -> None:
vals = [normalize([3, 1, 0, 0]), normalize([1, 1, 1, 1])]
o = Oracle(vals)
alloc = divide_and_choose(o)
assert alloc[0] == [(0, F(1, 6))]
assert alloc[1] == [(F(1, 6), 1)]
assert piece_value(vals[1], alloc[1]) == F(5, 6)
assert o.cuts == [1, 0] and o.evals == [0, 1]
check(vals, alloc)
def test_identical_valuations() -> None:
rng = random.Random(11)
for _ in range(50):
v = random_valuation(rng, rng.randint(1, 8), 6)
alloc = divide_and_choose(Oracle([v, v]))
assert piece_value(v, alloc[0]) == piece_value(v, alloc[1]) == F(1, 2)
def test_random_valuations() -> None:
rng = random.Random(12345)
for _ in range(400):
vals = [random_valuation(rng, rng.randint(1, 8), 6) for _ in range(2)]
o = Oracle(vals)
alloc = divide_and_choose(o)
check(vals, alloc)
assert o.total() == 2
def test_roles_swapped() -> None:
vals = [normalize([3, 1, 0, 0]), normalize([1, 1, 1, 1])]
alloc = divide_and_choose(Oracle(vals), cutter=1, chooser=0)
assert alloc[1] == [(F(1, 2), 1)] and alloc[0] == [(0, F(1, 2))]
assert piece_value(vals[0], alloc[0]) == 1
History
The method is old, and it appears in many stories of two people who share land or an inheritance. Steinhaus (1948) took the two-person method as known and asked how to extend it to people, which led to the last diminisher of Banach and Knaster. Brams and Taylor (1996) study it and its moving-knife versions in their first chapter.
References
- Steven J. Brams and Alan D. Taylor, Fair Division, From Cake-Cutting to Dispute Resolution, Cambridge University Press, 1996, Chapter 1, Proportionality for n = 2 (divide-and-choose, its properties, and moving-knife versions for two people)
- Jack Robertson and William Webb, Cake-Cutting Algorithms, Be Fair If You Can, A K Peters, 1998, Chapter 1, Fairly Dividing a Cake
- 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, Section 13.3 (classic cake cutting algorithms)
- Hugo Steinhaus, The problem of fair division, Econometrica 16(1) (1948), 101-104, the two-person method that the paper extends to n people
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 EWY1RW Edit this page History Report an error Markdown