<p>You might consider the algorithms that are used for generating Sudoku puzzles - presumably generalized to $n \times n$ - since (usually) Sudoku puzzles are supposed to have a unique solution. On the other hand, Sudoku puzzles are also usually <em>guaranteed</em> to have at least one solution... But finding that solution could still be a good benchmark for your solver.</p>

<p>You might use a Sudoku-generator together with a reduction to SAT, or you might think about how to apply the techniques used in Sudoku generation to more directly generate Unique SAT instances. For the former, obviously your SAT instances will have some structure, but it's unclear to me whether it's more or less structure than e.g. planting a solution or using the witness isolation technique. Probably depends on your needs and your solver.</p>

<p>The one reference I know of here is: <a href="http://zhangroup.aporc.org/images/files/Paper_3485.pdf">Sudoku Puzzles Generating: from Easy to Evil</a>.</p>

CC BY-SA 4.0
2023-12-22T09:21:13.533Z
"Sudoku puzzles are also usually guaranteed to have at least one solution...", I think soduko are usually guaranteed to have unique solution or no solution.
Jxb
CC BY-SA 3.0
2013-09-17T03:47:38.043Z
2013-09-17T03:47:38.043Z
Joshua Grochow
<p>I think a good test case would be to generate random uniquely satisfiable 3XOR instances (planted instances) with $\Theta(n)$ constraints and then convert them to 3SAT instances.</p>

CC BY-SA 4.0
2023-12-13T07:52:35.353Z
how to generate random uniquely satisfiable 3XOR instances
Jxb
CC BY-SA 4.0
2023-12-22T17:32:19.910Z
You can solve 3XOR in cubic time by linear algebra. Each new constraint you add either keeps soln set the same or cuts it exactly in half. Start adding random constraints and for each you add, check that it's still solvable and how many solns it has.
Joshua Grochow
CC BY-SA 3.0
2013-09-17T15:42:32.167Z
2013-09-17T15:42:32.167Z
Ryan O'Donnell
<p>imho one of the best ways to create "presumably hard" SAT instances while controlling the number of solutions is from integer factoring instances/circuits encoded in binary. the code is not very complex, it uses mainly EE addition circuits, and does not lead to "large" SAT instances. the number of solutions is equal to the number of factors (including "permutations" of the factors). therefore prime numbers generate exactly two solutions, $(1,p),(p,1)$. a single solution can be guaranteed with a further "comparison" constraint that restricts the factors to $a&lt;b$ or that $a \neq 1$ or $b \neq p$.</p>

<p>also with this approach is it is relatively easy to find numbers with <em>roughly</em> however many factors/solutions are desired. the <a href="http://en.wikipedia.org/wiki/Smooth_number" rel="nofollow noreferrer">"smoother"</a> the number, the more factors.</p>

<p>various researchers over the years have created this factoring SAT code (eg for DIMACS competition/arcihve which has stored some factoring instances in the past) but unfortunately there does not seem to be a publicly available version. see also the 1st link below for a ref where the code was written/implemented apparently for a graduate course.</p>

<ul>
<li><a href="https://cs.stackexchange.com/questions/6588/reducing-the-integer-factorization-problem-to-an-np-complete-problem">Reducing the integer factorization problem to an NP-Complete problem</a></li>
<li><a href="https://cs.stackexchange.com/questions/11718/generating-3sat-circuit-for-integer-factorization-example">Generating 3SAT circuit for Integer factorization example</a></li>
</ul>

<p>another empirical/iterative approach that may be useful for some, to create more "unstructured" instances: create random SAT instances $e_n$ near the transition point (the region where the equation has a probability 50% between "solvable and unsolvable"), and then solve the equation. if it is unsolvable, throw away and restart. if it is solvable, add clauses that restrict the solution "not" to be the found solution, obtaining $e_{n+1}$, and re-solve. repeat if necessary. when the equation $e_{n+1}$ is no longer solvable, the $e_n$ must have had a single/unique solution.</p>

CC BY-SA 3.0
2013-09-18T01:16:01.187Z
I mentioned the factoring approach in my answer earlier, but I also explained why it might not be an ideal testbed: "However, these constructions do not seem like a useful way to benchmark or optimize your solver. They all have a special structure, and there is no reason to believe that this structure is representative of real-world problems. In particular, SAT instances drawn from cryptographic problems are known to be extremely hard, much harder than SAT instances drawn from many other real-world applications of SAT solvers, so they aren't a very good basis for benchmarking your solver."
D.W.
CC BY-SA 3.0
2013-09-18T01:23:49.750Z
so the above is a different pov that if one wants very hard instances, obviously a natural test case for any solver, then factoring is indeed a promising way to go. seriously doubt that you could find any published opinions that mirror yours. to repeat, factoring instances have been put in DIMACS challenge archives by serious researcher(s) starting many years ago. anyway, your contrary opinion is not even really expressed in a self-consistent way. cryptography is indeed a foremost/applied _real world problem_ even more so than many abstract/abstruse/academic problems used for SAT instances...
vzn
CC BY-SA 3.0
2013-09-17T16:32:58.740Z
2013-09-17T16:40:28.387Z
2017-04-13T12:48:30.830Z
vzn
<p>You can easily generate directly Unique SAT formulas with reasonable size $(|F|&lt;n+2^k)$</p>

<p>Let $m$ be the unique model - say $m$ contains only "0"s (rename the variables later if needed).<br>
Let $F$ a $k$-SAT formula satisfied only by $m$ - the maximum size of $F$ is the total number of clauses satisfied by $m$ i.e. $(2^k-1) \binom{n}{k}$.</p>

<p>Take the $\binom{k}{1}$ clauses that eliminate all models assigning exactly one "1" among $x_1,x_2 \ldots x_k$:<br>
$(\lnot x_1, x_2 \ldots x_k)(x_1, \lnot x_2 \ldots x_k)\ldots (x_1,x_2 \ldots \lnot x_k)$</p>

<p>Take the $\binom{k}{2}$ clauses that eliminate all models assigning exactly two "1" among $x_1,x_2 \ldots x_k$:<br>
$(\lnot x_1, \lnot x_2, x_3 \ldots x_k)(\lnot x_1,x_2, \lnot x_3 \ldots x_k)\ldots (x_1,x_2 \ldots \lnot x_{k-1} \lnot x_k)$</p>

<p>Keep going until taking the only $\binom{k}{k}$ clause that eliminates all models assigning "1" to each variables among $x_1,x_2 \ldots x_k$.</p>

<p>The only models which are not yet eliminated assign all $x_1,x_2 \ldots x_k$ to "0". Since $m$ is a model, then take any set of $n-k$ clauses that eliminate all models assigning "1" to $x_i (k&lt;i\leq n)$ and $0$ to any $k-1$ variables among $x_1,x_2 \ldots x_k$, for instance:<br>
$(\lnot x_{k+1}, x_1 \ldots, x_{k-1}) \ldots (\lnot x_n, x_1 \ldots x_{k-1})$.</p>

<p>Then $|F|=\sum_{i=1}^k \binom{k}{i} +n-k = 2^k-1+n-k$</p>

<p>To get more clauses, add any clause containing at least one negated variable. To get an unsatisfiable formula, just add a clause with $k$ unnegated variables.</p>

CC BY-SA 3.0
2016-07-07T12:52:03.850Z
There is a problem in your answer : we have n variables and this means that and not k
Elaqqad
CC BY-SA 3.0
2013-09-15T22:46:22.970Z
2013-09-18T06:01:45.117Z
2013-09-18T06:01:45.117Z
Xavier Labouze
<p>Here is one way to generate a Unique $k$-SAT instance, given a SAT instance $\varphi$ that you know is satisfiable.  Consider the formula $\psi(x)$ given by</p>

<p>$$\varphi(x) \land h(x)=y,$$</p>

<p>where $h$ is a hash function that maps an assignment $x$ to a $k$-bit value (for some small value of $k$), and $y$ is a random $k$-bit value.  If $\varphi$ has about $2^k$ satisfying assignments, then (heuristically) we assume that $\psi$ will have exactly one satisfying assignment (with constant probability).  We can test whether this is the case using a SAT solver (namely, test whether $\psi$ is satisfiable; if it is, and $x_0$ is one satisfying assignment, test whether $\psi(x) \land x \ne x_0$ is satisfiable).  If $k$ is not known, you can find $k$ using binary search or just by iterating over each candidate value $k=1,2,\dots,n$ (where $n$ is the number of boolean variables in $x$).</p>

<p>You can choose the hash function freely.  You'll probably want to make it as simple as possible.  One extremely simple construction is to have $h$ pick out a random subset of $k$ bits from $x$.  A slightly more sophisticated construction is to have the $i$th bit of $h(x)$ be the xor of two randomly chosen bits from $x$ (choosing a separate pair of bit positions for each $i$, independently).  Keeping $h$ simple will keep $\psi$ relatively simple.</p>

<p>This kind of transformation is sometimes used/suggested, as part of a scheme for estimating the number of satisfying assignments to a formula $\varphi$; I've adapted it for your particular need.</p>

<p>You can find many testbeds of SAT instances on the Internet, and you could apply this transformation to all of them, to obtain a collection of Unique $k$-SAT instances.</p>

<hr>

<p>Another possibility would be to generate Unique $k$-SAT instances from cryptography.  For instance, suppose $f:\{0,1\}^n \to \{0,1\}^n$ is a cryptographic one-way permutation.  Let $x$ be a randomly chosen element of $\{0,1\}^n$, and let $y=f(x)$.  Then the formula $\varphi(x)$ given by $f(x)=y$ is a Unique $k$-SAT instance.  As another example, pick two large prime numbers $p,q$ randomly, and let $n=pq$.  Then the formula $\varphi(x,y)$ given by $x \cdot y = n \land x&gt;1 \land y&gt;1 \land x \le y$ (with the obvious correspondence between bit-strings and integers) is a Unique $k$-SAT instance.  However, these constructions do not seem like a useful way to benchmark or optimize your solver.  They all have a special structure, and there is no reason to believe that this structure is representative of real-world problems.  In particular, SAT instances drawn from cryptographic problems are known to be extremely hard, much harder than SAT instances drawn from many other real-world applications of SAT solvers, so they aren't a very good basis for benchmarking your solver.</p>

<hr>

<p>In general, all of the techniques mentioned in this answer have the drawback that they generate Unique $k$-SAT instances with a particular structure, so they might not be what you are looking for -- or, at least, you might not want to rely solely upon formulae generated in this way.  A better approach would be to identify applications of Unique $k$-SAT (who do you think is going to use your solver, and for what purpose?), and then try to obtain some realistic examples from those application domains.</p>

<p>For a related topic, see also <a href="https://cstheory.stackexchange.com/q/16751/5038">Generating interesting combinatorial optimization problems</a></p>

CC BY-SA 3.0
2013-09-16T17:02:30.720Z
Thanks, @RickyDemer!  I meant one-way permutation, but that wasn't what I wrote. Fixed.
D.W.
CC BY-SA 3.0
2013-09-11T22:34:38.943Z
2013-09-16T17:02:10.110Z
2017-04-13T12:32:28.080Z
D.W.
<p>This question is probably on the border line between on-topic and off-topic, however I've seen similar questions here, therefore I'll ask it.</p>

<hr>

<p>I'm implementing a Unique $k$-SAT solver, whose input is a $k$-CNF formula having at most $1$ satisfying assignment. To test its practical behaviour, I need a set of such formulas. I've searched for them on the web, and found nothing (while, on the other hand, it is very easy to find suites of ordinary $k$-CNF formulas).</p>

<blockquote>
  <p>Where can I find Unique $k$-SAT instances?</p>
</blockquote>

<p>Alternatively, I would be content also to know any procedure to generate uniquely satisfiable instances. The only approach I'm aware of goes under the name of <em>planted SAT instance generation</em>: you randomly generate an assignment of $n$ variables, then you generate only those clauses which agree with such assignment. This approach is unsatisfactory for my purposes, for the following reasons:</p>

<ul>
<li>The obtained formula may have further undesired satisfying assignments.</li>
<li>To be sure that the formula is satisfied uniquely by the desired assignment, you should introduce <em>all</em> the possible clauses that agree with it. This would produce formulas with too many clauses, which will probably be easy to solve and therefore not representative of the worst case behaviour of the solver. It is not obvious to me how we can efficiently force uniqueness while keeping the number of clauses reasonable.</li>
</ul>

<blockquote>
  <p>How can we generate uniquely satisfiable formulas with a reasonable number of clauses? By <em>reasonable</em> I mean far from the maximum $2^k \cdot {n \choose k}$.</p>
</blockquote>

CC BY-SA 3.0
2013-09-12T20:15:03.040Z
Given a SAT formula $F$ with $n$ variables and $m$ clauses. If the number of clauses are between $3^{n}-2^{n}$ and $3^{n}-2^{n}-2^{n-1}$ then the formula $F$ is either uniquely satisfiable or not satisfiable... I had worked out the equations for k-SAT as well. Will let you know if I find it.
Tayfun Pay
CC BY-SA 3.0
2013-12-04T21:20:03.553Z
If you have enough time on your hands (and the instances are small enough), you can generate instances at the phase transition and test them with a SAT solver.  If a formula has no solution, discard it.  If it has a solution X, add a clause insisting that the solution is not X, and run the solver again.  This is basic but slow.
Andrew D. King
<sat>
Unique $k$-SAT benchmarks
Giorgio Camerani
# Unique $k$-SAT benchmarks

(Asked by: username_0 on 2013-09-11)

This question is probably on the border line between on-topic and off-topic, however I've seen similar questions here, therefore I'll ask it.

---

I'm implementing a Unique $k$-SAT solver, whose input is a $k$-CNF formula having at most $1$ satisfying assignment. To test its practical behaviour, I need a set of such formulas. I've searched for them on the web, and found nothing (while, on the other hand, it is very easy to find suites of ordinary $k$-CNF formulas).

> Where can I find Unique $k$-SAT instances?

Alternatively, I would be content also to know any procedure to generate uniquely satisfiable instances. The only approach I'm aware of goes under the name of *planted SAT instance generation*: you randomly generate an assignment of $n$ variables, then you generate only those clauses which agree with such assignment. This approach is unsatisfactory for my purposes, for the following reasons:

* The obtained formula may have further undesired satisfying assignments.
* To be sure that the formula is satisfied uniquely by the desired assignment, you should introduce *all* the possible clauses that agree with it. This would produce formulas with too many clauses, which will probably be easy to solve and therefore not representative of the worst case behaviour of the solver. It is not obvious to me how we can efficiently force uniqueness while keeping the number of clauses reasonable.

> How can we generate uniquely satisfiable formulas with a reasonable number of clauses? By *reasonable* I mean far from the maximum $2^k \cdot {n \choose k}$.

**Tags:** sat

> **Comments:**
> **username_1**: Given a SAT formula $F$ with $n$ variables and $m$ clauses. If the number of clauses are between $3^{n}-2^{n}$ and $3^{n}-2^{n}-2^{n-1}$ then the formula $F$ is either uniquely satisfiable or not satisfiable... I had worked out the equations for k-SAT as well. Will let you know if I find it.  
> **username_2**: If you have enough time on your hands (and the instances are small enough), you can generate instances at the phase transition and test them with a SAT solver.  If a formula has no solution, discard it.  If it has a solution X, add a clause insisting that the solution is not X, and run the solver again.  This is basic but slow.  

---

## 5 answers

### Answer by username_7 on 2013-09-11. Score: 11 | Accepted answer

Here is one way to generate a Unique $k$-SAT instance, given a SAT instance $\varphi$ that you know is satisfiable. Consider the formula $\psi(x)$ given by

$$\varphi(x) \land h(x)=y,$$

where $h$ is a hash function that maps an assignment $x$ to a $k$-bit value (for some small value of $k$), and $y$ is a random $k$-bit value. If $\varphi$ has about $2^k$ satisfying assignments, then (heuristically) we assume that $\psi$ will have exactly one satisfying assignment (with constant probability). We can test whether this is the case using a SAT solver (namely, test whether $\psi$ is satisfiable; if it is, and $x\_0$ is one satisfying assignment, test whether $\psi(x) \land x \ne x\_0$ is satisfiable). If $k$ is not known, you can find $k$ using binary search or just by iterating over each candidate value $k=1,2,\dots,n$ (where $n$ is the number of boolean variables in $x$).

You can choose the hash function freely. You'll probably want to make it as simple as possible. One extremely simple construction is to have $h$ pick out a random subset of $k$ bits from $x$. A slightly more sophisticated construction is to have the $i$th bit of $h(x)$ be the xor of two randomly chosen bits from $x$ (choosing a separate pair of bit positions for each $i$, independently). Keeping $h$ simple will keep $\psi$ relatively simple.

This kind of transformation is sometimes used/suggested, as part of a scheme for estimating the number of satisfying assignments to a formula $\varphi$; I've adapted it for your particular need.

You can find many testbeds of SAT instances on the Internet, and you could apply this transformation to all of them, to obtain a collection of Unique $k$-SAT instances.

---

Another possibility would be to generate Unique $k$-SAT instances from cryptography. For instance, suppose $f:\{0,1\}^n \to \{0,1\}^n$ is a cryptographic one-way permutation. Let $x$ be a randomly chosen element of $\{0,1\}^n$, and let $y=f(x)$. Then the formula $\varphi(x)$ given by $f(x)=y$ is a Unique $k$-SAT instance. As another example, pick two large prime numbers $p,q$ randomly, and let $n=pq$. Then the formula $\varphi(x,y)$ given by $x \cdot y = n \land x>1 \land y>1 \land x \le y$ (with the obvious correspondence between bit-strings and integers) is a Unique $k$-SAT instance. However, these constructions do not seem like a useful way to benchmark or optimize your solver. They all have a special structure, and there is no reason to believe that this structure is representative of real-world problems. In particular, SAT instances drawn from cryptographic problems are known to be extremely hard, much harder than SAT instances drawn from many other real-world applications of SAT solvers, so they aren't a very good basis for benchmarking your solver.

---

In general, all of the techniques mentioned in this answer have the drawback that they generate Unique $k$-SAT instances with a particular structure, so they might not be what you are looking for -- or, at least, you might not want to rely solely upon formulae generated in this way. A better approach would be to identify applications of Unique $k$-SAT (who do you think is going to use your solver, and for what purpose?), and then try to obtain some realistic examples from those application domains.

For a related topic, see also [Generating interesting combinatorial optimization problems](https://cstheory.stackexchange.com/q/16751/5038)

> **Comments:**
> **username_7**: Thanks, @RickyDemer!  I meant one-way permutation, but that wasn't what I wrote. Fixed.  

---

### Answer by username_3 on 2013-09-17. Score: 7

You might consider the algorithms that are used for generating Sudoku puzzles - presumably generalized to $n \times n$ - since (usually) Sudoku puzzles are supposed to have a unique solution. On the other hand, Sudoku puzzles are also usually *guaranteed* to have at least one solution... But finding that solution could still be a good benchmark for your solver.

You might use a Sudoku-generator together with a reduction to SAT, or you might think about how to apply the techniques used in Sudoku generation to more directly generate Unique SAT instances. For the former, obviously your SAT instances will have some structure, but it's unclear to me whether it's more or less structure than e.g. planting a solution or using the witness isolation technique. Probably depends on your needs and your solver.

The one reference I know of here is: [Sudoku Puzzles Generating: from Easy to Evil](http://zhangroup.aporc.org/images/files/Paper_3485.pdf).

> **Comments:**
> **username_4**: "Sudoku puzzles are also usually guaranteed to have at least one solution...", I think soduko are usually guaranteed to have unique solution or no solution.  

---

### Answer by username_5 on 2013-09-17. Score: 5

I think a good test case would be to generate random uniquely satisfiable 3XOR instances (planted instances) with $\Theta(n)$ constraints and then convert them to 3SAT instances.

> **Comments:**
> **username_4**: how to generate random uniquely satisfiable 3XOR instances  
> **username_3**: You can solve 3XOR in cubic time by linear algebra. Each new constraint you add either keeps soln set the same or cuts it exactly in half. Start adding random constraints and for each you add, check that it's still solvable and how many solns it has.  

---

### Answer by username_6 on 2013-09-17. Score: 2

imho one of the best ways to create "presumably hard" SAT instances while controlling the number of solutions is from integer factoring instances/circuits encoded in binary. the code is not very complex, it uses mainly EE addition circuits, and does not lead to "large" SAT instances. the number of solutions is equal to the number of factors (including "permutations" of the factors). therefore prime numbers generate exactly two solutions, $(1,p),(p,1)$. a single solution can be guaranteed with a further "comparison" constraint that restricts the factors to $a

also with this approach is it is relatively easy to find numbers with *roughly* however many factors/solutions are desired. the ["smoother"](http://en.wikipedia.org/wiki/Smooth_number) the number, the more factors.

various researchers over the years have created this factoring SAT code (eg for DIMACS competition/arcihve which has stored some factoring instances in the past) but unfortunately there does not seem to be a publicly available version. see also the 1st link below for a ref where the code was written/implemented apparently for a graduate course.

* [Reducing the integer factorization problem to an NP-Complete problem](https://cs.stackexchange.com/questions/6588/reducing-the-integer-factorization-problem-to-an-np-complete-problem)
* [Generating 3SAT circuit for Integer factorization example](https://cs.stackexchange.com/questions/11718/generating-3sat-circuit-for-integer-factorization-example)

another empirical/iterative approach that may be useful for some, to create more "unstructured" instances: create random SAT instances $e\_n$ near the transition point (the region where the equation has a probability 50% between "solvable and unsolvable"), and then solve the equation. if it is unsolvable, throw away and restart. if it is solvable, add clauses that restrict the solution "not" to be the found solution, obtaining $e\_{n+1}$, and re-solve. repeat if necessary. when the equation $e\_{n+1}$ is no longer solvable, the $e\_n$ must have had a single/unique solution.

> **Comments:**
> **username_7**: I mentioned the factoring approach in my answer earlier, but I also explained why it might not be an ideal testbed: "However, these constructions do not seem like a useful way to benchmark or optimize your solver. They all have a special structure, and there is no reason to believe that this structure is representative of real-world problems. In particular, SAT instances drawn from cryptographic problems are known to be extremely hard, much harder than SAT instances drawn from many other real-world applications of SAT solvers, so they aren't a very good basis for benchmarking your solver."  
> **username_6**: so the above is a different pov that if one wants very hard instances, obviously a natural test case for any solver, then factoring is indeed a promising way to go. seriously doubt that you could find any published opinions that mirror yours. to repeat, factoring instances have been put in DIMACS challenge archives by serious researcher(s) starting many years ago. anyway, your contrary opinion is not even really expressed in a self-consistent way. cryptography is indeed a foremost/applied _real world problem_ even more so than many abstract/abstruse/academic problems used for SAT instances...  

---

### Answer by username_8 on 2013-09-15. Score: 1

You can easily generate directly Unique SAT formulas with reasonable size $(|F|

Let $m$ be the unique model - say $m$ contains only "0"s (rename the variables later if needed).  
Let $F$ a $k$-SAT formula satisfied only by $m$ - the maximum size of $F$ is the total number of clauses satisfied by $m$ i.e. $(2^k-1) \binom{n}{k}$.

Take the $\binom{k}{1}$ clauses that eliminate all models assigning exactly one "1" among $x\_1,x\_2 \ldots x\_k$:  
$(\lnot x\_1, x\_2 \ldots x\_k)(x\_1, \lnot x\_2 \ldots x\_k)\ldots (x\_1,x\_2 \ldots \lnot x\_k)$

Take the $\binom{k}{2}$ clauses that eliminate all models assigning exactly two "1" among $x\_1,x\_2 \ldots x\_k$:  
$(\lnot x\_1, \lnot x\_2, x\_3 \ldots x\_k)(\lnot x\_1,x\_2, \lnot x\_3 \ldots x\_k)\ldots (x\_1,x\_2 \ldots \lnot x\_{k-1} \lnot x\_k)$

Keep going until taking the only $\binom{k}{k}$ clause that eliminates all models assigning "1" to each variables among $x\_1,x\_2 \ldots x\_k$.

The only models which are not yet eliminated assign all $x\_1,x\_2 \ldots x\_k$ to "0". Since $m$ is a model, then take any set of $n-k$ clauses that eliminate all models assigning "1" to $x\_i (k
$(\lnot x\_{k+1}, x\_1 \ldots, x\_{k-1}) \ldots (\lnot x\_n, x\_1 \ldots x\_{k-1})$.

Then $|F|=\sum\_{i=1}^k \binom{k}{i} +n-k = 2^k-1+n-k$

To get more clauses, add any clause containing at least one negated variable. To get an unsatisfiable formula, just add a clause with $k$ unnegated variables.

> **Comments:**
> **username_9**: There is a problem in your answer : we have n variables and this means that and not k  

---

