Skip to main content

push_relabel

Function push_relabel 

Source
pub fn push_relabel<T, E, F>(
    graph: &mut Graph<T, E>,
    source: NodeIndex,
    sink: NodeIndex,
    capacity: F,
) -> f64
where F: FnMut(NodeIndex, NodeIndex, &E) -> f64,
Expand description

Push-Relabel 最大流算法(FIFO 队列优化)

使用预流推进策略,通过高度标签和 FIFO 队列优化

§内存优化

使用 HashMap<usize, HashMap<usize, f64>> 存储残量,空间复杂度 O(V+E)

§参数

  • graph - 有向图
  • source - 源点
  • sink - 汇点
  • capacity - 获取边容量的闭包

§返回

最大流值

§复杂度

  • 时间:O(V²E)(使用 FIFO 队列优化)
  • 空间:O(V + E)

§示例

use god_gragh::prelude::*;
use god_gragh::algorithms::flow::push_relabel;

let mut graph = GraphBuilder::directed()
    .with_nodes(vec!["S", "A", "B", "T"])
    .with_edges(vec![
        (0, 1, 10.0), // S->A
        (0, 2, 5.0),  // S->B
        (1, 2, 15.0), // A->B
        (1, 3, 10.0), // A->T
        (2, 3, 10.0), // B->T
    ])
    .build()
    .unwrap();

let source = graph.nodes().next().unwrap().index();
let sink = graph.nodes().nth(3).unwrap().index();
let max_flow = push_relabel(&mut graph, source, sink, |_, _, cap| *cap);
assert_eq!(max_flow, 15.0);