Algorithmintro
Last diminisher
Also called Banach-Knaster procedure.
A proportional cake cutting protocol for n agents. A piece passes from agent to agent, each may trim it to value 1/n, and the last agent to trim it takes it.
- Solves
- Cake cutting problem
- Time
- Θ(n^2) worst · best
- Space
- Θ(n) extra
- Where
- n = the number of agents
- Properties
- deterministicofflineiterative
- Evidence
- Proof sketchCode in 1 languages
The last diminisher is a protocol that gives each of agents a connected piece of a cake worth at least to that agent, so it finds a proportional division. In each round, one agent marks a piece worth to it, and the piece passes to the other remaining agents in turn. An agent that values the piece at more than trims it to by its own measure. The last agent to trim the piece takes it and leaves, and the others continue with the rest of the cake. In the Robertson-Webb query model it uses at most queries.
In plain words
A group of friends shares a cake. The first friend cuts a slice from the left end that it thinks is a fair share. The slice then goes around the group. Each friend looks at the slice. If the slice looks too large, the friend makes it smaller, so that it looks exactly fair to that friend. If the slice looks fair or small, the friend lets it pass.
When every friend has seen the slice, the last friend who made it smaller takes it. If nobody made it smaller, the friend who cut it takes it. That friend leaves the group with a fair share. The others think the slice was not too large, so they think enough cake is left for them. They repeat the method on the rest, until one friend remains and takes what is left.
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 a point with , and returns .
The algorithm
The remaining cake is always , and is the list of agents that do not have a piece yet, in a fixed order.
LAST-DIMINISHER(n)- 1
a ← 0; R ← (1, 2, …, n) - 2
while |R| > 1 do - 3
h ← the first agent of R; x ← CUT_h(a, 1/n) ▷ h marks [a, x] - 4
for each agent j of R after h, in order do - 5
if EVAL_j(a, x) > 1/n then ▷ j thinks [a, x] is too large - 6
x ← CUT_j(a, 1/n); h ← j ▷ j trims it and becomes the holder - 7
give [a, x] to h; remove h from R; a ← x - 8
give [a, 1] to the last agent of R
Each trim moves to the left, because the agent that trims values at more than and its new mark has value exactly . So the piece of a round is never larger than the first mark, and the holder always values at exactly .
Example
Split into four equal segments, and let the densities on them be for agent 1, for agent 2 and for agent 3, Each segment has width and each list sums to 4, so each agent values the whole cake at 1. For example, agent 2 values the first segment at . Here and .
| round | step | query | answer | holder |
|---|---|---|---|---|
| 1 | L3 | 1 | ||
| 1 | L5 | |||
| 1 | L6 | 2 | ||
| 1 | L5 | |||
| 1 | L6 | 3 | ||
| 2 | L3 | 1 | ||
| 2 | L5 | |||
| 2 | L6 | 2 |
Agent 3 gets , agent 2 gets , and agent 1 gets . The values for their owners are , and . The run makes 8 queries, which is : every agent trims in every round. The result is proportional but not envy-free: agent 3 values the piece of agent 2 at , and agent 2 values the piece of agent 1 at .
Correctness
Invariant. At the start of each round, every agent in has .
At the start, and , so the invariant holds. In a round, the final holder values at exactly , because is its own cut. Every other agent in values at most . For an agent after the last trim, line L5 said so. For an agent before the last trim, it valued an earlier, larger piece at most , or trimmed it to , and the final piece is a subset of that piece. So , which is the invariant for the next round with agents.
Every agent that gets a piece in line L7 gets exactly . The last agent gets , worth at least by the invariant with . So the division is proportional. The cut queries have answers because each agent in values at least . The full proof is in proofs/correctness.md.
Proof: The last diminisher is proportional and uses at most n^2 - 1 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 any answer has . Single points have value 0, so the end points shared by two pieces do not matter. We refer to the lines L1 to L8 of the page.
Lemma 1 (the piece shrinks). In one round, let be the mark of line L3 and the marks of the later executions of line L6, in order. Then , and the holder at the end of the round has for the final mark .
Proof. Line L6 runs for agent only when line L5 found for the current mark . The new mark has . Since is nondecreasing, . The holder is always the agent whose cut gave the current mark, so at the end.
Lemma 2 (the others think the piece is not too large). Let be the final mark of a round and the final holder. Every agent of has .
Proof. Let . If is the first agent of , it made the mark with , and since some later trim happened, so by Lemma 1. If comes later, it saw some mark in line L5. Either and it passed, or it trimmed to a mark with and was holder for a while. All later marks are at most (in the first case) or at most (in the second case), by Lemma 1. So in all cases lies inside an interval with , and .
Lemma 3 (invariant). At the test of line L2, every agent has .
Proof. By induction on the rounds. The first time, and , and . Suppose the invariant holds at the start of a round. All cut queries of the round have answers: each one asks for . Let be the final mark and the holder. For every , additivity and Lemma 2 give
Line L7 sets and removes , so the invariant holds for the new of size .
Theorem 1 (proportional). The output is a partition of into intervals, and for every agent .
Proof. Each round removes one agent from , so the loop of line L2 runs times. The pieces of line L7 are with , and line L8 gives . Lemma 1 and the answer of a cut query from give . So the pieces are intervals that cover and meet only at end points. Each agent in line L7 gets by Lemma 1. The last agent gets by Lemma 3 with .
Theorem 2 (safe for an honest agent). Suppose agent answers by , and every other agent answers by some nonnegative, additive and nonatomic valuation with of its own choice, not necessarily its true one. Then .
Proof. Lemma 1 uses only that each agent answers both its queries by one valuation, so it holds for the valuations and . In Lemma 2 and Lemma 3, take and alone: their proofs use Lemma 1 and the answers of agent only. They show that, as long as agent is in , . If agent gets a piece in line L7, the piece is its own cut, worth . If it is the last agent, it gets , worth at least .
Theorem 3 (query count). The protocol makes exactly eval queries, between and cut queries, and at most queries in total.
Proof. The round with makes one eval query for each of the agents after the first (line L5), one cut query in line L3, and at most cut queries in line L6. The rounds have . So the evals number , and the cuts number at least and at most . The total is at most . Line L8 makes no query.
Complexity
A round with agents makes eval queries (one per agent after the first) and between 1 and cut queries. Rounds run for . So the protocol makes exactly eval queries, between and cut queries, and at most queries in total. Even with no trims it makes queries, so its cost is in every case.
The bound is reached: if agent puts more weight to the left than every agent before it in the order (for example, density on segment of equal segments), every agent trims in every round. Extra space is for the list .
Properties
- Connected pieces. Each agent gets one interval, and the cake is cut at points in the end.
- Safe for an honest agent. The guarantee of for agent uses only the answers of agent . Whatever the other agents answer, an agent that cuts and trims by its true valuation gets at least .
- Envy only toward later pieces. An agent that gets a piece in round was waiting in every round before , so it values each earlier piece at most , and it gets exactly itself. So no agent envies a piece that was given out before its own. It may envy a later piece, as the example shows.
- Not equitable and not Pareto optimal in general. In the example the last agent gets and the others .
- Uses eval and cut queries. Unlike the Dubins-Spanier moving knife, it asks waiting agents to evaluate the current piece, and only some of them cut.
Variants and improvements
- Dubins-Spanier moving knife. A knife moves from to the right, and the first agent to call stop takes the piece. With cut queries, every remaining agent marks its point and the least mark wins. It gives the same kind of allocation with exactly cut queries and no evals.
- Even-Paz. The Even-Paz algorithm splits the agents into two halves at a median mark and recurses. It uses queries, which is optimal by the Edmonds-Pruhs lower bound.
- Other classic proportional methods. Brams and Taylor describe the lone divider and the lone chooser methods next to the last diminisher. They give the same guarantee of by other steps.
- Two agents. For the protocol is a form of divide and choose: agent 1 marks its half , and agent 2 either lets agent 1 take it or trims it to its own half point and takes the trimmed piece.
When to use it
Use the last diminisher when you want a simple proportional protocol with connected pieces that people can run by hand, one piece at a time. It is easy to explain and each agent can check its own guarantee. For many agents, the Even-Paz algorithm needs far fewer queries. If envy matters, use an envy-free procedure such as Selfridge-Conway for three agents.
Pitfalls
- Giving the piece to the first trimmer. The piece goes to the last agent that trims it, after all agents of have seen it. If it goes to an agent before the others have seen it, a later agent may value it at more than , and then that agent can be left with less than its share of the rest.
- Trimming from the wrong end. The trim must keep the left end and move the right end . If it moves , the remaining cake is no longer one interval.
- Ties. With the strict test of line L5, an agent that values the piece at exactly lets it pass. A test with also keeps the guarantees, but it adds cut queries and changes who gets the piece.
- Not envy-free. A proportional result can still leave agents with envy. Do not report it as envy-free for .
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()
last_diminisher.py 34 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/last-diminisher/. The authors are listed in prov/VJGVJA.jsonl.
from fractions import Fraction
from agtcake import Oracle
Piece = list[tuple[Fraction, Fraction]]
def last_diminisher(oracle: Oracle) -> dict[int, Piece]:
"""Banach-Knaster last diminisher for the n agents of the oracle.
Each agent i gets one interval, worth at least 1/n to it. In each round
the first remaining agent cuts a piece worth 1/n to it, and each later
agent trims the piece when it is worth more than 1/n to that agent.
"""
n = oracle.n
share = Fraction(1, n)
alloc: dict[int, Piece] = {}
a = Fraction(0) # @line L1
rest = list(range(n))
while len(rest) > 1: # @line L2
holder = rest[0] # @line L3
x = oracle.cut_query(holder, a, share)
for j in rest[1:]: # @line L4
if oracle.eval_query(j, a, x) > share: # @line L5
x = oracle.cut_query(j, a, share) # @line L6
holder = j
alloc[holder] = [(a, x)] # @line L7
rest.remove(holder)
a = x
alloc[rest[0]] = [(a, Fraction(1))] # @line L8
return alloc
test_last_diminisher.py 103 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/last-diminisher/. The authors are listed in prov/VJGVJA.jsonl.
import random
from fractions import Fraction
from agtcake import Oracle, normalize, piece_value, random_valuation, value
from last_diminisher import last_diminisher
F = Fraction
def check_partition(alloc, n) -> list:
"""The pieces are single intervals that tile [0, 1] from left to right."""
assert sorted(alloc) == list(range(n))
ivs = sorted(alloc[i][0] for i in range(n))
assert all(len(alloc[i]) == 1 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
return ivs
def check_rounds(vals, alloc) -> None:
"""Replay the rounds with the true valuations.
In each round, the receiver values its piece at exactly 1/n, and every
agent that is still waiting values that piece at most 1/n.
"""
n = len(vals)
share = F(1, n)
owner = {alloc[i][0]: i for i in range(n)}
waiting = set(range(n))
for iv in sorted(owner)[:-1]:
i = owner[iv]
assert piece_value(vals[i], [iv]) == share
for j in waiting - {i}:
assert piece_value(vals[j], [iv]) <= share
waiting.remove(i)
def test_example() -> None:
vals = [normalize([1, 1, 1, 1]), normalize([2, 1, 1, 0]), normalize([3, 1, 0, 0])]
o = Oracle(vals)
alloc = last_diminisher(o)
assert alloc == {2: [(0, F(1, 9))], 1: [(F(1, 9), F(11, 36))], 0: [(F(11, 36), 1)]}
assert [piece_value(vals[i], alloc[i]) for i in range(3)] == [F(25, 36), F(1, 3), F(1, 3)]
# agent 3 envies agent 2: proportional, not envy-free
assert piece_value(vals[2], alloc[1]) == F(17, 36)
assert o.evals == [0, 2, 1] and o.cuts == [2, 2, 1]
assert o.total() == 3 * 3 - 1 # every agent trims in every round
def test_identical_valuations() -> None:
rng = random.Random(3)
for n in range(1, 6):
v = random_valuation(rng, 6, 5)
o = Oracle([v] * n)
alloc = last_diminisher(o)
check_partition(alloc, n)
for i in range(n):
assert piece_value(v, alloc[i]) == F(1, n)
assert o.total_cuts() == n - 1 # nobody trims
assert o.total_evals() == n * (n - 1) // 2
def test_random_valuations() -> None:
rng = random.Random(2024)
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 = last_diminisher(o)
check_partition(alloc, n)
check_rounds(vals, alloc)
for i in range(n):
assert piece_value(vals[i], alloc[i]) >= F(1, n)
assert o.total_evals() == n * (n - 1) // 2
assert n - 1 <= o.total_cuts() <= n * (n + 1) // 2 - 1
assert o.total() <= n * n - 1 if n > 1 else o.total() == 0
def test_worst_case_reaches_bound() -> None:
# agent i puts more weight to the left than every agent before it
for n in range(2, 6):
vals = [normalize([2 ** (i * (n - j)) for j in range(n)]) for i in range(n)]
o = Oracle(vals)
alloc = last_diminisher(o)
check_partition(alloc, n)
for i in range(n):
assert piece_value(vals[i], alloc[i]) >= F(1, n)
assert o.total() == n * n - 1
def test_two_agents_is_cut_and_trim() -> None:
rng = random.Random(5)
for _ in range(50):
vals = [random_valuation(rng, 5, 4) for _ in range(2)]
alloc = last_diminisher(Oracle(vals))
left = min(alloc, key=lambda i: alloc[i][0][0])
x = alloc[left][0][1]
assert value(vals[left], 0, x) == F(1, 2)
assert value(vals[1 - left], 0, x) <= F(1, 2)
History
Steinhaus (1948) described the problem of fair division for people and gave this method, which he credited to his colleagues Stefan Banach and Bronislaw Knaster. It is the first protocol for proportional division for every . Brams and Taylor (1996) treat it as a classic method for , and the counts of queries in the Robertson-Webb model were studied later, with the method of Even and Paz (1984) and the matching lower bound of Edmonds and Pruhs (2006).
References
- Hugo Steinhaus, The problem of fair division, Econometrica 16(1) (1948), 101-104, the method for n people that Steinhaus credits to Banach and Knaster
- Steven J. Brams and Alan D. Taylor, Fair Division, From Cake-Cutting to Dispute Resolution, Cambridge University Press, 1996, Chapter 2, Proportionality for n > 2, the divisible case (from p. 30)
- 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 algorithms (from p. 313)
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 VJGVJA Edit this page History Report an error Markdown