#![feature(generators)]
#![feature(iter_from_generator)]
use std::iter::from_generator;
pub trait Hamiltonian
where
Self: Sized + Clone,
{
fn count(&self) -> usize;
fn current(&self) -> usize;
fn go_walk(&mut self, node: usize);
fn go_back(&mut self, steps: usize);
fn possible_moves(&self, start: usize) -> Vec<usize>;
}
pub fn backtracking<T: Hamiltonian>(graph: T) -> impl Iterator<Item = Vec<usize>> {
let mut stack: Vec<(Vec<usize>, usize)> = vec![(vec![graph.current()], graph.current())];
from_generator(move || {
while let Some((path, current)) = stack.pop() {
if path.len() == graph.count() {
yield path;
}
else {
for next in graph.possible_moves(current) {
let mut path = path.clone();
path.push(next);
stack.push((path, next));
}
}
}
})
}