Sub topic 13
Randomized Approximation for MAX-SAT
This chapter assumes you know linearity of expectation, how to read an approximation ratio for a maximization problem (from the earlier chapter on ratios), and the basic shape of a linear program (LP) and its relaxation, from the chapter on rounding for weighted vertex cover. The code uses only the Python standard library.
The problem
In MAX-SAT you get a Boolean formula in conjunctive normal form: a list of clauses, each an OR of literals. A literal is a variable such as x3 or its negation ¬x3. The goal is to find a true/false assignment that satisfies as many clauses as possible. MAX-3SAT is the special case where every clause has exactly three literals on three distinct variables.
Deciding whether all clauses can be satisfied is the NP-complete SAT problem, so maximizing the number of satisfied clauses is NP-hard. We want an assignment whose number of satisfied clauses is at least α·OPT for some α < 1.
A randomized algorithm gives its guarantee in expectation: E[ALG] ≥ α·OPT. Later sections show how to turn that into a guarantee that holds with high probability, or with certainty.
The simplest algorithm: flip a coin per variable
Set each variable to true or false independently with probability 1/2. That is the whole algorithm.
Analysis for MAX-3SAT
Fix one clause with three distinct variables. It fails only when all three literals are false. Each literal is false with probability 1/2, and the variables are independent, so
Pr[clause unsatisfied] = (1/2)³ = 1/8, Pr[clause satisfied] = 7/8.
Let Y be the number of satisfied clauses and let YC be the 0/1 indicator for clause C. Then Y = Σ YC. By linearity of expectation, which needs no independence between clauses,
E[Y] = Σ Pr[C satisfied] = 7m/8,
where m is the number of clauses. Since OPT ≤ m, we get E[Y] ≥ (7/8)·OPT.
The upper bound on the optimum here is trivial: OPT is at most the number of clauses. Bounding the optimum is the same proof pattern as in the earlier chapters. The bound is crude, yet it is enough for the best possible ratio.
import random
def random_max3sat(n_vars, clauses, seed=None):
rng = random.Random(seed)
assign = [None] + [rng.random() < 0.5 for _ in range(n_vars)]
satisfied = sum(
any(assign[abs(lit)] == (lit > 0) for lit in clause)
for clause in clauses
)
return assign[1:], satisfied
clauses = [(1, 2, 3), (-1, 2, -3), (1, -2, 3), (-1, -2, -3)]
print(random_max3sat(3, clauses, seed=42))Clauses are tuples of nonzero integers: 3 means x3 and -3 means ¬x3. Index 0 of assign is padding, so assign[i] is the value of xi. With seed 42 this prints ([False, True, True], 4), which satisfies all four clauses. Another seed might satisfy only three. The 7/8 guarantee is about the average.
Clauses of other lengths
For general MAX-SAT, a clause with k distinct literals is satisfied with probability 1 − 2⁻ᵏ. This is at least 1/2 for k ≥ 1, so the coin-flip algorithm is a 1/2-approximation. Short clauses are the weak spot. A unit clause (x1) is satisfied only half the time.
From expectation to high probability
An expected ratio does not tell you how often a single run is good. For MAX-3SAT a short argument bounds this. Let p = Pr[Y ≥ 7m/8]. Y takes integer values, so any value below 7m/8 is at most 7m/8 − 1/8. Then
7m/8 = E[Y] ≤ (7m/8 − 1/8)(1 − p) + m·p,
which rearranges to p ≥ 1/(m + 1). If you repeat independent trials until one reaches 7m/8, you need at most m + 1 trials in expectation. That is polynomial, so the result is a Las Vegas algorithm: it always meets the bound, and only the running time is random.
Derandomization by conditional expectations
You can remove the randomness entirely. Write E[Y] as the average of two conditional expectations:
E[Y] = ½·E[Y | x1 = true] + ½·E[Y | x1 = false].
At least one of the two terms is ≥ E[Y]. Fix x1 to the value with the larger conditional expectation, then repeat for x2, x3, and so on. The conditional expectation never decreases. After all variables are set, it equals the actual number of satisfied clauses, which is therefore ≥ 7m/8.
This works because each conditional expectation is cheap to compute. Given a partial assignment, a clause that already has a true literal counts 1. Otherwise, with u unset literals, it counts 1 − 2⁻ᵘ. A clause with u = 0 and no true literal has failed and counts 0.
def clause_prob(clause, partial):
unset = 0
for lit in clause:
val = partial.get(abs(lit))
if val is None:
unset += 1
elif val == (lit > 0):
return 1.0
return 1.0 - 0.5 ** unset
def derandomized_maxsat(n_vars, clauses):
partial = {}
for x in range(1, n_vars + 1):
partial[x] = True
e_true = sum(clause_prob(c, partial) for c in clauses)
partial[x] = False
e_false = sum(clause_prob(c, partial) for c in clauses)
partial[x] = e_true >= e_false
satisfied = sum(clause_prob(c, partial) == 1.0 for c in clauses)
return [partial[x] for x in range(1, n_vars + 1)], satisfied
clauses = [(1, 2, 3), (-1, 2, -3), (1, -2, 3), (-1, -2, -3)]
print(derandomized_maxsat(3, clauses))The running time is O(n·L), where L is the total formula length. The code assumes no clause repeats a variable. A clause containing both x and ¬x is always true and should be removed before running it.
Randomized rounding of an LP
Short clauses need a different tool. Let PC be the set of variables that appear positively in clause C, and NC the set that appear negated. Write the integer program and relax it:
- maximize ΣC zC
- subject to zC ≤ Σ{i ∈ PC} yi + Σ{i ∈ NC} (1 − y_i) for every clause C
- 0 ≤ yi ≤ 1 and 0 ≤ zC ≤ 1
Solve the LP to get y\, z\. The LP optimum is an upper bound: Σ z\C ≥ OPT. Now set each xi true independently with probability y\_i. This is randomized rounding: the fractional value becomes a probability.
For a clause with k literals, the AM-GM inequality and concavity give
Pr[C satisfied] ≥ (1 − (1 − 1/k)ᵏ)·z\_C ≥ (1 − 1/e)·z\C ≈ 0.632·z\*C.
For k = 1 the factor is 1, so unit clauses are handled perfectly. The factor falls as k grows. That is the reverse of the coin-flip algorithm.
Combining the two: a 3/4-approximation
Run both algorithms and keep the better result. The better of two values is at least their average. For a clause of length k, the average is at least
½·[(1 − 2⁻ᵏ) + (1 − (1 − 1/k)ᵏ)]·z\*_C
(for the coin-flip term we use 1 − 2⁻ᵏ ≥ (1 − 2⁻ᵏ)·z\*_C).
| k | Coin flip 1 − 2⁻ᵏ | LP rounding 1 − (1 − 1/k)ᵏ | Average |
|---|---|---|---|
| 1 | 0.500 | 1.000 | 0.750 |
| 2 | 0.750 | 0.750 | 0.750 |
| 3 | 0.875 | 0.704 | 0.790 |
| ≥ 4 | ≥ 0.938 | > 0.632 | > 0.78 |
Every row is at least 3/4. Summing over clauses gives E[best] ≥ (3/4)·Σ z\*_C ≥ (3/4)·OPT. The two methods cover each other's weak cases.
Common errors and limits
- Confusing expectation with a guarantee. A single run can fall below 7m/8. Repeat the algorithm or derandomize it if you need a certain bound.
- Assuming independence between clauses. Clauses that share variables are dependent. Linearity of expectation still holds, so the analysis needs no independence between clauses.
- Applying 7/8 to clauses with repeated or fewer variables. The 7/8 figure needs three distinct variables per clause.
(x1 ∨ x1 ∨ x2)is satisfied with probability only 3/4. - Treating 7/8 as improvable. Håstad proved that for any ε > 0, approximating MAX-3SAT within 7/8 + ε is NP-hard. The coin-flip algorithm is optimal unless P = NP. The chapter on the PCP theorem explains where such results come from.
Exercise
A MAX-SAT instance has 10 clauses with 2 distinct literals each and 6 clauses with 4 distinct literals each. What is the expected number of clauses satisfied by a uniformly random assignment?
Show answer
Each 2-literal clause is satisfied with probability 3/4 and each 4-literal clause with probability 15/16. By linearity of expectation, E[Y] = 10·(3/4) + 6·(15/16) = 7.5 + 5.625 = 13.125.
Exercise
Show that the coin-flip algorithm is no better than a 1/2-approximation for general MAX-SAT. Give an instance where its expected value is OPT/2.
Show answer
Take the single unit clause (x1). OPT = 1, and a random assignment satisfies it with probability 1/2, so E[Y] = 1/2 = OPT/2. (The instance (x1), (¬x1) does not show this, because there OPT = 1 and E[Y] = 1.)
Exercise
In the derandomized algorithm, why is it valid to choose the value of xi with the larger conditional expectation, and what does the conditional expectation equal once every variable is set?
Show answer
The current conditional expectation is the average of the two conditional expectations for xi = true and xi = false, so the larger of the two is at least the current value. The value therefore never decreases from its starting point of 7m/8. Once every variable is set, nothing random remains, and the conditional expectation equals the actual number of satisfied clauses. That number is therefore at least 7m/8.