Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Two_Sat< GT > Class Template Reference

2-SAT solver using Tarjan's SCC algorithm. More...

#include <Two_Sat.H>

Collaboration diagram for Aleph::Two_Sat< GT >:
[legend]

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_Satoperator= (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 GTimplication_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
 

Detailed Description

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
class Aleph::Two_Sat< GT >

2-SAT solver using Tarjan's SCC algorithm.

Determines satisfiability of 2-CNF formulas in linear time.

Template Parameters
GTDirected graph type for the internal implication graph. Defaults to List_Digraph.

Definition at line 83 of file Two_Sat.H.

Member Typedef Documentation

◆ Arc

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
using Aleph::Two_Sat< GT >::Arc = typename GT::Arc

Definition at line 87 of file Two_Sat.H.

◆ Node

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
using Aleph::Two_Sat< GT >::Node = typename GT::Node

Definition at line 86 of file Two_Sat.H.

Constructor & Destructor Documentation

◆ Two_Sat() [1/2]

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
Aleph::Two_Sat< GT >::Two_Sat ( const Two_Sat< GT > &  )
delete

Deleted copy constructor to avoid dangling internal Node* pointers.

◆ Two_Sat() [2/2]

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
Aleph::Two_Sat< GT >::Two_Sat ( const size_t  n)
inline

Construct a solver for n variables.

Parameters
nNumber 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.

Member Function Documentation

◆ add_at_most_one()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
void Aleph::Two_Sat< GT >::add_at_most_one ( const Array< size_t > &  lits)
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.

Parameters
litsArray of literal indices.
Exceptions
std::domain_errorif 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().

◆ add_clause()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
void Aleph::Two_Sat< GT >::add_clause ( size_t  a,
size_t  b 
)
inline

◆ add_clause_signed()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
void Aleph::Two_Sat< GT >::add_clause_signed ( const long  a,
const long  b 
)
inline

Add a clause using signed 1-based variables.

Parameters
aFirst signed variable.
bSecond 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().

◆ add_equiv()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
void Aleph::Two_Sat< GT >::add_equiv ( const size_t  a,
const size_t  b 
)
inline

Add equivalence constraint: a <-> b.

Parameters
aFirst literal.
bSecond literal.

Definition at line 256 of file Two_Sat.H.

References Aleph::Two_Sat< GT >::add_implication().

◆ add_implication()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
void Aleph::Two_Sat< GT >::add_implication ( const size_t  a,
const size_t  b 
)
inline

Add an implication a -> b.

Parameters
aAntecedent literal.
bConsequent 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().

◆ add_xor()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
void Aleph::Two_Sat< GT >::add_xor ( const size_t  a,
const size_t  b 
)
inline

Add XOR constraint: exactly_one(a, b).

Parameters
aFirst literal.
bSecond literal.

Definition at line 246 of file Two_Sat.H.

References Aleph::Two_Sat< GT >::add_clause(), and Aleph::Two_Sat< GT >::negate_lit().

◆ implication_graph()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
const GT & Aleph::Two_Sat< GT >::implication_graph ( ) const
inlinenoexcept
Returns
Read-only reference to the internal implication graph.

Definition at line 181 of file Two_Sat.H.

References Aleph::Two_Sat< GT >::ig.

◆ is_pos_lit()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
static constexpr bool Aleph::Two_Sat< GT >::is_pos_lit ( const size_t  lit)
inlinestaticconstexprnoexcept

Check if a literal is positive.

Parameters
litLiteral index.
Returns
true if positive, false if negative.

Definition at line 132 of file Two_Sat.H.

References Aleph::divide_and_conquer_partition_dp().

◆ is_satisfiable()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
bool Aleph::Two_Sat< GT >::is_satisfiable ( )
inline

Check if the formula is satisfiable.

Returns
true if satisfiable.

Definition at line 301 of file Two_Sat.H.

References Aleph::Two_Sat< GT >::solve().

◆ lit_var()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
static constexpr size_t Aleph::Two_Sat< GT >::lit_var ( const size_t  lit)
inlinestaticconstexprnoexcept

Get the variable index of a literal.

Parameters
litLiteral index.
Returns
Variable 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().

◆ neg_lit()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
static constexpr size_t Aleph::Two_Sat< GT >::neg_lit ( const size_t  var)
inlinestaticconstexprnoexcept

Get the negative literal for a variable.

Parameters
varVariable index (0 to n-1).
Returns
Literal index (odd).

Definition at line 111 of file Two_Sat.H.

Referenced by Aleph::Two_Sat< GT >::signed_to_lit(), and Aleph::Two_Sat< GT >::solve().

◆ negate_lit()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
static constexpr size_t Aleph::Two_Sat< GT >::negate_lit ( const size_t  lit)
inlinestaticconstexprnoexcept

Negate a literal.

Parameters
litLiteral index.
Returns
The opposite 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().

◆ num_nodes()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
size_t Aleph::Two_Sat< GT >::num_nodes ( ) const
inlinenoexcept
Returns
Number of nodes in the implication graph (2*n).

Definition at line 178 of file Two_Sat.H.

References Aleph::Two_Sat< GT >::num_vars.

◆ operator=()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
Two_Sat & Aleph::Two_Sat< GT >::operator= ( const Two_Sat< GT > &  )
delete

Deleted copy assignment to avoid dangling internal Node* pointers.

◆ pos_lit()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
static constexpr size_t Aleph::Two_Sat< GT >::pos_lit ( const size_t  var)
inlinestaticconstexprnoexcept

Get the positive literal for a variable.

Parameters
varVariable index (0 to n-1).
Returns
Literal index (even).

Definition at line 104 of file Two_Sat.H.

Referenced by Aleph::Two_Sat< GT >::signed_to_lit(), and Aleph::Two_Sat< GT >::solve().

◆ set_false()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
void Aleph::Two_Sat< GT >::set_false ( const size_t  a)
inline

Force literal a to be false.

Parameters
aLiteral index.

Definition at line 237 of file Two_Sat.H.

References Aleph::Two_Sat< GT >::negate_lit(), and Aleph::Two_Sat< GT >::set_true().

◆ set_true()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
void Aleph::Two_Sat< GT >::set_true ( const size_t  a)
inline

Force literal a to be true.

Parameters
aLiteral 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().

◆ signed_to_lit()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
static size_t Aleph::Two_Sat< GT >::signed_to_lit ( const long  v)
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).

Parameters
vSigned variable index (must be non-zero and not LONG_MIN).
Returns
Literal index.

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().

◆ size()

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
size_t Aleph::Two_Sat< GT >::size ( ) const
inlinenoexcept
Returns
Number of variables.

Definition at line 175 of file Two_Sat.H.

References Aleph::Two_Sat< GT >::num_vars.

◆ solve()

Member Data Documentation

◆ ig

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
GT Aleph::Two_Sat< GT >::ig
private

◆ lit_nodes

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
Array<Node *> Aleph::Two_Sat< GT >::lit_nodes
private

◆ num_vars

template<class GT = List_Digraph<Graph_Node<Empty_Class>, Graph_Arc<Empty_Class>>>
size_t Aleph::Two_Sat< GT >::num_vars
private

The documentation for this class was generated from the following file: