TExcelTAD
Home › Randomized Algorithms & Probabilistic Analysis › Schöning's Random Walk for 3-SAT

Schöning's Random Walk for 3-SAT

3-SAT is NP-complete (SAT and 3-CNF-SAT), so no polynomial-time algorithm is expected. But "exponential" is not one complexity: trying all 2n2^n assignments is the trivial bound, and beating its base is a central question of exact algorithms. Uwe Schöning (1999) showed that the random walk of Papadimitriou's 2-SAT algorithm, started from a random assignment and restarted after 3n3n steps, solves 3-SAT in expected time O((4/3)npoly⁡(n))O\bigl((4/3)^n \operatorname{poly}(n)\bigr). That is about 1.334n1.334^n instead of 2n2^n, and the analysis shows how a walk that drifts the wrong way can still succeed if it starts close and gives up early.

The algorithm

text
SCHOENING(F, n, tries)             // F: 3-CNF formula on n variables
0  for t = 0 to tries - 1
1      a = uniformly random assignment
2      for step = 0 to 3n - 1
3          if a satisfies F
4              return a
5          choose any clause C that a violates
6          flip one of the three variables of C, chosen uniformly at random
7  return "probably unsatisfiable"

Why a short walk from a random start works

Fix a satisfying assignment ss and let XX be the Hamming distance from the current assignment to ss. As in the 2-SAT analysis, a violated clause has at least one variable that disagrees with ss, now chosen with probability at least 13\frac13. So each step decreases XX with probability at least 13\frac13 and increases it with probability at most 23\frac23. The walk drifts away from ss; left alone it would almost never arrive. Two observations rescue it.

1. Success from distance jj within 3j3j steps has probability about 2−j2^{-j}. One way to reach 0 is to make exactly jj wrong moves and 2j2j right moves in the first 3j3j steps (with the right moves never falling behind). Counting those paths and using (3jj)≥13j+1⋅(3j)3jjj(2j)2j\binom{3j}{j} \ge \frac{1}{3j + 1}\cdot\frac{(3j)^{3j}}{j^j (2j)^{2j}},

qj≥1poly⁡(j)(3jj)(13)2j(23)j≥1poly⁡(j)(274)j(227)j=1poly⁡(j) 2−j.q_j \ge \frac{1}{\operatorname{poly}(j)} \binom{3j}{j} \left(\frac13\right)^{2j} \left(\frac23\right)^{j} \ge \frac{1}{\operatorname{poly}(j)} \left(\frac{27}{4}\right)^j \left(\frac{2}{27}\right)^{j} = \frac{1}{\operatorname{poly}(j)}\, 2^{-j} .

2. A random start is often close. The initial distance JJ is binomial with parameters nn and 12\frac12. Averaging 2−J2^{-J} over it,

E[2−J]=∑j=0n(nj)2−n2−j=2−n(1+12)n=(34)n.\mathbb{E}\bigl[2^{-J}\bigr] = \sum_{j=0}^n \binom{n}{j} 2^{-n} 2^{-j} = 2^{-n}\left(1 + \tfrac12\right)^n = \left(\frac34\right)^n .

Together: one try (a random start plus 3n3n steps) succeeds with probability p≥(3/4)n/poly⁡(n)p \ge (3/4)^n / \operatorname{poly}(n), so the expected number of tries is at most (4/3)npoly⁡(n)(4/3)^n \operatorname{poly}(n), and cp\frac{c}{p} tries fail with probability at most e−ce^{-c}. This is the idea of Schöning's proof; the polynomial factors are worked out in Schöning (1999) and in Mitzenmacher and Upfal, section 7.1. For kk-SAT the same argument gives (2(1−1k))n\bigl(2(1 - \frac1k)\bigr)^n up to polynomial factors.

The program computes, by dynamic programming over the distance, the exact probability that the worst-case walk (towards ss with probability exactly 13\frac13, and a reflecting wall at distance nn) reaches 0 within 3n3n steps, both from a fixed distance jj and averaged over a random start.

python · try it yourself
from math import comb

def success_from(n, steps):
    """prob[j] = Pr[walk from distance j hits 0 within `steps` steps], moving down w.p. 1/3."""
    prob = [1.0] + [0.0] * n                   # 0 steps left: success only at distance 0
    for _ in range(steps):
        new = [1.0] * (n + 1)
        for j in range(1, n + 1):
            up = prob[j + 1] if j < n else prob[j - 1]    # at distance n every flip helps
            new[j] = prob[j - 1] if j == n else prob[j - 1] / 3 + 2 * up / 3
        prob = new
    return prob

n = 30
prob = success_from(n, 3 * n)
print(" j   Pr[success within 3n steps]   2^-j")
for j in [1, 2, 5, 10, 15]:
    print(f"{j:2}   {prob[j]:27.3e}   {2 ** -j:.3e}")
avg = sum(comb(n, j) * 2 ** -n * prob[j] for j in range(n + 1))
print(f"uniform start, n = {n}: success per try {avg:.3e}, (3/4)^n = {0.75 ** n:.3e}")
print(f"expected tries {1 / avg:.0f}  vs  brute force 2^n = {2 ** n}")
 j   Pr[success within 3n steps]   2^-j
 1                     5.000e-01   5.000e-01
 2                     2.500e-01   2.500e-01
 5                     3.123e-02   3.125e-02
10                     9.709e-04   9.766e-04
15                     2.963e-05   3.052e-05
uniform start, n = 30: success per try 1.771e-04, (3/4)^n = 1.786e-04
expected tries 5646  vs  brute force 2^n = 1073741824

The success probability from distance jj is almost exactly 2−j2^{-j}, even better than the path-counting estimate. The reason is gambler's ruin: a walk on the integers that moves down with probability 13\frac13 ever reaches 0 from jj with probability exactly (1/32/3)j=2−j\bigl(\frac{1/3}{2/3}\bigr)^j = 2^{-j}, and when it does, it nearly always arrives within 3j3j steps. Averaged over the random start, the success probability per try is within one percent of (3/4)n(3/4)^n.

Running it

On satisfiable random formulas the real behaviour is much better than the worst-case analysis, because there are usually many satisfying assignments and the walk is not always pushed away. The program runs SCHOENING on random 3-CNF formulas with a planted solution and m=4.2nm = 4.2n clauses and counts the tries.

python · try it yourself
import random

def planted_3sat(n, m, rng):
    s = [rng.random() < 0.5 for _ in range(n)]
    clauses = []
    while len(clauses) < m:
        c = [(v, rng.random() < 0.5) for v in rng.sample(range(n), 3)]
        if any(s[v] == want for v, want in c):
            clauses.append(c)
    return clauses

def schoening(n, clauses, rng, max_tries):
    for t in range(1, max_tries + 1):
        a = [rng.random() < 0.5 for _ in range(n)]
        for _ in range(3 * n):
            bad = next((c for c in clauses if not any(a[v] == w for v, w in c)), None)
            if bad is None:
                return t
            v = bad[rng.randrange(3)][0]
            a[v] = not a[v]
    return None

rng = random.Random(140)
print(" n   tries in 5 runs   (4/3)^n")
for n in [10, 20, 30]:
    tries = [schoening(n, planted_3sat(n, int(4.2 * n), rng), rng, 2000) for _ in range(5)]
    print(f"{n:2}   {str(tries):18}  {(4 / 3) ** n:8.0f}")
 n   tries in 5 runs   (4/3)^n
10   [1, 1, 2, 1, 1]           18
20   [3, 7, 2, 1, 1]          315
30   [2, 3, 4, 5, 1]         5600

In C++

SCHOENING in C++ (fixed-seed std::mt19937) on larger planted formulas, with the same clause choice (the first violated clause). The stream differs from Python's, so individual counts differ.

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

struct Lit { int var; bool val; };
using Clause = std::vector<Lit>;

std::vector<Clause> planted(int n, int m, std::mt19937& rng) {
    std::vector<bool> s(n);
    for (int v = 0; v < n; ++v) s[v] = rng() & 1;
    std::vector<Clause> f;
    while ((int)f.size() < m) {
        int a = rng() % n, b = rng() % n, c = rng() % n;
        if (a == b || b == c || a == c) continue;
        Clause cl{{a, (rng() & 1) == 1}, {b, (rng() & 1) == 1}, {c, (rng() & 1) == 1}};
        for (const Lit& l : cl) if (s[l.var] == l.val) { f.push_back(cl); break; }
    }
    return f;
}

long long schoening(int n, const std::vector<Clause>& f, std::mt19937& rng) {
    for (long long t = 1;; ++t) {
        std::vector<bool> x(n);
        for (int v = 0; v < n; ++v) x[v] = rng() & 1;
        for (int step = 0; step < 3 * n; ++step) {
            const Clause* bad = nullptr;
            for (const Clause& c : f) {
                bool ok = false;
                for (const Lit& l : c) ok = ok || x[l.var] == l.val;
                if (!ok) { bad = &c; break; }
            }
            if (!bad) return t;
            int v = (*bad)[rng() % 3].var;
            x[v] = !x[v];
        }
    }
}

int main() {
    std::mt19937 rng(140);
    std::printf("  n   tries in 5 runs              (4/3)^n\n");
    for (int n : {20, 40, 60}) {
        std::printf("%3d  ", n);
        for (int r = 0; r < 5; ++r) std::printf(" %5lld", schoening(n, planted(n, (int)(4.2 * n), rng), rng));
        std::printf("   %10.0f\n", std::pow(4.0 / 3.0, n));
    }
}
  n   tries in 5 runs              (4/3)^n
 20      27     2     4     1     1          315
 40       4     3     2     1    12        99437
 60       6     3    39    75    18     31356256

Where this stands

Schöning's bound has been improved by derandomizing the walk (Moser and Scheder, 2011, deterministic (4/3+ε)n(4/3 + \varepsilon)^n) and by entirely different methods: the PPSZ algorithm of Paturi, Pudlák, Saks and Zane (2005), sharpened by Hertli (2011) and by Hansen, Kaplan, Zamir and Zwick (2019), solves 3-SAT in about 1.307n1.307^n time. The Exponential Time Hypothesis (Impagliazzo and Paturi, 2001) conjectures that some base c>1c > 1 is unavoidable: 3-SAT has no 2o(n)2^{o(n)} algorithm. That conjecture and its consequences are the subject of Parameterized & Fine-Grained Complexity.

Test yourself

In Schöning's walk, with what probability does a step decrease the distance to a fixed satisfying assignment, at least?

  1. 1/21/2
  2. 1/31/3
  3. 2/32/3
  4. 1/81/8

Why is the starting assignment chosen at random?

  1. To make the walk unbiased
  2. Because its distance JJ to a solution is binomial, and E[2−J]=(3/4)n\mathbb{E}[2^{-J}] = (3/4)^n, much larger than the success probability from a typical far start
  3. Because all-false is always bad
  4. To avoid cycling

What is the expected running time of Schöning's algorithm on a satisfiable 3-CNF formula, up to polynomial factors?

  1. 2n2^n
  2. (4/3)n(4/3)^n
  3. n3n^3
  4. (3/2)n(3/2)^n

Why does each try stop after 3n3n steps instead of running forever?

  1. Because the formula has 3n3n clauses
  2. Because the walk drifts away from the solution, so late success is unlikely; restarting from a fresh random point is better
  3. Because 3n3n steps always suffice
  4. To keep the error one-sided