# Horn-satisfiability

In formal logic, Horn-satisfiability, or HORNSAT, is the problem of deciding whether a given conjunction of propositional Horn clauses is satisfiable or not. Horn-satisfiability and Horn clauses are named after Alfred Horn.
A Horn clause is a clause with at most one positive literal, called the head of the clause, and any number of negative literals, forming the body of the clause. A Horn formula is a propositional formula formed by conjunction of Horn clauses.
Horn satisfiability is actually one of the "hardest" or "most expressive" problems which is known to be computable in polynomial time, in the sense that it is a P-complete problem. The extension of the problem for quantified Horn formulae can be also solved in polynomial time.
The Horn satisfiability problem can also be asked for propositional many-valued logics. The algorithms are not usually linear, but some are polynomial; see Hähnle (2001 or 2003) for a survey.

## Algorithm

The problem of Horn satisfiability is solvable in linear time. A polynomial-time algorithm for Horn satisfiability is recursive:
* A first termination condition is a formula in which all the clauses currently existing contain negative literals. In this case, all the variables currently in the clauses can be set to false.
* A second termination condition is an empty clause. In this case, the formula has no solutions.
* In the other cases, the formula contains a positive unit clause $l$, so we do a unit propagation: the literal $l$ is set to true, all the clauses containing $l$ are removed, and all clauses containing $\neg l$ have this literal removed. The result is a new Horn formula, so we reiterate.
This algorithm also allows determining a truth assignment of satisfiable Horn formulae: all variables contained in a unit clause are set to the value satisfying that unit clause; all other literals are set to false. The resulting assignment is the minimal model of the Horn formula, that is, the assignment having a minimal set of variables assigned to true, where comparison is made using set containment.
Using a linear algorithm for unit propagation, the algorithm is linear in the size of the formula.

### Examples
#### Trivial case

In the Horn formula
    (¬a ∨ ¬b ∨ c) ∧
    (¬b ∨ ¬c ∨ d) ∧
    (¬f ∨ ¬a ∨ b) ∧
    (¬e ∨ ¬c ∨ a) ∧
    (¬e ∨ f) ∧
    (¬d ∨ e) ∧
    (¬b ∨ ¬c),
each clause has a negated literal. Therefore, setting each variable to false satisfies all clauses, hence it is a solution.

#### Solvable case

In the Horn formula
    (¬a ∨ ¬b ∨ c) ∧
    (¬b ∨ ¬c ∨ f) ∧
    (¬f ∨ b) ∧
    (¬e ∨ ¬c ∨ a) ∧
    (f) ∧
    (¬d ∨ e) ∧
    (¬b ∨ ¬c),
one clause forces f to be true. Setting f to true and simplifying gives
    (¬a ∨ ¬b ∨ c) ∧
    (b) ∧
    (¬e ∨ ¬c ∨ a) ∧
    (¬d ∨ e) ∧
    (¬b ∨ ¬c).
Now b must be true. Simplification gives
    (¬a ∨ c) ∧
    (¬e ∨ ¬c ∨ a) ∧
    (¬d ∨ e) ∧
    (¬c).
Now it is a trivial case, so the remaining variables can all be set to false. Thus, a satisfying assignment is
    a = false,
    b = true,
    c = false,
    d = false,
    e = false,
    f = true.

#### Unsolvable case

In the Horn formula
    (¬a ∨ ¬b ∨ c) ∧
    (¬b ∨ ¬c ∨ f) ∧
    (¬f ∨ b) ∧
    (¬e ∨ ¬c ∨ a) ∧
    (f) ∧
    (¬d ∨ e) ∧
    (¬b),
one clause forces f to be true. Subsequent simplification gives
    (¬a ∨ ¬b ∨ c) ∧
    (b) ∧
    (¬e ∨ ¬c ∨ a) ∧
    (¬d ∨ e) ∧
    (¬b).
Now b has to be true. Simplification gives
    (¬a ∨ c) ∧
    (¬e ∨ ¬c ∨ a) ∧
    (¬d ∨ e) ∧
We obtained an empty clause, hence the formula is unsatisfiable.

## Generalization

A generalization of the class of Horn formulae is that of renamable-Horn formulae, which is the set of formulae that can be placed in Horn form by replacing some variables with their respective negation. Checking the existence of such a replacement can be done in linear time; therefore, the satisfiability of such formulae is in P as it can be solved by first performing this replacement and then checking the satisfiability of the resulting Horn formula. Horn satisfiability and renamable Horn satisfiability provide one of two important subclasses of satisfiability that are solvable in polynomial time; the other such subclass is 2-satisfiability.

### Dual-Horn SAT

A dual variant of Horn SAT is Dual-Horn SAT, in which each clause has at most one negative literal. Negating all variables transforms an instance of Dual-Horn SAT into Horn SAT. It was proven in 1951 by Horn that Dual-Horn SAT is in P.