pub struct TwoSat { /* private fields */ }
Expand description
2-SAT。
の形の論理式を にするような の割り当てがあるか判定する。 ただし、各々の は のうちのいずれかである。
Examples
use nekolib::math::TwoSat;
let mut ts = TwoSat::new(4);
ts.add_clause(1, 1);
ts.add_clause(-2, -2);
ts.add_clause(3, -4);
let w = ts.witness().unwrap();
assert!(w[1]);
assert!(!w[2]);
assert!(w[3] || !w[4]);
ts.add_clause(-1, -1);
assert_eq!(ts.witness(), None);
Implementations§
source§impl TwoSat
impl TwoSat
sourcepub fn new(n: usize) -> Self
pub fn new(n: usize) -> Self
で初期化する。
Examples
use nekolib::math::TwoSat;
let ts = TwoSat::new(1);
assert!(ts.witness().is_some());
sourcepub fn add_clause(&mut self, i: isize, j: isize)
pub fn add_clause(&mut self, i: isize, j: isize)
で更新する。
ただし、 のとき は を指すとする。
Examples
use nekolib::math::TwoSat;
let mut ts = TwoSat::new(3);
ts.add_clause(-1, 2);
ts.add_clause(1, 3);
sourcepub fn witness(&self) -> Option<Vec<bool>>
pub fn witness(&self) -> Option<Vec<bool>>
充足可能性を判定し、可能なら解を返す。
Examples
use nekolib::math::TwoSat;
let mut ts = TwoSat::new(3);
ts.add_clause(-1, 2);
ts.add_clause(-2, 3);
ts.add_clause(-3, -2);
let w = ts.witness().unwrap();
assert!(!w[1] || w[2]);
assert!(!w[2] || w[3]);
assert!(!w[3] || !w[2]);
// w == [_, false, false, true], for example.
ts.add_clause(2, 1);
assert_eq!(ts.witness(), None);
Auto Trait Implementations§
impl RefUnwindSafe for TwoSat
impl Send for TwoSat
impl Sync for TwoSat
impl Unpin for TwoSat
impl UnwindSafe for TwoSat
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more