0

I'm currently studying the 2SAT problem for an exam and I don't really understand how to check if a solution exists using the brute force. I know this seems a bit strange but I understand how to implement the implication graph a bit better but I'm not too sure how to implement a brute force strategy.

Could anyone share some insight? Maybe in pseudo code or java.

Thanks

John Smith
  • 635
  • 1
  • 10
  • 19
  • 1
    Maybe you are missing the basics of 2-SAT then ? The brute force method just generates all the permutations between true and false for all your variables, then you check whether the current permutation satisfies the set of clauses given. – mmgp Feb 08 '13 at 18:49
  • Sorry, The question may have been worded poorly. I understand that the brute force method generates all the permutations but I feel like the way I'm doing it, even for a brute force implementation, it is very brute.I created a 2D array containing all combinations possible given the input then checked the solutions row by row. It seemed to work for a small number of disjunctions, but the size of the array quickly gets out of hand and I get a heap space exception. – John Smith Feb 08 '13 at 19:04
  • Strongly connected components is simple enough to implement and much more efficient, and will tell you whether there is a solution or not. What is the problem with that ? – mmgp Feb 08 '13 at 19:53
  • Hahah I was just wondering if there was an "efficient" brute force method. But I guess those are kind of contradicting. – John Smith Feb 08 '13 at 22:42

2 Answers2

2

The variables in a formula can be encoded as bits in an integral value. The brute force method then boils down to range over all possible values that the integral "container" may take.

In other words, you have an array of integers, which represents all your formula's variables, and you increment the integers with carry, and at each step check the solution it represents against your formula. You stop when the solution is a match.

Here's a dead simple implementation for such a variable container:

class OverflowException extends RuntimeException {}

public class Variables {
    int[] data;
    int size;

    public Variables(int size_){
        size = size_;
        data = new int[1 + size/32];
    }
    public boolean get(int i){
         return (data[i/32] & (1 << i%32)) != 0;
    }
    public void set(int i, boolean v){
        if (v)
            data[i/32] |= (1 << i%32);
        else
            data[i/32] &= ~(1 << i%32);
    }
    public void increment(){
         int i;
         for (i=0; i < size/32; i++){
             data[i]++;
             if (data[i] != 0) return;
         }
         if (size%32 != 0){
             data[i]++;
             if ((data[i] & ~((1 << (size%32)) - 1)) != 0)
                 throw new OverflowException();
        }
    }
}

(Caveat emptor: code untested).

The variable array can also be more simply represented as a boolean container, but you might lose a bit in performance, because of the increment step (although that could be perhaps mitigated by using gray code instead of plain binary encoding for the increment operation, but the complexity of this implementation seems to indicate the contrary, and if you go for a complex solution, it might as well be a good sat2 solver instead).

didierc
  • 14,572
  • 3
  • 32
  • 52
  • I am not fluent in Java, so my code is probably not following the standard practices, please advise, or edit. – didierc Feb 08 '13 at 20:46
0

This is the reason we don't use brute force solutions :) , they consume to much resources. My algorithm would be not to create a matrix with all possibilities. But to create one assignment and then test it immediately. Then create the next one. You can halt when you found the first solution.

dolbi
  • 2,180
  • 1
  • 19
  • 25
  • At the moment, I am just looking to see if the 2SAT has a solution or not. Finding all possibilities takes too much :D – John Smith Feb 08 '13 at 19:18
  • As I mentioned you can halt the run on the first positive assignment. That way you don't need to allocate space for all assignments but just check them and halt. – dolbi Feb 08 '13 at 19:21
  • @dolbi I'm not sure whether you are joking or not, but this solution is just as bad as any brute force (whether they try to store all the permutations at once or not). Generating one permutation after another doesn't save any computational time, it is still `O(n!)` in the worst case. Generating all permutations at once is pointless, shouldn't even be considered. – mmgp Feb 08 '13 at 19:57
  • @mmgp this is what Yamato asked for, a brute force solution. I don't support it in any way iterating `2^n` permutation shouldn't be done, I agree. BUT, if you want to run it anyway and not to crush on out of memory, this is a valid alternative (in my opinion). – dolbi Feb 09 '13 at 10:32
  • @dolbi I know, I don't blame you :) – mmgp Feb 09 '13 at 12:27