|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
2-SAT solver using Tarjan's SCC algorithm. More...
#include <Two_Sat.H>
Public Types | |
| using | Node = typename GT::Node |
| using | Arc = typename GT::Arc |
Public Member Functions | |
| Two_Sat (const Two_Sat &)=delete | |
| Deleted copy constructor to avoid dangling internal Node* pointers. | |
| Two_Sat & | operator= (const Two_Sat &)=delete |
| Deleted copy assignment to avoid dangling internal Node* pointers. | |
| Two_Sat (const size_t n) | |
Construct a solver for n variables. | |
| size_t | size () const noexcept |
| size_t | num_nodes () const noexcept |
| const GT & | implication_graph () const noexcept |
| void | add_clause (size_t a, size_t b) |
Add a clause (a OR b). | |
| void | add_clause_signed (const long a, const long b) |
| Add a clause using signed 1-based variables. | |
| void | add_implication (const size_t a, const size_t b) |
Add an implication a -> b. | |
| void | set_true (const size_t a) |
Force literal a to be true. | |
| void | set_false (const size_t a) |
Force literal a to be false. | |
| void | add_xor (const size_t a, const size_t b) |
Add XOR constraint: exactly_one(a, b). | |
| void | add_equiv (const size_t a, const size_t b) |
Add equivalence constraint: a <-> b. | |
| void | add_at_most_one (const Array< size_t > &lits) |
| Add at-most-one constraint over a set of literals. | |
| bool | is_satisfiable () |
| Check if the formula is satisfiable. | |
| std::pair< bool, Array< bool > > | solve () |
| Solve the 2-SAT instance. | |
Static Public Member Functions | |
Literal helpers | |
| static constexpr size_t | pos_lit (const size_t var) noexcept |
| Get the positive literal for a variable. | |
| static constexpr size_t | neg_lit (const size_t var) noexcept |
| Get the negative literal for a variable. | |
| static constexpr size_t | negate_lit (const size_t lit) noexcept |
| Negate a literal. | |
| static constexpr size_t | lit_var (const size_t lit) noexcept |
| Get the variable index of a literal. | |
| static constexpr bool | is_pos_lit (const size_t lit) noexcept |
| Check if a literal is positive. | |
| static size_t | signed_to_lit (const long v) |
| Convert a signed 1-based variable to a literal index. | |
Private Attributes | |
| size_t | num_vars |
| GT | ig |
| Array< Node * > | lit_nodes |
2-SAT solver using Tarjan's SCC algorithm.
Determines satisfiability of 2-CNF formulas in linear time.
| GT | Directed graph type for the internal implication graph. Defaults to List_Digraph. |
Deleted copy constructor to avoid dangling internal Node* pointers.
|
inline |
Construct a solver for n variables.
| n | Number of Boolean variables. |
Definition at line 167 of file Two_Sat.H.
References Aleph::Two_Sat< GT >::ig, Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), and Aleph::Two_Sat< GT >::lit_nodes.
|
inline |
Add at-most-one constraint over a set of literals.
Ensures that at most one of the given literals is true. This adds O(k^2) clauses where k is the number of distinct literals.
Singletons are validated but produce no clauses. Duplicate literals are normalized. Pairs of identical literals are skipped to avoid adding redundant forcing clauses.
| lits | Array of literal indices. |
| std::domain_error | if any literal is out of range. |
Definition at line 274 of file Two_Sat.H.
References Aleph::Two_Sat< GT >::add_clause(), ah_domain_error_if, Aleph::divide_and_conquer_partition_dp(), FunctionalMethods< Container, T >::for_each(), Aleph::Two_Sat< GT >::lit_var(), Aleph::Two_Sat< GT >::negate_lit(), Aleph::Two_Sat< GT >::num_vars, Aleph::Array< T >::size(), and Aleph::Array< T >::to_array().
|
inline |
Add a clause (a OR b).
| a | First literal index. |
| b | Second literal index. |
| std::domain_error | if literals are out of range. |
Definition at line 188 of file Two_Sat.H.
References ah_domain_error_if, Aleph::Two_Sat< GT >::ig, Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::Two_Sat< GT >::lit_nodes, Aleph::Two_Sat< GT >::lit_var(), Aleph::Two_Sat< GT >::negate_lit(), and Aleph::Two_Sat< GT >::num_vars.
Referenced by Aleph::Two_Sat< GT >::add_at_most_one(), Aleph::Two_Sat< GT >::add_clause_signed(), Aleph::Two_Sat< GT >::add_implication(), Aleph::Two_Sat< GT >::add_xor(), and Aleph::Two_Sat< GT >::set_true().
Add a clause using signed 1-based variables.
| a | First signed variable. |
| b | Second signed variable. |
Definition at line 207 of file Two_Sat.H.
References Aleph::Two_Sat< GT >::add_clause(), ah_domain_error_if, Aleph::divide_and_conquer_partition_dp(), and Aleph::Two_Sat< GT >::signed_to_lit().
|
inline |
Add equivalence constraint: a <-> b.
| a | First literal. |
| b | Second literal. |
Definition at line 256 of file Two_Sat.H.
References Aleph::Two_Sat< GT >::add_implication().
|
inline |
Add an implication a -> b.
| a | Antecedent literal. |
| b | Consequent literal. |
Definition at line 221 of file Two_Sat.H.
References Aleph::Two_Sat< GT >::add_clause(), and Aleph::Two_Sat< GT >::negate_lit().
Referenced by Aleph::Two_Sat< GT >::add_equiv().
|
inline |
Add XOR constraint: exactly_one(a, b).
| a | First literal. |
| b | Second literal. |
Definition at line 246 of file Two_Sat.H.
References Aleph::Two_Sat< GT >::add_clause(), and Aleph::Two_Sat< GT >::negate_lit().
|
inlinenoexcept |
Definition at line 181 of file Two_Sat.H.
References Aleph::Two_Sat< GT >::ig.
|
inlinestaticconstexprnoexcept |
Check if a literal is positive.
| lit | Literal index. |
Definition at line 132 of file Two_Sat.H.
References Aleph::divide_and_conquer_partition_dp().
|
inline |
Check if the formula is satisfiable.
Definition at line 301 of file Two_Sat.H.
References Aleph::Two_Sat< GT >::solve().
|
inlinestaticconstexprnoexcept |
Get the variable index of a literal.
| lit | Literal index. |
Definition at line 125 of file Two_Sat.H.
References Aleph::divide_and_conquer_partition_dp().
Referenced by Aleph::Two_Sat< GT >::add_at_most_one(), and Aleph::Two_Sat< GT >::add_clause().
|
inlinestaticconstexprnoexcept |
Get the negative literal for a variable.
| var | Variable index (0 to n-1). |
Definition at line 111 of file Two_Sat.H.
Referenced by Aleph::Two_Sat< GT >::signed_to_lit(), and Aleph::Two_Sat< GT >::solve().
|
inlinestaticconstexprnoexcept |
Negate a literal.
| lit | Literal index. |
Definition at line 118 of file Two_Sat.H.
References Aleph::divide_and_conquer_partition_dp().
Referenced by Aleph::Two_Sat< GT >::add_at_most_one(), Aleph::Two_Sat< GT >::add_clause(), Aleph::Two_Sat< GT >::add_implication(), Aleph::Two_Sat< GT >::add_xor(), and Aleph::Two_Sat< GT >::set_false().
|
inlinenoexcept |
Definition at line 178 of file Two_Sat.H.
References Aleph::Two_Sat< GT >::num_vars.
|
delete |
Deleted copy assignment to avoid dangling internal Node* pointers.
|
inlinestaticconstexprnoexcept |
Get the positive literal for a variable.
| var | Variable index (0 to n-1). |
Definition at line 104 of file Two_Sat.H.
Referenced by Aleph::Two_Sat< GT >::signed_to_lit(), and Aleph::Two_Sat< GT >::solve().
|
inline |
Force literal a to be false.
| a | Literal index. |
Definition at line 237 of file Two_Sat.H.
References Aleph::Two_Sat< GT >::negate_lit(), and Aleph::Two_Sat< GT >::set_true().
|
inline |
Force literal a to be true.
| a | Literal index. |
Definition at line 229 of file Two_Sat.H.
References Aleph::Two_Sat< GT >::add_clause().
Referenced by Aleph::Two_Sat< GT >::set_false().
|
inlinestatic |
Convert a signed 1-based variable to a literal index.
Maps +k to pos_lit(k-1) and -k to neg_lit(k-1).
| v | Signed variable index (must be non-zero and not LONG_MIN). |
Definition at line 141 of file Two_Sat.H.
References ah_domain_error_if, Aleph::divide_and_conquer_partition_dp(), Aleph::Two_Sat< GT >::neg_lit(), and Aleph::Two_Sat< GT >::pos_lit().
Referenced by Aleph::Two_Sat< GT >::add_clause_signed().
|
inlinenoexcept |
Definition at line 175 of file Two_Sat.H.
References Aleph::Two_Sat< GT >::num_vars.
|
inline |
Solve the 2-SAT instance.
Definition at line 309 of file Two_Sat.H.
References Aleph::Tarjan_Connected_Components< GT, Itor, SA >::connected_components(), Aleph::Array< T >::create(), Aleph::divide_and_conquer_partition_dp(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Digraph_Iterator< GT, Filter >::has_curr(), Aleph::Two_Sat< GT >::ig, Aleph::Two_Sat< GT >::lit_nodes, Aleph::Two_Sat< GT >::neg_lit(), NODE_COUNTER, Aleph::Two_Sat< GT >::num_vars, Aleph::Two_Sat< GT >::pos_lit(), and Aleph::Array< T >::size().
Referenced by Aleph::Two_Sat< GT >::is_satisfiable().
|
private |
Definition at line 91 of file Two_Sat.H.
Referenced by Aleph::Two_Sat< GT >::Two_Sat(), Aleph::Two_Sat< GT >::add_clause(), Aleph::Two_Sat< GT >::implication_graph(), and Aleph::Two_Sat< GT >::solve().
|
private |
Definition at line 92 of file Two_Sat.H.
Referenced by Aleph::Two_Sat< GT >::Two_Sat(), Aleph::Two_Sat< GT >::add_clause(), and Aleph::Two_Sat< GT >::solve().
|
private |
Definition at line 90 of file Two_Sat.H.
Referenced by Aleph::Two_Sat< GT >::add_at_most_one(), Aleph::Two_Sat< GT >::add_clause(), Aleph::Two_Sat< GT >::num_nodes(), Aleph::Two_Sat< GT >::size(), and Aleph::Two_Sat< GT >::solve().