Algorithm Search⌘K

Algorithmadvanced

Devanur-Papadimitriou-Saberi-Vazirani algorithm

Also called DPSV algorithm, primal-dual Fisher market algorithm.

Computes an equilibrium of a linear Fisher market in polynomial time by raising prices and computing max flows in a primal-dual way.

Time
O(m^4 (log m + m log U + log M)) worst
Space
O(n m) extra
Where
M = the total budget; U = the largest utility; m = the number of goods; n = the number of buyers
Properties
deterministiciterative
Origin
Nikhil R. Devanur, Christos H. Papadimitriou, Amin Saberi, Vijay V. Vazirani, 2002

The DPSV algorithm, by Devanur, Papadimitriou, Saberi and Vazirani, computes the exact equilibrium of a Fisher market with linear utilities. It was the first combinatorial polynomial time algorithm for this problem. It is a primal-dual algorithm for the Eisenberg-Gale program: the prices are the dual variables, the money that flows from buyers to goods is the primal solution, and the algorithm is guided by the KKT conditions of the program instead of by LP duality. Prices start low and only go up. At all times, every good can be sold to buyers who like it best per unit of money, and the only defect is that buyers may keep money that they cannot spend. A maximum flow tells which sets of goods are already as expensive as they can be, and the algorithm raises the prices of the other goods until the surplus money is gone. The simple version below is easy to state and is exact, but its proven bound is exponential; the journal version adds balanced flows to get a polynomial bound, and Orlin's algorithm later made the bound strongly polynomial.

In plain words

Each buyer has money and likes each good to some degree. At given prices, a buyer only wants the goods that give her the most pleasure for each coin. Start with prices so low that every buyer could buy everything she wants, and make sure every good is wanted by someone. Then raise prices slowly, all by the same factor. At some point a group of goods becomes exactly as expensive as the total money of the buyers who want them. Those goods and their buyers are done, so freeze them and keep raising the other prices. If a buyer who is still active starts to like a frozen good as much as her own, wake up that frozen group again. When every good is frozen, every coin is spent and every good is sold.

The problem

There are nn buyers and mm goods, with one unit of each good. Buyer ii has a budget ei>0e_i > 0 and a linear utility ∑juijxij\sum_j u_{ij} x_{ij} with uij≥0u_{ij} \ge 0. Every buyer likes some good and every good is liked by some buyer. Find prices p>0p > 0 and an allocation in which every buyer spends her whole budget on goods of maximum bang per buck uij/pju_{ij} / p_j and every good is sold completely. This is the Fisher market equilibrium, and the prices are unique.

At prices pp, let αi=max⁡juij/pj\alpha_i = \max_j u_{ij} / p_j. The equality graph has an edge (i,j)(i, j) when uij/pj=αiu_{ij} / p_j = \alpha_i. The network N(p)N(p) has arcs from a source to each good jj with capacity pjp_j, from good jj to buyer ii with infinite capacity on every equality edge, and from buyer ii to a sink with capacity eie_i. For a set SS of goods, Γ(S)\Gamma(S) is the set of buyers joined to SS by an equality edge, p(S)=∑j∈Spjp(S) = \sum_{j \in S} p_j and e(Γ(S))=∑i∈Γ(S)eie(\Gamma(S)) = \sum_{i \in \Gamma(S)} e_i. The algorithm keeps the Invariant of the paper: the cut that separates the source from everything else is a minimum cut of N(p)N(p). By the max-flow min-cut theorem, this holds exactly when p(S)≤e(Γ(S))p(S) \le e(\Gamma(S)) for every set SS of goods (Lemma 5.1 of the paper). A set SS is tight when p(S)=e(Γ(S))p(S) = e(\Gamma(S)). Prices are an equilibrium exactly when the whole set of goods is also tight, so that a maximum flow saturates both the source arcs and the sink arcs.

The algorithm

The goods are split into active goods and frozen blocks. Each frozen block comes with its buyers, who are also frozen. The equality graph of the active part uses only active buyers, with αi\alpha_i taken over active goods. All arithmetic is exact.

  1. DPSV(utilities u, budgets e)
  2. 1p_j ← min_i e_i / m for every good j
  3. 2p_j ← max_i u_ij / α_i for every good j, where α_i = max_k u_ik / p_k at the prices of L1
  4. 3all goods and all buyers are active; no block is frozen
  5. 4while some good is active do
  6. 5(x1, S) ← the least factor x ≥ 1 at which a set of active goods goes tight, and the largest such set S
  7. 6x2 ← min over active buyers i and frozen goods j with u_ij > 0 of α_i p_j / u_ij (∞ if none)
  8. 7multiply the price of every active good by x = min(x1, x2)
  9. 8if x2 < x1 then make the frozen block that contains the good of x2 active again, with its buyers
  10. 9else freeze S with its buyers Γ(S) as a new block
  11. 10compute a maximum flow in N(p) and read the money that buyer i spends on good j from it
  12. 11return p and the money flows

Line L1 makes every price so low that the goods together cost at most the smallest budget. Line L2 lowers the price of each good until some buyer has it among her best buys, so every good has an edge; prices only go down in line L2, and the Invariant holds because every set of goods costs at most min⁡iei\min_i e_i and has at least one buyer.

Line L5 is Section 6 of the paper. Raising the active prices by a factor xx multiplies p(S)p(S) by xx and keeps the equality graph of the active part, so a set SS goes tight at x=e(Γ(S))/p(S)x = e(\Gamma(S)) / p(S), and x1x_1 is the least of these ratios. Start with SS the set of all active goods and x=e(Γ(S))/p(S)x = e(\Gamma(S)) / p(S). Compute a maximum flow in the network of SS with the source arcs scaled by xx. If it has value e(Γ(S))e(\Gamma(S)), return (x,S)(x, S). Otherwise, replace SS by the goods on the source side of the minimum cut whose source side is largest (the nodes that cannot reach the sink in the residual graph), and repeat. Each round removes at least one good (Lemma 6.3 of the paper), so line L5 costs at most mm maximum flows (Lemma 6.4).

Line L6 finds the first factor at which an active buyer ii starts to like a frozen good jj as much as her best active goods. Line L8 is the event in which this happens before any set goes tight; line L9 is the event in which a set goes tight first, and it also covers ties.

Example

Three buyers and three goods with u1=(1,4,3)u_1 = (1, 4, 3), u2=(4,2,1)u_2 = (4, 2, 1), u3=(1,1,2)u_3 = (1, 1, 2) and budgets e=(1,1,3)e = (1, 1, 3). Line L1 sets every price to 1/31/3, and line L2 keeps it: buyer 1 likes good 2 best, buyer 2 likes good 1 best, and buyer 3 likes good 3 best.

iteration active goods x1x_1 and the largest tight set x2x_2 event prices after
1 1, 2, 3 33, goods {1,2}\{1, 2\} with buyers {1,2}\{1, 2\} none freeze {1,2}\{1, 2\} at factor 33 (1,1,1)(1, 1, 1)
2 3 33, good {3}\{3\} with buyer {3}\{3\} 22, buyer 3 and good 1 unfreeze {1,2}\{1, 2\} at factor 22 (1,1,2)(1, 1, 2)
3 1, 2, 3 5/45/4, all goods with all buyers none freeze all at factor 5/45/4 (5/4,5/4,5/2)(5/4, 5/4, 5/2)

In iteration 1 the set {1,2}\{1, 2\} has ratio e({1,2})/p({1,2})=2/(2/3)=3e(\{1, 2\}) / p(\{1, 2\}) = 2 / (2/3) = 3, the set {3}\{3\} has ratio 99, and all goods together have ratio 55. In iteration 2, buyer 3 has bang per buck 22 on good 3, and goods 1 and 2 give her 11 per unit of money; when good 3 costs twice as much, they tie, so x2=2<3=x1x_2 = 2 < 3 = x_1. In iteration 3, buyer 3 is indifferent between all three goods, and all goods together cost 44 while the budgets add up to 55, so the ratio is 5/45/4. The flow of line L10 lets buyer 1 spend 11 on good 2, buyer 2 spend 11 on good 1, and buyer 3 spend 1/41/4, 1/41/4 and 5/25/2 on goods 1, 2 and 3. In units, buyer 1 gets 4/54/5 of good 2, buyer 2 gets 4/54/5 of good 1, and buyer 3 gets 1/51/5 of goods 1 and 2 and all of good 3, and every buyer's bang per buck on her goods is her maximum.

For the market u1=(2,1)u_1 = (2, 1), u2=(1,2)u_2 = (1, 2) with budgets (1,1)(1, 1), line L1 gives prices (1/2,1/2)(1/2, 1/2), the whole set goes tight at factor 22, and the algorithm returns p=(1,1)p = (1, 1), with each buyer spending her budget on her favorite good.

Correctness

The proof is in proofs/correctness.md. It keeps four invariants at the start of each iteration: the Invariant holds for the active part; no active buyer strictly prefers a frozen good to her best active goods; every frozen block is tight, satisfies the Invariant on its own, and its buyers like its goods best among all goods; and all prices are positive. Line L5 returns the true least factor and the largest tight set, because every set that goes tight first stays on the source side of every minimum cut. Prices never fall. After line L9 the remaining active part has no tight set, which bounds the number of iterations at a fixed price vector; and the prices of goods at the moments they are frozen or at which an unfreeze happens come from a finite set fixed by uu and ee, which bounds the number of iterations that raise prices. At the end every block is tight and every buyer is frozen, so the flow of line L10 spends every budget on best goods and sells every good.

Proof: The simple DPSV algorithm keeps its invariants, terminates, and returns the Fisher market equilibriumfull

Fix nn buyers with budgets ei>0e_i > 0, mm goods with one unit each, and utilities uij≥0u_{ij} \ge 0, where every buyer likes some good and every good is liked by some buyer. All arithmetic is exact. Write p(T)=∑j∈Tpjp(T) = \sum_{j \in T} p_j and e(K)=∑i∈Keie(K) = \sum_{i \in K} e_i.

At the start of an iteration, let HH be the set of active goods and AA the set of active buyers. For i∈Ai \in A let ai=max⁡j∈Huij/pja_i = \max_{j \in H} u_{ij} / p_j. The active graph has an edge (i,j)(i, j) for i∈Ai \in A, j∈Hj \in H with uij/pj=aiu_{ij} / p_j = a_i, and Γ(T)\Gamma(T) is the set of buyers joined to T⊆HT \subseteq H in it. For a frozen block (B,K)(B, K) (goods BB, buyers KK) and a buyer k∈Kk \in K, let βk=max⁡jukj/pj\beta_k = \max_j u_{kj} / p_j over all goods, and ΓK(T)\Gamma_K(T) for T⊆BT \subseteq B the buyers k∈Kk \in K with ukj/pj=βku_{kj} / p_j = \beta_k for some j∈Tj \in T.

Invariants. At line L4:

  • (I0) All prices are positive. HH and the blocks partition the goods, AA and the blocks partition the buyers, and ai>0a_i > 0 for every i∈Ai \in A.
  • (I1) p(T)≤e(Γ(T))p(T) \le e(\Gamma(T)) for every T⊆HT \subseteq H.
  • (I2) uij/pj≤aiu_{ij} / p_j \le a_i for every i∈Ai \in A and every frozen good jj.
  • (I3) Every block (B,K)(B, K) has p(B)=e(K)p(B) = e(K) and p(T)≤e(ΓK(T))p(T) \le e(\Gamma_K(T)) for every T⊆BT \subseteq B, and every k∈Kk \in K attains βk\beta_k on some good of BB.

Claim 1 (start). The invariants hold at the first iteration.

Proof. Let c=min⁡iei/mc = \min_i e_i / m be the price of line L1 and αi=max⁡kuik/c>0\alpha_i = \max_k u_{ik} / c > 0. Line L2 sets pj=max⁡iuij/αip_j = \max_i u_{ij} / \alpha_i, which is positive because some buyer likes jj, and at most cc because uij/αi≤cu_{ij} / \alpha_i \le c. For every buyer, the maximum of uij/pju_{ij} / p_j is still αi\alpha_i (a good of maximum utility keeps price cc, and no ratio rises above αi\alpha_i by the choice of pjp_j), and every good jj has an edge to the buyer that attains its maximum. So for every nonempty TT, Γ(T)\Gamma(T) is nonempty and p(T)≤mc=min⁡iei≤e(Γ(T))p(T) \le m c = \min_i e_i \le e(\Gamma(T)). No block is frozen, so (I2) and (I3) are empty. □\square

Claim 2 (line L5). Let x∗=min⁡∅≠T⊆He(Γ(T))/p(T)x^* = \min_{\emptyset \ne T \subseteq H} e(\Gamma(T)) / p(T). Line L5 makes at most mm rounds and returns x1=x∗x_1 = x^* and the largest set SS with e(Γ(S))=x∗p(S)e(\Gamma(S)) = x^* p(S). By (I1), x∗≥1x^* \ge 1.

Proof. For a set SS of goods and a factor xx, the network of line L5 has source arcs of capacity xpjx p_j for j∈Sj \in S, infinite arcs on the edges of SS, and sink arcs of capacity eie_i for i∈Γ(S)i \in \Gamma(S). A cut whose source side contains the goods X⊆SX \subseteq S must contain Γ(X)\Gamma(X) to be finite, and the cheapest such cut has capacity c(X)=x p(S∖X)+e(Γ(X))c(X) = x \, p(S \setminus X) + e(\Gamma(X)). The function cc is submodular in XX, because X↦e(Γ(X))X \mapsto e(\Gamma(X)) is a coverage function.

Call a set critical if its ratio e(Γ(T))/p(T)e(\Gamma(T)) / p(T) equals x∗x^*. Critical sets are closed under union: for critical T,T′T, T', e(Γ(T∪T′))+e(Γ(T∩T′))≤e(Γ(T))+e(Γ(T′))=x∗(p(T)+p(T′))e(\Gamma(T \cup T')) + e(\Gamma(T \cap T')) \le e(\Gamma(T)) + e(\Gamma(T')) = x^* (p(T) + p(T')), and e(Γ(T∩T′))≥x∗p(T∩T′)e(\Gamma(T \cap T')) \ge x^* p(T \cap T') (trivially if the intersection is empty), so e(Γ(T∪T′))≤x∗p(T∪T′)e(\Gamma(T \cup T')) \le x^* p(T \cup T'), with equality by the definition of x∗x^*. So there is a largest critical set S∗S^*.

Suppose the current SS contains S∗S^*, which holds at the start since S=HS = H. Let x=e(Γ(S))/p(S)≥x∗x = e(\Gamma(S)) / p(S) \ge x^*. The cut X=∅X = \emptyset has capacity x p(S)=e(Γ(S))x \, p(S) = e(\Gamma(S)), the same as X=SX = S. If the maximum flow has this value, then c(X)≥c(∅)c(X) \ge c(\emptyset) for all X⊆SX \subseteq S, that is e(Γ(X))≥x p(X)e(\Gamma(X)) \ge x \, p(X), so x≤x∗x \le x^*, hence x=x∗x = x^*; then SS is critical, and since S⊇S∗S \supseteq S^*, S=S∗S = S^*. Line L5 returns the right pair.

Otherwise x>x∗x > x^*, and let X1X_1 be the goods of any minimum cut. Then X1≠SX_1 \ne S, since c(S)=c(∅)c(S) = c(\emptyset) is not minimum, and X1⊇S∗X_1 \supseteq S^*. Indeed, let D=S∗∖X1D = S^* \setminus X_1 and suppose D≠∅D \ne \emptyset. By the choice of x∗x^*, e(Γ(S∗∩X1))≥x∗p(S∗∩X1)e(\Gamma(S^* \cap X_1)) \ge x^* p(S^* \cap X_1), so e(Γ(S∗))−e(Γ(S∗∩X1))≤x∗p(D)<x p(D)e(\Gamma(S^*)) - e(\Gamma(S^* \cap X_1)) \le x^* p(D) < x \, p(D), that is c(S∗)<c(S∗∩X1)c(S^*) < c(S^* \cap X_1). By submodularity, c(X1∪S∗)≤c(X1)+c(S∗)−c(X1∩S∗)<c(X1)c(X_1 \cup S^*) \le c(X_1) + c(S^*) - c(X_1 \cap S^*) < c(X_1), which contradicts the minimality of X1X_1. So the new S=X1S = X_1 is a proper subset of the old one and still contains S∗S^*. Each round removes a good, so there are at most mm rounds. □\square

Claim 3 (line L6). x2≥1x_2 \ge 1, and when it is finite, raising the active prices by any x≤x2x \le x_2 keeps uij/pj≤aiu_{ij} / p_j \le a_i (with the new prices and the new aia_i) for all i∈Ai \in A and frozen jj.

Proof. By (I2), aipj/uij≥1a_i p_j / u_{ij} \ge 1 for each pair in line L6. Multiplying the active prices by xx divides every aia_i by xx and keeps pjp_j for frozen jj, so uij/pj≤ai/x2≤ai/xu_{ij} / p_j \le a_i / x_2 \le a_i / x. □\square

Raising all active prices by one factor keeps the active graph: every uij/pju_{ij} / p_j with j∈Hj \in H and every aia_i are divided by the same number. Frozen buyers are unaffected, because their βk\beta_k is attained on frozen goods whose prices stay, and active goods only get dearer.

Claim 4 (line L9 keeps the invariants, and the new active part has no tight set). Suppose x=x1≤x2x = x_1 \le x_2. After line L9, the invariants hold, and every nonempty TT of the new active goods has p(T)<e(Γ′(T))p(T) < e(\Gamma'(T)), where Γ′\Gamma' is the new active graph.

Proof. After line L7, every T⊆HT \subseteq H has e(Γ(T))≥x1pold(T)=p(T)e(\Gamma(T)) \ge x_1 p_{\text{old}}(T) = p(T), with equality for T=ST = S, the largest such set (Claim 2). Let K=Γ(S)K = \Gamma(S) be frozen with SS.

(I3) for the new block: p(S)=e(K)p(S) = e(K). For T⊆ST \subseteq S, Γ(T)⊆K\Gamma(T) \subseteq K and p(T)≤e(Γ(T))p(T) \le e(\Gamma(T)). A buyer k∈Kk \in K has an edge into SS, so aka_k is attained on SS; by Claim 3, no frozen good beats aka_k; so βk=ak\beta_k = a_k is attained on SS, and ΓK(T)⊇Γ(T)\Gamma_K(T) \supseteq \Gamma(T) for T⊆ST \subseteq S. Old blocks keep (I3) by the remark after Claim 3.

New active buyers A′=A∖KA' = A \setminus K and goods H′=H∖SH' = H \setminus S. A buyer i∈A′i \in A' has no edge into SS, so her maximum over HH is attained in H′H': her aia_i is unchanged and positive, and her edges are the same. So Γ′(T)=Γ(T)∖K\Gamma'(T) = \Gamma(T) \setminus K for T⊆H′T \subseteq H'. Then Γ(S∪T)⊆K∪Γ′(T)\Gamma(S \cup T) \subseteq K \cup \Gamma'(T), and if T≠∅T \ne \emptyset had p(T)≥e(Γ′(T))p(T) \ge e(\Gamma'(T)), then e(Γ(S∪T))≤e(K)+e(Γ′(T))≤p(S)+p(T)e(\Gamma(S \cup T)) \le e(K) + e(\Gamma'(T)) \le p(S) + p(T), so S∪TS \cup T would be a tight set larger than SS. Hence p(T)<e(Γ′(T))p(T) < e(\Gamma'(T)), which gives (I1).

(I2): for j∈Sj \in S and i∈A′i \in A', uij/pj<aiu_{ij} / p_j < a_i because (i,j)(i, j) is not an edge; for older frozen goods it is Claim 3. (I0) is clear. □\square

Claim 5 (line L8 keeps the invariants). Suppose x=x2<x1x = x_2 < x_1, attained by i0∈Ai_0 \in A and a good j0j_0 of the block (B,K)(B, K). After line L8, the invariants hold, and every set TT of the new active goods with p(T)=e(Γ′′(T))p(T) = e(\Gamma''(T)) in the new active graph Γ′′\Gamma'' is a subset of B∖{j0}B \setminus \{j_0\}.

Proof. After line L7, every nonempty T⊆HT \subseteq H has e(Γ(T))≥x1pold(T)>x2pold(T)=p(T)e(\Gamma(T)) \ge x_1 p_{\text{old}}(T) > x_2 p_{\text{old}}(T) = p(T). By Claim 3, (I2) holds at the new prices, with equality for (i0,j0)(i_0, j_0).

The new active goods are H′′=H∪BH'' = H \cup B and buyers A′′=A∪KA'' = A \cup K. For i∈Ai \in A, the maximum over H′′H'' equals aia_i by (I2), so her old edges stay and she may gain edges into BB, among them (i0,j0)(i_0, j_0). For k∈Kk \in K, the maximum over H′′H'' is βk\beta_k, attained in BB by (I3), so her block edges stay. Take T=T1∪T2T = T_1 \cup T_2 with T1⊆HT_1 \subseteq H and T2⊆BT_2 \subseteq B. Then Γ′′(T)\Gamma''(T) contains the disjoint sets Γ(T1)⊆A\Gamma(T_1) \subseteq A and ΓK(T2)⊆K\Gamma_K(T_2) \subseteq K, so e(Γ′′(T))≥e(Γ(T1))+e(ΓK(T2))≥p(T1)+p(T2)e(\Gamma''(T)) \ge e(\Gamma(T_1)) + e(\Gamma_K(T_2)) \ge p(T_1) + p(T_2), which is (I1). Equality needs T1=∅T_1 = \emptyset, and it fails if j0∈T2j_0 \in T_2, because then i0∈Γ′′(T)∖Ki_0 \in \Gamma''(T) \setminus K adds ei0>0e_{i_0} > 0.

(I2) for the remaining blocks: buyers of AA by Claim 3, buyers of KK because βk\beta_k is the maximum over all goods. (I3) for the remaining blocks holds by the remark after Claim 3. (I0) is clear. □\square

Claim 6 (termination). The loop of lines L4 to L9 ends after finitely many iterations.

Proof. Every factor is at least 11 (Claims 2 and 3), so prices never fall. Call an iteration flat if its factor is 11 and rising otherwise.

Flat iterations. By Claim 4, after a freeze the next x1x_1 is above 11, so the next iteration is not a flat freeze. By Claim 5, a flat freeze right after an unfreeze of the block BB freezes fewer goods than BB has. At the very start there is no preceding unfreeze. So in a maximal run of consecutive flat iterations, every freeze except possibly the first is right after an unfreeze in the run. Group the run, after the possible first freeze, into units: an unfreeze alone, or an unfreeze followed by a freeze. Each unit lowers the number of frozen goods by at least 11, and the first freeze raises it by at most mm, so a run has at most 2m2m units and at most 4m+14m + 1 iterations.

Rising iterations. Let PP be the set of all numbers of the form e(K) rj/∑l∈Crle(K) \, r_j / \sum_{l \in C} r_l, where CC is a set of goods, KK a set of buyers, and rr a vector on CC obtained by choosing a spanning tree of a bipartite graph on C∪KC \cup K whose edges have uij>0u_{ij} > 0 and setting rr along the tree by rj′=rjuij′/uijr_{j'} = r_j u_{ij'} / u_{ij} from one root with r=1r = 1. PP is finite. When line L9 freezes SS with K=Γ(S)K = \Gamma(S), the components of the equality graph on (S,K)(S, K) split SS and KK, each satisfies p(C)≤e(Γ(C))p(C) \le e(\Gamma(C)), and they add up to equality, so each component is tight; along its edges pj′/pj=uij′/uijp_{j'} / p_j = u_{ij'} / u_{ij}, so every price in SS is in PP. A frozen price does not change, so frozen goods always have prices in PP. When line L8 fires at a rising factor, the buyer i0i_0 ties her best active good kk with j0j_0, so pk=pj0ui0k/ui0j0p_k = p_{j_0} u_{i_0 k} / u_{i_0 j_0} lies in the finite set Q=P∪{q uik/uij:q∈P}Q = P \cup \{q \, u_{ik} / u_{ij} : q \in P\}. At a rising freeze, the prices of SS lie in P⊆QP \subseteq Q. So each rising iteration leaves some good kk whose price it has just strictly raised at a value in QQ. For a fixed good, these values strictly increase from one such iteration to the next, since prices never fall. So there are at most m ∣Q∣m \, |Q| rising iterations.

Between two rising iterations there is at most one run of flat iterations, so the loop makes at most (m∣Q∣+1)(4m+2)(m |Q| + 1)(4m + 2) iterations. □\square

The bound is far from sharp. With integer data, Lemma 7.1 of the paper bounds the denominators of the prices in a newly tight set by Δ=mUm\Delta = m U^m, and Corollary 7.5 bounds the phases of its Algorithm 1 by MΔ2M \Delta^2.

Claim 7 (output). The returned (p,x)(p, x) is the Fisher market equilibrium: every buyer spends exactly eie_i, only on goods jj with uij/pj=max⁡kuik/pku_{ij} / p_j = \max_k u_{ik} / p_k, and every good jj receives exactly pjp_j, so it is sold completely.

Proof. The loop ends with H=∅H = \emptyset. In the last iteration, line L9 froze all active goods S=HS = H, and every active buyer had an edge into HH because aia_i is attained there, so all buyers are frozen too. By (I3), every block (B,K)(B, K) is tight, its buyers attain their global maximum βk\beta_k on BB, and p(T)≤e(ΓK(T))p(T) \le e(\Gamma_K(T)) for T⊆BT \subseteq B. The edges of ΓK\Gamma_K are equality edges of N(p)N(p). By the max-flow min-cut theorem (Lemma 5.1 of the paper), the network of the block has a flow that saturates every source arc of BB, of total p(B)=e(K)p(B) = e(K), so it saturates every sink arc of KK too. The blocks partition goods and buyers, so N(p)N(p) has a flow of value ∑iei=∑jpj\sum_i e_i = \sum_j p_j, and the maximum flow of line L10 saturates all source and sink arcs. Its flow from good jj to buyer ii is the money she spends on jj, on equality edges only. A buyer who spends her whole budget only on goods of maximum bang per buck has an optimal bundle, so this is an equilibrium. The equilibrium prices of a linear Fisher market are unique (see the Eisenberg-Gale program). ■\blacksquare

Complexity

Each iteration costs at most m+1m + 1 maximum flows on n+m+2n + m + 2 nodes and at most nm+n+mnm + n + m arcs. With Edmonds-Karp, as in the implementation, one flow costs O((n+m)(nm)2)O((n + m) (nm)^2) exact operations on rationals. With integer utilities and budgets, Corollary 7.5 of the paper bounds its Algorithm 1 by at most MΔ2M \Delta^2 phases and O(Mm2Δ2)O(M m^2 \Delta^2) max-flow computations, where Δ=mUm\Delta = m U^m. This bound is exponential in mm, and the paper does not know a polynomial bound for this version.

The balanced-flow version of Section 8 raises only the prices of goods next to buyers with large surplus, measured in a flow that minimizes the ℓ2\ell_2 norm of the surplus vector. It needs O(m4(log⁡m+mlog⁡U+log⁡M))O(m^4 (\log m + m \log U + \log M)) max-flow computations, which is polynomial in the bit length of the input. It is not strongly polynomial: the number of flows depends on UU and MM. The extra space is O(nm)O(nm) for the network and the flows.

Properties

  • Prices only rise. Every factor in line L7 is at least 11. Equality edges still appear and disappear, because the bang per buck of a buyer falls when her goods get dearer.
  • Exact and rational. With rational data, every price is rational, and the output is exact. Lemma 7.1 of the paper bounds the denominators of the prices of a newly tight set by Δ=mUm\Delta = m U^m, which also shows that the equilibrium has polynomial bit length.
  • Primal-dual with KKT conditions. At all times buyers spend only on equality edges and no buyer has a better good than her edges; only the condition that each buyer spends her whole budget is relaxed, and it holds at the end.
  • Deterministic. The output is the unique equilibrium price vector, with one equilibrium allocation.

Variants and improvements

  • Balanced flows. The polynomial version of Section 8 measures progress by the ℓ2\ell_2 norm of the surplus vector, which falls by a polynomial factor in each phase.
  • Strongly polynomial. Orlin's algorithm (2010) combines the same flow network with scaling and finds the equilibrium with a number of operations that depends only on nn and mm.
  • Other utilities. The paper and the AGT book chapter describe extensions of the primal-dual approach, for example to spending constraint utilities (Vazirani). For utilities that are not linear, some market problems are PPAD-hard; see PPAD-hardness of market equilibria.
  • Dynamics instead of algorithms. Proportional response dynamics and tatonnement approach the same equilibrium without a central planner, but only in the limit.

When to use it

Use it when an exact equilibrium of a linear Fisher market is needed and the market is small or medium sized, and when exact rational prices matter more than speed. It is also a good model for primal-dual algorithms that are guided by KKT conditions. For large markets where an approximate answer is enough, a convex solver on the Eisenberg-Gale program or proportional response dynamics is simpler. For guaranteed worst-case bounds, use the balanced-flow version or Orlin's algorithm.

Pitfalls

  • Starting with prices that are too high in line L1 breaks the Invariant before the first iteration: a buyer who wants a set of goods may not be able to pay for it, and later tight sets are then wrong. The prices of line L1 must cost at most the smallest budget together.
  • Skipping line L2 leaves goods with no equality edge. Such a good has no buyer, so the set that contains only it violates p(S)≤e(Γ(S))p(S) \le e(\Gamma(S)).
  • Raising all prices in line L7, frozen ones included, breaks the tight blocks: their price then exceeds the money of their buyers.
  • Freezing SS in line L9 without its buyers leaves buyers whose money is already used up by SS in the active part, so their budgets are counted twice in later tight sets.
  • Floating point arithmetic in lines L2, L5 and L6 makes the equality tests uij/pj=αiu_{ij} / p_j = \alpha_i fail at random, so edges are lost. Use exact rationals.
  • Reading the allocation from the last flow of line L5 is wrong, because that flow covers only the last active part. Line L10 computes a fresh flow over all goods.

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.

dpsv_algorithm.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/dpsv-algorithm/. The authors are listed in prov/MZRKQA.jsonl.
"""The primal-dual algorithm of Devanur, Papadimitriou, Saberi and Vazirani for linear Fisher markets.

Buyers are 0, ..., n - 1 and goods are 0, ..., m - 1, with one unit of each good. u[i][j] >= 0
is the utility of buyer i for all of good j, and e[i] > 0 is the budget of buyer i. Every buyer
must like some good and every good must be liked by some buyer. This is the simple version of
the algorithm (Algorithm 1 of the paper): prices only go up, by a common factor on the active
goods, until a set of goods goes tight and is frozen. All numbers are exact Fractions.
"""

from collections import deque
from fractions import Fraction


def max_flow(num_nodes, cap, s, t):
    """Edmonds-Karp maximum flow. cap maps (a, b) to a capacity. Returns the flow value, the
    flow on each arc of cap, and the residual capacities."""
    res = {}
    adj = [[] for _ in range(num_nodes)]
    for (a, b), c in cap.items():
        if (a, b) not in res and (b, a) not in res:
            adj[a].append(b)
            adj[b].append(a)
        res[(a, b)] = res.get((a, b), Fraction(0)) + c
        res.setdefault((b, a), Fraction(0))
    value = Fraction(0)
    while True:
        prev = {s: None}
        queue = deque([s])
        while queue and t not in prev:
            a = queue.popleft()
            for b in adj[a]:
                if b not in prev and res[(a, b)] > 0:
                    prev[b] = a
                    queue.append(b)
        if t not in prev:
            break
        path, b = [], t
        while prev[b] is not None:
            path.append((prev[b], b))
            b = prev[b]
        push = min(res[arc] for arc in path)
        for a, b in path:
            res[(a, b)] -= push
            res[(b, a)] += push
        value += push
    flow = {arc: c - res[arc] for arc, c in cap.items()}
    return value, flow, res, adj


def reaches_sink(num_nodes, res, adj, t):
    """The nodes that can reach t in the residual graph."""
    seen = {t}
    queue = deque([t])
    while queue:
        b = queue.popleft()
        for a in adj[b]:
            if a not in seen and res[(a, b)] > 0:
                seen.add(a)
                queue.append(a)
    return seen


def bang(u, p, i, goods):
    """The maximum bang per buck of buyer i over the given goods."""
    return max(Fraction(u[i][j]) / p[j] for j in goods)


def tight_set(u, e, p, goods, buyers, edges):
    """Section 6 of the paper: the least factor x >= 1 at which a set of the given goods goes
    tight, and the largest such set S. A set S is tight at x when x p(S) = e(Gamma(S)), where
    Gamma(S) is the set of buyers joined to S by an edge."""
    S = set(goods)
    while True:
        nbrs = {i for (i, j) in edges if j in S}
        x = sum(e[i] for i in nbrs) / sum(p[j] for j in S)
        m = len(u[0])
        src, snk = 0, len(u) + m + 1
        big = sum(e) * (x + 1) + 1  # larger than any finite cut, so never cut
        cap = {}
        for j in S:
            cap[(src, 1 + j)] = x * p[j]
        for i, j in edges:
            if j in S:
                cap[(1 + j, 1 + m + i)] = big
        for i in nbrs:
            cap[(1 + m + i, snk)] = e[i]
        value, _, res, adj = max_flow(snk + 1, cap, src, snk)
        if value == sum(e[i] for i in nbrs):
            return x, S
        to_sink = reaches_sink(snk + 1, res, adj, snk)
        # the largest minimum cut: every node that cannot reach the sink stays on the source side
        S = {j for j in S if 1 + j not in to_sink}


def equality_flow(u, e, p):
    """A maximum flow in the network N(p): source to good j with capacity p_j, good j to buyer i
    on every equality edge, buyer i to sink with capacity e_i. Returns (value, x) with x[i][j]
    the money that buyer i spends on good j."""
    n, m = len(u), len(u[0])
    src, snk = 0, n + m + 1
    alpha = [bang(u, p, i, range(m)) for i in range(n)]
    cap = {}
    for j in range(m):
        cap[(src, 1 + j)] = p[j]
    for i in range(n):
        cap[(1 + m + i, snk)] = e[i]
        for j in range(m):
            if u[i][j] > 0 and Fraction(u[i][j]) / p[j] == alpha[i]:
                cap[(1 + j, 1 + m + i)] = sum(e) + 1
    value, flow, _, _ = max_flow(snk + 1, cap, src, snk)
    x = [[flow.get((1 + j, 1 + m + i), Fraction(0)) for j in range(m)] for i in range(n)]
    return value, x


def dpsv(u, e, trace=None):
    """Return equilibrium prices p and money spent x[i][j] for the linear Fisher market (u, e).
    If trace is a list, one tuple (factor, event, goods) is appended per raise of prices."""
    n, m = len(u), len(u[0])
    e = [Fraction(b) for b in e]
    p = [min(e) / m for _ in range(m)]  # @line L1
    alpha = [bang(u, p, i, range(m)) for i in range(n)]
    for j in range(m):  # @line L2
        p[j] = max(Fraction(u[i][j]) / alpha[i] for i in range(n) if u[i][j] > 0)
    active_goods = set(range(m))  # @line L3
    frozen = []  # list of (goods, buyers) of frozen components
    while active_goods:  # @line L4
        frozen_buyers = set().union(*[b for _, b in frozen]) if frozen else set()
        active_buyers = [i for i in range(n) if i not in frozen_buyers]
        a = {i: bang(u, p, i, active_goods) for i in active_buyers}
        edges = [(i, j) for i in active_buyers for j in active_goods
                 if u[i][j] > 0 and Fraction(u[i][j]) / p[j] == a[i]]
        x1, S = tight_set(u, e, p, active_goods, active_buyers, edges)  # @line L5
        x2, edge2 = None, None
        for i in active_buyers:  # @line L6
            for j in range(m):
                if j not in active_goods and u[i][j] > 0:
                    x = a[i] * p[j] / u[i][j]
                    if x2 is None or x < x2:
                        x2, edge2 = x, (i, j)
        x = x1 if x2 is None or x1 <= x2 else x2
        for j in active_goods:  # @line L7
            p[j] *= x
        if x2 is not None and x2 < x1:  # @line L8
            k = next(c for c, (goods, _) in enumerate(frozen) if edge2[1] in goods)
            goods, _ = frozen.pop(k)
            active_goods |= goods
            if trace is not None:
                trace.append((x, "unfreeze", sorted(goods)))
        else:  # @line L9
            buyers = {i for (i, j) in edges if j in S}
            frozen.append((set(S), buyers))
            active_goods -= S
            if trace is not None:
                trace.append((x, "freeze", sorted(S)))
    value, x = equality_flow(u, e, p)  # @line L10
    return p, x  # @line L11
test_dpsv_algorithm.py 167 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/dpsv-algorithm/. The authors are listed in prov/MZRKQA.jsonl.

import random
import unittest
from fractions import Fraction
from itertools import combinations

from dpsv_algorithm import dpsv

# The example of the page: 3 buyers and 3 goods.
EXAMPLE_U = [[1, 4, 3], [4, 2, 1], [1, 1, 2]]
EXAMPLE_E = [1, 1, 3]


def is_equilibrium(u, e, p, x):
    """Exact check: every buyer spends all her money, only on goods of maximum bang per buck,
    and every good is sold out (all prices are positive)."""
    n, m = len(u), len(u[0])
    if any(q <= 0 for q in p):
        return False
    for i in range(n):
        best = max(Fraction(u[i][j]) / p[j] for j in range(m))
        if sum(x[i]) != e[i]:
            return False
        for j in range(m):
            if x[i][j] < 0 or (x[i][j] > 0 and Fraction(u[i][j]) / p[j] != best):
                return False
    return all(sum(x[i][j] for i in range(n)) == p[j] for j in range(m))


def brute_force_prices(u, e):
    """All equilibrium price vectors, by trying every set of spending edges as the support:
    the prices on a connected support are fixed by the utility ratios and by the budgets."""
    n, m = len(u), len(u[0])
    all_edges = [(i, j) for i in range(n) for j in range(m) if u[i][j] > 0]
    found = set()
    for k in range(1, len(all_edges) + 1):
        for H in combinations(all_edges, k):
            p = prices_from_support(u, e, H)
            if p is None:
                continue
            x = support_flow(u, e, p, H)
            if x is not None and is_equilibrium(u, e, p, x):
                found.add(tuple(p))
    return found


def prices_from_support(u, e, H):
    n, m = len(u), len(u[0])
    rel = {}  # node -> price (goods) or bang per buck (buyers), up to a factor per component
    comps = []
    for start in range(m):
        if ("g", start) in rel:
            continue
        rel[("g", start)] = Fraction(1)
        stack, comp = [("g", start)], [("g", start)]
        while stack:
            kind, a = stack.pop()
            for i, j in H:
                if kind == "g" and j == a:
                    nxt, val = ("b", i), Fraction(u[i][j]) / rel[("g", j)]
                elif kind == "b" and i == a:
                    nxt, val = ("g", j), Fraction(u[i][j]) / rel[("b", i)]
                else:
                    continue
                if nxt in rel:
                    if rel[nxt] != val:
                        return None
                else:
                    rel[nxt] = val
                    stack.append(nxt)
                    comp.append(nxt)
        comps.append(comp)
    if any(("b", i) not in rel for i in range(n)):
        return None
    p = [None] * m
    for comp in comps:
        money = sum(Fraction(e[a]) for kind, a in comp if kind == "b")
        if money == 0:
            return None
        scale = money / sum(rel[(kind, a)] for kind, a in comp if kind == "g")
        for kind, a in comp:
            if kind == "g":
                p[a] = rel[(kind, a)] * scale
    return p


def support_flow(u, e, p, H):
    """Spend along the support H by solving the flow on a forest (peel leaves); None if H has a
    cycle or the flow would be negative."""
    n, m = len(u), len(u[0])
    if len(H) > n + m - 1:
        return None
    x = [[Fraction(0)] * m for _ in range(n)]
    left = set(H)
    need = {("b", i): Fraction(e[i]) for i in range(n)}
    need.update({("g", j): p[j] for j in range(m)})
    while left:
        deg = {}
        for i, j in left:
            deg[("b", i)] = deg.get(("b", i), 0) + 1
            deg[("g", j)] = deg.get(("g", j), 0) + 1
        leaf = next((v for v, d in deg.items() if d == 1), None)
        if leaf is None:
            return None
        (i, j) = next(ed for ed in left if ("b", ed[0]) == leaf or ("g", ed[1]) == leaf)
        amount = need[leaf]
        if amount < 0:
            return None
        x[i][j] = amount
        need[("b", i)] -= amount
        need[("g", j)] -= amount
        left.discard((i, j))
    if any(v != 0 for v in need.values()):
        return None
    return x


def random_market(rng, n, m):
    while True:
        u = [[rng.choice([0, 1, 2, 3, 4, 5]) for _ in range(m)] for _ in range(n)]
        if all(any(row) for row in u) and all(any(u[i][j] for i in range(n)) for j in range(m)):
            return u, [rng.randint(1, 4) for _ in range(n)]


class TestDPSV(unittest.TestCase):
    def test_check_value(self):
        p, x = dpsv([[2, 1], [1, 2]], [1, 1])
        self.assertEqual(p, [1, 1])
        self.assertEqual(x, [[1, 0], [0, 1]])

    def test_example(self):
        trace = []
        p, x = dpsv(EXAMPLE_U, EXAMPLE_E, trace)
        self.assertEqual(p, [Fraction(5, 4), Fraction(5, 4), Fraction(5, 2)])
        self.assertTrue(is_equilibrium(EXAMPLE_U, EXAMPLE_E, p, x))
        self.assertEqual(x, [[0, 1, 0], [1, 0, 0], [Fraction(1, 4), Fraction(1, 4), Fraction(5, 2)]])
        self.assertEqual(trace, [(Fraction(3), "freeze", [0, 1]), (Fraction(2), "unfreeze", [0, 1]),
                                 (Fraction(5, 4), "freeze", [0, 1, 2])])

    def test_one_buyer(self):
        # A single buyer must find every good a best buy, so prices are proportional to utilities.
        p, x = dpsv([[1, 3, 2]], [6])
        self.assertEqual(p, [1, 3, 2])
        self.assertEqual(x, [[1, 3, 2]])

    def test_random_against_brute_force(self):
        rng = random.Random(7)
        for _ in range(40):
            n, m = rng.randint(1, 3), rng.randint(1, 3)
            u, e = random_market(rng, n, m)
            p, x = dpsv(u, e)
            self.assertTrue(is_equilibrium(u, e, p, x))
            self.assertEqual(brute_force_prices(u, e), {tuple(p)})

    def test_prices_never_fall(self):
        rng = random.Random(11)
        for _ in range(30):
            u, e = random_market(rng, 3, 3)
            trace = []
            dpsv(u, e, trace)
            self.assertTrue(all(f >= 1 for f, _, _ in trace))


if __name__ == "__main__":
    unittest.main()

History

Devanur, Papadimitriou, Saberi and Vazirani presented the algorithm at FOCS 2002. Their first analysis had a subtle flaw, and the journal version (Journal of the ACM, 2008) fixed it with balanced flows and proved a polynomial bound. It was the first combinatorial polynomial time algorithm for the linear Fisher market, and it showed that the primal-dual method works for a convex program whose KKT conditions mix primal and dual variables. The paper stated a strongly polynomial algorithm as an open question, and Orlin answered it in 2010.

References

  1. Nikhil R. Devanur, Christos H. Papadimitriou, Amin Saberi and Vijay V. Vazirani, Market equilibrium via a primal-dual algorithm for a convex program, Journal of the ACM 55(5), Article 22 (2008), Section 3 (the network N(p) and the Invariant), Section 5 (Algorithm 1 and Lemma 5.1), Section 6 (Lemma 6.4, finding the tight set), Section 7 (Lemma 7.1 and Corollary 7.5), Section 8 (balanced flows and the polynomial bound)
  2. Noam Nisan, Tim Roughgarden, Eva Tardos and Vijay V. Vazirani (eds.), Algorithmic Game Theory, Cambridge University Press, 2007, Chapter 5, Vijay V. Vazirani, Combinatorial algorithms for market equilibria
  3. James B. Orlin, Improved algorithms for computing Fisher's market clearing prices, STOC 2010, 291-300, the whole paper (a strongly polynomial algorithm, which answers the open question that the DPSV paper states)
  4. Edmund Eisenberg and David Gale, Consensus of subjective probabilities: the pari-mutuel method, Annals of Mathematical Statistics 30(1), 165-168 (1959), the whole paper (the convex program whose KKT conditions the algorithm satisfies)

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