Docsity

A Linear Time Algorithms with Examples - Study Guide | CS 6505, Study notes of Computer Science

Toggle sidebar
bg1
A Linear Time Algorithm for 2CNFSAT
Let Fbe a CNF Boolean formula with nvariables and mclauses such that each clause has exactly two literals.
1. Construct a directed graph GFfrom Fas follows:
•Create two vertices for each variable, one for each literal of the variable: 2nvertices.
•Treat each clause as a pair of Boolean implications and add edges corresponding to the implications.
For instance, if the clause is (l1,l2) then add an edge from the vertex NOT l1to the vertex l2and an
edge from the vertex NOT l2to the vertex l1.
2. Note that if x→yis an edge in this graph then y→xis an edge in Gas well.
(Exercise: Show that if there is a directed path in Gfrom l1to l2then there is a directed path in Gfrom l2
to l1.)
3. Construct the strongly connected components of G.
4. Fis not satisfiable iff a variable occurs both as a positive literal and a negative literal in a strongly connected
component.
•Claim: If a variable occurs both as a positive literal and a negative literal in a strongly connected
component then Fis not satisfiable.
Proof: Suppose, for some variable x, both its positive literal xand its negative literal xoccur in a
strongly connected component.
–Then there is a directed path Pfrom xto x, say: x→u1→ → · · · → ur→x.
–Set xto TRUE. The literal u1must get the truth value TRUE since the edge x→u1represents
an implication. For the same reason, all the literals along the path must get the truth value TRUE.
But, the literal xappears on the path and must be set to TRUE. But, the truth value for xmust
be FALSE since xis set to TRUE. Contradiction.
–Thus, xcannot be set to TRUE.
–Similarly, xcannot be set to FALSE.
–Therefore, if, for some variable, both xand xoccur in the same strongly connected component, F
is not satisfiable.
•Conversely, if none of the strongly connected components of Gcontain both the positive literal xand
the negative literal xof any variable xthen Fis satisfiable.
Proof: The proof is constructive and gives a way to assign a satisfying truth assignment to the variables.
(All literals in the same strongly connected component get the same truth assignment.)
–Let C1, C2, . . . , Ckbe the strongly connected components of G.
–Construct the following meta-graph H= (V0, E0):
1
pf2

Partial preview of the text

Download A Linear Time Algorithms with Examples - Study Guide | CS 6505 and more Study notes Computer Science in PDF only on Docsity!

A Linear Time Algorithm for 2CNFSAT

Let F be a CNF Boolean formula with n variables and m clauses such that each clause has exactly two literals.

  1. Construct a directed graph GF from F as follows:
    • Create two vertices for each variable, one for each literal of the variable: 2n vertices.
    • Treat each clause as a pair of Boolean implications and add edges corresponding to the implications. For instance, if the clause is (l 1 , l 2 ) then add an edge from the vertex NOT l 1 to the vertex l 2 and an edge from the vertex NOT l 2 to the vertex l 1.
  2. Note that if x → y is an edge in this graph then y → x is an edge in G as well. (Exercise: Show that if there is a directed path in G from l 1 to l 2 then there is a directed path in G from l 2 to l 1 .)
  3. Construct the strongly connected components of G.
  4. F is not satisfiable iff a variable occurs both as a positive literal and a negative literal in a strongly connected component. - Claim: If a variable occurs both as a positive literal and a negative literal in a strongly connected component then F is not satisfiable.

Proof: Suppose, for some variable x, both its positive literal x and its negative literal x occur in a strongly connected component.

  • Then there is a directed path P from x to x, say: x → u 1 → → · · · → ur → x.
  • Set x to TRUE. The literal u 1 must get the truth value TRUE since the edge x → u 1 represents an implication. For the same reason, all the literals along the path must get the truth value TRUE. But, the literal x appears on the path and must be set to TRUE. But, the truth value for x must be FALSE since x is set to TRUE. Contradiction.
  • Thus, x cannot be set to TRUE.
  • Similarly, x cannot be set to FALSE.
  • Therefore, if, for some variable, both x and x occur in the same strongly connected component, F is not satisfiable.
  • Conversely, if none of the strongly connected components of G contain both the positive literal x and the negative literal x of any variable x then F is satisfiable.

Proof: The proof is constructive and gives a way to assign a satisfying truth assignment to the variables. (All literals in the same strongly connected component get the same truth assignment.)

  • Let C 1 , C 2 ,... , Ck be the strongly connected components of G.
  • Construct the following meta-graph H = (V ′, E′):

1

∗ Create a vertex Ci for each connected component Ci for 1 ≤ i ≤ k. ∗ For every pair of vertices Ci, Cj ∈ V ′^ add a directed edge from Ci to Cj if G contains a directed edge between some x ∈ Ci and some y ∈ Cj.

  • Let Ci be a vertex in H. That is, C is a strongly connected component of G. Then, there is a strongly connected component in G consisting of vertices ¯l for every l ∈ Ci. Denote the vertex in H corresponding to this strongly connected component as C¯i. (Assume that Ci = Ci.)
  • For every vertex Ci in H, it is true that Ci 6 = Ci.
  • If there is a directed edge from a strongly connected component, say Ci to a strongly connected component Cj in the graph H, then there is a directed edge from the strongly connected component Cj to the strongly connected component Ci.
  • H is acyclic. Therefore, H has at least one sink. Topologically sort the graph H. The vertex C in H with the highest topological number is a sink.
  • Repeat until no more vertices in H: ∗ Pick the vertex C in H with the highest topological number and assign all the literals in C the truth value TRUE; assign all the literals in C the truth value FALSE. ∗ Delete C and C from the graph H. (Call the resulting graph H as well.)
  • This truth assignment must satisfy the formula.
  • Suppose not. ∗ There is some clause, say (l 1 , l 2 ) that must be false. That is, both l 1 and l 2 were assigned the truth values FALSE by this assignment. ∗ Let l 1 occur in vertex Ci in H and l 2 occur in Cj in H. Then, l 1 occurs in vertex Ci and l 2 occurs in vertex Cj. By construction of GF , there is an edge from l 2 to l 1 and an edge from l 1 to l 2. ∗ In the topological ordering of H, the vertex Ci comes after the vertex Cj and the vertex Cj after Ci. ∗ If l 2 is assigned false then the vertex Cj comes after the vertex Cj implying that the vertex Ci comes after the vertex Ci. Then, the literals in vertex Ci (including l 1 ) must have received the truth value TRUE instead of FALSE. Contradiction.
  1. Time:
  • The graph GF has 2n vertices and 2m edges. Constructing the graph GF takes O(n + m) time.
  • It takes O(n + m) time to find the strongly connected components of G and construct H.
  • The directed acyclic graph H can be topologically sorted in O(n + m) time.
  • It takes O(n) time to assign truth values to the literals in all the strongly connected components.