Interface FlowNetwork
-
- All Known Subinterfaces:
FlowNetwork.Int
public interface FlowNetwork
Flow on graph edges, with capacities and flows values.A flow network on graph edges is defined as two functions: the capacity function \(C:E \rightarrow R\) and flow function \( F:E \rightarrow R\). The capacity function define how many units of flow an edge can transfer from its source to its target. The flow function define the number of units of flow that are currently transferred along each edge. The capacity of any edge must be non negative, and the edge's flow must be smaller or equal to its capacity.
Problems formulated using flow networks involve a source and a sink vertices. The source is a vertex from which the flow is originated, and every flow going along its edges must reach the sink vertex using the edges of the graphs while not violating the capacities of the network. For each vertex except the source and sink the sum of flow units going along
Graph.inEdges(int)
must be equal to the sum of flow units going alongGraph.outEdges(int)
.A flow is most intuitively defined on directed graphs, as the flow on an edge is transferred from one vertex to another in some direction, but we can define and solve flow problem on undirected graphs as well. Technically, the flows values returned by
getFlow(int)
can either be positive or negative for undirected edges, with values absolutely smaller than the capacity of the edge. A positive flow \(+f\) value assigned to edgee
means a flow directed fromedgeSource(e)
toedgeTarget(e)
with \(f\) units of flow. A negative flow \(-f\) value assigned to edgee
means a flow directed fromedgeTarget(e)
toedgeSource(e)
(opposite direction) with \(|-f|\) units of flow (seegetFlow(int)
).Graph g = ...; FlowNetwork net = FlowNetwork.createAsEdgeWeight(g); for (int e : g.edges()) f.setCapacity(e, 1); int sourceVertex = ...; int targetVertex = ...; MaximumFlow maxFlowAlg = MaximumFlow.newBuilder().build(); double totalFlow = maxFlowAlg.computeMaximumFlow(g, net, sourceVertex, targetVertex); System.out.println("The maximum flow that can be pushed in the network is " + totalFlow); for (int e : g.edges()) { double capacity = net.getCapacity(e); double flow = net.getFlow(e); System.out.println("flow on edge " + e + ": " + flow + "/" + capacity); }
- Author:
- Barak Ugav
- See Also:
MaximumFlow
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
FlowNetwork.Int
Flow on graph edges, with integer capacities and flows values.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description static FlowNetwork
createAsEdgeWeight(Graph g)
Create a flow network by adding edge weights usingGraph.addEdgesWeights(java.lang.Object, java.lang.Class<? super E>)
.double
getCapacity(int edge)
Get the capacity of an edge.double
getFlow(int edge)
Get the amount of flow units going along an edge.void
setCapacity(int edge, double capacity)
Set the capacity of an edge.void
setFlow(int edge, double flow)
Set the amount of flow units going along an edge.
-
-
-
Method Detail
-
getCapacity
double getCapacity(int edge)
Get the capacity of an edge.- Parameters:
edge
- an edge identifier in the graph- Returns:
- the capacity of the edge
- Throws:
IndexOutOfBoundsException
- ifedge
is not a valid edge identifier
-
setCapacity
void setCapacity(int edge, double capacity)
Set the capacity of an edge.- Parameters:
edge
- an edge identifier in the graphcapacity
- the new capacity of the edge- Throws:
IndexOutOfBoundsException
- ifedge
is not a valid edge identifierIllegalArgumentException
- ifcapacity
is negative
-
getFlow
double getFlow(int edge)
Get the amount of flow units going along an edge.If the graph is directed, a flow of \(f\) units on
e
, for \(0 \leq f \leq cap(e)\), means a flow of \(f\) units of flow fromedgeSource(e)
toedgeTarget(e)
.If the graph is undirected, a flow of \(+f\) units on
e
, for \(0 \leq f \leq cap(e)\), means a flow of \(f\) units of flow fromedgeSource(e)
toedgeTarget(e)
, while a flow of \(-f\) units one
, for \(-cap(e) \leq -f < 0\), means a flow of \(|-f|\) units of flow fromedgeTarget(e)
toedgeSource(e)
(opposite direction).- Parameters:
edge
- an edge identifier in the graph- Returns:
- the amount of flow units going along an edge
- Throws:
IndexOutOfBoundsException
- ifedge
is not a valid edge identifier
-
setFlow
void setFlow(int edge, double flow)
Set the amount of flow units going along an edge.- Parameters:
edge
- an edge identifier in the graphflow
- the new flow of the edge- Throws:
IndexOutOfBoundsException
- ifedge
is not a valid edge identifier
-
createAsEdgeWeight
static FlowNetwork createAsEdgeWeight(Graph g)
Create a flow network by adding edge weights usingGraph.addEdgesWeights(java.lang.Object, java.lang.Class<? super E>)
.Unless
setCapacity(int, double)
orsetFlow(int, double)
are used, the capacity and flow of each edge will be zero.- Parameters:
g
- a graph- Returns:
- a flow network implemented as edge weights
-
-