81template <
class GT = List_Digraph<Graph_Node<Empty_Class>,
82 Graph_Arc<Empty_Class>>>
104 pos_lit(
const size_t var)
noexcept {
return 2 * var; }
111 neg_lit(
const size_t var)
noexcept {
return 2 * var + 1; }
144 <<
"Two_Sat::signed_to_lit: variable index 0 is invalid";
146 <<
"Two_Sat::signed_to_lit: variable index LONG_MIN is invalid";
149 return pos_lit(
static_cast<size_t>(v - 1));
170 for (
size_t i = 0; i < 2 * n; ++i)
191 <<
"Two_Sat::add_clause: literal " << a
192 <<
" refers to variable " <<
lit_var(a)
193 <<
" but only " <<
num_vars <<
" variables exist";
195 <<
"Two_Sat::add_clause: literal " << b
196 <<
" refers to variable " <<
lit_var(b)
197 <<
" but only " <<
num_vars <<
" variables exist";
210 <<
"Two_Sat::add_clause_signed: variable index 0 is invalid";
212 b == std::numeric_limits<long>::min())
213 <<
"Two_Sat::add_clause_signed: variable index LONG_MIN is invalid";
277 for (
size_t i = 0; i <
lits.size(); ++i)
281 <<
"Two_Sat::add_at_most_one: literal " <<
lit
283 <<
" but only " <<
num_vars <<
" variables exist";
293 for (
size_t i = 0; i < u.
size(); ++i)
294 for (
size_t j = i + 1; j < u.
size(); ++j)
303 return solve().first;
336 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.
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 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.
Main namespace for Aleph-w library functions.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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.