Solving 2-SAT with Markov Chains

2-SAT is a CNF over nn variables

F=C1C2Cm,Ci=Li1Li2i[m]F = C_{1}\land C_{2}\land \dots \land C_{m}, C_{i} = L_{i_{1}} \lor L_{i_{2}} \forall i \in [m]

We want to assign truth values to each of the variables to satisfy FF (such an assignment may or may not exist).

Algorithm for 2-SAT

  1. Start with an arbitrary truth assignment
  2. Repeat until all clauses are satisfied or at most 2mn22mn^{2} many iterations
    1. Choose an arbitrary clause that is not satisfied
    2. Choose a literal at random and flip the value of the variable corresponding to the literal.
  3. If a valid truth assignment was found return that assignment, else return UNSAT\text{UNSAT}.

Correctness & Guarantees of Algorithm

Let’s take SS to be a satisfying assignment for the nn variables.
AiA_{i} : Assignment to the variables after step ii.
XiX_{i} : Number of variables having the same values in AiA_{i} and SS

Xi={jAi(xj)=S(xj)}X_{i} = |\{j | A_{i}(x_{j}) = S(x_{j})\}|

If Xi=nX_{i'} = n, then Ai=SA_{i'} = S and that is a satisfying assignment.

Expected Time to Reach SS

We know that

P[Xi+1=1Xi=0]=1\mathbb{P}[X_{i+1}=1|X_{i} = 0 ] = 1

Suppose 1Xin11 \leq X_{i} \leq n - 1

And let CC be a clause that is not satisfied by AiA_{i}. If this is the case, then we know at least one of the two variable assignments in CC is inconsistent with SS.
If both variable assignments were consistent with SS, then the clause should’ve been satisfied, else SS wouldn’t be a satisfying assignment.

If we randomly pick any one of the two variables and flip them, the probability that Xi+1X_{i+1} increases is 12 \geq \frac{1}{2}.

P[Xi+1=j+1Xi=j]12P[Xi+1=j1Xi=j]12\begin{aligned} \mathbb{P}[X_{i+1} = j + 1 | X_{i} = j] \geq \frac{1}{2}\\ \mathbb{P}[X_{i+1} = j - 1 | X_{i} = j] \leq \frac{1}{2} \end{aligned}

Equivalence with Markov Chain

While the graph induced by X0,X1,,XnX_{0}, X_{1}, \dots, X_{n} may not be a Markov chain, we can study a very similar Markov process and analyze the expected running time of this algorithm.

We look at Y0,Y1,,YnY_{0}, Y_{1}, \dots, Y_{n}.

P[Yi+1=1Yi=0]=1P[Yi+1=j+1Yi=j]=12P[Yi+1=j1Yi=j]=12\begin{aligned} \mathbb{P}[Y_{i+1} = 1 | Y_{i} = 0] &= 1\\ \mathbb{P}[Y_{i+1}=j+1 |Y_{i}=j] &= \frac{1}{2}\\ \mathbb{P}[Y_{i+1}=j-1 |Y_{i}=j] &= \frac{1}{2} \end{aligned}

The transition probability matrix would be given as

Pij={12if j=i+1 or i1 1in11if i=0 and j=i1if i=j=n0otherwiseP_{ij}= \begin{cases} \frac{1}{2} & \text{if } j=i+1 \text{ or } i - 1\ \forall 1 \leq i \leq n - 1\\ 1 & \text{if } i = 0 \text { and } j = i\\ 1 & \text{if } i = j = n \\ 0 & \text{otherwise} \end{cases}

Deriving a formula for expected number of steps

We have a random variable ZjZ_{j} that represents the number of steps to reach nn from jj. Let hj=E[Zj]h_{j}= \mathbb{E}[Z_{j}].

We know that hn=0h_{n}= 0 and h0=h1+1h_{0} = h_{1} + 1.

Further hjh_{j} \geq expected number of steps to fully agree with SS starting from A0A_{0} which agrees with SS in jj locations, as the probability of XiX_{i} increasing is 12\geq \frac{1}{2}.

Zj={1+Zj1with probability 121+Zj+1with probability 12Z_{j} = \begin{cases} 1 + Z_{j-1} & \text{with probability } \frac{1}{2}\\ 1 + Z_{j+1} & \text{with probability } \frac{1}{2} \end{cases}

Hence,

E[Zj]=12(1+E[Zj1])+12(1+E[Zj+1])2hj=hj+1+hj1+2hj+1hj=(hjhj1)2j=1k(hj+1hj)=j=1k((hjhj1)2)\begin{aligned} \mathbb{E}[Z_{j}] &= \frac{1}{2}(1 + \mathbb{E}[Z_{j-1}]) + \frac{1}{2}(1 + \mathbb{E}[Z_{j+1}])\\ 2h_{j} &= h_{j+1} + h_{j-1} + 2\\ h_{j+1} - h_{j} &= (h_{j} - h_{j-1}) - 2\\ \sum\limits_{j=1}^{k}(h_{j+1} - h_{j}) &= \sum\limits_{j=1}^{k}\left((h_{j}- h_{j-1}) - 2\right) \end{aligned}

This gives us

hk+1h1=hkh02khk+1=hk+(h1h0)2khk+1=hk2k1\begin{aligned} h_{k+1}- h_{1}&= h_{k}- h_{0}- 2k\\ h_{k+1} &= h_{k} + (h_{1}- h_{0}) - 2k\\ h_{k+1} &= h_{k} - 2k - 1 \end{aligned}

This can be rearranged to give us

hkhk+1=2k+1h_{k}- h_{k+1} = 2k + 1

Expected Steps from h0h_{0}

Using this, we can derive the expected value for all indices,

hn=0hn1=2(n1)+1+hn=2(n1)+1\begin{aligned} h_{n} &= 0\\ h_{n - 1} &= 2(n - 1) + 1 + h_{n}=2(n-1)+1\\ \dots \end{aligned}
k=0n1(hkhk+1)=k=0n1(2k+1)h0hn=2n(n1)2+n=n2h0=n2\begin{aligned} \sum\limits_{k=0}^{n-1}(h_{k}- h_{k+1}) &= \sum\limits_{k=0}^{n-1}(2k + 1)\\ h_{0}- h_{n}&= \frac{2n(n-1)}{2} + n=n^{2}\\ h_{0}&= n^{2} \end{aligned}

Therefore, we know that the expected number of steps to find a satisfying assignment is n2\leq n^{2}.

Success Probability

By Markov’s Inequality

P[Z0>2n2]E[Z0]2n2=h02n2=n22n2=12\mathbb{P}[Z_{0} > 2n^{2}] \leq \frac{\mathbb{E}[Z_{0}]}{2n^{2}} = \frac{h_{0}}{2n^{2}} = \frac{n^{2}}{2n^{2}} = \frac{1}{2}

Now, we can boost the success probability by running this algorithm multiple times. We run this for 2mn22mn^{2} steps, and analyze as if we run the experiment mm times.

When we split this into mm blocks each, the probability of failure in each block is at most 12\frac{1}{2}, with the condition that no satisfying assignment was found in the previous block.

For Markov Chains, the history does not matter, so if we aren’t able to find a satisfying assignment after (2n2)i(2n^{2})\cdot i steps, it’s as if we just started a new trial with an “arbitrary assignment”.

Hence, the probability of success in finding a satisfying assignment after 2mn22mn^{2} steps is

112m1 - \frac{1}{2^{m}}

If we take m=nm=n, the runtime ends up to be 2n3=O(n3)2n^{3} = \mathcal{O}(n^{3}), with success probability 112n1 - \frac{1}{2^{n}}.

Published Feb 2, 2023