MAX-SAT: Random Assignments, LP Rounding and the Better of Two
MAX-SAT: given a CNF formula with clauses over variables and weights , find an assignment maximizing the total weight of satisfied clauses. It is NP-hard even when every clause has two literals. This lesson develops three algorithms, each with a clean analysis, and shows that two weak ones combine into a strong one: the uniformly random assignment is good for long clauses, LP rounding is good for short ones, and the better of the two achieves on every formula (Goemans and Williamson, 1994).
Throughout, clauses contain no repeated variable and no complementary pair, and is the length of clause . Since MAX-SAT is a maximization problem, an -approximation satisfies weight at least with .
The random assignment
Set every variable true with probability , independently. Clause fails only if all its literals are false:
This is a -approximation in expectation, derandomized by the method of conditional expectations into Johnson's algorithm (1974); for formulas whose clauses all have exactly 3 literals it gives , which is optimal unless P = NP (Håstad, 2001). See The Method of Conditional Expectations. The weak spot is clauses of length 1: a unit clause is satisfied only half the time.
The LP relaxation and randomized rounding
Let and be the variables appearing positively and negatively in clause . Variables model the truth values and whether clause is satisfied:
Every assignment gives a feasible integral point, so : here the LP is an upper bound. Rounding sets true with probability , independently.
Lemma. Clause is satisfied with probability at least .
Proof. Rename variables so that every literal of the clause is positive (replace by for negated ones); the clause becomes with . It fails with probability . By the inequality of arithmetic and geometric means,
The function is concave on with , so it lies above the chord: . Finally for every .
Summing, . Rounding is perfect for unit clauses ( gives factor 1) and weakest for long ones, the opposite of the random assignment.
The better of the two
Write and . Run both algorithms and keep the better assignment. Its expected weight is at least the average of the two expectations:
using . The program tabulates the average.
from math import e
print(" k uniform p_k LP rounding q_k average")
for k in range(1, 8):
p = 1 - 2 ** -k
q = 1 - (1 - 1 / k) ** k
print(f"{k:2} {p:12.4f} {q:17.4f} {(p + q) / 2:9.4f}")
print("limits: p -> 1, q -> 1 - 1/e =", round(1 - 1 / e, 4), " average ->", round((2 - 1 / e) / 2, 4)) k uniform p_k LP rounding q_k average
1 0.5000 1.0000 0.7500
2 0.7500 0.7500 0.7500
3 0.8750 0.7037 0.7894
4 0.9375 0.6836 0.8105
5 0.9688 0.6723 0.8205
6 0.9844 0.6651 0.8247
7 0.9922 0.6601 0.8261
limits: p -> 1, q -> 1 - 1/e = 0.6321 average -> 0.8161
Theorem (Goemans and Williamson, 1994). The better of the random assignment and LP rounding is a -approximation for MAX-SAT.
Proof. for every : equality for ( and 1) and ( and ), and for , and , so the average exceeds . Hence .
The guarantee is relative to the LP, and the LP has integrality gap exactly : the four clauses , , , have but the LP sets and everywhere for value 4. So no analysis against this LP can prove more than .
Computing the expectations exactly
The program builds a random weighted formula with clauses of length 1 to 3, solves the LP, and computes the exact expected weight of both algorithms (a product formula per clause, no sampling), next to the optimum by brute force.
import random
from itertools import product
from scipy.optimize import linprog
rng = random.Random(4)
n, m = 10, 30
clauses = []
for _ in range(m):
k = rng.choice([1, 1, 2, 2, 3])
vs = rng.sample(range(n), k)
clauses.append(([(v, rng.random() < 0.5) for v in vs], rng.randint(1, 5))) # (variable, negated)
# LP variables: y_0..y_{n-1}, z_0..z_{m-1}; maximize sum w_j z_j -> minimize -sum w_j z_j
c = [0] * n + [-w for _, w in clauses]
A, b = [], []
for j, (lits, w) in enumerate(clauses): # z_j - sum_pos y_i + sum_neg y_i <= #neg
row = [0] * (n + m)
row[n + j] = 1
for v, neg in lits:
row[v] += 1 if neg else -1
A.append(row)
b.append(sum(neg for _, neg in lits))
res = linprog(c, A_ub=A, b_ub=b, bounds=[(0, 1)] * (n + m), method="highs")
y = [float(v) for v in res.x[:n]]
lp = -float(res.fun)
def expected(prob_true):
total = 0.0
for lits, w in clauses:
fail = 1.0
for v, neg in lits:
fail *= prob_true[v] if neg else 1 - prob_true[v]
total += w * (1 - fail)
return total
def weight(assign):
return sum(w for lits, w in clauses if any(assign[v] != neg for v, neg in lits))
opt = max(weight(a) for a in product([False, True], repeat=n))
r, l = expected([0.5] * n), expected(y)
print(f"total weight {sum(w for _, w in clauses)}, OPT {opt}, LP {lp:.2f}")
print(f"E[random] = {r:6.2f} ({r / opt:.3f} OPT)")
print(f"E[LP rounding] = {l:6.2f} ({l / opt:.3f} OPT)")
print(f"average = {(r + l) / 2:6.2f} >= 3/4 LP = {0.75 * lp:.2f}")total weight 95, OPT 85, LP 85.50
E[random] = 67.75 (0.797 OPT)
E[LP rounding] = 74.75 (0.879 OPT)
average = 71.25 >= 3/4 LP = 64.12
Both expectations can be turned into deterministic assignments by the method of conditional expectations, fixing variables one at a time, since the conditional expectation is again a sum of per-clause product formulas.
One rounding function for 3/4
Goemans and Williamson also showed that a single rounding achieves : set true with probability for any function with on , for example . A positive literal is then false with probability and a negative one with probability , so a clause fails with probability at most , and the concave function lies above its chord: on .
State of the art
- Semidefinite programming improves the factor: about for MAX-2SAT (Lewin, Livnat and Zwick, 2002) and about for MAX-SAT in general (Avidor, Berkovitch and Zwick, 2005), building on the vector programs of Goemans-Williamson Hyperplane Rounding.
- For MAX-E3SAT (exactly 3 literals) the random assignment's is optimal unless P = NP (Håstad, 2001); see Tight Thresholds: MAX-3SAT, Clique and Set Cover.
Test yourself
Which clauses are hardest for the uniformly random assignment, and which for LP rounding?
- Long clauses for both
- Unit clauses for the random assignment (probability ); long clauses for LP rounding (factor tending to )
- Unit clauses for both
- Neither depends on clause length
Why is the better of the two assignments at least in expectation?
- Because both algorithms are -approximations
- The maximum is at least the average, and for every clause length
- Because the LP is integral
- Because of the union bound
In the LP rounding lemma, which two facts give ?
- Markov's and Chebyshev's inequalities
- The AM-GM inequality on and the concavity of
- Linearity of expectation and the union bound
- Complementary slackness and duality
Why can no analysis against this LP prove a ratio above ?
- The LP is NP-hard to solve
- The four 2-clauses on with all sign patterns have OPT 3 but LP value 4
- Because
- Because unit clauses exist