Skip to main content

ShortestPath

Trait ShortestPath 

Source
pub trait ShortestPath{
    // Provided method
    fn min_cost_max_flow(
        &self,
        source: &Self::Vertex,
        sink: &Self::Vertex,
    ) -> McmfResult<<Self::Edge as FlowEdge>::Capacity, <Self::Edge as FlowEdge>::Cost> { ... }
}
Expand description

Computes the minimum-cost maximum flow of a directed tagged graph.

§Preconditions

  • The graph must be directed; the method panics otherwise.
  • The graph must contain no negative-cost cycles. Individual edges may have negative costs, but any cycle in the graph must have a non-negative total cost. Violating this precondition panics with a negative-cycle message.
  • The answer (flow value and total cost) is assumed to fit into u64 and i64 respectively.

§Algorithm

The successive shortest path method: each iteration finds a shortest augmenting path from the source to the sink in the residual network with SPFA, pushes as much flow as the bottleneck of the path allows, and repeats until the sink is unreachable. The input graph is never modified; only a per-edge flow table is maintained inside the call.

§Complexity

ItemComplexity
One SPFA run$O(VE)$ amortized
Augmentations$O(F)$, $F$ the max flow
Total$O(F \cdot VE)$ worst case

§Examples

use algograph::algorithm::mcmf::{FlowEdge, spfa::ShortestPath};
use algograph::graph::*;
use algograph::graph::directed::TreeBackedGraph;
use algograph::tagged::Edge as TaggedEdge;
use algograph::tagged::{GrowableTaggedGraph, NaiveTaggedGraph};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct MyEdge {
    source: VertexId,
    sink: VertexId,
    capacity: i64,
    cost: i64,
}

impl TaggedEdge for MyEdge {
    fn source(&self) -> VertexId {
        self.source
    }
    fn sink(&self) -> VertexId {
        self.sink
    }
}

impl FlowEdge for MyEdge {
    type Capacity = i64;
    type Cost = i64;

    fn capacity(&self) -> i64 {
        self.capacity
    }
    fn cost(&self) -> i64 {
        self.cost
    }
}

let mut graph = NaiveTaggedGraph::<u32, MyEdge, TreeBackedGraph>::new();
let s = graph.overwrite_vertex(0);
let a = graph.overwrite_vertex(1);
let t = graph.overwrite_vertex(2);
graph.add_edge(MyEdge {
    source: s,
    sink: a,
    capacity: 5,
    cost: 1,
});
graph.add_edge(MyEdge {
    source: a,
    sink: t,
    capacity: 3,
    cost: 2,
});

let res = graph.min_cost_max_flow(&0u32, &2u32);
assert_eq!(res.max_flow, 3);
assert_eq!(res.min_cost, 9);

Provided Methods§

Source

fn min_cost_max_flow( &self, source: &Self::Vertex, sink: &Self::Vertex, ) -> McmfResult<<Self::Edge as FlowEdge>::Capacity, <Self::Edge as FlowEdge>::Cost>

Computes the minimum-cost maximum flow from source to sink.

When source equals sink, a zero flow with zero cost is returned.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§