networkx.algorithms.flow.shortest_augmenting_path¶
-
shortest_augmenting_path
(G, s, t, capacity='capacity', residual=None, value_only=False, two_phase=False, cutoff=None)[source]¶ Find a maximum single-commodity flow using the shortest augmenting path algorithm.
This function returns the residual network resulting after computing the maximum flow. See below for details about the conventions NetworkX uses for defining residual networks.
This algorithm has a running time of \(O(n^2 m)\) for \(n\) nodes and \(m\) edges.
- Parameters
G (NetworkX graph) – Edges of the graph are expected to have an attribute called ‘capacity’. If this attribute is not present, the edge is considered to have infinite capacity.
s (node) – Source node for the flow.
t (node) – Sink node for the flow.
capacity (string) – Edges of the graph G are expected to have an attribute capacity that indicates how much flow the edge can support. If this attribute is not present, the edge is considered to have infinite capacity. Default value: ‘capacity’.
residual (NetworkX graph) – Residual network on which the algorithm is to be executed. If None, a new residual network is created. Default value: None.
value_only (bool) – If True compute only the value of the maximum flow. This parameter will be ignored by this algorithm because it is not applicable.
two_phase (bool) – If True, a two-phase variant is used. The two-phase variant improves the running time on unit-capacity networks from \(O(nm)\) to \(O(\min(n^{2/3}, m^{1/2}) m)\). Default value: False.
cutoff (integer, float) – If specified, the algorithm will terminate when the flow value reaches or exceeds the cutoff. In this case, it may be unable to immediately determine a minimum cut. Default value: None.
- Returns
R – Residual network after computing the maximum flow.
- Return type
NetworkX DiGraph
- Raises
NetworkXError – The algorithm does not support MultiGraph and MultiDiGraph. If the input graph is an instance of one of these two classes, a NetworkXError is raised.
NetworkXUnbounded – If the graph has a path of infinite capacity, the value of a feasible flow on the graph is unbounded above and the function raises a NetworkXUnbounded.
See also
maximum_flow()
,minimum_cut()
,edmonds_karp()
,preflow_push()
Notes
The residual network
R
from an input graphG
has the same nodes asG
.R
is a DiGraph that contains a pair of edges(u, v)
and(v, u)
iff(u, v)
is not a self-loop, and at least one of(u, v)
and(v, u)
exists inG
.For each edge
(u, v)
inR
,R[u][v]['capacity']
is equal to the capacity of(u, v)
inG
if it exists inG
or zero otherwise. If the capacity is infinite,R[u][v]['capacity']
will have a high arbitrary finite value that does not affect the solution of the problem. This value is stored inR.graph['inf']
. For each edge(u, v)
inR
,R[u][v]['flow']
represents the flow function of(u, v)
and satisfiesR[u][v]['flow'] == -R[v][u]['flow']
.The flow value, defined as the total flow into
t
, the sink, is stored inR.graph['flow_value']
. Ifcutoff
is not specified, reachability tot
using only edges(u, v)
such thatR[u][v]['flow'] < R[u][v]['capacity']
induces a minimums
-t
cut.Examples
>>> import networkx as nx >>> from networkx.algorithms.flow import shortest_augmenting_path
The functions that implement flow algorithms and output a residual network, such as this one, are not imported to the base NetworkX namespace, so you have to explicitly import them from the flow package.
>>> G = nx.DiGraph() >>> G.add_edge('x','a', capacity=3.0) >>> G.add_edge('x','b', capacity=1.0) >>> G.add_edge('a','c', capacity=3.0) >>> G.add_edge('b','c', capacity=5.0) >>> G.add_edge('b','d', capacity=4.0) >>> G.add_edge('d','e', capacity=2.0) >>> G.add_edge('c','y', capacity=2.0) >>> G.add_edge('e','y', capacity=3.0) >>> R = shortest_augmenting_path(G, 'x', 'y') >>> flow_value = nx.maximum_flow_value(G, 'x', 'y') >>> flow_value 3.0 >>> flow_value == R.graph['flow_value'] True