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 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 steps, solves 3-SAT in expected time . That is about instead of , 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
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 and let be the Hamming distance from the current assignment to . As in the 2-SAT analysis, a violated clause has at least one variable that disagrees with , now chosen with probability at least . So each step decreases with probability at least and increases it with probability at most . The walk drifts away from ; left alone it would almost never arrive. Two observations rescue it.
1. Success from distance within steps has probability about . One way to reach 0 is to make exactly wrong moves and right moves in the first steps (with the right moves never falling behind). Counting those paths and using ,
2. A random start is often close. The initial distance is binomial with parameters and . Averaging over it,
Together: one try (a random start plus steps) succeeds with probability , so the expected number of tries is at most , and tries fail with probability at most . 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 -SAT the same argument gives up to polynomial factors.
The program computes, by dynamic programming over the distance, the exact probability that the worst-case walk (towards with probability exactly , and a reflecting wall at distance ) reaches 0 within steps, both from a fixed distance and averaged over a random start.
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 is almost exactly , even better than the path-counting estimate. The reason is gambler's ruin: a walk on the integers that moves down with probability ever reaches 0 from with probability exactly , and when it does, it nearly always arrives within steps. Averaged over the random start, the success probability per try is within one percent of .
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 clauses and counts the tries.
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.
#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 ) 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 time. The Exponential Time Hypothesis (Impagliazzo and Paturi, 2001) conjectures that some base is unavoidable: 3-SAT has no 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?
Why is the starting assignment chosen at random?
- To make the walk unbiased
- Because its distance to a solution is binomial, and , much larger than the success probability from a typical far start
- Because all-false is always bad
- To avoid cycling
What is the expected running time of Schöning's algorithm on a satisfiable 3-CNF formula, up to polynomial factors?
Why does each try stop after steps instead of running forever?
- Because the formula has clauses
- Because the walk drifts away from the solution, so late success is unlikely; restarting from a fresh random point is better
- Because steps always suffice
- To keep the error one-sided