Abstract
We present a novel algorithm based on combinatorial operations on lists for computing the number of models on two conjunctive normal form Boolean formulas whose restricted graph is represented by a grid graph Gm,n. We show that our algorithm is correct and its time complexity is
For this class of formulas, we show that our proposal improves the asymptotic behavior of the time-complexity with respect of the current leader algorithm for counting models on two conjunctive form formulas of this kind.
Introduction
The decision problem SAT (F), where F is a Boolean formula, consists in determining whether F has a model, that is, an assignment to the variables of F such that when evaluated with respect to classical Boolean logic it returns true as a result. If F is in two Conjunctive Normal Form (2-CNF) then SAT (F) can be solved in polynomial time, however if F is in k-CNF, k > 2, then SAT (F) is an NP-Complete problem. Its counting version # k-SAT(F) is a classic #P-Complete problem even when F is in 2-CNF, the latter denoted as #2SAT [1].
Counting combinatorial objects is a challenging and relevant area of research in mathematics, computer sciences and physics. Counting problems, being mathematically relevant by themselves, are closely related to practical problems. Several relevant counting problems are hard time complexity problems. For example, #k-SAT (the problem of counting models for a Boolean formula) is of special concern to artificial intelligence (AI), and it has a direct relationship to automated theorem proving, as well as to approximate reasoning [2–4].
# k-SAT is related to other counting problems, e.g. in approximate reasoning, in the cases of the generation of explanation to propositional queries, repairing inconsistent databases, estimating the degree of belief in propositional theories, in a truth maintenance systems, in Bayesian inference [2, 6]. The previous problems come from several AI applications such as expert systems, planning, approximate reasoning, etc.
Although the #2SAT problem is #P-Complete, there are instances that can be solved in polynomial time [7, 8]. For example, if the graph representation of the formula is acyclic, then the number of models can be computed in lineal time [7]. Currently, the algorithms that are used to solve the problem for any formula F in 2-CNF, decompose F into sub-formulas until there are base cases in which it can be counted efficiently.
The algorithm with the best time complexity so far was developed by Wahlström [9]. The Wahlström’s algorithm has an upper bound for its time-complexity of order O (1 .2377 n ), where n represents the number of variables in the formula. The Wahlström’s algorithm uses the number of times a variable appears in the formula (being it the variable or its negation) as the criterion for choosing it. The two criteria for stopping the algorithm are when F =∅ or when ∅ ∈ F.
In this paper we present an algorithm based on combinatorial operations on lists to compute #2SAT models on the so called grid formulas. We show that for this particular type of formulas our algorithm improves the asymptotic behavior of the time-complexity of the leader algorithm by Wahlström.
Preliminaries
Grid graphs
A grid graph of size m × n is a graph Gm,n with vertex set
V = {(i, j) :1 ≤ i ≤ m, 1 ≤ j ≤ n}
and edge set
E = {((j, i) , (j + 1, i)) ∣1 ≤ j < m, 1 ≤ i ≤ n} ∪ {((j, i) , (j, i + 1)) ∣1 ≤ j ≤ m, 1 ≤ i < n}
There is a large volume of literature devoted to count structures in a grid graph, e.g., spanning trees, Hamiltonian cycles, independent sets, acyclic orientations, k-coloring, and so on [10–13]. For example, the study of the number of independent sets on grid structures is closely related to the ‘’hard-square model" used in statistical physics and, of particular interest is the so-called hard-square entropy constant [14]. Applications of the counting objects on grids also include for instance tiling and efficient coding schemes in data storage [15].
Euler [11] presents a method to calculate the Fibonacci number of a grid graph, calculating a new parameter b (m, n) using the transfer matrix method. Calkin [10] calculates the number of independent sets on grid graphs using the transfer matrix method. Guillen et al. [13] present an extension of the transfer matrix method to count the number of satisfying assignments of Boolean formulas in 2-CNF. Here, we begin considering the transfer matrix method in order to build a list operation method for counting objects on a grid structure and some of its structure variants.
Conjunctive Normal Form
Let X = {x1, . . . , x
n
} be a set of n Boolean variables (that is, they can only take two possible values 1 or 0). A literal is a variable x
i
, denoted in this paper as
Let V (Y) be the set of variables involved in the object Y, where Y can be a literal, a clause or a Boolean formula. For example, for the clause
An assignment s in F is a Boolean function s : F → {0, 1}. s is defined as:
The assignment can be extended to conjunctions and disjunctions as follows: s (x ∧ y) =1 if s (x) = s (y) =1, otherwise s (x ∧ y) =0 s (xlory) =0 if s (x) = s (y) =0, otherwise s (x ∨ y) =1
Let F be a Boolean formula in CNF, it is said that s satisfies F (denoted as s ⊨ F), if for each clause c in F, it holds s (c) =1. On the other hand, it is said that F is contradicted by s (s
Given a CNF F, the SAT problem consists of determining whether F has a model. The # k-SAT(F) problem consists of counting the number of models of F defined over υ (F). #2SAT denotes # k-SAT for formulas in 2-CNF.
The restricted graph of a 2-CNF
There are some graphical representations of a Conjunctive Normal Form, in this case the signed primary graph (restricted graph) [6] will be used.
Let F be a 2-CNF, its restricted graph is denoted by G
F
= (V (F) , E (F)) where the vertices of the graph are the variables V (F) = v (F) and
For x ∈ V (F), δ (x) denotes its degree, that is the number of incident edges in x. Each edge
Let S = {+ , -} be a set of signs. A graph with edges labeled S is the pair (G, Ψ), where G = (V, E) is a restricted graph, and Ψ is a function with domain E and range S. Ψ (e) is called the label from the edge e ∈ E. Let G = (V, E, Ψ) be a restricted graph with labeled edges on S × S. Let x and y be two different vertices in V. If e = {x, y} is an edge and ψ (e) = (s, s′), then s (s′) is called the adjacent sign to x (y).
Notice that a restricted graph of a 2-CNF can be a multigraph, since two fixed variables can be involved in more than one clause of the formula, forming so parallel edges. Furthermore, a unitary clause defines a loop in the constraint graph.
Let ρ : 2 - CNF → G F be the function whose domain is the space of non strict Boolean formulas in 2-CNF and codomain the set of multi-graphs. It is clear that ρ is a bijection. Furthermore, any 2-CNF formula has a unique signed restricted graph (up to isomorphism) associated via ρ and viceversa, any signed constraint graph G F has a unique formula associated via ρ-1.
Methods already reported to compute #2SAT
The basic idea considered in related papers to count models on a restricted graph G consists on computing a tuple (α
i
, β
i
) over each vertex x
i
, where α
i
represents the number of times that x
i
appears positive in the models of G, and β
i
the number of times x
i
appears negative in the models of G. For example, a formula with a simple variable {x
i
} has associated the tuple (1, 1) to the vertex x
i
. Given an edge (clause)
There are reported methods to count models in some graphical representations of a 2-CNF formula F [5, 16], here we state the method needed in the paper: If the graph represents a path e.g a formula of the form
A known method for counting models on grid formulas comes from an adaptation of the Calkin transfer matrix method [10, 13]. We will present a novel method based on lists that provides a better asymptotic behaviour with respect to other algorithms for computing #2SAT on grid formulas.
Let P m be the restricted path graph of m vertices representing a 2-CNF F. A satisfiable set S m over P m is the set of assignments of F that satisfies the formula represented in the graph. The size of each element in S m is m representing the assignment of each variable.
The satisfiable set can be divided on two subsets S p i and S n i , where S p i represents the assignments where x i appears positive, and S n i the assignments where x i appears negated.
The set S
m
is inductively constructed where S
p
1
= {1} and S
n
1
= {0}, and the inductive step consists of a modified version of the equation 1:
For example on the path formed by the edge {x1, x2}, signs (1, 1), and the initial sets S p 1 = {1} and S n 1 = {0}, which represents the initial partial assignments on x1.
The operation S p i-1 1 represents the concatenation of the elements in S p i-1 with 1 at the end of the chain.
Then, for the edge {x1, x2}, with signs (1, 1), the calculation of the sets S p 2 and S n 2 is as follows:
(S p 2 , S n 2 ) = (S p 1 1 ∪ S n 1 1, S p 1 0) that is, S p 2 = {11, 01} and S n 2 = {10}.
Now we can calculate the satisfiable set as the union of both sets, S2 = S p 2 ∪ S n 2 = {11, 01, 10}.
The calculation of the set S5 associated to P5 = {{x1, x2} , {x2, x3} , {x3, x4} , {x4, x5}}, with signs {(1, 1) , (1, 1) , (1, 1) , (1, 1)} can be done in a sequential way using the equation 1_sat_set, in the following way.
S1={1,0}
S2={01,10,11}
S3={011,101,111,010,110}
S4={0111,1011,1111,0101,1101,0110,1010,
1110}
S5= {01111,10111,11111,01011,11011,01101,
10101,11101,01110,10110,11110,11010}
The size of the set S5 is the number of models of the formula P5. Each value represents an assignment to the variables that satisfies the formula. For example the element 10101 means that the assignment s (x1) = s (x3) = s (x5) =1 and s (x2) = s (x4) =0 is a model of the formula P5. The application of Equation 1_sat_set provides us an exponential-time procedure for enumerate all model of a path formula.
We will consider the grid graph Gm,n as the union of n paths, each path with m vertices. The path
List counting method
Let

Path identification over Gm,n.

List l i with initial values.

Counting models on path P2 using Equation 1.

Construction of the satisfiable set S2 for a path P2.
On a path P3, with edges

Path P3 with 5 models computed via equation 1.

Constructing the set S3 for P3 computed via equation 1_sat_set.

Path P3 with 4 models calculated via equation 1.

Constructing the set S3 for P3 calculated via equation 1_sat_set.
In both figures (Figs. 6 and 8) the size of the set S3, calculated through the equation 1_sat_set, provides the number of models on each graph, respectively.
A list l
i
is built, where
Given the grid graph Gm,2, with paths
Our method compare the satisfiable assignments over
We call this method as the counting transfer list. It calculates the values on the list l2 using the values on l1 and the elements of the set z1.
The algorithm 1 compares each index of

Grid graph G2,2.
Given the grid graph G2,2 in Fig. 9, V (G2,2) = {(1, 1) , (2, 1) , (1, 2) , (2, 2)} and E (G2,2) = {((1, 1) 1, (2, 1) 1) , ((1, 2) 1, (2, 2) 0) , ((1, 1) 0, (1, 2) 0) , ((2, 1) 0, (2, 2) 1)}. The construction of the lists l1, l2 and the set z1 through the satisfiable sets
l1 = [1, 1, 1]
l2 = [1, 1, 1]
z1 = {(0, 0) 1, (0, 1) 2}
The first set of comparisons are computed using the first index of
The first comparison is made using the element
Comparisons of l2 [1] versus l1
The value of l2 [1] is the sum of the elements l1 [h], whose index in
The total of comparisons are shown in Table 2.
Total of comparisons of l2 versus l1
Thus, the new values of the list l2 are:

List l2.
The number of models in F is the sum of the elements in the updated list l2, having
In brief, the list counting method is a combinatorial algorithm that operates on two lists
Let us consider the grid graph Gm,n with paths
The general algorithm 3 shows a method for counting the number of models of Gm,n.
The algorithm 3 works dividing the grid graph Gm,n in n paths
Through Lemma 2, the number of models of the grid graph Gm,n is given by the sum of the elements in the list l
n
,
The flexibility of this combinatorial operations on lists, with respect to the transfer matrix method, allows the possible existence and processing of diagonal edges, which the matrix method does not allow.
Complexity analysis
The computation of the satisfiable set S m is an exponential time method, the max size of a set S m is related to the (m + 2) - th Fibonacci number. For example, the computation of a monotone positive formula is similar to the calculation of independent sets of its restricted graph. The number of independent sets of a path P m is given directly by the value of the m + 2 Fibonacci number. The tightest upper bound for the growth of the Fibonacci number function F n is of order O (1 .618 n ).
The general algorithm 3 constructs the n lists

Complexity comparison of the Wahlström algorithm versus the combinatorial operations on lists for grid graphs.
In consideration of the worst case scenario, when n is equal to m, and the number of vertices in the grid graph is t = n2, the total of operations can be simplified by having an upper bound of order
We have compared our proposal versus the Wahlström algorithm, which have a time-complexity of order O (1 .2377 t ), and where t = n · m is the total number of vertices in the graph.
The plot in Fig. 11 shows the complexity-time functions for both algorithms: Wahlström’s algorithm and the counting transfer list. The plot shows two intersection intervals: (0.0459, 64.9822) when Wahlström gives a better time complexity; and the interval (64.9822, ∞) when our algorithm has a better asymptotic time behavior with respect to the Wahlström’s algorithm.
In this paper we have introduced a novel algorithm to compute the # 2SAT (F), when F is a formula whose restricted graph is represented by a grid graph Gm,n. We show that our algorithm is correct and its time complexity is
