Skip to main content

max_flow

Function max_flow 

Source
pub fn max_flow(
    graph: &Graph,
    source: VertexId,
    target: VertexId,
    capacity: Option<&[f64]>,
) -> IgraphResult<MaxFlow>
Expand description

Full maximum-flow computation: value, per-edge flow, cut edges, and source-side / sink-side vertex partitions.

Counterpart of igraph_maxflow in references/igraph/src/flow/flow.c (igraph C uses Goldberg-Tarjan push-relabel; this implementation uses Dinic’s algorithm). The scalar max-flow value is unique by the max-flow / min-cut theorem; the flow decomposition and partition may differ between algorithms, but the invariants are the same.

§Arguments

  • graph — input graph (directed or undirected).
  • source — source vertex id (0 ≤ source < vcount()).
  • target — sink vertex id (0 ≤ target < vcount(), target != source).
  • capacity — optional per-edge capacity in the graph’s edge-id order. When None, each edge contributes unit capacity. When Some(c), c.len() must equal graph.ecount(), and every entry must be finite and ≥ 0.

§Returns

A MaxFlow containing:

  • value — the maximum total flow from source to target.
  • flow — per-edge flow values (flow.len() == ecount()). For directed graphs the flow on each edge is non-negative and respects the capacity constraint. For undirected graphs the flow may be negative (indicating flow in the reverse direction of the edge’s stored orientation).
  • cut — edge ids of the minimum cut (saturated edges crossing the source-side / sink-side partition boundary).
  • partition — source-side vertex ids (sorted ascending).
  • partition2 — sink-side vertex ids (sorted ascending).

§Errors

§Examples

use rust_igraph::{Graph, max_flow};

// Two parallel paths of capacity 1 each → max flow = 2.
let mut g = Graph::new(4, true).unwrap();
g.add_edge(0, 1).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(2, 3).unwrap();
let cap = vec![1.0, 1.0, 1.0, 1.0];
let result = max_flow(&g, 0, 3, Some(&cap)).unwrap();
assert!((result.value - 2.0).abs() < 1e-12);
assert_eq!(result.flow.len(), 4);