83 cout <<
"==========================================================\n";
84 cout <<
"Example 1: Basic Boolean Formula\n";
85 cout <<
"==========================================================\n\n";
101 <<
" (x0 OR x1) AND (~x0 OR x2) AND (~x1 OR ~x2) AND (x0 OR x2)\n\n";
103 auto [ok, assignment] = sat.
solve();
107 cout <<
"SATISFIABLE. Assignment:\n";
108 for (
size_t i = 0; i < assignment.size(); ++i)
109 cout <<
" x" << i <<
" = " << (assignment(i) ?
"true" :
"false")
113 cout <<
"UNSATISFIABLE\n";
124 cout <<
"==========================================================\n";
125 cout <<
"Example 2: Signed 1-based Variable API\n";
126 cout <<
"==========================================================\n\n";
140 cout <<
"Formula (same as Example 1, using signed API):\n"
141 <<
" (+1 OR +2) AND (-1 OR +3) AND (-2 OR -3) AND (+1 OR +3)\n\n";
143 auto [ok, assignment] = sat.
solve();
147 cout <<
"SATISFIABLE. Assignment:\n";
148 for (
size_t i = 0; i < assignment.size(); ++i)
149 cout <<
" x" << i <<
" = " << (assignment(i) ?
"true" :
"false")
153 cout <<
"UNSATISFIABLE\n";
164 cout <<
"==========================================================\n";
165 cout <<
"Example 3: Graph 2-Coloring via 2-SAT\n";
166 cout <<
"==========================================================\n\n";
175 cout <<
"Path graph 0 - 1 - 2 - 3:\n";
181 auto [ok, assignment] = sat.
solve();
182 cout <<
" 2-colorable? " << (ok ?
"YES" :
"NO") <<
"\n";
185 cout <<
" Coloring:";
186 for (
size_t i = 0; i < 4; ++i)
187 cout <<
" node" << i <<
"=" << (assignment(i) ?
"A" :
"B");
195 cout <<
"Triangle 0 - 1 - 2 - 0:\n";
201 auto [ok, _] = sat.
solve();
202 cout <<
" 2-colorable? " << (ok ?
"YES" :
"NO") <<
"\n\n";
207 cout <<
"Square 0 - 1 - 2 - 3 - 0:\n";
214 auto [ok, assignment] = sat.
solve();
215 cout <<
" 2-colorable? " << (ok ?
"YES" :
"NO") <<
"\n";
218 cout <<
" Coloring:";
219 for (
size_t i = 0; i < 4; ++i)
220 cout <<
" node" << i <<
"=" << (assignment(i) ?
"A" :
"B");
233 cout <<
"==========================================================\n";
234 cout <<
"Example 4: Team Assignment with Constraints\n";
235 cout <<
"==========================================================\n\n";
245 const char * names[] = {
"Alice",
"Bob",
"Charlie",
"Diana",
"Eve"};
254 cout <<
"Constraints:\n"
255 <<
" Alice and Bob: same team\n"
256 <<
" Charlie and Diana: not both on team A\n"
257 <<
" Eve: must be on team A\n"
258 <<
" Eve and Bob: different teams\n\n";
260 auto [ok, assignment] = sat.
solve();
264 cout <<
"SATISFIABLE. Assignment:\n";
265 for (
size_t i = 0; i < 5; ++i)
266 cout <<
" " << names[i] <<
" -> team "
267 << (assignment(i) ?
"A" :
"B") <<
"\n";
270 cout <<
"UNSATISFIABLE: constraints cannot be met\n";
281 cout <<
"==========================================================\n";
282 cout <<
"Example 5: Different Graph Backends\n";
283 cout <<
"==========================================================\n\n";
288 auto run = [](
const string & name,
auto & sat) {
289 sat.add_xor(sat.pos_lit(0), sat.pos_lit(1));
291 auto [ok, assignment] = sat.solve();
292 cout <<
" " << name <<
": ";
294 cout <<
"x0=" << assignment(0) <<
" x1=" << assignment(1) <<
"\n";
302 run(
"List_Digraph ", sat);
310 run(
"List_SDigraph", sat);
318 run(
"Array_Digraph", sat);
2-SAT solver based on Tarjan's SCC algorithm.
Generic directed graph (digraph) wrapper template.
Arc for graphs implemented with simple adjacency lists.
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.
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.
void set_true(const size_t a)
Force literal a to be true.
static constexpr size_t neg_lit(const size_t var) noexcept
Get the negative literal for a variable.
void add_equiv(const size_t a, const size_t b)
Add equivalence constraint: a <-> b.
void add_xor(const size_t a, const size_t b)
Add XOR constraint: exactly_one(a, b).
void add_clause(size_t a, size_t b)
Add a clause (a OR b).
Main namespace for Aleph-w library functions.
Array-based graph implementation.
Simple graph implementation with adjacency lists.
void example_team_assignment()
void example_graph_backends()
void example_2_coloring()
void example_basic_formula()
void example_signed_api()