use crate::graph::impl_generic::{max_flow_impl, min_cost_flow_impl};
use crate::graph::traits::flow::FlowAlgorithms;
use crate::graph::traits::types::{FlowResult, GraphData, MinCostFlowOptions};
use numr::error::Result;
use numr::runtime::cpu::{CpuClient, CpuRuntime};
impl FlowAlgorithms<CpuRuntime> for CpuClient {
fn max_flow(
&self,
graph: &GraphData<CpuRuntime>,
source: usize,
sink: usize,
) -> Result<FlowResult<CpuRuntime>> {
max_flow_impl(self, graph, source, sink)
}
fn min_cost_flow(
&self,
graph: &GraphData<CpuRuntime>,
source: usize,
sink: usize,
options: &MinCostFlowOptions,
) -> Result<FlowResult<CpuRuntime>> {
min_cost_flow_impl(self, graph, source, sink, options)
}
}
#[cfg(test)]
mod tests {
use super::*;
use numr::runtime::cpu::CpuDevice;
#[test]
fn test_max_flow() {
let device = CpuDevice::new();
let client = CpuClient::new(device.clone());
let graph = GraphData::from_edge_list::<f64>(
&[0, 0, 1, 1, 2],
&[1, 2, 2, 3, 3],
Some(&[10.0, 8.0, 3.0, 5.0, 7.0]),
4,
true,
&device,
)
.unwrap();
let result = client.max_flow(&graph, 0, 3).unwrap();
assert!(result.max_flow > 0.0);
assert!(result.max_flow <= 12.0 + 1e-10);
}
}