Interface MaximumFlow
-
public interface MaximumFlow
Calculate the maximum flow in a flow network.A maximum flow is firstly a valid flow, namely for each vertex except the source and sink the sum of flow units going along
Graph.inEdges(Object)
must be equal to the sum of flow units going alongGraph.outEdges(Object)
. In addition, a maximum flow maximize the number of flow units originated at the source and reaching the sink, which is equivalent to the sum of flows going out(in) of the source(sink) subtracted by the sum of flows going in(out) to the source(sink).Use
newInstance()
to get a default implementation of this interface. A builder obtained vianewBuilder()
may support different options to obtain different implementations.Graph<String, Integer> g = ...; WeightsDouble<Integer> capacities = g.addEdgesWeights("capacity", double.class); for (Integer e : g.edges()) capacities.set(e, 1); String sourceVertex = ...; String targetVertex = ...; MaximumFlow maxFlowAlg = MaximumFlow.newInstance(); Flow<String, Integer> flow = maxFlowAlg.computeMaximumFlow(g, capacities, sourceVertex, targetVertex); System.out.println("The maximum flow that can be pushed in the network is " + flow.getSupply(sourceVertex)); for (Integer e : g.edges()) { double capacity = capacities.get(e); double f = flow.getFlow(e); System.out.println("flow on edge " + e + ": " + f + "/" + capacity); }
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
MaximumFlow.Builder
A builder forMaximumFlow
objects.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description <V,E>
Flow<V,E>computeMaximumFlow(Graph<V,E> g, WeightFunction<E> capacity, Collection<V> sources, Collection<V> sinks)
Calculate the maximum flow in a network between a set of sources and a set of sinks.<V,E>
Flow<V,E>computeMaximumFlow(Graph<V,E> g, WeightFunction<E> capacity, V source, V sink)
Calculate the maximum flow in a network between a source and a sink.static MaximumFlow.Builder
newBuilder()
Create a new maximum flow algorithm builder.static MaximumFlow
newInstance()
Create a new maximum flow algorithm object.
-
-
-
Method Detail
-
computeMaximumFlow
<V,E> Flow<V,E> computeMaximumFlow(Graph<V,E> g, WeightFunction<E> capacity, V source, V sink)
Calculate the maximum flow in a network between a source and a sink.Some algorithm might run faster for integer capacities, and
WeightFunctionInt
can be passed ascapacity
.If
g
is anIntGraph
, its better to pass aIWeightFunction
ascapacity
to avoid boxing/unboxing. Ifg
is anIntGraph
, the returned object isIFlow
.- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- a graphcapacity
- a capacity edge weight functionsource
- a source vertexsink
- a sink vertex- Returns:
- the flows computed for each edge
- Throws:
IllegalArgumentException
- if the source and the sink are the same vertex
-
computeMaximumFlow
<V,E> Flow<V,E> computeMaximumFlow(Graph<V,E> g, WeightFunction<E> capacity, Collection<V> sources, Collection<V> sinks)
Calculate the maximum flow in a network between a set of sources and a set of sinks.Some algorithm might run faster for integer capacities, and
WeightFunctionInt
can be passed ascapacity
.If
g
is anIntGraph
, its better to pass aIWeightFunction
ascapacity
to avoid boxing/unboxing. Ifg
is anIntGraph
, the returned object isIFlow
.- Type Parameters:
V
- the vertices typeE
- the edges type- Parameters:
g
- a graphcapacity
- a capacity edge weight functionsources
- a set of source verticessinks
- a set of sink vertices- Returns:
- the flows computed for each edge
- Throws:
IllegalArgumentException
- if a vertex is both a source and a sink, or if a vertex appear twice in the source or sinks sets
-
newInstance
static MaximumFlow newInstance()
Create a new maximum flow algorithm object.This is the recommended way to instantiate a new
MaximumFlow
object. TheMaximumFlow.Builder
might support different options to obtain different implementations.- Returns:
- a default implementation of
MaximumFlow
-
newBuilder
static MaximumFlow.Builder newBuilder()
Create a new maximum flow algorithm builder.Use
newInstance()
for a default implementation.- Returns:
- a new builder that can build
MaximumFlow
objects
-
-