Randomized algorithms for search problems where a found answer is a certificate (one-sided error: “yes” is always right), so repetition drives the failure probability down exponentially.
| Algorithm | Problem | Time | Error |
|---|---|---|---|
| color coding (Alon, Yuster, Zwick 1995) | simple path (or tree, cycle) with vertices | expected, i.e. | misses with probability |
| random walk for 2-SAT (Papadimitriou 1991) | 2-SAT | expected flips | per flips; the linear SCC algorithm is better in practice: SCC and 2-SAT |
| Schöning’s walk (1999) for -SAT | 3-SAT | exponential, but beats | |
| Karger, Karger–Stein | global minimum cut | Global Min Cut | |
| random restarts of local search | MAX-SAT, partitioning | heuristic | Heuristic Optimization |
Color coding
Colour the vertices randomly with colours and look only for colourful paths (all colours distinct): these are automatically simple, and they are found by a DP over subsets of colours instead of subsets of vertices. A fixed -path is colourful with probability , so about colourings suffice. Variants: -cycles, trees of size (DP over the tree), weighted versions (minimum weight colourful path), and derandomization by perfect hash families.
// Is there a simple path with exactly k vertices? Monte Carlo with one-sided error; trials ~ e^k * ln(1/delta).
bool hasKPath(int n, const vector<vector<int>>& adj, int k, int trials, mt19937_64& rng) {
if (k == 1) return n > 0;
vector<int> col(n); vector<vector<char>> dp(1 << k, vector<char>(n));
while (trials--) {
for (auto& c : col) c = rng() % k;
for (auto& row : dp) fill(all(row), 0);
for (int v = 0; v < n; v++) dp[1 << col[v]][v] = 1;
for (int mask = 1; mask < (1 << k); mask++) for (int v = 0; v < n; v++) if (dp[mask][v])
for (int u : adj[v]) if (!(mask >> col[u] & 1)) dp[mask | 1 << col[u]][u] = 1;
for (int v = 0; v < n; v++) if (dp[(1 << k) - 1][v]) return true; // a colourful path is simple
}
return false;
}Proof: color coding is correct and the trial count suffices
dp[mask][v]is 1 iff some path ends at whose vertices have pairwise distinct colours forming exactlymask(induction on the path length: extend by a neighbour with a new colour). A colourful path visits distinct colours, hence distinct vertices: it is simple, so “yes” answers are always right. If a -vertex path exists, a random colouring makes it colourful with probability (the colour sequences on , of which are permutations), and gives . After independent colourings the miss probability is , which is for . Each trial costs . Checked against exhaustive DFS on random graphs with up to 10 vertices.
Random walks for satisfiability
// Clauses as vectors of literals: +(v+1) for x_v, -(v+1) for not x_v. Returns a satisfying assignment or empty.
vector<char> walkSAT(int n, const vector<vector<int>>& cls, ll restarts, ll stepsPerRestart, mt19937_64& rng) {
auto sat = [&](const vector<int>& c, const vector<char>& x) { for (int l : c) if ((l > 0) == (bool)x[abs(l) - 1]) return true; return false; };
for (ll r = 0; r < restarts; r++) {
vector<char> x(n); for (auto& b : x) b = rng() & 1; // random start
for (ll s = 0; s <= stepsPerRestart; s++) {
vector<int> bad; for (int i = 0; i < sz(cls); i++) if (!sat(cls[i], x)) bad.push_back(i);
if (bad.empty()) return x;
if (s == stepsPerRestart) break;
const auto& c = cls[bad[rng() % sz(bad)]];
int v = abs(c[rng() % sz(c)]) - 1; x[v] ^= 1; // flip a random literal of a violated clause
}
}
return {};
}
// 2-SAT (Papadimitriou): walkSAT(n, cls, 50, 2 * n * n, rng) -> failure <= 2^-50 if satisfiable
// 3-SAT (Schoening): walkSAT(n, cls, c * pow(4/3., n), 3 * n, rng)Proof: Papadimitriou's O(n^2) bound and Schöning's (4/3)^n
Fix a satisfying assignment and let be the number of variables where the current assignment differs from it. A violated clause is satisfied by , so at least one of its literals disagrees with on its variable; flipping a uniformly random literal of the clause decreases with probability for 2-clauses and for 3-clauses (and otherwise increases it by 1).
2-SAT. performs a walk on that moves down with probability at least . The expected time to hit 0 from any start is at most that of the unbiased walk reflected at , which is at most (the hitting time of the fair walk satisfies , , , solved by ). The walk can also stop earlier at another satisfying assignment. By Markov, steps fail with probability ; restarts fail with probability .
3-SAT. From distance , one way to reach 0 within steps is bad and good steps: probability at least , and gives . The starting distance is , so the success probability of one restart is at least , and restarts of steps succeed with constant probability. Unsatisfiable formulas are always reported as such (one-sided). Checked against brute force on random 2-SAT and 3-SAT instances with up to 10 variables.
Related notes
- Randomized Algorithms
- Satisfiability
- Hamiltonian Paths and TSP (the exact DP)