Algorithmcore
Even-Paz algorithm
Also called Even-Paz divide and conquer.
A proportional cake cutting protocol that cuts at the median of the agents' half-value marks and recurses on each half, with O(n log n) queries.
- Solves
- Cake cutting problem
- Time
- Θ(n log n) worst
- Space
- Θ(n) extra
- Where
- n = the number of agents
- Properties
- deterministicofflinerecursive
- Evidence
- Proof sketchCode in 1 languages
The Even-Paz algorithm is a divide and conquer protocol for proportional division of a cake among agents. Each agent marks the point that splits the current piece in the ratio by its own measure. The cake is cut at the median mark: the agents with the smaller marks share the left part, the others share the right part, and each group repeats the method. Each agent gets a connected piece worth at least to it. The protocol uses queries in the Robertson-Webb query model, and the Edmonds-Pruhs lower bound shows that no deterministic protocol does better.
In plain words
A large group of friends shares a cake. Instead of cutting one fair slice at a time, they split the group in two. Each friend draws a line where the cake splits into a left part that is fair for half of the group and a right part that is fair for the other half, as that friend sees it. The cake is cut at the middle line of all the lines.
The friends whose lines are left of the cut think the left part is big enough for half the group, so they take it. The other friends think the same of the right part. Each half then does the same thing on its own part, until each friend is alone with a piece. Because the group halves each time, each friend draws only a few lines.
The problem
The cake is . Agent has a nonnegative, additive and nonatomic valuation with . The protocol must output disjoint intervals that cover , with for every . It learns the valuations only by queries: returns , and returns the least point with .
The algorithm
The recursive call gets a set of agents and an interval . It guarantees that each agent gets at least . The first call is \textsc{Even-Paz}(\{1, \dots, n\}, [0, 1]), where is known and needs no query.
EVEN-PAZ(S, [a, b]) ▷ each i in S gets at least V_i([a, b]) / |S|- 1
if |S| = 1 then give [a, b] to the agent of S; return - 2
k ← |S|; k1 ← ⌊k/2⌋ - 3
for each agent i of S do - 4
w_i ← EVAL_i(a, b) ▷ no query at the root, where w_i = 1 - 5
m_i ← CUT_i(a, w_i · k1/k) ▷ [a, m_i] is worth k1/k of [a, b] to i - 6
sort S by (m_i, i) - 7
x ← the k1-th mark; L ← the first k1 agents; R ← the other k − k1 agents - 8
EVEN-PAZ(L, [a, x]) - 9
EVEN-PAZ(R, [x, b])
The cut point is a median of the marks: marks are at most , and the other marks are at least . Ties go to the agent with the smaller index, so the split of into and is well defined. For the call is a form of divide and choose in which both agents mark their half points and the cake is cut at the smaller mark.
Example
Split into four equal segments, and let the densities be for agent 1, for agent 2, for agent 3 and for agent 4. Each list sums to 4, so each agent values the whole cake at 1.
| call | agents | marks | cut | left group | right group | |
|---|---|---|---|---|---|---|
| 1, 2, 3, 4 | 1, 1, 1, 1 | 3, 1 | 2, 4 | |||
| 1, 3 | 1 | 3 | ||||
| 2, 4 | 2 | 4 |
In the second call, both marks are , and the tie goes to agent 1. The pieces are for agent 1, for agent 3, for agent 2 and for agent 4. Their values for the owners are , , and , all at least . The run makes 8 cut queries and 4 eval queries, 12 in total. The result is not envy-free: agent 1 values the piece of agent 2 at .
Correctness
Claim. A call on and gives each agent a piece worth at least to it.
The proof is by induction on . For the agent gets all of . For , let . If , then , so , and the call on gives at least . If , then , so and ; the call on gives at least . At the root, and , so each agent gets at least . The full proof is in proofs/correctness.md.
Proof: The Even-Paz algorithm is proportional and uses at most 2n ceil(lg n) - n queriesfull
Setting. The cake is , and agent has a nonnegative, additive and nonatomic valuation with . For , the map is continuous and nondecreasing on , since is nonatomic. So has an answer whenever , and the least answer has . Single points have value 0. We refer to the lines L1 to L9 of the page, and we write .
Lemma 1 (the cut queries have answers). In a call on and , the query of line L5 asks for , and its answer lies in .
Proof. , so the asked value is at most , and an answer exists. The least answer is at most , because .
So the cut point of line L7 lies in , and the calls of lines L8 and L9 are on intervals inside that meet only at .
Lemma 2 (the split). After the sort of line L6, every agent has and every agent has , and , .
Proof. The sort is by first, and is the mark of the -th agent in sorted order. For , and .
Theorem 1 (each agent gets its share of the interval). A call on a set of agents and an interval gives the agents of disjoint intervals that cover , and agent gets an interval worth at least to it.
Proof. By strong induction on . For , line L1 gives all of to the one agent, worth .
Let and for an agent . By Lemma 2 the two recursive calls have fewer than agents, so the induction hypothesis applies to them, and together they cover .
If , then , and monotonicity gives . The call on gives agent at least
If , then , so , and additivity gives . The call on gives agent at least
Corollary (proportional). The first call has , and , so each agent gets one interval worth at least , and the intervals partition . The root uses without a query; this is correct because the valuations are normalized.
Remark (safe for an honest agent). The proof for agent uses only the answers of agent and the fact that is chosen so that agent lands on the side of that its own mark selects. It uses nothing about the answers of other agents, except that their marks lie in . So an agent that answers by gets at least whatever the others answer.
Lemma 3 (the shape of the recursion). For , the tree of calls has leaves, every internal call has two children, and every leaf has depth or . Exactly leaves have depth , and leaves have depth .
Proof. The sizes of the calls at depth are or , by induction on : halving or into floor and ceiling parts gives or . Since , every call at depth has size at least , so it is internal. Hence depth has calls, each of size or , so of size 1 or 2. Let be the number of size 1. Then , so leaves have depth , and the calls of size 2 have children of size 1 at depth .
Theorem 2 (query count). For , the protocol makes cut queries and eval queries, so
queries in total, with equality when is a power of 2. For it makes no query.
Proof. A call on agents asks each of its agents one cut query, and one eval query unless it is the root. So agent gets one cut query for each internal call on the path from the root to its leaf, that is, as many cut queries as the depth of its leaf, and one eval fewer. By Lemma 3 the sum of the depths is
This is the count of cut queries. It is at most because . The eval queries number fewer, . The total is , and gives the bound , with equality exactly when . For , line L1 returns at once.
Complexity
A call on agents makes cut queries and eval queries (none at the root), and then calls itself on and agents. With and , the protocol makes queries, because the root skips its evals. The recursion has depth , and each level asks each agent at most one cut and one eval. A closed form is
with equality when is a power of 2. For this is 12, as in the example. The cut queries number at most . So the cost is on every input, the same shape as the cost of merge sort. The sort of line L6 takes comparisons, which the query model does not count, and the extra space is for the marks and the recursion.
Properties
- Connected pieces. Each call cuts its interval at one point, so each agent gets one interval.
- Safe for an honest agent. The guarantee of for agent uses only the answers of agent and the choice of as a median. An agent that answers by its true valuation gets at least whatever the others answer.
- Optimal query count. The Edmonds-Pruhs lower bound shows that every deterministic proportional protocol needs queries, even with pieces that are unions of intervals. Woeginger and Sgall had shown this before for connected pieces.
- Not envy-free, not equitable. The example shows envy, and the values of the agents differ.
- Balanced recursion. The split keeps the depth at . Any split with keeps the guarantee, but an unbalanced split costs more queries. The split gives a protocol much like the Dubins-Spanier moving knife, with queries.
Variants and improvements
- No evals. The evals of line L4 can be dropped. Give each agent a budget with , starting at , and mark at . An agent in goes on with budget , and an agent in with budget , and the same induction shows that each agent gets at least . This version uses at most cut queries and no eval query.
- Randomized version. Even and Paz also gave a randomized protocol with expected cut queries. Edmonds and Pruhs showed that randomized protocols still need queries when the answers are approximate.
- Linear cuts. A 2026 preprint by Arndt, Moseley, Im and Pruhs (arXiv 2606.07238) gives a deterministic protocol with cut queries and eval queries. It does not beat queries in total, but it shows that cuts alone need not grow as .
- Unequal shares. When agent is entitled to a share instead of , the same idea works with splits by entitlement instead of by count. The query complexity of this case was studied by Cseh and Fleiner.
- Simpler quadratic protocols. The last diminisher and the Dubins-Spanier procedure use queries but give one piece per round, which is easier to run by hand.
When to use it
Use Even-Paz when is large and queries are costly, for example when each query is a question to a person or a call to a slow model of preferences. It gives connected pieces and the best possible order of queries for deterministic proportional division. If envy matters, it is not enough: use an envy-free protocol such as Selfridge-Conway for three agents.
Pitfalls
- Marks of the wrong size. Line L5 asks for a fraction of the value of the current interval , not of the whole cake. With the whole cake as the base, the induction fails.
- Odd . For odd the two groups differ in size. The mark must use with , the size of the left group. A mark at value breaks the guarantee for the larger group.
- Ties in the marks. When several marks equal , the agents with these marks must be split by a fixed rule so that exactly agents go left. Each agent with is safe in either group.
- Recursion on the wrong side. The left group must get and the right group , with the -th mark in sorted order. Swapping them gives no guarantee.
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()
even_paz_algorithm.py 57 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/even-paz-algorithm/. The authors are listed in prov/E1V4HN.jsonl.
from fractions import Fraction
from typing import Optional
from agtcake import Oracle
Piece = list[tuple[Fraction, Fraction]]
def even_paz(oracle: Oracle) -> dict[int, Piece]:
"""Even-Paz divide and conquer for the n agents of the oracle.
Each agent gets one interval, worth at least 1/n to it.
"""
alloc: dict[int, Piece] = {}
agents = list(range(oracle.n))
whole = {i: Fraction(1) for i in agents} # V_i([0, 1]) = 1 needs no query
_even_paz(oracle, agents, Fraction(0), Fraction(1), whole, alloc)
return alloc
def _even_paz(
oracle: Oracle,
agents: list[int],
a: Fraction,
b: Fraction,
known: Optional[dict[int, Fraction]],
alloc: dict[int, Piece],
) -> None:
"""Divide [a, b] among agents. known holds V_i([a, b]) when it is known."""
k = len(agents)
if k == 1: # @line L1
alloc[agents[0]] = [(a, b)]
return
k1 = k // 2 # @line L2
worth: dict[int, Fraction] = {}
marks: dict[int, Fraction] = {}
for i in agents: # @line L3
w = known[i] if known is not None else oracle.eval_query(i, a, b) # @line L4
worth[i] = w
marks[i] = oracle.cut_query(i, a, w * k1 / k) # @line L5
order = sorted(agents, key=lambda i: (marks[i], i)) # @line L6
left, right = order[:k1], order[k1:]
x = marks[order[k1 - 1]] # @line L7
_even_paz(oracle, left, a, x, None, alloc) # @line L8
_even_paz(oracle, right, x, b, None, alloc) # @line L9
def query_count(n: int) -> int:
"""The exact number of queries of even_paz on n agents (cuts plus evals)."""
def t(k: int) -> int:
return 0 if k <= 1 else 2 * k + t(k // 2) + t(k - k // 2)
return t(n) - n if n > 1 else 0
test_even_paz_algorithm.py 85 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/even-paz-algorithm/. The authors are listed in prov/E1V4HN.jsonl.
import random
from fractions import Fraction
from agtcake import Oracle, normalize, piece_value, random_valuation
from even_paz_algorithm import even_paz, query_count
F = Fraction
def ceil_lg(n: int) -> int:
return (n - 1).bit_length()
def check_partition(alloc, n) -> None:
assert sorted(alloc) == list(range(n))
assert all(len(alloc[i]) == 1 for i in range(n))
ivs = sorted(alloc[i][0] for i in range(n))
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
def test_example() -> None:
vals = [normalize(d) for d in ([1, 1, 1, 1], [0, 1, 1, 2], [2, 2, 0, 0], [1, 0, 0, 3])]
o = Oracle(vals)
alloc = even_paz(o)
assert alloc == {
0: [(0, F(1, 4))],
2: [(F(1, 4), F(1, 2))],
1: [(F(1, 2), F(13, 16))],
3: [(F(13, 16), 1)],
}
assert [piece_value(vals[i], alloc[i]) for i in range(4)] == [F(1, 4), F(3, 8), F(1, 2), F(9, 16)]
assert o.cuts == [2, 2, 2, 2] and o.evals == [1, 1, 1, 1]
assert o.total() == query_count(4) == 12
def test_query_count_formula() -> None:
for n in range(2, 65):
L = ceil_lg(n)
assert query_count(n) == 2 * n * L - 2 ** (L + 1) + n
assert query_count(n) <= 2 * n * L - n
if n == 2**L:
assert query_count(n) == 2 * n * L - n
assert query_count(1) == 0
def test_random_valuations() -> None:
rng = random.Random(31337)
for _ in range(150):
n = rng.randint(1, 5)
vals = [random_valuation(rng, rng.randint(1, 8), 6) for _ in range(n)]
o = Oracle(vals)
alloc = even_paz(o)
check_partition(alloc, n)
for i in range(n):
assert piece_value(vals[i], alloc[i]) >= F(1, n)
assert o.total() == query_count(n)
assert o.total_cuts() <= n * ceil_lg(n)
assert o.total_evals() == query_count(n) - o.total_cuts()
def test_more_agents_few_segments() -> None:
rng = random.Random(4)
for n in (6, 7, 8):
vals = [random_valuation(rng, 4, 3) for _ in range(n)]
o = Oracle(vals)
alloc = even_paz(o)
check_partition(alloc, n)
for i in range(n):
assert piece_value(vals[i], alloc[i]) >= F(1, n)
assert o.total() == query_count(n)
def test_identical_valuations() -> None:
rng = random.Random(6)
for n in range(1, 6):
v = random_valuation(rng, 6, 4)
alloc = even_paz(Oracle([v] * n))
check_partition(alloc, n)
for i in range(n):
assert piece_value(v, alloc[i]) == F(1, n)
History
Even and Paz published the protocol in 1984 in "A note on cake cutting", in Discrete Applied Mathematics. It improved the queries of the last diminisher (Steinhaus, 1948) and of the Dubins-Spanier procedure (1961) to . The same paper gave a randomized protocol with expected cuts. The question whether is optimal stayed open until Woeginger and Sgall (2007) proved for connected pieces and Edmonds and Pruhs (SODA 2006; ACM Transactions on Algorithms 2011) proved it for all deterministic protocols.
References
- Shimon Even and Azaria Paz, A note on cake cutting, Discrete Applied Mathematics 7(3) (1984), pp. 285-296 (the divide and conquer protocol with O(n log n) cuts, and the randomized protocol with O(n) expected cuts)
- Jeff Edmonds and Kirk Pruhs, Cake cutting really is not a piece of cake, ACM Transactions on Algorithms 7(4) (2011), Article 51; Section 2, Theorem 4 (the matching lower bound for deterministic protocols)
- 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 algorithms (from p. 313), and Section 13.4, complexity of cake cutting (from p. 316)
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 E1V4HN Edit this page History Report an error Markdown