Generalizations of Watched Literals for Backtracking Search Allen Van Gelder 237 B.E., University of California, Santa Cruz, CA 95064 E-mail avg@cs.ucsc.edu. December 1, 2001 Abstract The technique of watching two literals per clause to determine when a clause becomes a unit clause was introduced recently in the Chaff satisfiability program. That program does not perform either equivalent-literal detection or binary-clause detection. We describe a generalization of the technique that handles the tracking necessary to support equivalent-literal detection and binary-clause detection. Both generalizations are designed to preserve the important property of the original method that unbinding a variable is very efficient. We decribe a data-structure technique called lazy deletion that permits several operations to be performed in amortized constant time, where the more straightforward implementation might require linear time per operation. The overall efficiency of generalized technique is analyzed. Preliminary implementation results are reported. 1 Introduction A new technique for boolean constraint propagation was reported recently, as part of the satisfiability solver named Chaff2 [MMZ 01]. The previously favored technique was published by Dalal and Etherington [DE92], and involved maintaining an exact count of how many literals in each clause were not currently contradicted. The new technique, which may be called watching two literals, only keeps track of whether at least two literals in the clause are not currently contradicted. In the Chaff2 environment, this is enough information because it only processes unit clauses. This note investigates how to generalize the idea (efficiently) to environments where (a) binary clauses need to be detected, and/or (b) equivalent-literal identification is being carried out. The main problem is that in the Chaff2 environment (essentially a DPLL environment [DLL62]) a variable Ú has three possible states: free, true, or false, whereas in the presence of equivalent-literal identification there are ¾ Ò ½µ additional states: equivalent-to Ù, where Ù is any other variable in the formula, and the formula contains Ò variables in all. A major point made by the authors of Chaff2 is that the bookkeeping techniques can make an order-of-magnitude difference in the program s speed. In particular, they point out that watching two literals requires no work when a literal binding is retracted during backtracking. Another point they make is that the cache ( cache always refers to the computer s hardware cache) is used very efficiently, compared to earlier techniques. The generalizations we propose try to preserve these properties. For that reason, we would like to avoid straightforward methods such as replacing equivalent literals throughout the formula, or setting all literals in an equivalence class to true when one of them is set to true. First we review the basic method (filling in some details omitted from the Chaff2 paper), then we describe the generalizations to accommodate equivalent-literal identification. We note that equivalent-literal identification has been found to be effective in earlier studies [Pre95, VGT96, Li00]. In this note we do not study how to detect equivalent literals, but only how to utilize the information, once they are detected. 1
Nearly all complete satisfiability solvers are in the DPLL family (for Davis, Putnam, Loveland, and Logemann [DLL62]). They search for a satisfying assignment by fixing variables one by one and backtracking when an assignment forces the formula to be false. The procedure is not very effective in its original form, but it has been enhanced with various techniques to reduce the search space. Techniques to choose the branch variable are a separate topic, not treated here. Reasoning techniques can be broadly classified as preorder and postorder. Preorder techniques are applied as the search goes forward, and include unit-clause propagation (also called boolean constraint propagation), binary-clause reasoning, equivalent-literal identification, and others [BS92, Pre95, VGT96, Li00]. Postorder techniques are applied when the search is about to backtrack, because a conflict has been discovered [SS96, Zha97, BS97, MMZ 01]. Postorder techniques are variously called non-chronological backtracking, conflictdirected back-jumping, and learning. The learning label is based on the fact that new clauses can be added to the formula; we call these clauses postorder lemmas. A postorder lemma consists of the negations of the literals that make up a conflict set. L. Zhang et al. study various strategies for constructing conflict sets [ZMMM01]. In none of these works have preorder and postorder techniques been combined (except for unit-clause propagation). A companion paper [VG02] addresses this issue. For this note we do not study how to derive postorder lemmas, but we assume they will be derived and might have hundreds or thousands of literals in one clause. It is in such long clauses that watching only two literals pays major dividends. 2 Notation and Basic Data Structures In CNF, the formula is a conjunction of clauses and each clause is a disjunction of literals; each literal is a propositional variable Ü or its negation Ü. We denote a clause as Õ ½ Õ ¾ Õ and a formula as ½ ¾ Ñ. An empty formula is true and, the empty clause, is false. The formula has Ò variables, Ñ clauses, and Ä (occurrences of) literals; Ñ and Ä can vary if clauses are added and deleted. We use Ä for the length of the formula. The variables of the formula are represented by positive integers, ½ Ò; negative literals are negative integers. To facilitate equivalent-literal identification, true is represented by Ò ½µ and false by Ò ½µ. Two arrays of integers record the clauses in the formula, as suggested in Figure 1. The Ð Ù Ç Ø array permits any clause to be located in constant time, given its clause number, which never changes. The clause contents are stored in Ð Ù Ä Ø ; if this array is compacted after some clauses are deleted, Ð Ù Ç Ø is updated simultaneously. Note that the clause information in Ð Ù Ä Ø is a sequence of integers interpreted as follows: ÒÏ Ø, ÐÄ Ò Ø, Ð Ø ½,, Ð Ø ÐÄ Ò Ø. The number of watched literals is ÒÏ Ø. We assume ÐÄ Ò Ø ½. Ð Ù Ç Ø ¼ ½ ¼¼¼ ¼ ½ ¾ ½¼¼½ Ð Ù Ä Ø 1 3 25 14 33 2 2 25 33 2 4 25 19 15 16 0 5 9 15 5000 Figure 1: Basic clause arrays for a formula of length 3000 (i.e., 3000 literal occurrences) and clauses numbered 1 1000. Clause 1 contains three literals (25, 14, 33) and one watched literal (25). Clause 2 contains two literals (25, 33) and two watched literals. Clause 3 has been deleted. Clause 4 contains four literals ( 25, 19, 15, 16) and two watched literals ( 25, 19). 2
3 The Two-Watched-Literal strategy The basic idea of the watched-literal strategy is that a watched literal is either free or bound to true. When a watched literal becomes bound to false, it ceases to be watched. For the two-watched-literal strategy, an array of lists, called, is maintained; it is indexed by literals from Ò to Ò. The list of integers Õ (usually) specifies the numbers of the clauses in which literal Õ is a watched literal. This is true as long as Õ has not been bound to false. For a normal clause, the first two literals are its watched literals, and ÒÏ Ø = 2. As variables are bound during the search, this property is maintained, if possible. Using Figure 1 as an example, suppose variable 25 is being bound to true. The list ¹¾ will contain 4, as well as other clauses where 25 is watched. Clauses in this list need to be updated, as 25 ceases to be a watched literal. We search to the right for a literal that is not false. We first find 19, so we swap 25 with 19. But this just puts 25 in the second position, where it still should be watched, so we search to right again for a literal that is not false. Suppose 15 is false, but 16 is either free or true. We swap 25 with 16, restoring the watched-literal property for the the first two literals of the rearranged clause. To complete the bookkeeping, we delete clause 4 from the list for literal 25, and insert clause 4 in the list for 16. (Note that the deletion can be accomplished in constant time because the list traversal is at the node for clause 4.) The new configuration (including ÒÏ Ø and ÐÄ Ò Ø ) is (2, 4, 19, 16, 15, 25). Now we turn to the exception. Again suppose variable 25 is being bound to true. This time, assume that 15 and 16 are both false. We still swap 25 with 19. But the second search fails. To record this, we reduce ÒÏ Ø to 1. This denotes that clause 4 is effectively a unit clause, and it implies the binding 19 = true. The configuration is: (1, 4, 19, 25, 16, 15). It might seem logical now to delete clause 4 from the list for 25, because it is no longer watched, but here is the trick: By leaving this clause in the list, when we are finished updating clauses, the list ¹¾ will contain precisely those clauses that became unit clauses as a result of binding 25 to true. In all such clauses 25 appears as the second literal. Therefore, if 25 is unbound by backtracking, the list ¹¾ contains exactly those clauses that are no longer effectively unit clauses, and 25 is instantly their second watched literal. Possibly, this is the basis for the claim by the Chaff2 authors that variable unbinding can be done in constant time. However, as we have presented the procedure, when a variable binding (say 25 = true) is retracted, to preserve the semantics of the data structure, it is necessary to visit all the clauses in ¹¾ and add 1 to ÒÏ Ø. The Chaff2 authors do not explain how they change the status of all these clauses (from unit clauses to non-unit clauses) in constant time. Also, depending on the implementation, ÒÏ Ø might be reduced to 0 when a contradiction is discovered, before conflict processing begins. Continuing with the previous example, the binding 25 = true made clause 4 into the (effectively) unit clause [ 19]. It might also produce a unit clause [19] elsewhere, and the processing of the binding 19 = true might subtract 1 again from ÒÏ Ø in clause 4. Keeping clause 4 on the list ¹½ avoids having to rebuild the list when the binding of 19 is retracted. 4 Three Watched Literals The generalization to three watched literals is straightforward. The value of ÒÏ Ø ranges from 0 to 3 instead of 0 to 2. Now, when a binding, say Õ true, causes a failing search for a replacement literal in a clause where Õ is watched and ÒÏ Ø = 3, the clause remains on the list ¹Õ and 1 is subtracted from ÒÏ Ø. This clause would be put on a list of effectively binary clauses. Similarly, if ÒÏ Ø, we know a replacement cannot 3
1 3 3 10 5 6 7 4 8 10 0 1 2 3 4 5 6 7 8 9 10 Figure 2: An in-tree without path compression to record equivalence classes. 3 is the leader of 2; 10 is the leader of 4, 8, and 9; 1, 5, 6, and 7 are their own leaders. be found, so we subtract 1 from ÒÏ Ø, leave the clause on the Õ list, and queue the clause for special processing according to its effective length. The complications arise when equivalent-literal identification is introduced. 5 Equivalent Literals and Watched Literals Equivalent-literal identification is a procedure that identifies a set of literals that must have the same truth value or else a conflict (empty clause) results. Let s assume the result is a set statements of the form Õ :=, where Õ and are literals; called the leader of the set. Of course, there is also a set containing Õ. We are not concerned with the details of this procedure. At any point in the computation, the literals of the formula are partitioned into equivalence classes, such as. Some equivalence classes contain a single literal. As the search goes forward, if the equation Ô Õ is discovered (presumably from analysis of the effectively binary clauses, but how does not matter), then the equivalence classes of Ô and Õ are merged. Upon backtracking, it becomes necessary to unequate Ô and Õ. That is, the present equivalence class containing them both has to be dissolved into the two former equivalence classes. Although very efficient data structures for going forward are known (under the names union-find, dynamic equivalence relations, path compression), we are not aware of corresponding results for backing up. An apparently satisfactory method is to simply record the previous state in the union-find data structure (essentially an in-tree embedded in an array; see Figure 2) every time a change to that structure is made going forward. If previous states are recorded in a LIFO stack, and equations are unbound in LIFO order, then backing out the changes should not cost more than installing them. One possible way to incorporate the knowledge that Õ, where is the leader of Õ s equivalence class is to replace all occurrences of Õ by (and Õ by ) throughout the formula. This is not in keeping with the spirit of the watched-literal strategy, and is likely to be expensive, so we propose a different approach. Throughout this discussion we assume that we are normally watching Ï literals, where Ï ¾. 5.1 Invariants The invariants to be maintained for each clause are the following: 1. The range for ÒÏ Ø is 0 through Ï. 2. If ÒÏ Ø =, then literals 1 through of the clause have pairwise distinct leaders and none of those leaders is bound to false. 3. If ÒÏ Ø Ï, then literals ½µ through the end of the clause either are bound to false or have the same leader as one of the literals 1 through. These invariants need to be preserved through the following kinds of operations: 1. Literal Õ, currently a leader, is bound to some other leader,. 2. Literal Õ, currently a leader, is bound to true or false. 4
3. Literal Õ, currently a leader, is unbound from true or false, becoming free. 4. Literal Õ, currently not a leader, is unbound from being equivalent to some other leader,, and becomes a leader. The other leader,, must be free before the unbinding. Consequently, Õ will be free after the unbinding. The procedures to preserve the invariants through these operations are described next. 5.2 Binding Operations To process the equivalence binding, Õ :=, we examine each clause in the list Õ. Let be one such clause. 1. If none of the first Ï literals of is equivalent to Õ, this is a stale entry in Õ s list and should be deleted; this is part of the lazy deletion technique discussed in Section 5.3. 2. If exactly one of of the first Ï literals of is equivalent to Õ, we simply add to (leaving on the list for Õ also). 3. If more than one of of the first Ï literals of is equivalent to Õ, we try to replace Õ with a different watched literal. First Õ is swapped to position ÒÏ Ø in the clause, the rightmost position for a watched literal. Then, we try to find a literal Ô in clause such that the leader of Ô is free or is bound to true; also the leader of Ô must be different from the leader of any currently watched literal in. (a) If this succeeds, we swap Õ and Ô; we are done with for now and delete it from Õ s list, while inserting it into the lists for Ô and the leader of Ô, if Ô is not a leader. (b) If no such replacement literal can be found, we subtract 1 from ÒÏ Ø and leave on the list for Õ. In addition is inserted in a list of short clauses. We assume that clauses of lengths less than Ï receive special processing, otherwise there is no reason to watch Ï literals. The details of how short clauses are managed depends on the algorithm; we only require that the clauses that shrank in their number of watched literals as a result of this equivalence binding can be identified efficiently when the binding is retracted. However, if ÒÏ Ø Ï at the beginning of the operation, the search for a replacement must fail and can be skipped; item (3b) can be executed immediately. Finally, since Õ is not a leader, it is not considered for future branching. Only variables that are leaders are eligible to be bound to true or false. Say Õ is being bound to false, either because it was derived as a unit clause or it is being branched upon. Let us consider the updating procedure for the watched literals. Each clause in Õ is visited. First, if none of the first Ï literals is equivalent to Õ, is a stale entry in Õ s list and should be deleted; this is part of the lazy deletion technique discussed in Section 5.3. Now suppose that one or more of the first Ï literals is equivalent to Õ. As described above under equivalence binding, if ÒÏ Ø = Ï, we try to find a new literal Ô to watch; if we succeed we can delete from Õ s list and insert on the list for Ô and on the list for the leader of Ô, if Ô is not a leader. Suppose this effort fails, or ÒÏ Ø Ï so we know it must fail. Then we subtract 1 from ÒÏ Ø and leave in Õ s list. One of the themes of the literal-watching strategy is that the expensive operations are efficient in their cache usage [MMZ 01]. Our proposal tries to continue with this theme. Notice that a potentially expensive part of the update operations just described consists of searching through the clause literals for replacements, when the clause is very long, as is typical for postorder lemmas. However, because this searching uses contiguous memory, a high cache hit rate may be expected. Simultaneously, the search needs to check entries in the equivalence data structure, which is something like Figure 2. Here the cache usage is probably less favorable, but the entire structure is Ò integers, a fair portion of which might live in the cache. We do not presently have tools to measure cache usage. 5
5.3 Unbinding and Lazy Deletion We now turn to the requirements for retracting a binding. Say the binding Õ false is being retracted. We know that all clauses on the list Õ have ÒÏ Ø Ï. For simplicity and safety we visit each and add 1 to ÒÏ Ø. The list resumes its usual function of identifying those clauses in which Õ, or something equivalent to Õ, is being watched. Say the equivalence binding Õ := is being retracted. Possibly the clauses in Õ should no longer be in. However, can contain many clauses other than those that were added when the equivalence binding Õ := was processed going forward. It would be quite inefficient to search for those that need to be deleted. Instead we adopt a lazy deletion technique. We leave the incorrect clauses in for the time being, and we wait until the next time we traverse the list to find them and delete them. Such incorrect entries in the list are called stale. Since must be free and a leader at the time Õ := was bound going forward, the same is true at the time it is being retracted. Thus the next time the list is traversed, it will be for some kind of binding to going forward (either false or := some other leader). During this traversal, stale list entries can be detected and deleted. For all clauses whose value of ÒÏ Ø decreased due to the binding Õ :=, as described in Section 5.2, item (3b), their ÒÏ Ø is now increased by 1 to cancel the decrease. We assume some kind of short clause lists identify these clauses. 5.4 A Partial Example We begin the example with a configuration slightly different from Figure 1. Recall that the clause format is ÒÏ Ø, ÐÄ Ò Ø, Ð Ø ½,. Only a few clauses are shown and only the relevant list entries are shown. clause information 1 2 3 25 14 33 2 2 2 25 33 4 2 4 25 19 15 16 leader 10 15 16 19 25 1 2 4 4 Assume the equivalence binding 25 := 15 occurs. This causes ½ and ¹½ to receive the clauses in which 25 or 25 was being watched. clause information 1 2 3 25 14 33 2 2 2 25 33 4 2 4 25 19 15 16 leader 10 15 16 19 15 4 1 2 2 1 4 4 Now assume these equivalence bindings occur: 19 := 10; 16 := 10; 15 := 10. The first binding causes clause 4 to be inserted in the list for 10 because clause 4 is on the list for 19 and 10 is not equivalent to 25 at this point. The second binding does not change this part of the data structure. The third binding affects clause 4 even though 15 is not watched because an earlier equivalence put clause 4 on the list for 15. But now all the literals in clause 4 have leader 10, so they are all equivalent. Consequently, ÒÏ Ø is decreased by 1 and clause 4 is inserted in the short clause list for the binding 15 := 10. In addition, clauses 1 and 2 are inserted in the list for 10. 6
clause information 1 2 3 25 14 33 2 2 2 25 33 4 1 4 19 25 15 16 leader 10 10 10 10 10 4 4 1 2 1 2 2 1 4 4 Next, literal 10 is bound to true because clause 4 is effectively a unit clause. But clauses 1 and 2 are on the list for 10 so clause 2 becomes a unit clause for 33 and in clause 1 the literal 25 replaced by 33 as a watched literal. Notice that clause 1 is removed from the list for 10, because that list is being traversed, but it remains in the lists for 15 and 25. Also notice that clause 1 is not removed from the list for 10, because it contains fewer than Ï watched literals. clause information 1 2 3 14 33 25 2 1 2 33 25 4 1 4 19 25 15 16 leader 10 10 10 10 10 4 4 1 2 2 2 1 4 4 By further unit propagation of 33, clause 1 becomes a unit clause for 14. Now let us trace what happens as the above bindings are retracted. Unbinding 14 and 33 reverts to the above diagram. As 10 is unbound the clauses in the list for 10 have their values of ÒÏ Ø increased by 1. Then 15 is unbound from its equivalence to 10. This means that clause 4 becomes a stale entry in the list for 10. We are not confident that all stale entries will be at the beginning of the list for 10, and we do not want to search for them, so they are allowed to remain. The short clause list for the binding being retracted tells us that clause 4 needs to have ÒÏ Ø increased by 1. The bindings 16 := 10 and 19 := 10 are retracted next. Now the state is: clause information 1 2 3 14 33 25 2 2 2 33 25 4 2 4 19 25 15 16 leader 10 15 16 19 15 4 4 1 2 2 2 1 4 4 Notice that retracting 15 := 10 caused the leader of 25 to revert to 15. If path compression is used for equivalences, then a journal/rollback strategy [VGT96] is needed to allow path decompression, so earlier leaders can be recovered. Also notice the stale entries in the lists for 10 and 10. If the binding 25 := 15 is retracted, there will be three more stale entries in the lists for 15 and 15. These stale entries will be lazily deleted the next time each list is traversed as part of a new binding operation. 5.5 Cost of Operations To summarize, the watched-literal strategy in the presence of literal equivalences requires finding the leader of a literal (the find operation) in many situations. Roughly speaking, the costs of operations are multiplied by the cost of finding leaders, compared to an environment without literal equivalences. This cost can be held to a near-constant per find by using the path compression strategy and in-trees, but the constant factor is substantial, possibly 5 to 50. For this to pay off, literal equivalences have to bring about large reductions in the search space. There is some evidence for this [VGT96, Li00], but it has to be re-evaluated in the context of postorder lemmas. 7
We also observe that several operations involving a watched literal require checking each of the other watched literals in the clause. This cost is proportional to Ï ½. In practice Ï is either 2 or 3, so this part of the cost is about double for three watched literals, compared to two. 6 Conclusion The procedures described have been implemented using a two-literal watching policy and equivalent literal processing. However, the clauses supervised under this policy are only those derived as postorder lemmas (i.e., as the result of conflicts). The original formula is handled by older methods. The implementation is grafted onto an existing program, ¾ÐÎ Ê which is described in greater detail in a companion paper [VG02]. The situation in which watching only two literals pays greatest dividends is when very long clauses are present. Postorder lemmas tend to be long clauses. The most important statement we can make at this time is that the method works correctly in hundreds of tests. The performance is satisfactory in terms of time, but there is nothing with which to make a direct comparison. The bottlenecks for the ¾ÐÎ Ê program are elsewhere: the memory use is too high due to keeping too many postorder lemmas, and the time is too great due to inefficiencies in binary clause processing. One of the purposes of preparing this report is to assist other researchers to incorporate the literal-watching strategy in environments that perform equivalent-literal processing (e.g., [Li00]), hopefully starting from a better base than we did. Acknowledgments This work was supported in part by NSF grants CCR-9505036 and CCR-9503830. References [BS92] [BS97] [DE92] [DLL62] A. Billionnet and A. Sutter. An efficient algorithm for the 3-satisfiability problem. Operations Research Letters, 12:29 36, July 1992. R. J. Bayardo, Jr. and R. C. Schrag. Using CSP look-back techniques to solve real-world SAT instances. In Proceedings Fourteenth National Conference on Artificial Intelligence (AAAI-97), pages 203 208, 1997. M. Dalal and D. Etherington. A hierarchy of tractable satisfiability problems. Information Processing Letters, 44:173 180, December 1992. M. Davis, G. Logemann, and D. Loveland. A machine program for theorem-proving. Communications of the ACM, 5:394 397, 1962. [Li00] C. M. Li. Integrating equivalency reasoning into davis-putnam procedure. In AAAI, 2000. [MMZ 01] M. Moskewicz, C. Madigan, Y. Zhao, L. Zhang, and S. Malik. Chaff: Engineering an efficient SAT solver. In 39th Design Automation Conference, June 2001. [Pre95] D. Pretolani. Efficiency and stability of hypergraph SAT algorithms. In D. S. Johnson and M. Trick, editors, Cliques, Coloring, and Satisfiability: Second DIMACS Implementation Challenge., DIMACS Series in Discrete Mathematics and Theoretical Computer Science. American Mathematical Society, 1995. 8
[SS96] [VG02] J. P. Silva and K. A. Sakallah. GRASP a new search algorithm for satisfiability. In Proc. IEEE/ACM Int l Conf. on Computer-Aided Design, pages 220 227. IEEE Comput. Soc. Press, 1996. Allen Van Gelder. Extracting (easily) checkable proofs from a satisfiability solver that employs both preorder and postorder resolution. In Seventh Int l Symposium on AI and Mathematics, Ft. Lauderdale, FL, 2002. (Also available at ØÔ»» ØÔº ºÙ º Ù»ÔÙ» Ú» » عÔÖ ¹ÔÓ ØºÔ º Þ.). [VGT96] A. Van Gelder and Y. K. Tsuji. Satisfiability testing with more reasoning and less guessing. In D. S. Johnson and M. Trick, editors, Cliques, Coloring, and Satisfiability: Second DIMACS Implementation Challenge., DIMACS Series in Discrete Mathematics and Theoretical Computer Science. American Mathematical Society, 1996. (also at ØÔ»» ØÔº ºÙ º Ù»ÔÙ» Ú» ÐÓ ¹ØÖºÔ º ). [Zha97] H. Zhang. SATO: An efficient propositional prover. In 14th International Conference on Automated Deduction, pages 272 275, 1997. [ZMMM01] L. Zhang, C. Madigan, M. Moskewicz, and S. Malik. Efficient conflict driven learning in a boolean satisfiability solver. In ICCAD, Nov. 2001. 9