pub fn ford_fulkerson(graph: &mut FlowGraph, source: usize, sink: usize) -> f64Expand description
Computes the maximum flow from source to sink using the
Ford-Fulkerson algorithm with DFS augmenting paths.
Repeatedly finds an augmenting path from source to sink in the
residual graph using DFS, then pushes the bottleneck capacity along that
path. Terminates when no augmenting path exists.
§When to use this vs edmonds_karp
Ford-Fulkerson with DFS is simple but has a worst-case complexity of
O(E · max_flow) — it can be very slow if max_flow is large and augmenting
paths carry only 1 unit of flow each. Prefer edmonds_karp for general
use since BFS augmentation guarantees polynomial time regardless of
capacity values.
Ford-Fulkerson is primarily useful as an educational reference and for small graphs or integer capacities where max_flow is bounded.
§Arguments
graph— a mutableFlowGraph; flow values are updated in place.source— index of the source node.sink— index of the sink node.
§Returns
The total maximum flow value sent from source to sink.
§Complexity
O(E · max_flow). Not guaranteed to terminate with irrational capacities (use integer or rational capacities).
§Examples
use graph_flow::{FlowGraph, ford_fulkerson};
// Simple two-path network:
// 0 --10-- 1 --10-- 3
// | |
// +---5--- 2 --5----+
let mut g = FlowGraph::new(4);
g.add_edge(0, 1, 10.0);
g.add_edge(0, 2, 5.0);
g.add_edge(1, 3, 10.0);
g.add_edge(2, 3, 5.0);
let flow = ford_fulkerson(&mut g, 0, 3);
assert_eq!(flow, 15.0);