Struct nekolib::algo::ExactCover

source ·
pub struct ExactCover { /* private fields */ }
Expand description

Exact cover。

$n$ 行 $m$ 列の 0/1 行列 $A$ が与えられたとき、行 $\{0, 1, \dots, n-1\}$ の部分集合 $S$ であって、各列 $0\le j\lt m$ に対して $A_{i, j}=1$ なる $i\in S$ がちょうど 1 つ存在するものを探す。

$\gdef\I{\textcolor{red}{1}}$ $$ A = \begin{pmatrix} 0 & 0 & \I & 0 & \I & \I & 0 \\ 1 & 0 & 0 & 1 & 0 & 0 & 1 \\ 0 & 1 & 1 & 0 & 0 & 1 & 0 \\ \I & 0 & 0 & \I & 0 & 0 & 0 \\ 0 & \I & 0 & 0 & 0 & 0 & \I \\ 0 & 0 & 0 & 1 & 1 & 0 & 1 \\ \end{pmatrix} $$ のような行列 $A$ に対しては、$\{0, 3, 4\}$ が解となる。

Idea

Dancing links と呼ばれるデータ構造を用いる。

todo!()

Examples

use nekolib::algo::ExactCover;
let a = vec![                 
    vec![0, 0, 1, 0, 1, 1, 0],
    vec![1, 0, 0, 1, 0, 0, 1],
    vec![0, 1, 1, 0, 0, 1, 0],
    vec![1, 0, 0, 1, 0, 0, 0],
    vec![0, 1, 0, 0, 0, 0, 1],
    vec![0, 0, 0, 1, 1, 0, 1],
];
let ec = ExactCover::from_matrix(&a);
assert_eq!(ec.any(), Some(vec![3, 0, 4]));

References

Implementations§

source§

impl ExactCover

source

pub fn from_matrix(a: &Vec<Vec<usize>>) -> Self

与えられた行列に対して前計算を行う。

source

pub fn all(self) -> Vec<Vec<usize>>

解を全て探す。

source

pub fn any(self) -> Option<Vec<usize>>

解を探す。一つ見つかった時点で打ち切る。

Trait Implementations§

source§

impl Default for ExactCover

source§

fn default() -> ExactCover

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V