pub mod dijkstra;
pub mod flow;
pub mod matching;
pub use flow::{
FlowNetwork, MaxFlowResult, MinCostFlowProblem, MinCostFlowResult, max_flow, min_cost_flow,
};
use petgraph::graph::{DiGraph, EdgeIndex, NodeIndex};
use serde::{Deserialize, Serialize};
pub type NodeId = NodeIndex;
pub type EdgeId = EdgeIndex;
pub type Graph<N, E> = DiGraph<N, E>;
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct FlowEdge {
pub capacity: i64,
pub cost: i64,
pub flow: i64,
}
impl FlowEdge {
pub fn new(capacity: i64, cost: i64) -> Self {
Self {
capacity,
cost,
flow: 0,
}
}
pub fn with_capacity(capacity: i64) -> Self {
Self::new(capacity, 0)
}
pub fn residual(&self) -> i64 {
self.capacity - self.flow
}
}
#[derive(Debug, Clone)]
pub struct Path {
pub nodes: Vec<NodeId>,
pub cost: i64,
}
impl Path {
pub fn empty() -> Self {
Self {
nodes: Vec::new(),
cost: 0,
}
}
pub fn is_empty(&self) -> bool {
self.nodes.is_empty()
}
pub fn len(&self) -> usize {
self.nodes.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_flow_edge() {
let mut edge = FlowEdge::new(10, 5);
assert_eq!(edge.residual(), 10);
edge.flow = 3;
assert_eq!(edge.residual(), 7);
}
}