Sub topic 13
Randomized Approximation for MAX-SAT
This chapter assumes you know basic discrete probability, specifically expectation and linearity of expectation. It also assumes you are comfortable with the approximation ratio for maximization problems from earlier chapters. The goal is a simple randomized algorithm that satisfies 7/8 of the clauses of a MAX-3SAT instance in expectation. You will also see how to turn that expected guarantee into a deterministic one.
The Problem
A CNF formula is a conjunction (AND) of clauses. Each clause is a disjunction (OR) of literals, and a literal is a variable or its negation. In MAX-SAT the input is a CNF formula with m clauses, and the goal is a true/false assignment to the variables that satisfies as many clauses as possible. MAX-3SAT is the version where every clause contains exactly three literals on three distinct variables. Deciding whether all clauses can be satisfied is already NP-complete, so the optimization version is NP-hard.
The Algorithm: Flip a Coin per Variable
Set each variable to true or false independently with probability 1/2. Nothing else is involved.
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))In the code, literal k means variable k and -k means its negation. With seed 42 this run prints ([False, True, True], 4), so all four clauses are satisfied. Other seeds will give different counts.
The Analysis
A clause with three distinct variables is false only when all three of its literals are false. Each literal is false with probability 1/2, and the variables are independent, so the clause is false with probability (1/2)³ = 1/8. It is therefore satisfied with probability 7/8.
Let Y_j be 1 if clause j is satisfied and 0 otherwise. By linearity of expectation:
E[number satisfied] = Σ E[Y_j] = (7/8)·m ≥ (7/8)·OPT
The final step uses the trivial upper bound OPT ≤ m. This is the same pattern as in earlier chapters: compare the algorithm's output with a bound on the optimum instead of the optimum itself.
The calculation has a useful side effect. A random variable cannot always be below its mean, so every MAX-3SAT instance has an assignment that satisfies at least 7/8 of its clauses.
The same argument works for other clause lengths:
| Clause length k | P(clause satisfied) | Guarantee |
|---|---|---|
| 1 | 1/2 | 1/2 |
| 2 | 3/4 | 3/4 |
| 3 | 7/8 | 7/8 |
| k | 1 − 2⁻ᵏ | ≥ 1/2 for general MAX-SAT |
From Expectation to a Deterministic Guarantee
An expected guarantee does not promise that any single run does well. You can fix this in two ways.
- Repetition. Run the algorithm several times and keep the best assignment. A standard Markov-type argument shows that a polynomial number of runs reaches (7/8)·m with high probability.
- Method of conditional expectations. Fix the variables one at a time. For x₁, compute the expected number of satisfied clauses when x₁ = true and when x₁ = false, with all remaining variables still random. The current expectation is the average of these two values, so at least one of them is at least as large. Keep that value and continue with x₂. Each conditional expectation is a sum of per-clause probabilities, so it takes polynomial time to compute.
For general MAX-SAT, randomized rounding of an LP relaxation reaches 1 − 1/e. Running both methods and taking the better result gives 3/4. For MAX-3SAT, Håstad showed that 7/8 + ε cannot be achieved for any ε > 0 unless P = NP, so the coin-flip algorithm is optimal. The hardness chapters explain where results like this come from.
Exercise
For the four clauses in the code example, what is the expected number of satisfied clauses under a uniformly random assignment? What does this imply about the optimum?
Show answer
Each clause has three distinct variables, so each is satisfied with probability 7/8. The expected count is 4 · 7/8 = 3.5. Some assignment must reach at least the expectation, and counts are whole numbers, so some assignment satisfies all 4 clauses and OPT = 4.
Exercise
Show that the coin-flip algorithm is a 1/2-approximation for general MAX-SAT, where clauses can have any length of at least 1 and no clause repeats a variable.
Show answer
A clause with k ≥ 1 distinct variables is satisfied with probability 1 − 2⁻ᵏ ≥ 1/2. By linearity of expectation the expected count is at least m/2, and m/2 ≥ OPT/2 because OPT ≤ m.