TExcelTAD
Home › Approximation Algorithms › Uncapacitated Facility Location

Uncapacitated Facility Location

A company must decide which warehouses to open and which customers each one serves. Opening warehouse ii costs fif_i; serving customer jj from warehouse ii costs cijc_{ij}. The uncapacitated facility location problem (UFL) asks for a set of open facilities and an assignment of every client to an open facility minimizing the total opening plus connection cost. In the metric version the costs cijc_{ij} come from distances between facilities and clients and satisfy the triangle inequality. UFL is a favourite test bed for approximation techniques: LP rounding, primal-dual, dual fitting and local search all give constant factors for it. This lesson is an overview, with the primal-dual 3-approximation of Jain and Vazirani (2001) worked out and run.

The LP and its dual

With yiy_i = "facility ii is open" and xijx_{ij} = "client jj uses facility ii":

(P)min⁡∑ifiyi+∑i,jcijxijs.t.∑ixij≥1  ∀j,xij≤yi  ∀i,j,x,y≥0,(D)max⁡∑jvjs.t.∑jwij≤fi  ∀i,vj−wij≤cij  ∀i,j,v,w≥0.\begin{aligned} (P) \quad & \min \sum_i f_i y_i + \sum_{i,j} c_{ij} x_{ij} && \text{s.t.} \quad \sum_i x_{ij} \ge 1 \ \ \forall j, \quad x_{ij} \le y_i \ \ \forall i, j, \quad x, y \ge 0, \\ (D) \quad & \max \sum_j v_j && \text{s.t.} \quad \sum_j w_{ij} \le f_i \ \ \forall i, \quad v_j - w_{ij} \le c_{ij} \ \ \forall i, j, \quad v, w \ge 0 . \end{aligned}

The dual has a natural reading: vjv_j is the budget client jj brings. Of it, cijc_{ij} pays the connection to ii, and anything above that, wij=max⁡(0,vj−cij)w_{ij} = \max(0, v_j - c_{ij}), is offered towards opening facility ii. A facility cannot be offered more than its opening cost.

The Jain-Vazirani algorithm

Phase 1 (grow the budgets). All clients start unconnected with vj=0v_j = 0. Raise the budgets of all unconnected clients at the same speed (think of time tt, with vj=tv_j = t).

  • When vjv_j reaches cijc_{ij}, the edge ijij becomes tight, and from then on jj contributes vj−cijv_j - c_{ij} to facility ii.
  • When the contributions to a facility reach fif_i, it is temporarily opened, and every client with a tight edge to it is connected and stops growing.
  • A client whose budget reaches cijc_{ij} for an already temporarily open ii is connected immediately and stops.

Phase 1 ends when every client is connected. The final vv (with wij=max⁡(0,vj−cij)w_{ij} = \max(0, v_j - c_{ij})) is a feasible dual.

Phase 2 (resolve conflicts). Two temporarily open facilities conflict if some client contributes positively to both. Go through them in order of opening time and keep a maximal set II of pairwise non-conflicting facilities. Open exactly II, and connect every client to its nearest open facility.

Theorem (Jain and Vazirani, 2001). For metric costs, the solution satisfies

∑jcσ(j)j+3∑i∈Ifi≤3∑jvj≤3 OPT,\sum_{j} c_{\sigma(j) j} + 3 \sum_{i \in I} f_i \le 3 \sum_j v_j \le 3\,\text{OPT},

so it is a 3-approximation.

Proof sketch. Each facility in II is paid for exactly by the contributions of its clients, and no client contributes to two facilities of II, so ∑i∈Ifi\sum_{i \in I} f_i is covered by disjoint parts of the budgets. A client jj with a tight edge to some i∈Ii \in I (a directly connected client) pays its connection cijc_{ij} and its contribution wijw_{ij} out of vjv_j, in total exactly vjv_j. Any other client jj was connected in phase 1 through a temporarily open facility i′i' with ci′j≤vjc_{i'j} \le v_j. If i′∉Ii' \notin I, it conflicts with some i∈Ii \in I opened no later, through a client j′j' with tight edges to both. Because ii opened no later than i′i' and j′j' stopped growing when its first tight facility opened, vj′≤ti′≤vjv_{j'} \le t_{i'} \le v_j (where ti′t_{i'} is the opening time of i′i'), and by the triangle inequality cij≤ci′j+ci′j′+cij′≤vj+vj′+vj′≤3vjc_{ij} \le c_{i'j} + c_{i'j'} + c_{ij'} \le v_j + v_{j'} + v_{j'} \le 3 v_j. Summing, connection costs plus three times the opening costs are at most 3∑jvj3\sum_j v_j, and the dual value is at most OPT. □\square

The bound is stronger than a 3-approximation: opening costs are counted three times on the left. Algorithms with this Lagrangian multiplier preserving property are exactly what is needed to handle the related kk-median problem by Lagrangian relaxation, the original motivation of Jain and Vazirani.

python · try it yourself
import random
from fractions import Fraction as Fr
from itertools import combinations
from scipy.optimize import linprog

rng = random.Random(3)
fac = [(rng.randint(0, 20), rng.randint(0, 20)) for _ in range(6)]
cli = [(rng.randint(0, 20), rng.randint(0, 20)) for _ in range(12)]
f = [rng.randint(8, 25) for _ in fac]
c = [[abs(a[0] - b[0]) + abs(a[1] - b[1]) for b in cli] for a in fac]      # Manhattan distance: a metric
F, D = range(len(fac)), range(len(cli))

def jain_vazirani():
    v, frozen, opened, t = {}, set(), {}, Fr(0)
    def paid(i, t):
        return sum(max(0, (v[j] if j in frozen else t) - c[i][j]) for j in D)
    while len(frozen) < len(cli):
        cands = [Fr(c[i][j]) for i in F for j in D if j not in frozen and c[i][j] > t]
        for i in F:
            if i not in opened:
                rate = sum(1 for j in D if j not in frozen and c[i][j] <= t)
                if rate:
                    cands.append(t + (f[i] - paid(i, t)) / rate)
        t = min(x for x in cands if x >= t)
        for i in F:                                    # facilities fully paid at time t open
            if i not in opened and paid(i, t) >= f[i]:
                opened[i] = t
        for j in D:                                    # clients with a tight edge to an open facility stop
            if j not in frozen and any(c[i][j] <= t for i in opened):
                frozen.add(j)
                v[j] = t
    order = sorted(opened, key=lambda i: (opened[i], i))
    I = []
    for i in order:                                    # phase 2: maximal non-conflicting set
        if all(not any(v[j] > c[i][j] and v[j] > c[k][j] for j in D) for k in I):
            I.append(i)
    cost = sum(f[i] for i in I) + sum(min(c[i][j] for i in I) for j in D)
    return I, cost, sum(v.values()), order

def optimum():
    return min(sum(f[i] for i in S) + sum(min(c[i][j] for i in S) for j in D)
               for k in range(1, len(fac) + 1) for S in combinations(F, k))

def lp_value():
    nf, nc = len(fac), len(cli)
    cost = f + [c[i][j] for i in F for j in D]
    A, b = [], []
    for j in D:                                        # -sum_i x_ij <= -1
        A.append([0] * nf + [-1 if jj == j else 0 for i in F for jj in D]); b.append(-1)
    for i in F:
        for j in D:                                    # x_ij - y_i <= 0
            row = [0] * (nf + nf * nc)
            row[i], row[nf + i * nc + j] = -1, 1
            A.append(row); b.append(0)
    return linprog(cost, A_ub=A, b_ub=b, bounds=[(0, None)] * len(cost), method="highs").fun

I, alg, dual, temp = jain_vazirani()
print("temporarily opened (in order):", temp, " kept:", I)
print(f"dual sum {float(dual):.2f} <= LP {float(lp_value()):.2f} <= OPT {optimum()} <= ALG {alg} <= 3 * dual = {float(3 * dual):.2f}")
temporarily opened (in order): [0, 1, 4]  kept: [0, 1]
dual sum 123.00 <= LP 124.00 <= OPT 124 <= ALG 124 <= 3 * dual = 369.00

Facility 4 opened in phase 1 but conflicted with an earlier one and was closed again. On this instance the result is optimal, and the dual is within one unit of the LP: the guarantee of 3 is a worst case that random instances rarely approach.

In C++

Phase 1 as an event simulation in floating point, phase 2 and the final assignment, on a small line instance: facilities and clients on a road, with distance ∣a−b∣\lvert a - b \rvert.

cpp · try it yourself
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>

int main() {
    std::vector<double> facPos = {0, 5, 10, 15}, f = {10, 10, 10, 10};
    std::vector<double> cliPos = {1, 3, 9, 12, 14, 19, 22, 28, 31};
    int nf = facPos.size(), nc = cliPos.size();
    auto c = [&](int i, int j) { return std::fabs(facPos[i] - cliPos[j]); };
    std::vector<double> v(nc, 0), openTime(nf, -1);
    std::vector<bool> frozen(nc, false);
    double t = 0;
    auto paid = [&](int i, double tt) {
        double s = 0;
        for (int j = 0; j < nc; j++) s += std::max(0.0, (frozen[j] ? v[j] : tt) - c(i, j));
        return s;
    };
    while (std::count(frozen.begin(), frozen.end(), true) < nc) {
        double next = 1e18;
        for (int i = 0; i < nf; i++)
            for (int j = 0; j < nc; j++) if (!frozen[j] && c(i, j) > t) next = std::min(next, c(i, j));
        for (int i = 0; i < nf; i++) {
            if (openTime[i] >= 0) continue;
            int rate = 0;
            for (int j = 0; j < nc; j++) if (!frozen[j] && c(i, j) <= t) rate++;
            if (rate) next = std::min(next, t + (f[i] - paid(i, t)) / rate);
        }
        t = next;
        for (int i = 0; i < nf; i++) if (openTime[i] < 0 && paid(i, t) >= f[i] - 1e-9) openTime[i] = t;
        for (int j = 0; j < nc; j++)
            for (int i = 0; i < nf; i++)
                if (!frozen[j] && openTime[i] >= 0 && c(i, j) <= t + 1e-9) { frozen[j] = true; v[j] = t; }
    }
    std::vector<int> order;
    for (int i = 0; i < nf; i++) if (openTime[i] >= 0) order.push_back(i);
    std::sort(order.begin(), order.end(), [&](int a, int b) { return openTime[a] < openTime[b]; });
    std::vector<int> I;
    for (int i : order) {
        bool ok = true;
        for (int k : I)
            for (int j = 0; j < nc; j++) if (v[j] > c(i, j) + 1e-9 && v[j] > c(k, j) + 1e-9) ok = false;
        if (ok) I.push_back(i);
    }
    double cost = 0, dual = 0;
    for (int i : I) cost += f[i];
    for (int j = 0; j < nc; j++) {
        double best = 1e18;
        for (int i : I) best = std::min(best, c(i, j));
        cost += best;
        dual += v[j];
    }
    std::printf("temporarily opened:");
    for (int i : order) std::printf(" %d (t = %.2f)", i, openTime[i]);
    std::printf("\nkept:");
    for (int i : I) std::printf(" %d", i);
    std::printf("\ncost %.2f, dual %.2f, cost / dual %.3f\n", cost, dual, cost / dual);
}
temporarily opened: 2 (t = 5.67) 3 (t = 6.67) 0 (t = 7.00)
kept: 2 0
cost 91.00, dual 73.67, cost / dual 1.235

Facility 3 opened second, but a client between positions 10 and 15 contributed to both 2 and 3, so phase 2 keeps only the earlier one, and the clients near 15 travel to facility 2. The cost stays well within three times the dual.

Without the triangle inequality

The metric assumption is essential. Hochbaum (1982) observed that set cover is a special case of non-metric UFL: make every set a facility with opening cost wSw_S, every element a client, and let cSe=0c_{Se} = 0 if e∈Se \in S and ∞\infty (in practice a huge number) otherwise. A solution of finite cost opens a family of sets covering every element, and its cost is the weight of that family, so the two optima coincide and the (1−ε)ln⁡n(1 - \varepsilon)\ln n hardness of set cover carries over. Greedy over "stars" (a facility with a group of clients) gives the matching O(log⁡n)O(\log n) upper bound.

python · try it yourself
from itertools import combinations

sets = {"A": ({1, 2, 3}, 4), "B": ({3, 4, 5}, 4), "C": ({5, 6, 1}, 4), "D": ({2, 4, 6}, 5), "E": ({1, 2, 3, 4, 5, 6}, 13)}
elements = sorted(set().union(*(S for S, _ in sets.values())))
BIG = 10 ** 6
f = {name: w for name, (S, w) in sets.items()}
c = {(name, e): 0 if e in S else BIG for name, (S, _) in sets.items() for e in elements}

def ufl_cost(open_set):
    return sum(f[i] for i in open_set) + sum(min(c[(i, e)] for i in open_set) for e in elements)

def set_cover_cost(chosen):
    covered = set().union(*(sets[i][0] for i in chosen))
    return sum(sets[i][1] for i in chosen) if covered == set(elements) else None

names = list(sets)
subsets = [S for k in range(1, len(names) + 1) for S in combinations(names, k)]
best_ufl = min(subsets, key=ufl_cost)
best_sc = min((S for S in subsets if set_cover_cost(S) is not None), key=set_cover_cost)
print("UFL optimum:", best_ufl, ufl_cost(best_ufl))
print("set cover optimum:", best_sc, set_cover_cost(best_sc))
UFL optimum: ('A', 'B', 'C') 12
set cover optimum: ('A', 'B', 'C') 12

Both problems choose the same sets at the same cost. In the metric version this construction is impossible: an element at distance 0 from two sets would force those sets to be at distance 0 from each other.

The landscape

Result Ratio Method
Hochbaum (1982), non-metric costs O(log⁡n)O(\log n) greedy, as set cover; optimal up to the constant, since set cover reduces to non-metric UFL
Shmoys, Tardos and Aardal (1997) 3.16 LP rounding with filtering: the first constant factor
Jain and Vazirani (2001) 3 primal-dual (this lesson)
Jain, Mahdian, Markakis, Saberi and Vazirani (2003) 1.61 greedy analysed by dual fitting with a factor-revealing LP
Byrka (2007); Li (2013) 1.5; 1.488 LP rounding combined with the greedy algorithm
Guha and Khuller (1999) no 1.463 hardness, under the assumption NP ⊈\not\subseteq DTIME(nO(log⁡log⁡n))(n^{O(\log \log n)})

The gap between 1.463 and 1.488 is one of the smallest between known algorithms and hardness for a classical problem. The capacitated version (each facility serves at most uiu_i clients) is much harder for LP methods, since its natural LP has an unbounded integrality gap, yet local search gives constant factors.

The mini project of this course puts facility location next to set cover in one toolkit: Mini Project: A Set Cover and Facility Location Toolkit.

Test yourself

In the Jain-Vazirani dual, what does wij=max⁡(0,vj−cij)w_{ij} = \max(0, v_j - c_{ij}) represent?

  1. The connection cost of client jj
  2. The part of client jj's budget offered towards opening facility ii, beyond what the connection costs
  3. The opening cost of facility ii
  4. A primal variable

Why do two kept facilities never share a contributing client?

  1. Because facilities are far apart
  2. Phase 2 keeps a set of pairwise non-conflicting facilities, where conflict means a client contributes positively to both
  3. Because each client contributes to one facility only in phase 1
  4. Because of complementary slackness

A client whose phase-1 facility was closed is reconnected at cost at most what?

  1. vjv_j
  2. 3vj3 v_j, through a path of three edges whose costs are bounded by budgets, using the triangle inequality
  3. fif_i
  4. OPT