Analysis of Algorithms Minimum Mean-Weight Cycles Uri Zwick Tel Aviv University December 2015 Last modified: December 9, 2015 1 Minimum Mean-Weight Cycles Let πΊ = (π, πΈ, π€), where π€: πΈ → β. The mean (average) weight of a cycle πΆ is π€(πΆ)/|πΆ|. Find a cycle πΆ with minimum mean weight. Generalizes the problem of finding a negative cycle. Useful in several applications (e.g., min cost flow). Finding a minimum weight cycle is NP-hard. (Hamiltonian cycle.) If π€: πΈ → {−π, … , 0, … , π}, we can use Goldberg’s algorithm + binary search to get an π π π log π log ππ -time algorithm. Can we get a strongly-polynomial time algorithm? 2 Karp’s algorithm [Karp (1978)] πΏπ π£ − πΏπ (π£) π = min max π£∈π 0≤π<π π−π ∗ π∗ – the minimum mean weight of a cycle in πΊ. πΏπ (π£) – the smallest weight of a π-edge path that ends at π£. Add a source vertex π and 0-edges (π , π£) for all π£ ∈ π. πΏπ (π£) is then be the length of the shortest path from π to π£ that uses exactly π + 1 edges. The values πΏπ (π£), for every π£ ∈ π and 0 ≤ π < π, can be computed in π(ππ) time using the Bellman-Ford algorithm. Total running time of Karp’s algorithm is thus π(ππ). Exercise: How do we find a minimum mean weight cycle? (Variant of) Bellman-Ford πΏ0 π£ = 0, for every π£ ∈ π. πΏπ π£ = min πΏπ−1 π’ + π€ π’, π£ | π’, π£ ∈ πΈ , π > 0 , π£ ∈ π. (How is this different from the “standard” Bellman-Ford?) We may have πΏπ π£ = ∞, for some π, i.e., there is no π-edge path ending at π£. This does not cause any problems. (Why?) We can also remove such vertices from consideration. (Why?) Running time is π(ππ). Exercise: Implement Karp’s algorithm in π(ππ) time using only π(π) extra space. (In particular, we cannot store πΏπ (π£) for every π and every π£, as this requires Θ(π2 ) space.) Karp’s algorithm [Karp (1978)] πΏπ π£ − πΏπ (π£) π = min max π£∈π 0≤π<π π−π π∗ – the minimum mean weight of a cycle in πΊ. Theorem: π∗ = π. If we subtract π∗ from all edge weights then: The minimum mean weight of a cycle in πΊ is decreased by π∗ and becomes 0. πΏπ (π£) is decreased by ππ∗ , πΏπ (π£) is decreased by ππ∗ . Hence π is also decreased by π∗ . If it thus enough to prove the claim when π∗ = 0. Karp’s algorithm [Karp (1978)] πΏπ π£ − πΏπ (π£) π = min max π£∈π 0≤π<π π−π Lemma: If π∗ = 0, then π = 0. Claim 1: If π∗ = 0, then π ≥ 0. ∀π£ ∃π πΏπ π£ ≤ πΏπ π£ As π ∗ = 0, the are no negative cycles. Thus, the distances in πΊ are well defined. For every π£ ∈ π, let π = π(π£) be such that πΏ π£ = πΏπ (π£), i.e., where πΏ π£ is the distance from π to π£. πΏπ π£ = πΏ π£ ≤ πΏπ π£ . max(πΏπ π£ − πΏπ π£ )/(π − π) ≥ 0 π π = minπ£ max(πΏπ π£ − πΏπ π£ )/(π − π) ≥ 0 π Karp’s algorithm [Karp (1978)] πΏπ π£ − πΏπ (π£) π = min max π£∈π 0≤π<π π−π Lemma: If π∗ = 0, then π = 0. Claim 2: If π∗ = 0, then π ≤ 0. ∃π£ ∀π πΏπ π£ ≥ πΏπ π£ As π ∗ = 0, the are no negative cycles. There is a potential function π: π → β such that π€π π’, π£ = π€ π’, π£ + π π’ − π π£ ≥ 0 , for every π’, π£ ∈ πΈ. The expression above is unchanged if distances are computed with respect to π€π rather than π€. π π€ = π(π€π ) Karp’s algorithm [Karp (1978)] Claim 2: If π∗ = 0, then π ≤ 0. ∃π£ ∀π πΏπ π£ ≥ πΏπ π£ Let πΆ be a 0-cycle. Let π’ be a node of πΆ, and let π be a shortest path from π to π’. For every π’, π£ ∈ πΆ ∪ π, we have π€π π’, π£ = 0. Let π be the number of edges on π. Follow π − π edges around the cycle starting from π’. Let π£ be the final vertex reached. 0 π£ 0 0 π 0 0 0 π’ πΏπ π£ = 0 ≤ πΏπ π£ , for every 0 ≤ π < π. πΆ 0 0 0 Karp’s algorithm [Karp (1978)] Claim 2: If π∗ = 0, then π ≤ 0. ∃π£ ∀π πΏπ π£ ≥ πΏπ π£ Same proof without potentials. Let π be the sum of weights along the cycle from π’ to π£. The sum of the weight from π£ to π’ is then −π. πΏπ π£ ≤ π€ π + π (as this is a path of π edges) π€ π ≤ πΏπ π£ − π (as π is a shortest path) πΏπ π£ ≤ π€ π + π ≤ πΏπ (π£) π£ π π’ π πΆ π −π