how to minimize this logic (or-gate CNF)? - algorithm

I'm writing down the truth table for the OR-gate in a circuit satisfiability problem (this has to do with reduction of the 3-satisfiability problem).
I have:
a b c c = (a OR b)
1 1 1 1
1 1 0 0
1 0 1 1
1 0 0 0
0 1 1 1
0 1 0 0
0 0 1 0
0 0 0 1
So if I take the rows with 0 in the column c = (a OR b), and negate the a,b,c then i get the four clauses:
!a AND !b AND c
a AND !b AND !c
a AND !b and c
a AND b AND !c
I'm trying to minimize these clauses. I know that the right answer is:
c OR !a
c OR !b
c OR !a or !b
How do I minimize those four clauses? is there a program online? I used wolfram, but it didn't output the right answer, so if anyone has a second to help that would be amazing

It is easy to see that the last column is almost the same as c column except two last rows.
And for the two last rows it have values from column c swapped. So we can write as:
if (a ∨ b) then c else ¬c
if c then t else f can be represented as CNF (c ∨ t) ∧ (¬c ∨ f) so the formula above will look like:
(a ∨ b ∨ ¬c) ∧ (¬(a ∨ b) ∨ c)
= (a ∨ b ∨ ¬c) ∧ ((¬a ∧ ¬b) ∨ c)
= (a ∨ b ∨ ¬c) ∧ (¬a ∨ c) ∧ (¬b ∨ c)
And the last one is exactly what you want.

Related

How to reduce k-independent set problem to 3-SAT

So I got this homework question and we are asked to reduce a k-independent set satisfiability problem to a 3-SAT set of clauses under the conjunctive normal form.
So for G(V, E) we have verticies set V = {x1, x2, x3, x4, x5, x6} and edges set E = { e1 = (x1,x3), e2 = (x1,x5), e3 = (x1,x6), e4 = (x2,x5), e5 = (x2,x6), e6 = (x3,x4), e7 = (x3,x5), e8 = (x5,x6) }
My first approach to this is to have a clause per edge as we can't have an edge between two vertex in the independent set :
e1: (¬x1 v ¬x3)
e2: (¬x1 v ¬x5)
e3: (¬x1 v ¬x6)
e4: (¬x2 v ¬x5)
e5: (¬x2 v ¬x6)
e6: (¬x3 v ¬x4)
e7: (¬x3 v ¬x5)
e8: (¬x5 v ¬x6)
But the problem is, for k = 3 for example, how to write clauses to ensure that at least 3 different variables (xi) are set to true ?
This is achievable using Weighted-2-satisfiability, but seems hard to achieve just using good old 3-SAT.
Any hints to how to proceed ?
If it's this G and k = 3 that you care about, it's probably easiest to write clauses (xi ∨ xj ∨ xk ∨ xℓ) for all {i, j, k, ℓ} ⊆ V and then reduce them to 3-CNF, e.g., (x ∨ y ∨ z ∨ w) becomes (v ∨ x ∨ y) ∧ (¬v ∨ z ∨ w), where v is a new variable.
In general, you're going to want to
Define a Boolean circuit to compute x1 + … + xn ≥ k (you can evaluate x1 + … + xn − k in two's complement arithmetic using ripple-carry adders and then invert the sign bit).
Translate this circuit into a 3-CNF formula. First, replace gates with more than two inputs with several two-input gates. Then for each node in the circuit create a variable. For each gate write four clauses constraining the output, one for each possible input, e.g., if there's an AND gate with inputs x and y and output z, then write clauses (x ∨ y ∨ ¬z) ∧ (x ∨ ¬y ∧ ¬z) ∧ (¬x ∨ y ∨ ¬z) ∧ (¬x ∨ ¬y ∨ z). A XOR gate would be (x ∨ y ∨ ¬z) ∧ (x ∨ ¬y ∧ z) ∧ (¬x ∨ y ∨ z) ∧ (¬x ∨ ¬y ∨ ¬z).

Expression Tree of logical expression

I need to transform this: (a < b)∧(b < c)∨(c < d)
expression into an expression tree, but I can't figure out a way that looks correct. Here is what I got:
I'm predicting this will be a long answer, because I will go through the general thinking process for achieving a solution. For the inpatient, the solution is at the end.
Is your solution correct?
Well, it depends. Your picture would be totally correct if ∨ took precedence over ∧, in which case you would need to apply ∧ only after (b < c) ∨ (c < d) has a result. It would also be correct if you forced precedence using parenthesis like so:
(a < b) ∧ ( (b < c) ∨ (c < d) )
That said, when talking about operator precedence, typically ∧/and has precedence over ∨/or. When precedence is the same, the evaluation happens from left to right, meaning the right side depends on the result of the left side.
The highest the precedence of an operator, the lower it appears in the tree.
The rest of the answer will assume the usual operator precedence.
How to approach this problem?
The best approach to this sort of problems is to decompose the expression. Even better, if we decompose using the prefix/polish notation, it will be more natural to build up the tree later.
Given: (a < b) ∧ (b < c) ∨ (c < d)
Let's decompose it into parts:
x = (a < b), which translates to prefix: < a b
y = (b < c), which translates to prefix: < b c
z = (c < d), which translates to prefix: < c d
We now have 3 inequality expressions decomposed as x, y and z.
Now to the logical operators.
i = x ∧ y, which translates to prefix: ∧ x y
j = i ∨ z, which translates to prefix: ∨ i z
We now have 2 logical expressions decomposed as i and j. Note how they depend on x, y, z. But also, that j depends on i. Dependencies are important because you know tree leaves have no dependencies.
How to build the tree?
To sum up, this is what we decomposed from the original expression:
x = < a b
y = < b c
z = < c d
i = ∧ x y
j = ∨ i z
Let's approach it bottom-up.
Considering the dependencies, the leaves are obviously the most independent elements: a, b, c and d.
Let's build the bottom of the tree considering all appearances of these independent elements in the decomposition we just made (b and c appear twice, we put them twice).
a b b c c d
Now let's build x, y and z which depends only on a, b, c and d. I'll be using / and \ for building my ASCII art as equivalent to your picture lines.
x y z
< < <
/ \ / \ / \
a b b c c d
Now as we've seen, i depends only on x and y. So we can put it there now. We can't add j just yet, because we need i to be there first.
i
∧
/ \
x y z
< < <
/ \ / \ / \
a b b c c d
Now we are just missing j, which depends on i and z.
j
∨
/ \
/ \
i \
∧ \
/ \ \
x y z
< < <
/ \ / \ / \
a b b c c d
And we have a full expression tree. As you see, each dependency level will result in a tree level.
To be completely accurate, a Breadth-First Search in this tree would have to consider that z is on the same level of i, so the correct representation of the tree would have to put z one level higher:
j
∨
/ \
/ \
i z
∧ <
/ \ / \
x y c d
< <
/ \ / \
a b b c
Just one more note, for it to be completely clear, for the expression (a < b) ∧ ( (b < c) ∨ (c < d) ), the decomposition result would be:
x = < a b
y = < b c
z = < c d
j = ∨ y z
i = ∧ x j
Which would, in turn, result in the tree in your picture.
Hope this helps your future endeavours on building expression trees.

Boolean formula for a graph colouring algorithm

I have a graph which has 5 vertexes.
Graph g1 = new Graph(5);
g1.addEdge(0, 1);
g1.addEdge(0, 2);
g1.addEdge(1, 2);
g1.addEdge(1, 3);
g1.addEdge(3, 2);
g1.addEdge(3, 4);
g1.addEdge(4, 2);
System.out.println("Coloring of graph 1");
g1.greedyColoring();
I need to express the problem of coloring this graph as a boolean expression.
Suppose a, b, and c are three colours, and the literal ai means vertex i has color a. Then the above graph could be colored like this:
a0, b1, c2, a3, b4
How can I get a boolean formula that, when satisfied, provides a solution for coloring this graph?
You are looking for coloring all vertices in a graph, each with one out of three available colors, such that no two adjacent nodes have the same color.
There are thus three types of conditions:
1. Two adjacent nodes cannot have the same color
So for instance, the edge (0, 1) will imply these three constraints:
Node 0 and 1 cannot both have color a
Node 0 and 1 cannot both have color b
Node 0 and 1 cannot both have color c
Translated into a boolean expression:
¬a0 ∨ ¬a1
¬b0 ∨ ¬b1
¬c0 ∨ ¬c1
You would need to generate such triplets of disjunctions for all edges. So in total you'll have 3 x 7 = 21 boolean disjunctions:
¬a0 ∨ ¬a1 ¬a0 ∨ ¬a2 ¬a1 ∨ ¬a2 ¬a1 ∨ ¬a3 ¬a3 ∨ ¬a2 ¬a3 ∨ ¬a4 ¬a4 ∨ ¬a2
¬b0 ∨ ¬b1 ¬b0 ∨ ¬b2 ¬b1 ∨ ¬b2 ¬b1 ∨ ¬b3 ¬b3 ∨ ¬b2 ¬b3 ∨ ¬b4 ¬b4 ∨ ¬b2
¬c0 ∨ ¬c1 ¬c0 ∨ ¬c2 ¬c1 ∨ ¬c2 ¬c1 ∨ ¬c3 ¬c3 ∨ ¬c2 ¬c3 ∨ ¬c4 ¬c4 ∨ ¬c2
2. All nodes must get a color
So for instance, for node 0 we will have this constraint:
Node 0 must have color a, b, or c
Translated into a boolean expression:
a0 ∨ b0 ∨ c0
You would need to do the same for all nodes, so in total you'll have 5 such expressions:
a0 ∨ b0 ∨ c0
a1 ∨ b1 ∨ c1
a2 ∨ b2 ∨ c2
a3 ∨ b3 ∨ c3
a4 ∨ b4 ∨ c4
3. No nodes can get more than one color
So for instance, for node 0 we will have:
Node 0 cannot have both color a and b
Node 0 cannot have both color a and c
Node 0 cannot have both color b and c
Translated into a boolean expression:
¬a0 ∨ ¬b0
¬a0 ∨ ¬c0
¬b0 ∨ ¬c0
You would need to do the same for all nodes, so in total you'll have 3 * 5 = 15 such expressions for this type:
¬a0 ∨ ¬b0 ¬a1 ∨ ¬b1 ¬a2 ∨ ¬b2 ¬a3 ∨ ¬b3 ¬a4 ∨ ¬b4
¬a0 ∨ ¬c0 ¬a1 ∨ ¬c1 ¬a2 ∨ ¬c2 ¬a3 ∨ ¬c3 ¬a4 ∨ ¬c4
¬b0 ∨ ¬c0 ¬b1 ∨ ¬c1 ¬b2 ∨ ¬c2 ¬b3 ∨ ¬c3 ¬b4 ∨ ¬c4
Conclusion
All the above disjunctions (there are 21 + 5 + 15 = 41 of them) need to be true (conjugated). Such a problem is a Boolean satisfiability problem, and more particularly 3-SAT, and is an NP-Complete problem.
Code to produce the boolean expressions
The following code assumes that the Graph object will exposes a nodes list where each node has an id and neighbors.
The disjunctions are output as strings, each on a separate line:
Graph g1 = new Graph(5);
g1.addEdge(0, 1);
g1.addEdge(0, 2);
g1.addEdge(1, 2);
g1.addEdge(1, 3);
g1.addEdge(3, 2);
g1.addEdge(3, 4);
g1.addEdge(4, 2);
char colors[] = {'a', 'b', 'c'};
// Type 1
for (Node node : g1.nodes) {
for (Node neighbor : node.neighbors) {
for (char color : colors) {
System.out.println(String.format("¬%1$c%2$d ∨ ¬%1$c%3$d", color, node.id, neighbor.id));
}
}
}
// Type 2
for (Node node : g1.nodes) {
String expr[] = new String[colors.length];
int i = 0;
for (char color : colors) {
expr[i++] = String.format("%s%d", color, node.id);
}
System.out.println(String.join(" ∨ ", expr));
}
// Type 3
for (Node node : g1.nodes) {
for (char color1 : colors) {
for (char color2 : colors) {
if (color1 < color2) {
System.out.println(String.format("¬%1$c%3$d ∨ ¬%2$c%3$d", color1, color2, node.id));
}
}
}
}
See it run on repl.it.
You get an equation for each edge and connect them with and (ci is the color used for vertex i):
c0 != c1 && c0 != c2 && c1 != c2 && c1 != c3 && c2 != c3 && c2 != c4 && c3 != c4
The boolean formula only checks if you have found a valid coloring for the graph, not if the number of colors is minimized.

How many possible assignments does a a CNF sentence have?

I'm having some trouble understanding the following:
When we look at satisfiability problems in conjunctive normal form, an underconstrained problem is one with relatively few clauses constraining the variables. For eg. here is a randomly generated 3-CNF sentence with five symbols and five clauses.
(Each clause contains 3 randomly selected distinct symbols, each of which is negated with 50% probability.)
(¬D ∨ ¬B ∨ C) ∧ (B ∨ ¬A ∨ ¬C) ∧ (¬C ∨ ¬B ∨ E) ∧ (E ∨ ¬D ∨ B) ∧ (B ∨ E ∨ ¬C)
16 of the 32 possible assignments are models of this sentence, so, on an average, it would take just 2 random guesses to find the model.
I don't understand the last line- saying that there are 32 possible assignments. How is it 32? And how are only 16 of them models of the sentence? Sorry, but i'm finding this concept a bit confusing. Thanks.
There are 2^5=32 possible assignments of the two values true and false to 5 variables:
1: 00000
2: 00001
3: 00010
...
31: 11110
32: 11111
16 of those assignments satisfy (I didn't check) the given formula, thus are models of it.

Proving the Associativity of OR

I need help proving the following:
(a ∨ b) ∨ c = a ∨ (b ∨ c)
I don't want the answer... just a hint that will help me understand the process of proving this.
Thank you.
Why not just prove it by doing all possible values of a, b and c = True, False? -- there are only 2^3 = 8 different cases.
Here's a start, for a=T, b=F, c=T
(a v b) v c = a ∨ (b ∨ c)
(T v F) v T = T v (F v T)
T v T = T v T
T = T
(However, this isn't really a programming question...)
What is your axiom set?
Not knowing the set, you could build a truth table

Resources