TExcelTAD
Home › Randomized Algorithms & Probabilistic Analysis › The Method of Conditional Expectations

The Method of Conditional Expectations

A random cut of a graph cuts half of its edges on average, so a cut with at least m/2m/2 edges exists (Expectation Tricks: A Recap). Can we find one without randomness? The method of conditional expectations makes the random choices one at a time, always picking the option that keeps the conditional expectation of the outcome as high as possible. Since an expectation is an average over the options, the best option is never below average, so the final, fully deterministic outcome is at least the original expectation. The idea goes back to Erdős and Selfridge (1973) and was developed as an algorithmic tool by Spencer and by Raghavan (1988). This lesson applies it to MAX-CUT and MAX-SAT.

The method

Let a randomized algorithm make independent choices X1,…,XnX_1, \ldots, X_n and produce a value f(X1,…,Xn)f(X_1, \ldots, X_n) with E[f]≥μ\mathbb{E}[f] \ge \mu. Suppose that for any fixed values x1,…,xix_1, \ldots, x_i of the first ii choices we can compute the conditional expectation

Ei(x1,…,xi)=E[f∣X1=x1,…,Xi=xi].E_i(x_1, \ldots, x_i) = \mathbb{E}\bigl[f \bigm| X_1 = x_1, \ldots, X_i = x_i\bigr].

By the law of total expectation, Ei−1(x1,…,xi−1)E_{i-1}(x_1, \ldots, x_{i-1}) is an average of the values Ei(x1,…,xi−1,x)E_i(x_1, \ldots, x_{i-1}, x) over the possible xx, weighted by Pr⁡[Xi=x]\Pr[X_i = x]. An average is at most the maximum, so choosing

xi=arg⁡max⁡xEi(x1,…,xi−1,x)x_i = \arg\max_{x} E_i(x_1, \ldots, x_{i-1}, x)

guarantees Ei(x1,…,xi)≥Ei−1(x1,…,xi−1)E_i(x_1, \ldots, x_i) \ge E_{i-1}(x_1, \ldots, x_{i-1}). Chaining from E0=E[f]≥μE_0 = \mathbb{E}[f] \ge \mu to En=f(x1,…,xn)E_n = f(x_1, \ldots, x_n), the deterministic result is at least μ\mu. The only requirement is computing the conditional expectations exactly, or a pessimistic estimator that upper-bounds the failure probability and can be maintained the same way (Raghavan, 1988), which is how Chernoff-bound arguments are derandomized.

MAX-CUT

Put vertices 0,…,n−10, \ldots, n-1 on side LL or RR one at a time. After some vertices are placed, an edge between two placed vertices is cut or not, and an edge with an unplaced endpoint is cut with probability 12\frac12. So

Ei=#{cut edges among placed vertices}+12#{edges with an unplaced endpoint}.E_i = \#\{\text{cut edges among placed vertices}\} + \frac12 \#\{\text{edges with an unplaced endpoint}\}.

Placing vertex vv changes only the edges from vv to already placed vertices: putting vv opposite to most of its placed neighbours maximises EiE_i. The method therefore reduces to a greedy rule, and the result cuts at least m/2m/2 edges. Since no cut has more than mm edges, this is a deterministic 12\frac12-approximation for MAX-CUT. (The best known ratio, 0.8780.878, comes from semidefinite programming and randomized hyperplane rounding: Goemans and Williamson, 1995; see Approximation Algorithms.)

text
MAX-CUT-DERANDOMIZED(G)            // vertices 0 .. n-1
0  side = empty assignment
1  for v = 0 to n - 1
2      a = number of placed neighbours of v on side L
3      b = number of placed neighbours of v on side R
4      side[v] = R if a >= b else L   // cut the larger group
5  return side
python · try it yourself
import random
from itertools import product

def derandomized_cut(n, adj):
    side, trace = {}, []
    for v in range(n):
        a = sum(1 for u in adj[v] if side.get(u) == 0)
        b = sum(1 for u in adj[v] if side.get(u) == 1)
        side[v] = 1 if a >= b else 0
        trace.append(max(a, b))
    return side, trace

def cut_value(edges, side):
    return sum(side[u] != side[v] for u, v in edges)

rng = random.Random(170)
n = 14
edges = [(u, v) for u in range(n) for v in range(u + 1, n) if rng.random() < 0.35]
adj = [[] for _ in range(n)]
for u, v in edges:
    adj[u].append(v)
    adj[v].append(u)
m = len(edges)

side, trace = derandomized_cut(n, adj)
print(f"n = {n}, m = {m}: guaranteed m/2 = {m / 2}")
print(f"edges cut when each vertex is placed: {trace} -> total {sum(trace)}")
print("derandomized cut value:", cut_value(edges, side))
rand = [cut_value(edges, [rng.randrange(2) for _ in range(n)]) for _ in range(2000)]
print(f"random cuts: mean {sum(rand) / len(rand):.2f}")
best = max(cut_value(edges, (0,) + bits) for bits in product((0, 1), repeat=n - 1))
print("maximum cut (brute force over 2^13 cuts):", best)
n = 14, m = 44: guaranteed m/2 = 22.0
edges cut when each vertex is placed: [0, 0, 0, 1, 1, 1, 3, 2, 3, 4, 3, 4, 3, 4] -> total 29
derandomized cut value: 29
random cuts: mean 22.09
maximum cut (brute force over 2^13 cuts): 31

Each vertex cuts at least half of the edges to its earlier neighbours, and every edge is counted once, when its later endpoint is placed: that is the m/2m/2 guarantee in its greedy form.

MAX-SAT and MAX-3SAT

Given a CNF formula with mm clauses, MAX-SAT asks for an assignment satisfying as many clauses as possible. Set each variable to true with probability 12\frac12, independently. A clause with kk distinct literals is false only if all kk are false:

E[#satisfied]=∑j=1m(1−2−kj)≥m2,\mathbb{E}[\#\text{satisfied}] = \sum_{j=1}^m \left(1 - 2^{-k_j}\right) \ge \frac{m}{2},

and for MAX-E3SAT (exactly three literals per clause) it is 78m\frac78 m. The conditional expectation given a partial assignment is just as easy: a clause already satisfied contributes 1, a clause whose literals are all set and false contributes 0, and a clause with rr unset literals and no true one contributes 1−2−r1 - 2^{-r}. Fixing variables one by one with the method gives Johnson's algorithm (1974): a deterministic assignment satisfying at least 78m\frac78 m clauses of any 3-CNF formula. Remarkably, 78\frac78 is optimal: for every ε>0\varepsilon > 0, satisfying a 78+ε\frac78 + \varepsilon fraction of the clauses of satisfiable 3-CNF formulas is NP-hard (Håstad, 2001).

python · try it yourself
import random

def expected_sat(clauses, assign):
    total = 0.0
    for c in clauses:
        if any(assign.get(v) == want for v, want in c):
            total += 1
        else:
            unset = sum(1 for v, _ in c if v not in assign)
            total += 1 - 2.0 ** -unset           # 0 if every literal is set and false
    return total

def johnson(n, clauses):
    assign, history = {}, [expected_sat(clauses, {})]
    for v in range(n):
        assign[v] = True
        t = expected_sat(clauses, assign)
        assign[v] = False
        f = expected_sat(clauses, assign)
        assign[v] = t >= f
        history.append(max(t, f))
    return assign, history

rng = random.Random(171)
n, m = 30, 200
clauses = [[(v, rng.random() < 0.5) for v in rng.sample(range(n), 3)] for _ in range(m)]
assign, history = johnson(n, clauses)
sat = sum(any(assign[v] == w for v, w in c) for c in clauses)
print(f"{m} clauses, 7m/8 = {7 * m / 8}")
print("conditional expectation after fixing 0, 5, 10, .., 30 variables:",
      [round(history[i], 2) for i in range(0, n + 1, 5)])
print("clauses satisfied by Johnson's assignment:", sat)
rand = []
for _ in range(300):
    a = [rng.random() < 0.5 for _ in range(n)]
    rand.append(sum(any(a[v] == w for v, w in c) for c in clauses))
print(f"random assignments: mean {sum(rand) / len(rand):.1f}, best of 300: {max(rand)}")
200 clauses, 7m/8 = 175.0
conditional expectation after fixing 0, 5, 10, .., 30 variables: [175.0, 177.38, 179.38, 181.12, 184.75, 187.12, 190.0]
clauses satisfied by Johnson's assignment: 190
random assignments: mean 174.9, best of 300: 187

The conditional expectation never decreases, from exactly 78m=175\frac78 m = 175 at the start to the number of satisfied clauses at the end.

In C++

The derandomized MAX-CUT in C++ on a random graph with 2000 vertices and about 20 000 edges (fixed-seed std::mt19937 for the graph only; the algorithm itself is deterministic). Each vertex is placed opposite the majority of its already placed neighbours.

cpp · try it yourself
#include <cstdio>
#include <random>
#include <utility>
#include <vector>

int main() {
    std::mt19937 rng(172);
    const int n = 2000, m = 20000;
    std::vector<std::pair<int, int>> edges;
    std::vector<std::vector<int>> adj(n);
    while ((int)edges.size() < m) {
        int u = rng() % n, v = rng() % n;
        if (u == v) continue;
        edges.push_back({u, v});
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    std::vector<int> side(n, -1);
    for (int v = 0; v < n; ++v) {
        int a = 0, b = 0;
        for (int u : adj[v]) { if (side[u] == 0) ++a; else if (side[u] == 1) ++b; }
        side[v] = a >= b ? 1 : 0;
    }
    int cut = 0;
    for (auto [u, v] : edges) cut += side[u] != side[v];
    std::printf("n = %d, m = %d: derandomized cut %d edges (m/2 = %d, ratio %.3f)\n", n, m, cut, m / 2, double(cut) / m);
}
n = 2000, m = 20000: derandomized cut 12378 edges (m/2 = 10000, ratio 0.619)

Test yourself

Why does the method of conditional expectations never end below the original expectation?

  1. Because the choices are random
  2. Because each conditional expectation is an average of the next ones, so the best option is at least the average
  3. Because the function is linear
  4. Because of Markov's inequality

For MAX-CUT, what does the method of conditional expectations reduce to?

  1. Brute force
  2. Placing each vertex on the side opposite to the majority of its already placed neighbours
  3. Random sampling
  4. A maximum flow

What does Johnson's algorithm guarantee for a 3-CNF formula with mm clauses of exactly three distinct literals?

  1. m/2m/2 satisfied clauses
  2. 7m/87m/8 satisfied clauses
  3. All clauses if satisfiable
  4. 3m/43m/4 satisfied clauses

What must be computable to apply the method of conditional expectations?

  1. The optimal solution
  2. The conditional expectation (or a pessimistic estimator) of the objective given any partial assignment
  3. The variance of the objective
  4. The number of optimal solutions