use crate::core::{Graph, IgraphError, IgraphResult, VertexId};
use super::max_flow::max_flow_value;
pub fn edge_disjoint_paths(graph: &Graph, source: VertexId, target: VertexId) -> IgraphResult<i64> {
let flow = max_flow_value(graph, source, target, None)?;
#[allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss
)]
{
if !flow.is_finite() || flow < 0.0 || flow > i64::MAX as f64 {
return Err(IgraphError::Internal(
"unit-capacity max-flow value is not representable as i64",
));
}
Ok(flow.round() as i64)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::IgraphError;
#[test]
fn rejects_source_equals_target() {
let mut g = Graph::new(2, true).expect("graph");
g.add_edge(0, 1).expect("edge");
let err = edge_disjoint_paths(&g, 0, 0).unwrap_err();
match err {
IgraphError::InvalidArgument(_) => {}
other => panic!("expected InvalidArgument, got {other:?}"),
}
}
#[test]
fn rejects_out_of_range_source() {
let g = Graph::new(2, true).expect("graph");
let err = edge_disjoint_paths(&g, 5, 0).unwrap_err();
match err {
IgraphError::VertexOutOfRange { id, n } => {
assert_eq!(id, 5);
assert_eq!(n, 2);
}
other => panic!("expected VertexOutOfRange, got {other:?}"),
}
}
#[test]
fn rejects_out_of_range_target() {
let g = Graph::new(2, true).expect("graph");
let err = edge_disjoint_paths(&g, 0, 5).unwrap_err();
match err {
IgraphError::VertexOutOfRange { id, n } => {
assert_eq!(id, 5);
assert_eq!(n, 2);
}
other => panic!("expected VertexOutOfRange, got {other:?}"),
}
}
#[test]
fn isolated_endpoints_have_no_paths() {
let g = Graph::new(4, true).expect("graph");
assert_eq!(edge_disjoint_paths(&g, 0, 3).expect("paths"), 0);
}
#[test]
fn single_edge_one_path() {
let mut g = Graph::new(2, true).expect("graph");
g.add_edge(0, 1).expect("edge");
assert_eq!(edge_disjoint_paths(&g, 0, 1).expect("paths"), 1);
}
#[test]
fn two_parallel_paths() {
let mut g = Graph::new(4, true).expect("graph");
for (s, t) in [(0u32, 1u32), (1, 3), (0, 2), (2, 3)] {
g.add_edge(s, t).expect("edge");
}
assert_eq!(edge_disjoint_paths(&g, 0, 3).expect("paths"), 2);
}
#[test]
fn parallel_arcs_count_each_as_path() {
let mut g = Graph::new(2, true).expect("graph");
for _ in 0..4 {
g.add_edge(0, 1).expect("edge");
}
assert_eq!(edge_disjoint_paths(&g, 0, 1).expect("paths"), 4);
}
#[test]
fn directed_no_reverse_path() {
let mut g = Graph::new(4, true).expect("graph");
for (s, t) in [(0u32, 1u32), (1, 2), (2, 3)] {
g.add_edge(s, t).expect("edge");
}
assert_eq!(edge_disjoint_paths(&g, 0, 3).expect("paths"), 1);
assert_eq!(edge_disjoint_paths(&g, 3, 0).expect("paths"), 0);
}
#[test]
fn c_unit_fixture_directed_0_to_5() {
let mut g = Graph::new(6, true).expect("graph");
let arcs = [
(0u32, 1u32),
(0, 2),
(1, 2),
(1, 3),
(2, 4),
(3, 4),
(3, 5),
(4, 5),
(3, 3),
];
for (s, t) in arcs {
g.add_edge(s, t).expect("edge");
}
assert_eq!(edge_disjoint_paths(&g, 0, 5).expect("paths"), 2);
assert_eq!(edge_disjoint_paths(&g, 0, 3).expect("paths"), 1);
assert_eq!(edge_disjoint_paths(&g, 3, 0).expect("paths"), 0);
assert_eq!(edge_disjoint_paths(&g, 3, 5).expect("paths"), 2);
}
#[test]
fn c_unit_fixture_undirected_4_to_3() {
let mut g = Graph::new(6, false).expect("graph");
for (s, t) in [
(0u32, 1u32),
(0, 2),
(1, 2),
(1, 3),
(2, 4),
(3, 4),
(3, 5),
(4, 5),
(3, 3),
] {
g.add_edge(s, t).expect("edge");
}
assert_eq!(edge_disjoint_paths(&g, 4, 3).expect("paths"), 3);
}
#[test]
fn matches_st_edge_connectivity() {
use super::super::st_edge_connectivity::st_edge_connectivity;
let mut g = Graph::new(5, false).expect("graph");
for i in 0u32..5 {
for j in (i + 1)..5 {
g.add_edge(i, j).expect("edge");
}
}
assert_eq!(
edge_disjoint_paths(&g, 0, 4).expect("paths"),
st_edge_connectivity(&g, 0, 4).expect("ec")
);
}
}
#[cfg(all(test, feature = "proptest-harness"))]
mod proptests {
use super::super::st_edge_connectivity::st_edge_connectivity;
use super::*;
use crate::core::Graph;
use proptest::prelude::*;
fn xorshift(mut r: u64) -> u64 {
r ^= r << 13;
r ^= r >> 7;
r ^= r << 17;
r
}
fn build_random(seed: u64, n: u32, m_max: u32, directed: bool) -> Graph {
let mut g = Graph::new(n, directed).expect("graph");
let mut state = seed | 1;
for _ in 0..m_max {
state = xorshift(state);
let u = u32::try_from(state % u64::from(n)).expect("modulo fits");
state = xorshift(state);
let v = u32::try_from(state % u64::from(n)).expect("modulo fits");
if u == v {
continue;
}
g.add_edge(u, v).expect("edge");
}
g
}
proptest! {
#[test]
fn menger_equals_st_edge_connectivity(
seed in any::<u64>(),
n in 2u32..8,
m in 1u32..16,
directed in any::<bool>(),
) {
let g = build_random(seed, n, m, directed);
let s = u32::try_from(seed % u64::from(n)).expect("modulo fits");
let t_raw = u32::try_from(xorshift(seed) % u64::from(n)).expect("modulo fits");
let t = if t_raw == s { (s + 1) % n } else { t_raw };
prop_assume!(s != t);
let paths = edge_disjoint_paths(&g, s, t).expect("paths");
let ec = st_edge_connectivity(&g, s, t).expect("ec");
prop_assert_eq!(
paths,
ec,
"Menger violated: paths={} ec={} (n={}, m={}, directed={}, seed={})",
paths, ec, n, m, directed, seed
);
}
}
}