83template <AlephGraph GT = List_Digraph<Graph_Node<Empty_Class>,
84 Graph_Arc<Empty_Class>>>
106 pos_lit(
const size_t var)
noexcept {
return 2 * var; }
113 neg_lit(
const size_t var)
noexcept {
return 2 * var + 1; }
146 <<
"Two_Sat::signed_to_lit: variable index 0 is invalid";
148 <<
"Two_Sat::signed_to_lit: variable index LONG_MIN is invalid";
151 return pos_lit(
static_cast<size_t>(v - 1));
172 for (
size_t i = 0; i < 2 * n; ++i)
193 <<
"Two_Sat::add_clause: literal " << a
194 <<
" refers to variable " <<
lit_var(a)
195 <<
" but only " <<
num_vars <<
" variables exist";
197 <<
"Two_Sat::add_clause: literal " << b
198 <<
" refers to variable " <<
lit_var(b)
199 <<
" but only " <<
num_vars <<
" variables exist";
212 <<
"Two_Sat::add_clause_signed: variable index 0 is invalid";
214 b == std::numeric_limits<long>::min())
215 <<
"Two_Sat::add_clause_signed: variable index LONG_MIN is invalid";
279 for (
size_t i = 0; i <
lits.size(); ++i)
283 <<
"Two_Sat::add_at_most_one: literal " <<
lit
285 <<
" but only " <<
num_vars <<
" variables exist";
295 for (
size_t i = 0; i < u.
size(); ++i)
296 for (
size_t j = i + 1; j < u.
size(); ++j)
305 return solve().first;
338 for (
size_t i = 0; i <
num_vars; ++i)
Tarjan's algorithm for strongly connected components.
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
Simple dynamic array with automatic resizing and functional operations.
static Array create(size_t n)
Create an array with n logical elements.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Array to_array() const
Copy to Aleph::Array (requires copyable elements).
bool has_curr() const noexcept
Return true the iterator has an current arc.
Dynamic set backed by balanced binary search trees with automatic memory management.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Graph_Node< Node_Info > Node
The graph type.
Graph_Arc< Arc_Info > Arc
The node class type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Filtered iterator for outcoming arcs of a node.
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
void connected_components(const GT &g, DynList< GT > &blk_list, DynList< typename GT::Arc * > &arc_list)
Computes the strongly connected components (SCCs) of a digraph.
2-SAT solver using Tarjan's SCC algorithm.
void add_clause_signed(const long a, const long b)
Add a clause using signed 1-based variables.
Two_Sat(const size_t n)
Construct a solver for n variables.
static constexpr size_t pos_lit(const size_t var) noexcept
Get the positive literal for a variable.
std::pair< bool, Array< bool > > solve()
Solve the 2-SAT instance.
Two_Sat(const Two_Sat &)=delete
Deleted copy constructor to avoid dangling internal Node* pointers.
static size_t signed_to_lit(const long v)
Convert a signed 1-based variable to a literal index.
void set_true(const size_t a)
Force literal a to be true.
void add_implication(const size_t a, const size_t b)
Add an implication a -> b.
static constexpr bool is_pos_lit(const size_t lit) noexcept
Check if a literal is positive.
void set_false(const size_t a)
Force literal a to be false.
const GT & implication_graph() const noexcept
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.
void add_at_most_one(const Array< size_t > &lits)
Add at-most-one constraint over a set of literals.
Two_Sat & operator=(const Two_Sat &)=delete
Deleted copy assignment to avoid dangling internal Node* pointers.
static constexpr size_t lit_var(const size_t lit) noexcept
Get the variable index of a literal.
void add_equiv(const size_t a, const size_t b)
Add equivalence constraint: a <-> b.
Array< Node * > lit_nodes
void add_xor(const size_t a, const size_t b)
Add XOR constraint: exactly_one(a, b).
size_t num_nodes() const noexcept
bool is_satisfiable()
Check if the formula is satisfiable.
void add_clause(size_t a, size_t b)
Add a clause (a OR b).
size_t size() const noexcept
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
#define NODE_COUNTER(p)
Get the counter of a node.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Main namespace for Aleph-w library functions.
Dynamic array container with automatic resizing.
Lazy and scalable dynamic array implementation.
Dynamic set implementations based on balanced binary search trees.
Generic graph and digraph implementations.