mirror of
https://github.com/Noratrieb/monte-carlo-tree-search.git
synced 2026-01-14 07:15:07 +01:00
dfs and bfs
This commit is contained in:
commit
fdba35e4ac
3 changed files with 92 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
/target
|
||||
Cargo.lock
|
||||
.idea
|
||||
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "game_trees"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
81
src/lib.rs
Normal file
81
src/lib.rs
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
pub struct Node<T> {
|
||||
value: T,
|
||||
children: Vec<Node<T>>,
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! tree {
|
||||
($first:expr $(, $($rest:expr),*)?) => {
|
||||
$crate::Node {
|
||||
value: $first,
|
||||
children: vec![$($($rest),*)?],
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
mod search {
|
||||
use crate::Node;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
pub fn breadth_first_search<T: Eq>(tree: &Node<T>, searched: &T) -> bool {
|
||||
let mut candidates = VecDeque::new();
|
||||
candidates.push_back(tree);
|
||||
|
||||
loop {
|
||||
if let Some(candidate) = candidates.pop_front() {
|
||||
if candidate.value == *searched {
|
||||
return true;
|
||||
}
|
||||
|
||||
candidates.extend(candidate.children.iter());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
pub fn depth_first_search<T: Eq>(tree: &Node<T>, searched: &T) -> bool {
|
||||
if tree.value == *searched {
|
||||
return true;
|
||||
}
|
||||
|
||||
for child in &tree.children {
|
||||
if depth_first_search(&child, searched) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::search;
|
||||
|
||||
#[test]
|
||||
fn dfs() {
|
||||
let tree = tree!(1, tree!(6, tree!(5), tree!(4)), tree!(6));
|
||||
|
||||
let has_seven = search::depth_first_search(&tree, &7);
|
||||
let has_five = search::depth_first_search(&tree, &5);
|
||||
|
||||
assert!(!has_seven);
|
||||
assert!(has_five);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bfs() {
|
||||
let tree = tree!(1, tree!(6, tree!(5), tree!(4)), tree!(6));
|
||||
|
||||
let has_seven = search::breadth_first_search(&tree, &7);
|
||||
let has_five = search::breadth_first_search(&tree, &5);
|
||||
|
||||
assert!(!has_seven);
|
||||
assert!(has_five);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue