Safe Haskell | None |
---|---|
Language | GHC2021 |
Solves 2-SAT.
For variables \(x_0, x_1, \cdots, x_{N - 1}\) and clauses with form
- \((x_i = f) \lor (x_j = g)\)
it decides whether there is a truth assignment that satisfies all clauses.
Example
>>>
import AtCoder.TwoSat qualified as TS
>>>
import Data.Bit (Bit(..))
>>>
ts <- TS.new 1
>>>
TS.addClause ts 0 False 0 False -- x_0 == False || x_0 == False
>>>
TS.satisfiable ts
True>>>
TS.answer ts
[0]
Since: 1.0.0
Synopsis
- data TwoSat s
- new :: PrimMonad m => Int -> m (TwoSat (PrimState m))
- addClause :: (HasCallStack, PrimMonad m) => TwoSat (PrimState m) -> Int -> Bool -> Int -> Bool -> m ()
- satisfiable :: PrimMonad m => TwoSat (PrimState m) -> m Bool
- answer :: PrimMonad m => TwoSat (PrimState m) -> m (Vector Bit)
- unsafeAnswer :: PrimMonad m => TwoSat (PrimState m) -> m (Vector Bit)
TwoSat
Constructor
new :: PrimMonad m => Int -> m (TwoSat (PrimState m)) #
Creates a 2-SAT of \(n\) variables and \(0\) clauses.
Constraints
- \(0 \leq n\)
Complexity
- \(O(n)\)
Since: 1.0.0
Clause building
addClause :: (HasCallStack, PrimMonad m) => TwoSat (PrimState m) -> Int -> Bool -> Int -> Bool -> m () #
Adds a clause \((x_i = f) \lor (x_j = g)\).
Constraints
- \(0 \leq i \lt n\)
- \(0 \leq j \lt n\)
Complexity
- \(O(1)\) amortized.
Since: 1.0.0
Solvers
answer :: PrimMonad m => TwoSat (PrimState m) -> m (Vector Bit) #
Returns a truth assignment that satisfies all clauses of the last call of satisfiable
. If we
call it before calling satisfiable
or when the last call of satisfiable
returns False
, it
returns the vector of length \(n\) with undefined elements.
Complexity
- \(O(n)\)
Since: 1.0.0