Refactor graph and misc
This commit is contained in:
parent
5f5fe0c149
commit
8f163269bd
22 changed files with 126 additions and 45 deletions
7
factorio-graph/Cargo.toml
Normal file
7
factorio-graph/Cargo.toml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "factorio-graph"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
tracing = "0.1.41"
|
||||
2
factorio-graph/src/lib.rs
Normal file
2
factorio-graph/src/lib.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod priority_queue;
|
||||
pub mod wheighted_graph;
|
||||
231
factorio-graph/src/priority_queue/binary_heap.rs
Normal file
231
factorio-graph/src/priority_queue/binary_heap.rs
Normal file
|
|
@ -0,0 +1,231 @@
|
|||
use std::{collections::HashMap, fmt::Debug};
|
||||
|
||||
use super::PriorityQueue;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BinaryHeap<Item> {
|
||||
nextfree: usize,
|
||||
data: Vec<BinaryHeapEntry<Item>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct BinaryHeapEntry<Item> {
|
||||
id: usize,
|
||||
item: Item,
|
||||
}
|
||||
|
||||
impl<Item> BinaryHeap<Item>
|
||||
where
|
||||
Item: PartialOrd,
|
||||
{
|
||||
fn downheap(&mut self, index: usize) {
|
||||
let left = 2 * index + 1;
|
||||
let right = 2 * index + 2;
|
||||
|
||||
if right < self.data.len() {
|
||||
let smaller = if self.data[left].item < self.data[right].item {
|
||||
left
|
||||
} else {
|
||||
right
|
||||
};
|
||||
if self.data[index].item > self.data[smaller].item {
|
||||
self.data.swap(index, smaller);
|
||||
self.downheap(smaller);
|
||||
}
|
||||
} else if left < self.data.len() && self.data[index].item > self.data[left].item {
|
||||
self.data.swap(index, left);
|
||||
self.downheap(left);
|
||||
}
|
||||
}
|
||||
|
||||
fn upheap(&mut self, index: usize) {
|
||||
if index > 0 {
|
||||
let parent = (index - 1) / 2;
|
||||
if self.data[parent].item > self.data[index].item {
|
||||
self.data.swap(parent, index);
|
||||
self.upheap(parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn search(&self, id: usize) -> Option<usize> {
|
||||
for (i, d) in self.data.iter().enumerate() {
|
||||
if d.id == id {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<Item> PriorityQueue<Item> for BinaryHeap<Item>
|
||||
where
|
||||
Item: PartialOrd + Clone,
|
||||
{
|
||||
type Handle = usize;
|
||||
|
||||
fn insert(&mut self, item: Item) -> Self::Handle {
|
||||
self.data.push(BinaryHeapEntry {
|
||||
id: self.nextfree,
|
||||
item: item.clone(),
|
||||
});
|
||||
self.upheap(self.data.len() - 1);
|
||||
self.nextfree += 1;
|
||||
self.nextfree - 1
|
||||
}
|
||||
|
||||
fn pop_min(&mut self) -> Option<Item> {
|
||||
if self.data.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let d = self.data.swap_remove(0);
|
||||
self.downheap(0);
|
||||
Some(d.item)
|
||||
}
|
||||
}
|
||||
|
||||
fn decrease_key(&mut self, handle: &Self::Handle, f: impl Fn(&mut Item)) {
|
||||
if let Some(index) = self.search(*handle) {
|
||||
f(&mut self.data[index].item);
|
||||
self.upheap(index);
|
||||
}
|
||||
}
|
||||
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
data: Vec::new(),
|
||||
nextfree: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Trace<P> {
|
||||
inner: P,
|
||||
}
|
||||
|
||||
impl<P, I> PriorityQueue<I> for Trace<P>
|
||||
where
|
||||
I: PartialOrd + Clone + Debug,
|
||||
P: PriorityQueue<I>,
|
||||
{
|
||||
type Handle = P::Handle;
|
||||
|
||||
fn new() -> Self {
|
||||
println!("New priority queue.");
|
||||
Self { inner: P::new() }
|
||||
}
|
||||
|
||||
fn insert(&mut self, item: I) -> Self::Handle {
|
||||
println!("Insert: {item:?}");
|
||||
self.inner.insert(item)
|
||||
}
|
||||
|
||||
fn pop_min(&mut self) -> Option<I> {
|
||||
let min = self.inner.pop_min();
|
||||
println!("Pop min: {min:?}");
|
||||
min
|
||||
}
|
||||
|
||||
fn decrease_key(&mut self, handle: &Self::Handle, f: impl Fn(&mut I)) {
|
||||
self.inner.decrease_key(handle, |i| {
|
||||
let old_i = i.clone();
|
||||
f(i);
|
||||
println!("Decrease key: {old_i:?} -> {i:?}");
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FastBinaryHeap<Item> {
|
||||
nextfree: usize,
|
||||
data: Vec<BinaryHeapEntry<Item>>,
|
||||
map: HashMap<usize, usize>,
|
||||
}
|
||||
|
||||
impl<Item> FastBinaryHeap<Item>
|
||||
where
|
||||
Item: PartialOrd,
|
||||
{
|
||||
fn downheap(&mut self, index: usize) {
|
||||
let left = 2 * index + 1;
|
||||
let right = 2 * index + 2;
|
||||
|
||||
if right < self.data.len() {
|
||||
let smaller = if self.data[left].item < self.data[right].item {
|
||||
left
|
||||
} else {
|
||||
right
|
||||
};
|
||||
if self.data[index].item > self.data[smaller].item {
|
||||
self.data.swap(index, smaller);
|
||||
self.downheap(smaller);
|
||||
}
|
||||
} else if left < self.data.len() && self.data[index].item > self.data[left].item {
|
||||
self.data.swap(index, left);
|
||||
self.downheap(left);
|
||||
}
|
||||
self.map.insert(self.data[index].id, index);
|
||||
}
|
||||
|
||||
fn upheap(&mut self, index: usize) {
|
||||
if index > 0 {
|
||||
let parent = (index - 1) / 2;
|
||||
if self.data[parent].item > self.data[index].item {
|
||||
self.data.swap(parent, index);
|
||||
self.upheap(parent);
|
||||
}
|
||||
}
|
||||
self.map.insert(self.data[index].id, index);
|
||||
}
|
||||
|
||||
fn search(&self, id: usize) -> Option<usize> {
|
||||
self.map.get(&id).copied()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Item> PriorityQueue<Item> for FastBinaryHeap<Item>
|
||||
where
|
||||
Item: PartialOrd + Clone,
|
||||
{
|
||||
type Handle = usize;
|
||||
|
||||
fn insert(&mut self, item: Item) -> Self::Handle {
|
||||
self.map.insert(self.nextfree, self.data.len());
|
||||
self.data.push(BinaryHeapEntry {
|
||||
id: self.nextfree,
|
||||
item: item.clone(),
|
||||
});
|
||||
self.upheap(self.data.len() - 1);
|
||||
self.nextfree += 1;
|
||||
self.nextfree - 1
|
||||
}
|
||||
|
||||
fn pop_min(&mut self) -> Option<Item> {
|
||||
if self.data.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let d = self.data.swap_remove(0);
|
||||
self.map.remove(&d.id);
|
||||
if !self.data.is_empty() {
|
||||
self.downheap(0);
|
||||
}
|
||||
Some(d.item)
|
||||
}
|
||||
}
|
||||
|
||||
fn decrease_key(&mut self, handle: &Self::Handle, f: impl Fn(&mut Item)) {
|
||||
if let Some(index) = self.search(*handle) {
|
||||
f(&mut self.data[index].item);
|
||||
self.upheap(index);
|
||||
}
|
||||
}
|
||||
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
data: Vec::new(),
|
||||
map: HashMap::new(),
|
||||
nextfree: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
278
factorio-graph/src/priority_queue/fibonacci_heap.rs
Normal file
278
factorio-graph/src/priority_queue/fibonacci_heap.rs
Normal file
|
|
@ -0,0 +1,278 @@
|
|||
use std::fmt::Debug;
|
||||
|
||||
use super::PriorityQueue;
|
||||
|
||||
type Index = u32;
|
||||
|
||||
pub struct FibonacciHeap<I> {
|
||||
min: Option<Index>,
|
||||
data: Vec<Option<Node<I>>>,
|
||||
free: Vec<Index>,
|
||||
temp: [Option<Index>; 64],
|
||||
}
|
||||
|
||||
impl<I: std::fmt::Debug> std::fmt::Debug for FibonacciHeap<I> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
struct PrintData<'a, I>(&'a Vec<Option<Node<I>>>);
|
||||
impl<I: Debug> Debug for PrintData<'_, I> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_map()
|
||||
.entries(self.0.iter().enumerate().filter_map(|d| {
|
||||
if let (i, Some(c)) = d {
|
||||
Some((i, c))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}))
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
f.debug_struct("FibonacciHeap")
|
||||
.field("min", &self.min)
|
||||
.field("data", &PrintData(&self.data))
|
||||
.field("free", &self.free)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FibonacciHeapHandle(Index);
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Node<I> {
|
||||
item: I,
|
||||
parent: Index,
|
||||
left: Index,
|
||||
right: Index,
|
||||
child: Index,
|
||||
mark: bool,
|
||||
rank: u16,
|
||||
}
|
||||
|
||||
impl<I> Node<I> {
|
||||
fn new(item: I) -> Self {
|
||||
Self {
|
||||
item,
|
||||
parent: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
child: 0,
|
||||
mark: false,
|
||||
rank: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> FibonacciHeap<I>
|
||||
where
|
||||
I: PartialOrd,
|
||||
{
|
||||
fn add_to_list(&mut self, list: Index, i: Index) {
|
||||
let right = self.get(list).right;
|
||||
let n = self.get_mut(i);
|
||||
n.right = right;
|
||||
n.left = list;
|
||||
self.get_mut(right).left = i;
|
||||
self.get_mut(list).right = i;
|
||||
}
|
||||
|
||||
fn remove_from_list(&mut self, i: Index) -> Option<Index> {
|
||||
let left = self.get(i).left;
|
||||
if left == i {
|
||||
return None;
|
||||
}
|
||||
|
||||
let right = self.get(i).right;
|
||||
|
||||
self.get_mut(left).right = right;
|
||||
self.get_mut(right).left = left;
|
||||
|
||||
Some(right)
|
||||
}
|
||||
|
||||
fn insert_tree(&mut self, i: Index) {
|
||||
if let Some(min) = self.min {
|
||||
self.add_to_list(min, i);
|
||||
|
||||
self.get_mut(i).parent = i;
|
||||
|
||||
if self.get(i).item < self.get(min).item {
|
||||
self.min = Some(i);
|
||||
}
|
||||
} else {
|
||||
let m = self.get_mut(i);
|
||||
m.parent = i;
|
||||
m.left = i;
|
||||
m.right = i;
|
||||
self.min = Some(i);
|
||||
}
|
||||
}
|
||||
|
||||
fn union(&mut self, a: Index, b: Index) -> Index {
|
||||
let min;
|
||||
let max;
|
||||
|
||||
if self.get(a).item < self.get(b).item {
|
||||
min = a;
|
||||
max = b;
|
||||
} else {
|
||||
min = b;
|
||||
max = a;
|
||||
}
|
||||
|
||||
if self.get(min).rank == 0 {
|
||||
self.get_mut(min).child = max;
|
||||
self.get_mut(min).rank = 1;
|
||||
let m = self.get_mut(max);
|
||||
m.parent = min;
|
||||
m.left = max;
|
||||
m.right = max;
|
||||
} else {
|
||||
self.get_mut(max).parent = min;
|
||||
self.add_to_list(self.get(min).child, max);
|
||||
self.get_mut(min).rank += 1;
|
||||
}
|
||||
|
||||
min
|
||||
}
|
||||
fn insert_temp(&mut self, i: Index) {
|
||||
let rank = self.get(i).rank;
|
||||
if let Some(t) = self.temp[rank as usize].take() {
|
||||
let m = self.union(t, i);
|
||||
|
||||
self.insert_temp(m);
|
||||
} else {
|
||||
self.temp[rank as usize] = Some(i);
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_data(&mut self, data: I) -> Index {
|
||||
if let Some(i) = self.free.pop() {
|
||||
self.data[i as usize] = Some(Node::new(data));
|
||||
i
|
||||
} else {
|
||||
let i = self.data.len() as Index;
|
||||
self.data.push(Some(Node::new(data)));
|
||||
i
|
||||
}
|
||||
}
|
||||
|
||||
fn get(&self, i: Index) -> &Node<I> {
|
||||
self.data.get(i as usize).unwrap().as_ref().unwrap()
|
||||
}
|
||||
|
||||
fn get_mut(&mut self, i: Index) -> &mut Node<I> {
|
||||
self.data.get_mut(i as usize).unwrap().as_mut().unwrap()
|
||||
}
|
||||
|
||||
fn remove_data(&mut self, i: Index) -> I {
|
||||
let n = self.data[i as usize].take().unwrap();
|
||||
|
||||
self.free.push(i);
|
||||
|
||||
n.item
|
||||
}
|
||||
|
||||
fn cut(&mut self, i: Index) {
|
||||
if self.get(i).item < self.get(self.min.unwrap()).item {
|
||||
self.min = Some(i);
|
||||
}
|
||||
let parent = self.get(i).parent;
|
||||
if parent == i {
|
||||
return;
|
||||
}
|
||||
let c = self.remove_from_list(i);
|
||||
if self.get(parent).rank > 1 {
|
||||
self.get_mut(parent).child = c.unwrap();
|
||||
}
|
||||
self.get_mut(parent).rank -= 1;
|
||||
|
||||
self.add_to_list(self.min.unwrap(), i);
|
||||
self.get_mut(i).mark = false;
|
||||
self.get_mut(i).parent = i;
|
||||
|
||||
if self.get(parent).mark {
|
||||
self.cut(parent);
|
||||
} else {
|
||||
self.get_mut(parent).mark = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> PriorityQueue<I> for FibonacciHeap<I>
|
||||
where
|
||||
I: PartialOrd,
|
||||
{
|
||||
type Handle = FibonacciHeapHandle;
|
||||
|
||||
fn new() -> Self {
|
||||
FibonacciHeap {
|
||||
min: None,
|
||||
data: Vec::new(),
|
||||
free: Vec::new(),
|
||||
temp: [None; 64],
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(&mut self, item: I) -> Self::Handle {
|
||||
let i = self.insert_data(item);
|
||||
|
||||
self.insert_tree(i);
|
||||
|
||||
FibonacciHeapHandle(i)
|
||||
}
|
||||
|
||||
fn pop_min(&mut self) -> Option<I> {
|
||||
if let Some(min) = self.min.take() {
|
||||
if self.get(min).rank != 0 {
|
||||
let mut c = self.get(min).child;
|
||||
loop {
|
||||
let t = c;
|
||||
c = self.get(c).right;
|
||||
self.add_to_list(min, t);
|
||||
self.get_mut(t).parent = t;
|
||||
self.get_mut(t).mark = false;
|
||||
if c == self.get(min).child {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(m) = self.remove_from_list(min) {
|
||||
let mut c = m;
|
||||
loop {
|
||||
let t = c;
|
||||
c = self.get(c).right;
|
||||
self.insert_temp(t);
|
||||
if c == m {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for i in 0..self.temp.len() {
|
||||
if let Some(i) = self.temp[i].take() {
|
||||
if let Some(min) = self.min {
|
||||
self.add_to_list(min, i);
|
||||
if self.get(i).item < self.get(min).item {
|
||||
self.min = Some(i);
|
||||
}
|
||||
} else {
|
||||
self.get_mut(i).left = i;
|
||||
self.get_mut(i).right = i;
|
||||
self.min = Some(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(self.remove_data(min))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn decrease_key(&mut self, handle: &Self::Handle, f: impl Fn(&mut I)) {
|
||||
let h = self.get_mut(handle.0);
|
||||
f(&mut h.item);
|
||||
self.cut(handle.0);
|
||||
}
|
||||
}
|
||||
98
factorio-graph/src/priority_queue/mod.rs
Normal file
98
factorio-graph/src/priority_queue/mod.rs
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
pub mod binary_heap;
|
||||
pub mod fibonacci_heap;
|
||||
|
||||
pub trait PriorityQueue<Item>
|
||||
where
|
||||
Item: PartialOrd,
|
||||
{
|
||||
type Handle;
|
||||
fn new() -> Self;
|
||||
// fn with_capacity() -> Self {
|
||||
// Self::new()
|
||||
// }
|
||||
|
||||
fn insert(&mut self, item: Item) -> Self::Handle;
|
||||
fn pop_min(&mut self) -> Option<Item>;
|
||||
fn decrease_key(&mut self, handle: &Self::Handle, f: impl Fn(&mut Item));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::binary_heap::{BinaryHeap, FastBinaryHeap};
|
||||
use super::fibonacci_heap::FibonacciHeap;
|
||||
use super::PriorityQueue;
|
||||
|
||||
macro_rules! test_generics {
|
||||
($($fun_mul:ident )+ ; $gen:ident $($gen_mul:ident)+) => {
|
||||
test_generics!(@internal_mod $($fun_mul )*; $gen);
|
||||
test_generics!($($fun_mul )*; $($gen_mul )*);
|
||||
};
|
||||
($($fun_mul:ident )+ ;$gen:ident) => {
|
||||
test_generics!(@internal_mod $($fun_mul )*; $gen);
|
||||
};
|
||||
(@internal_mod $($fun_mul:ident )+; $gen:ident) => {
|
||||
#[allow(non_snake_case)]
|
||||
mod $gen {
|
||||
test_generics!(@internal $($fun_mul )*; $gen);
|
||||
}
|
||||
};
|
||||
(@internal $fun:ident $($fun_mul:ident )+; $gen:ident) => {
|
||||
test_generics!(@internal $fun; $gen);
|
||||
test_generics!(@internal $($fun_mul )*; $gen);
|
||||
};
|
||||
(@internal $fun:ident; $gen:ident) => {
|
||||
|
||||
#[test]
|
||||
fn $fun() {
|
||||
super::super::$fun::<super::super::$gen<usize>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod test_macro {
|
||||
test_generics!(basic_generic decrease_key_generic; BinaryHeap FastBinaryHeap FibonacciHeap);
|
||||
}
|
||||
|
||||
fn basic_generic<T: PriorityQueue<usize>>() {
|
||||
let mut q = T::new();
|
||||
|
||||
q.insert(2);
|
||||
q.insert(3);
|
||||
q.insert(10);
|
||||
q.insert(12);
|
||||
q.insert(9);
|
||||
q.insert(6);
|
||||
q.insert(4);
|
||||
q.insert(5);
|
||||
q.insert(8);
|
||||
q.insert(7);
|
||||
q.insert(11);
|
||||
q.insert(1);
|
||||
|
||||
assert_eq!(q.pop_min(), Some(1));
|
||||
assert_eq!(q.pop_min(), Some(2));
|
||||
assert_eq!(q.pop_min(), Some(3));
|
||||
assert_eq!(q.pop_min(), Some(4));
|
||||
assert_eq!(q.pop_min(), Some(5));
|
||||
assert_eq!(q.pop_min(), Some(6));
|
||||
assert_eq!(q.pop_min(), Some(7));
|
||||
assert_eq!(q.pop_min(), Some(8));
|
||||
assert_eq!(q.pop_min(), Some(9));
|
||||
assert_eq!(q.pop_min(), Some(10));
|
||||
assert_eq!(q.pop_min(), Some(11));
|
||||
assert_eq!(q.pop_min(), Some(12));
|
||||
assert_eq!(q.pop_min(), None);
|
||||
}
|
||||
|
||||
fn decrease_key_generic<T: PriorityQueue<usize>>() {
|
||||
let mut q = T::new();
|
||||
|
||||
q.insert(4);
|
||||
let h = q.insert(5);
|
||||
|
||||
q.decrease_key(&h, |i| *i -= 3);
|
||||
|
||||
assert_eq!(q.pop_min(), Some(2));
|
||||
assert_eq!(q.pop_min(), Some(4));
|
||||
}
|
||||
}
|
||||
27
factorio-graph/src/wheighted_graph/adjacency_list.rs
Normal file
27
factorio-graph/src/wheighted_graph/adjacency_list.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
use super::WheightedGraph;
|
||||
|
||||
pub struct WheightedAdjacencyList {
|
||||
pub(crate) nodes: Vec<usize>,
|
||||
pub(crate) edges: Vec<(usize, f64)>,
|
||||
}
|
||||
|
||||
impl WheightedGraph for WheightedAdjacencyList {
|
||||
type Node = usize;
|
||||
|
||||
fn num_edges(&self, node: &Self::Node) -> usize {
|
||||
assert!(*node < self.nodes.len());
|
||||
if *node == self.nodes.len() - 1 {
|
||||
self.nodes.len() - self.nodes[0]
|
||||
} else {
|
||||
self.nodes[node + 1] - self.nodes[*node]
|
||||
}
|
||||
}
|
||||
|
||||
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> {
|
||||
if num < self.num_edges(node) {
|
||||
Some(self.edges[self.nodes[*node] + num])
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
50
factorio-graph/src/wheighted_graph/mod.rs
Normal file
50
factorio-graph/src/wheighted_graph/mod.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
pub mod adjacency_list;
|
||||
pub mod shortest_path;
|
||||
pub mod steiner_tree;
|
||||
|
||||
pub trait WheightedGraph: Sized {
|
||||
type Node;
|
||||
fn num_edges(&self, node: &Self::Node) -> usize {
|
||||
self.edge_iter(node).count()
|
||||
}
|
||||
|
||||
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)>;
|
||||
|
||||
fn edge_iter<'a, 'b>(&'a self, node: &'b Self::Node) -> WheightedGraphEdgeIter<'a, 'b, Self> {
|
||||
WheightedGraphEdgeIter::new(self, node)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WheightedGraphEdgeIter<'a, 'b, G>
|
||||
where
|
||||
G: WheightedGraph,
|
||||
{
|
||||
graph: &'a G,
|
||||
node: &'b G::Node,
|
||||
count: usize,
|
||||
}
|
||||
|
||||
impl<'a, 'b, G> WheightedGraphEdgeIter<'a, 'b, G>
|
||||
where
|
||||
G: WheightedGraph,
|
||||
{
|
||||
pub fn new(graph: &'a G, node: &'b G::Node) -> Self {
|
||||
Self {
|
||||
graph,
|
||||
node,
|
||||
count: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<G> Iterator for WheightedGraphEdgeIter<'_, '_, G>
|
||||
where
|
||||
G: WheightedGraph,
|
||||
{
|
||||
type Item = (G::Node, f64);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.count += 1;
|
||||
self.graph.edge(self.node, self.count - 1)
|
||||
}
|
||||
}
|
||||
277
factorio-graph/src/wheighted_graph/shortest_path.rs
Normal file
277
factorio-graph/src/wheighted_graph/shortest_path.rs
Normal file
|
|
@ -0,0 +1,277 @@
|
|||
use std::{collections::HashMap, fmt::Debug, hash::Hash, hash::Hasher};
|
||||
|
||||
use tracing::{field::Empty, trace_span};
|
||||
|
||||
use crate::priority_queue::PriorityQueue;
|
||||
|
||||
use super::WheightedGraph;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct QueueObject<N> {
|
||||
node: N,
|
||||
score: f64,
|
||||
}
|
||||
|
||||
impl<N> QueueObject<N> {
|
||||
fn new(node: N, score: f64) -> Self {
|
||||
Self { node, score }
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> PartialOrd for QueueObject<N> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
self.score.partial_cmp(&other.score)
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> PartialEq for QueueObject<N> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.score == other.score
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> Hash for QueueObject<N>
|
||||
where
|
||||
N: Hash,
|
||||
{
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.node.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct MapObject<N, H> {
|
||||
parent: N,
|
||||
score: f64,
|
||||
key: Option<H>,
|
||||
}
|
||||
|
||||
impl<N, H> MapObject<N, H> {
|
||||
fn new(parent: N, score: f64, key: H) -> Self {
|
||||
Self {
|
||||
parent,
|
||||
score,
|
||||
key: Some(key),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dijkstra<G, P, E>(graph: &G, start: G::Node, end: E) -> Option<Vec<G::Node>>
|
||||
where
|
||||
P: PriorityQueue<QueueObject<G::Node>> + Debug,
|
||||
P::Handle: Debug,
|
||||
G::Node: Eq + Hash + Clone + Debug,
|
||||
G: WheightedGraph,
|
||||
E: Fn(&'_ G::Node) -> bool,
|
||||
{
|
||||
let span = trace_span!("graph", seen_nodes = Empty, visited_nodes = Empty).entered();
|
||||
if end(&start) {
|
||||
return Some(vec![start]);
|
||||
}
|
||||
|
||||
let mut map: HashMap<G::Node, MapObject<G::Node, P::Handle>> = HashMap::new();
|
||||
|
||||
let mut q = P::new();
|
||||
q.insert(QueueObject::new(start.clone(), 0.0));
|
||||
|
||||
let mut visited_nodes: usize = 1;
|
||||
|
||||
let mut end_node = None;
|
||||
while let Some(o) = q.pop_min() {
|
||||
visited_nodes += 1;
|
||||
if let Some(m) = map.get_mut(&o.node) {
|
||||
m.key = None;
|
||||
}
|
||||
|
||||
if end(&o.node) {
|
||||
end_node = Some(o.node);
|
||||
break;
|
||||
}
|
||||
|
||||
for (node, wheight) in graph.edge_iter(&o.node) {
|
||||
let score = o.score + wheight;
|
||||
if let Some(n) = map.get_mut(&node) {
|
||||
if let Some(h) = &n.key {
|
||||
if score < n.score {
|
||||
n.parent = o.node.clone();
|
||||
n.score = score;
|
||||
q.decrease_key(h, |i| i.score = score);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let h = q.insert(QueueObject::new(node.clone(), o.score + wheight));
|
||||
map.insert(node, MapObject::new(o.node.clone(), o.score + wheight, h));
|
||||
}
|
||||
}
|
||||
// dbg!(&q);
|
||||
}
|
||||
|
||||
span.record("seen_nodes", map.len());
|
||||
span.record("visited_nodes", visited_nodes);
|
||||
|
||||
if let Some(end_node) = end_node {
|
||||
map.get(&end_node)?;
|
||||
|
||||
let mut result = vec![end_node];
|
||||
|
||||
// dbg!(&map);
|
||||
|
||||
loop {
|
||||
let parent = map.get(result.last().expect("last")).expect("get");
|
||||
result.push(parent.parent.clone());
|
||||
if parent.parent == start {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
result.reverse();
|
||||
|
||||
Some(result)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
struct GraphWrapper<'a, G, F> {
|
||||
g: &'a G,
|
||||
f: F,
|
||||
}
|
||||
|
||||
impl<G: WheightedGraph, F: Fn(&G::Node) -> f64> WheightedGraph for GraphWrapper<'_, G, F> {
|
||||
type Node = G::Node;
|
||||
|
||||
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> {
|
||||
self.g.edge(node, num).map(|(n, s)| {
|
||||
let s = s - (self.f)(node) + (self.f)(&n);
|
||||
(n, s)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn a_star<G, P, E, F>(graph: &G, start: G::Node, end: E, dist: F) -> Option<Vec<G::Node>>
|
||||
where
|
||||
P: PriorityQueue<QueueObject<G::Node>> + Debug,
|
||||
P::Handle: Debug,
|
||||
G::Node: Eq + Hash + Clone + Debug,
|
||||
G: WheightedGraph,
|
||||
E: Fn(&'_ G::Node) -> bool,
|
||||
F: Fn(&G::Node) -> f64,
|
||||
{
|
||||
let g = GraphWrapper { g: graph, f: dist };
|
||||
|
||||
dijkstra::<GraphWrapper<G, F>, P, E>(&g, start, end)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
||||
use crate::{
|
||||
priority_queue::{binary_heap::BinaryHeap, fibonacci_heap::FibonacciHeap},
|
||||
wheighted_graph::{
|
||||
WheightedGraph, adjacency_list::WheightedAdjacencyList, shortest_path::dijkstra,
|
||||
},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn trivial() {
|
||||
let a = WheightedAdjacencyList {
|
||||
nodes: vec![0, 1],
|
||||
edges: vec![(1, 1.0)],
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
dijkstra::<WheightedAdjacencyList, BinaryHeap<_>, _>(&a, 0, |&n| n == 1),
|
||||
Some(vec![0, 1])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple() {
|
||||
let a = WheightedAdjacencyList {
|
||||
nodes: vec![0, 2, 3, 5, 5, 7, 10],
|
||||
edges: vec![
|
||||
(1, 2.0),
|
||||
(4, 10.0),
|
||||
(2, 3.0),
|
||||
(3, 2.0),
|
||||
(5, 1.0),
|
||||
(0, 4.0),
|
||||
(2, 5.0),
|
||||
(2, 9.0),
|
||||
(3, 8.0),
|
||||
(4, 0.0),
|
||||
(5, 7.0),
|
||||
],
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
dijkstra::<WheightedAdjacencyList, BinaryHeap<_>, _>(&a, 0, |&n| n == 4),
|
||||
Some(vec![0, 1, 2, 5, 4])
|
||||
);
|
||||
assert_eq!(
|
||||
dijkstra::<WheightedAdjacencyList, BinaryHeap<_>, _>(&a, 0, |&n| n == 6),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
dijkstra::<WheightedAdjacencyList, FibonacciHeap<_>, _>(&a, 0, |&n| n == 4),
|
||||
Some(vec![0, 1, 2, 5, 4])
|
||||
);
|
||||
assert_eq!(
|
||||
dijkstra::<WheightedAdjacencyList, FibonacciHeap<_>, _>(&a, 0, |&n| n == 6),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
struct Grid {
|
||||
width: usize,
|
||||
height: usize,
|
||||
}
|
||||
|
||||
impl WheightedGraph for Grid {
|
||||
type Node = (usize, usize);
|
||||
|
||||
fn num_edges(&self, node: &Self::Node) -> usize {
|
||||
let mut c = 0;
|
||||
|
||||
if node.0 > 0 {
|
||||
c += 1
|
||||
}
|
||||
|
||||
if node.0 < self.width - 1 {
|
||||
c += 1
|
||||
}
|
||||
|
||||
if node.1 > 0 {
|
||||
c += 1
|
||||
}
|
||||
|
||||
if node.1 < self.height - 1 {
|
||||
c += 1
|
||||
}
|
||||
|
||||
c
|
||||
}
|
||||
|
||||
fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> {
|
||||
dbg!(&node);
|
||||
let edges = [
|
||||
(node.0 > 0).then_some((node.0.saturating_sub(1), node.1)),
|
||||
(node.0 < self.width - 1).then_some((node.0 + 1, node.1)),
|
||||
(node.1 > 0).then_some((node.0, node.1.saturating_sub(1))),
|
||||
(node.1 < self.height - 1).then_some((node.0, node.1 + 1)),
|
||||
];
|
||||
|
||||
edges.iter().flatten().nth(num).map(|&p| (p, 1.0))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grid() {
|
||||
let g = Grid {
|
||||
width: 600,
|
||||
height: 600,
|
||||
};
|
||||
|
||||
dbg!(dijkstra::<Grid, BinaryHeap<_>, _>(&g, (0, 0), |&n| n == (g.width - 1, g.height - 1)));
|
||||
}
|
||||
}
|
||||
17
factorio-graph/src/wheighted_graph/steiner_tree.rs
Normal file
17
factorio-graph/src/wheighted_graph/steiner_tree.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
use crate::priority_queue::PriorityQueue;
|
||||
use std::hash::Hash;
|
||||
use std::{collections::HashSet, fmt::Debug};
|
||||
|
||||
use super::{WheightedGraph, shortest_path::QueueObject};
|
||||
|
||||
pub fn takaheshi_matsuyama<G, P>(graph: G, nodes: &[G::Node]) -> Option<Vec<Vec<G::Node>>>
|
||||
where
|
||||
P: PriorityQueue<QueueObject<G::Node>> + Debug,
|
||||
P::Handle: Debug,
|
||||
G::Node: Eq + Hash + Clone,
|
||||
G: WheightedGraph,
|
||||
{
|
||||
let end_nodes: HashSet<G::Node> = HashSet::from_iter(nodes.iter().cloned());
|
||||
|
||||
todo!()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue