Algorithmcore
Envy-cycle elimination
Also called Lipton-Markakis-Mossel-Saberi algorithm.
Gives each good to an agent no one envies, rotating bundles along envy cycles to create one. It returns an EF1 allocation for monotone valuations.
- Time
- O(m n^3) worst
- Space
- O(n^2 + m) extra
- Where
- m = the number of goods; n = the number of agents
- Model
- Word RAM
- Properties
- deterministicofflineiterative
- Evidence
- Proof sketchCode in 1 languages
Envy-cycle elimination gives indivisible goods to agents one at a time, always to an agent that nobody envies. If every agent is envied, the envy graph has a cycle, and the bundles move one step along the cycle, which makes every agent on it better off and removes envy edges; then some agent is unenvied. Lipton, Markakis, Mossel and Saberi gave it in 2004. For every monotone valuation, not only additive ones, the output is envy-free up to one good (EF1), and it uses value queries. This makes it the standard way to get EF1 beyond additive valuations, where round-robin allocation can fail.
In plain words
Goods arrive one by one. Each new good goes to a person whom nobody envies. Then nobody can complain much about that person: before the gift, nobody wanted that person's share, so after the gift the envy is about one good at most.
Sometimes everybody is envied by somebody. Follow the envy from person to person and you come back to where you started: Ann wants Bob's share, Bob wants Cid's share, and Cid wants Ann's share. Pass the shares around the circle so that each of them gets the share they wanted. Everyone on the circle is now happier, and nobody else notices any change. Repeat until someone is unenvied, and give the good to that person.
The problem
There are agents and indivisible goods . Agent has a valuation on sets of goods with , given by a value oracle. The valuations are monotone: implies . The goal is an EF1 allocation of all goods: for all agents with , some has .
The envy graph of a partial allocation has the agents as vertices and an edge when .
The algorithm
ENVY-CYCLE-ELIMINATION(v_1, ..., v_n, g_1, ..., g_m) ▷ monotone valuations, value oracle- 1
A_i ← ∅ for every agent i - 2
for each good g in the order g_1, ..., g_m do - 3
G ← the envy graph of A - 4
while every agent has an incoming edge in G do - 5
C ← a cycle c_0, c_1, ..., c_(k-1) of G in which c_(t+1) envies c_t (indices mod k) - 6
give the bundle A_(c_t) to agent c_(t+1) for every t - 7
G ← the envy graph of the new A - 8
i ← the agent of smallest index with no incoming edge in G - 9
A_i ← A_i ∪ {g} - 10
return A
Line L4 tests for a source of the envy graph. When there is none, every vertex has an incoming edge, so walking backward along incoming edges must repeat a vertex, and line L5 finds a cycle in time, as in cycle detection. Line L6 hands each envied bundle on the cycle to the agent that envies it. Line L8 breaks ties by index. The allocation is partial until the last good.
Example
Three agents value five goods additively:
| agent 1 | 5 | 6 | 9 | 1 | 8 |
| agent 2 | 4 | 1 | 3 | 2 | 6 |
| agent 3 | 8 | 4 | 7 | 9 | 2 |
- Good : nobody envies anybody, so agent 1 gets .
- Good : agents 2 and 3 envy agent 1, so agent 2, the first unenvied agent, gets .
- Good : agent 3 has nothing and is unenvied, so it gets . Now .
- Good : agent 1 envies 2 and 3 (values 6 and 9 against 5), agent 2 envies 1 and 3 (4 and 3 against 1), and agent 3 envies 1 (8 against 7). Every agent is envied. Line L5 finds the cycle of agents 1 and 2, which envy each other, and line L6 swaps their bundles: . Now nobody envies agent 1, which values at 6, so agent 1 gets : .
- Good : agent 1 envies 3 (9 against 7), agent 3 envies 1 and 2 (13 and 8 against 7), and agent 2 envies nobody. Every agent is envied again. Agents 1 and 3 envy each other, so they swap: . Now nobody envies anybody, and agent 1 gets .
The output is , , , worth 17, 4 and 13 to their owners, after 2 rotations. Agent 2 values at 9; without it is worth , so the envy is within EF1.
Correctness
The partial allocation is EF1 before every step. A rotation in line L6 keeps the same bundles and gives each agent on the cycle a bundle it strictly prefers, while the others keep theirs; the EF1 test of an agent against a bundle only gets easier when its own value rises. Line L9 gives to an agent that nobody envies, so for every other agent , , and removing meets EF1. The loop of line L4 stops because each rotation removes at least two edges from the envy graph, while each new good adds at most . The full proof is in proofs/correctness.md.
Proof: Envy-cycle elimination ends and returns an EF1 allocation for monotone valuationsfull
Setting. There are agents and goods . Each valuation is defined on all sets of goods, has , and is monotone: implies . We refer to the lines L1 to L10 of the page. A partial allocation is a list of disjoint bundles . It is EF1 if for all agents with some has . The envy graph has an edge when , and is its number of edges.
Lemma 1 (a cycle exists in line L5). If every agent has an incoming edge in , then has a cycle with in which envies for every , indices taken mod .
Proof. Start at any agent and let be an agent with an edge . Among the agents two are equal, say with and least. Then is a cycle of the required kind. It has agents, because there are no loops: is false.
Lemma 2 (a rotation keeps EF1 and raises the values on the cycle). Let be EF1 and let be the result of line L6. Then for every agent on the cycle, for every other agent, the bundles of are those of in another order, and is EF1.
Proof. Agent gets , which it values more than because it envies . Agents off the cycle keep their bundles. The bundles of the cycle agents are permuted among them, so the set of bundles does not change.
Now take agents with , and let . If for some , then EF1 of gives a good with . Otherwise , which happens only when is on the cycle; then for any , by monotonicity.
Lemma 3 (giving the good to an unenvied agent keeps EF1). Let be EF1, let have no incoming edge in , and let be with for a good outside all bundles. Then is EF1.
Proof. For an agent and the bundle , remove : , since does not envy . For an agent and a bundle with , nothing changed. For agent and a bundle , EF1 of gives with , by monotonicity.
Lemma 4 (edge count). A rotation of line L6 over a cycle of agents gives . The step of line L9 gives .
Proof. Rotation: by Lemma 2 the set of bundles is the same. For an agent off the cycle, its own bundle and the set of the other bundles are the same, so its out-degree is the same. For an agent on the cycle, its new own bundle is . A bundle that envies in is worth more to than , hence more than , so also envied its owner in ; and the bundle , which envied in , is now its own. So the out-degree of drops by at least 1, and the agents of the cycle lose at least edges.
Line L9: only changes. An edge with is unchanged. An edge out of in means , so it was in . New edges can only point into , and there are at most of them.
Theorem. The algorithm ends after at most rotations, and it returns an EF1 allocation of all goods.
Proof. The edge count starts at 0 in line L1 and is never negative. By Lemma 4 the steps of line L9 add at most edges in total, and each rotation removes at least edges (Lemma 1). So the total number of rotations is at most , every loop of line L4 ends, and line L8 then finds an agent with no incoming edge.
The empty allocation of line L1 is EF1. By Lemma 2 each rotation keeps EF1, and by Lemma 3 each step of line L9 keeps EF1, since its receiver has no incoming edge. The good of line L2 is outside all bundles, because each good is given once. After the last good, every good lies in exactly one bundle, and the allocation is EF1.
Complexity
Count the edges of the envy graph. Line L9 can add only edges into , at most , and cannot add edges out of , because is monotone. A rotation over a cycle of agents keeps the out-degree of every agent off the cycle and lowers the out-degree of every agent on it by at least 1, so it removes at least 2 edges. Hence the whole run makes at most rotations. Each envy graph costs value queries and time; with graphs in line L3 and one per rotation in line L7, the run uses value queries and other steps. The extra space is for the graph plus the bundles.
Properties
- EF1 for monotone valuations. For any monotone valuations, not only additive ones.
- Envy bounded by one marginal value. The proof gives more: when envies the owner of a bundle , the envy is at most the marginal value of the last good that joined . This is the form of the guarantee in Lipton et al.
- Not Pareto optimal. The rule ignores efficiency. For two agents and goods worth and , the order gives to agent 1; then agent 2 is unenvied (agent 1 values its own good at 0 and the empty bundle at 0) and gets , and both agents have 0 while the swap would give both 1.
- No rotations with identical valuations. With one common valuation, an edge means , so the envy graph has no cycle and line L4 never loops.
- The order of the goods matters. It changes the output, but not the guarantee.
Variants and improvements
- Goods in decreasing order. For identical additive valuations, giving the goods in decreasing order of value makes line L8 choose a bundle of least value, which gives an EFX allocation; the EFX page has the proof.
- Making the envy graph acyclic. The rotations of lines L4 to L7, run until no cycle is left, turn any allocation into one with an acyclic envy graph and with every agent at least as well off. The EFX algorithms of Chaudhury, Garg and Mehlhorn use this step (Fact 5 in their paper), as on the EFX existence for three agents page.
- Additive valuations. Round-robin allocation is simpler and faster, at time.
When to use it
Use it when valuations are monotone but not additive, such as submodular or coverage valuations, and EF1 is the goal; it needs only a value oracle. Use it also when goods are handed out one at a time and the partial allocation must stay EF1 after each good. For additive valuations, round-robin is enough.
Pitfalls
- Giving the good to any agent. If line L8 picks an envied agent, the new good adds to a bundle that someone already prefers to its own, and EF1 can fail after one more good. The receiver must have no incoming edge.
- Rotating in the wrong direction. In line L6 each agent must receive the bundle of the agent that it envies. Moving bundles the other way lowers values and can break EF1.
- Not rebuilding the graph. Line L7 must recompute the envy graph after a rotation, because every edge out of a cycle agent can change. Reusing the old graph can rotate along a cycle that no longer exists.
- Non-monotone valuations. The step in line L9 uses . If adding a good can lower a value, the receiver may start to envy others beyond one good, and the output can fail EF1.
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
# Part of https://algorithm.tamnd.com/envy-cycle-elimination/. The authors are listed in prov/0PA93X.jsonl.
from fractions import Fraction
from typing import Callable, FrozenSet, Optional, Sequence
Valuation = Callable[[FrozenSet[int]], object]
def additive(values: Sequence[Sequence]) -> list[Valuation]:
"""Turn a matrix values[i][g] into additive valuation functions."""
rows = [[Fraction(x) for x in row] for row in values]
return [lambda s, r=r: sum((r[g] for g in s), Fraction(0)) for r in rows]
def envy_graph(vals: Sequence[Valuation], bundles: Sequence[FrozenSet[int]]) -> list[list[int]]:
"""out[i] lists the agents j whose bundle agent i values strictly more than its own."""
n = len(bundles)
own = [vals[i](bundles[i]) for i in range(n)]
return [[j for j in range(n) if j != i and vals[i](bundles[j]) > own[i]] for i in range(n)]
def find_cycle(out: list[list[int]]) -> list[int]:
"""A cycle of the envy graph when every agent has an incoming edge.
Walks backward from agent 0 along incoming edges until an agent
repeats. Returns [c_0, ..., c_(k-1)] where c_(t+1) envies c_t and c_0
envies c_(k-1).
"""
n = len(out)
pred = [-1] * n
for i in range(n):
for j in out[i]:
if pred[j] < 0:
pred[j] = i
seen = {}
walk = []
x = 0
while x not in seen:
seen[x] = len(walk)
walk.append(x)
x = pred[x]
return walk[seen[x]:]
def envy_cycle_elimination(
vals: Sequence[Valuation], m: int, order: Optional[Sequence[int]] = None, stats: Optional[dict] = None
) -> list[FrozenSet[int]]:
"""Lipton, Markakis, Mossel and Saberi's envy-cycle elimination.
vals[i] maps a frozenset of goods (from 0..m-1) to the value of agent i.
Each valuation must be monotone: adding a good never lowers the value.
The goods are given out in the given order (default 0..m-1). Each good
goes to an agent that no one envies; when every agent is envied, the
bundles turn along an envy cycle first. Ties go to the smallest agent
index. Returns the bundles, which form an EF1 allocation. If stats is a
dict, stats["rotations"] gets the number of cycle rotations.
Cost: each good adds at most n - 1 envy edges and each rotation removes
at least 2, so there are at most m(n-1)/2 rotations in total. Each envy
graph needs n^2 value queries, so the run uses O(m n^3) value queries
and O(m n^3) other steps.
"""
n = len(vals)
if n == 0:
raise ValueError("at least one agent is needed")
goods = list(range(m)) if order is None else list(order)
if sorted(goods) != list(range(m)):
raise ValueError("order must be a permutation of the goods")
bundles = [frozenset() for _ in range(n)] # @line L1
rotations = 0
for g in goods: # @line L2
out = envy_graph(vals, bundles) # @line L3
while True:
indeg = [0] * n
for i in range(n):
for j in out[i]:
indeg[j] += 1
if min(indeg) == 0: # @line L4
break
cycle = find_cycle(out) # @line L5
k = len(cycle)
old = {c: bundles[c] for c in cycle}
for t in range(k): # @line L6
bundles[cycle[(t + 1) % k]] = old[cycle[t]]
rotations += 1
out = envy_graph(vals, bundles) # @line L7
i = indeg.index(0) # @line L8
bundles[i] = bundles[i] | {g} # @line L9
if stats is not None:
stats["rotations"] = rotations
return bundles # @line L10
test_envy_cycle_elimination.py 134 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/envy-cycle-elimination/. The authors are listed in prov/0PA93X.jsonl.
import itertools
import random
import pytest
from envy_cycle_elimination import additive, envy_cycle_elimination, envy_graph, find_cycle
def is_ef1(vals, bundles) -> bool:
"""EF1 by its definition, for any valuation functions."""
n = len(bundles)
for i in range(n):
mine = vals[i](bundles[i])
for j in range(n):
if i == j or not bundles[j]:
continue
if not any(mine >= vals[i](bundles[j] - {g}) for g in bundles[j]):
return False
return True
def random_monotone(rng: random.Random, m: int):
"""A random monotone set function: random values on subsets, then the running maximum over subsets."""
table = {}
for mask in range(1 << m):
best = rng.randint(0, 20)
for g in range(m):
if mask >> g & 1:
best = max(best, table[mask ^ (1 << g)])
table[mask] = best
def v(s):
mask = 0
for g in s:
mask |= 1 << g
return table[mask]
return v
def random_coverage(rng: random.Random, m: int):
cover = [frozenset(e for e in range(4) if rng.random() < 0.4) for _ in range(m)]
weight = [rng.randint(1, 6) for _ in range(4)]
def v(s):
seen = set()
for g in s:
seen |= cover[g]
return sum(weight[e] for e in seen)
return v
def test_worked_example():
values = [[5, 6, 9, 1, 8], [4, 1, 3, 2, 6], [8, 4, 7, 9, 2]]
vals = additive(values)
stats = {}
bundles = envy_cycle_elimination(vals, 5, stats=stats)
assert bundles == [frozenset({2, 4}), frozenset({0}), frozenset({1, 3})]
assert stats["rotations"] == 2
assert [vals[i](bundles[i]) for i in range(3)] == [17, 4, 13]
assert envy_graph(vals, bundles) == [[], [0], []]
assert vals[1](bundles[0] - {4}) <= 4
assert is_ef1(vals, bundles)
def test_find_cycle():
out = [[1], [2], [0]]
cycle = find_cycle(out)
k = len(cycle)
for t in range(k):
assert cycle[t] in out[cycle[(t + 1) % k]]
def test_ef1_additive_random():
rng = random.Random(11)
for _ in range(600):
n, m = rng.randint(1, 4), rng.randint(0, 7)
values = [[rng.randint(0, 9) for _ in range(m)] for _ in range(n)]
order = list(range(m))
rng.shuffle(order)
stats = {}
bundles = envy_cycle_elimination(additive(values), m, order, stats)
assert sorted(g for b in bundles for g in b) == list(range(m))
assert is_ef1(additive(values), bundles)
assert stats["rotations"] <= m * n * (n - 1) // 2
def test_ef1_monotone_random():
rng = random.Random(12)
for _ in range(400):
n, m = rng.randint(2, 4), rng.randint(1, 7)
vals = [random_monotone(rng, m) if rng.random() < 0.5 else random_coverage(rng, m) for _ in range(n)]
stats = {}
bundles = envy_cycle_elimination(vals, m, stats=stats)
assert sorted(g for b in bundles for g in b) == list(range(m))
assert is_ef1(vals, bundles)
assert stats["rotations"] <= m * n * (n - 1) // 2
def test_identical_valuations_need_no_rotation():
rng = random.Random(13)
for _ in range(100):
n, m = rng.randint(2, 4), rng.randint(1, 7)
row = [rng.randint(0, 9) for _ in range(m)]
stats = {}
bundles = envy_cycle_elimination(additive([row] * n), m, stats=stats)
assert stats["rotations"] == 0
assert is_ef1(additive([row] * n), bundles)
def test_non_monotone_can_fail():
# A valuation that drops when a second good is added breaks the invariant.
found = False
rng = random.Random(14)
for _ in range(3000):
m = 3
table = {s: rng.randint(0, 5) for k in range(m + 1) for s in itertools.combinations(range(m), k)}
vals = [lambda s, t=table: t[tuple(sorted(s))]] * 2
bundles = envy_cycle_elimination(vals, m)
if not is_ef1(vals, bundles):
found = True
break
assert found
def test_bad_input():
with pytest.raises(ValueError):
envy_cycle_elimination([], 2)
with pytest.raises(ValueError):
envy_cycle_elimination(additive([[1, 2]]), 2, order=[0, 0])
History
Lipton, Markakis, Mossel and Saberi introduced the envy graph and its cycle rotations at EC 2004, with the guarantee that envy is at most the largest marginal value of one good. The same envy-graph step is a building block of later EFX algorithms, such as the one for three agents by Chaudhury, Garg and Mehlhorn.
References
- Richard J. Lipton, Evangelos Markakis, Elchanan Mossel and Amin Saberi, On approximately fair allocations of indivisible goods, Proceedings of the 5th ACM Conference on Electronic Commerce (EC 2004), 125-131, the envy-graph algorithm and its bound on envy by the largest marginal value of a single good
- Bhaskar Ray Chaudhury, Jugal Garg and Kurt Mehlhorn, EFX exists for three agents, Journal of the ACM 71(1) (2024), 1-27 (conference version EC 2020; arXiv:2002.05119), Fact 5 (the envy graph can be made acyclic, after Lipton et al.)
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 0PA93X Edit this page History Report an error Markdown