pub trait FlowGraph {
fn vertex_count(&self) -> usize;
}
#[derive(Debug, Clone)]
pub struct MinCut {
pub partition: Vec<bool>,
}
impl MinCut {
pub fn new(partition: Vec<bool>) -> Self {
Self { partition }
}
pub fn same_side(&self, v: usize, s_representative: usize) -> bool {
if v >= self.partition.len() || s_representative >= self.partition.len() {
panic!("Vertex index out of bounds in MinCut::same_side. v: {}, s_rep: {}, partition_len: {}",
v, s_representative, self.partition.len());
}
self.partition[v] == self.partition[s_representative]
}
}
#[derive(Debug, Clone, thiserror::Error)]
pub enum MaxFlowError {
#[error("Source and sink are the same vertex (s: {0}, t: {0})")]
SourceEqualsSink(usize),
#[error("Vertex {0} not found in graph")]
VertexNotFound(usize),
#[error("Maximum iterations ({0}) reached in max-flow computation")]
MaxIterationsReached(usize),
#[error("Graph has 0 vertices, max-flow is undefined")]
EmptyGraph,
#[error("Internal max-flow error: {0}")]
InternalError(String),
}
pub trait MaxFlowSolver<G: FlowGraph> {
type Flow: Copy + Default + PartialOrd + std::ops::AddAssign + std::fmt::Debug;
fn max_flow_min_cut(
&self,
graph: &G,
source: usize,
sink: usize,
) -> Result<(Self::Flow, MinCut), MaxFlowError>;
}
pub trait OriginalGraphView: FlowGraph {
fn all_edges(&self) -> Box<dyn Iterator<Item = (usize, usize, f64)> + '_>;
}