Does anyone know of a good program to convert CNF files with any number of variables per clause to CNF files with exactly 3 variables per clause (3-CNF)? I've seen this algorithm in computer science books but can't find an implementation anywhere and would hate to waste time implementing it myself if others have already done it. Thanks!
Converter from SAT to 3-SAT
4.9k views Asked by Elliot Gorokhovsky At
2
There are 2 answers
2
On
SAT is not solvable in polynomial time (according to the current knowledge). 2-SAT is solvable in polynomial time.
So a conversion from generic SAT to 2-SAT will not be fast (not in polynomial time), otherwise we would find a polynomial-time algorithm for SAT.
In other words, the time needed to convert SAT to 2-SAT is roughly the same as the time to just solve SAT.
Maybe you mean 3-SAT, not 2-SAT?
I didn't know any program to do that either, but the algorithm is really simple so just I wrote the following python script (download) that reads a general CNF in DIMACS format and writes the CNF of an equivalent 3-SAT problem in DIMACS format:
The interesting bit is of course the
for clause in cnf:
loop that takes the general sat problem stored incnf
and transforms it into a 3-sat instance stored innew_cnf
. It does this by translating a clause such asinto the following set of clauses.
The first three clauses are added to
new_cnf
. The last clause is not 3-sat so the algorithm is re-run on this last clause, yielding the following new clauses:This are all 3-sat clauses, so they are added to
new_cnf
and the algorithm continues with the next clause fromcnf
. (If the last clause were not 3-sat, the algorithm would keep working on it until only 3-sat clauses are left. The length of the last clause approximately halves with each iteration.)