Ford–Fulkerson Algorithm
   HOME

TheInfoList



OR:

The Ford–Fulkerson method or Ford–Fulkerson algorithm (FFA) is a
greedy algorithm A greedy algorithm is any algorithm that follows the problem-solving heuristic of making the locally optimal choice at each stage. In many problems, a greedy strategy does not produce an optimal solution, but a greedy heuristic can yield locally ...
that computes the maximum flow in a
flow network In graph theory, a flow network (also known as a transportation network) is a directed graph where each edge has a capacity and each edge receives a flow. The amount of flow on an edge cannot exceed the capacity of the edge. Often in operations re ...
. It is sometimes called a "method" instead of an "algorithm" as the approach to finding augmenting paths in a residual graph is not fully specified or it is specified in several implementations with different running times. It was published in 1956 by L. R. Ford Jr. and D. R. Fulkerson. The name "Ford–Fulkerson" is often also used for the Edmonds–Karp algorithm, which is a fully defined implementation of the Ford–Fulkerson method. The idea behind the algorithm is as follows: as long as there is a path from the source (start node) to the sink (end node), with available capacity on all edges in the path, we send flow along one of the paths. Then we find another path, and so on. A path with available capacity is called an
augmenting path In graph theory, a flow network (also known as a transportation network) is a directed graph where each edge has a capacity and each edge receives a flow. The amount of flow on an edge cannot exceed the capacity of the edge. Often in operations res ...
.


Algorithm

Let G(V,E) be a graph, and for each edge from to , let c(u,v) be the capacity and f(u,v) be the flow. We want to find the maximum flow from the source to the sink . After every step in the algorithm the following is maintained: : This means that the flow through the network is a ''legal flow'' after each round in the algorithm. We define the residual network G_f(V,E_f) to be the network with capacity c_f(u,v) = c(u,v) - f(u,v) and no flow. Notice that it can happen that a flow from to is allowed in the residual network, though disallowed in the original network: if f(u,v)>0 and c(v,u)=0 then c_f(v,u)=c(v,u)-f(v,u)=f(u,v)>0. :Inputs Given a Network G = (V,E) with flow capacity , a source node , and a sink node :Output Compute a flow from to of maximum value :# f(u,v) \leftarrow 0 for all edges (u,v) :# While there is a path from to in G_f, such that c_f(u,v) > 0 for all edges (u,v) \in p: :## Find c_f(p) = \min\ :## For each edge (u,v) \in p :### f(u,v) \leftarrow f(u,v) + c_f(p) (''Send flow along the path'') :### f(v,u) \leftarrow f(v,u) - c_f(p) (''The flow might be "returned" later'') The path in step 2 can be found with, for example,
breadth-first search Breadth-first search (BFS) is an algorithm for searching a tree data structure for a node that satisfies a given property. It starts at the tree root and explores all nodes at the present depth prior to moving on to the nodes at the next dept ...
(BFS) or
depth-first search Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible al ...
in G_f(V,E_f). The former is known as the Edmonds–Karp algorithm. When no more paths in step 2 can be found, will not be able to reach in the residual network. If is the set of nodes reachable by in the residual network, then the total capacity in the original network of edges from to the remainder of is on the one hand equal to the total flow we found from to , and on the other hand serves as an upper bound for all such flows. This proves that the flow we found is maximal. See also
Max-flow Min-cut theorem In computer science and optimization theory, the max-flow min-cut theorem states that in a flow network, the maximum amount of flow passing from the ''source'' to the ''sink'' is equal to the total weight of the edges in a minimum cut, i.e., the ...
. If the graph G(V,E) has multiple sources and sinks, we act as follows: Suppose that T=\ and S=\. Add a new source s^* with an edge (s^*,s) from s^* to every node s\in S , with capacity c(s^*,s)=d_s=\sum_c(s,u). And add a new sink t^* with an edge (t, t^*) from every node t\in T to t^* , with capacity c(t, t^*)=d_t=\sum_c(v,t). Then apply the Ford–Fulkerson algorithm. Also, if a node has capacity constraint d_u, we replace this node with two nodes u_,u_, and an edge (u_,u_) , with capacity c(u_,u_)=d_u. We can then apply the Ford–Fulkerson algorithm.


Complexity

By adding the flow augmenting path to the flow already established in the graph, the maximum flow will be reached when no more flow augmenting paths can be found in the graph. However, there is no certainty that this situation will ever be reached, so the best that can be guaranteed is that the answer will be correct if the algorithm terminates. In the case that the algorithm does not terminate, the flow might not converge towards the maximum flow. However, this situation only occurs with irrational flow values. When the capacities are integers, the runtime of Ford–Fulkerson is bounded by O(E f) (see
big O notation Big ''O'' notation is a mathematical notation that describes the asymptotic analysis, limiting behavior of a function (mathematics), function when the Argument of a function, argument tends towards a particular value or infinity. Big O is a memb ...
), where E is the number of edges in the graph and f is the maximum flow in the graph. This is because each augmenting path can be found in O(E) time and increases the flow by an integer amount of at least 1, with the upper bound f. A variation of the Ford–Fulkerson algorithm with guaranteed termination and a runtime independent of the maximum flow value is the Edmonds–Karp algorithm, which runs in O(VE^2) time.


Integer flow example

The following example shows the first steps of Ford–Fulkerson in a flow network with 4 nodes, source A and sink D. This example shows the worst-case behaviour of the algorithm. In each step, only a flow of 1 is sent across the network. If breadth-first-search were used instead, only two steps would be needed.


Non-terminating example

Consider the flow network shown on the right, with source s, sink t, capacities of edges e_1 = 1, e_2 = r=(\sqrt-1)/2 and e_3 = 1, and the capacity of all other edges some integer M \ge 2. The constant r was chosen so, that r^2 = 1 - r. We use augmenting paths according to the following table, where p_1 = \, p_2 = \ and p_3 = \. Note that after step 1 as well as after step 5, the residual capacities of edges e_1, e_2 and e_3 are in the form r^n, r^ and 0, respectively, for some n \in \mathbb. This means that we can use augmenting paths p_1, p_2, p_1 and p_3 infinitely many times and residual capacities of these edges will always be in the same form. Total flow in the network after step 5 is 1 + 2(r^1 + r^2). If we continue to use augmenting paths as above, the total flow converges to \textstyle 1 + 2\sum_^\infty r^i = 3 + 2r. However, note that there is a flow of value 2M + 1, by sending M units of flow along sv_1t, 1 unit of flow along sv_2v_3t, and M units of flow along sv_4t. Therefore, the algorithm never terminates and the flow does not converge to the maximum flow. Another non-terminating example based on the
Euclidean algorithm In mathematics, the Euclidean algorithm,Some widely used textbooks, such as I. N. Herstein's ''Topics in Algebra'' and Serge Lang's ''Algebra'', use the term "Euclidean algorithm" to refer to Euclidean division or Euclid's algorithm, is a ...
is given by , where they also show that the worst case running-time of the Ford-Fulkerson algorithm on a network G(V,E) in
ordinal numbers In set theory, an ordinal number, or ordinal, is a generalization of ordinal numerals (first, second, th, etc.) aimed to extend enumeration to infinite sets. A finite set can be enumerated by successively labeling each element with the leas ...
is \omega^.


Python implementation of the Edmonds–Karp algorithm

import collections class Graph: """ This class represents a directed graph using adjacency matrix representation. """ def __init__(self, graph): self.graph = graph # residual graph self.row = len(graph) def bfs(self, s, t, parent): """ Returns true if there is a path from source 's' to sink 't' in residual graph. Also fills parent[] to store the path. """ # Mark all the vertices as not visited visited = [False] * self.row # Create a queue for BFS queue = collections.deque() # Mark the source node as visited and enqueue it queue.append(s) visited = True # Standard BFS loop while queue: u = queue.popleft() # Get all adjacent vertices of the dequeued vertex u # If an adjacent has not been visited, then mark it # visited and enqueue it for ind, val in enumerate(self.graph : if (visited nd

False) and (val > 0): queue.append(ind) visited nd= True parent nd= u # If we reached sink in BFS starting from source, then return # true, else false return visited # Returns the maximum flow from s to t in the given graph def edmonds_karp(self, source, sink): # This array is filled by BFS and to store path parent = 1* self.row max_flow = 0 # There is no flow initially # Augment the flow while there is path from source to sink while self.bfs(source, sink, parent): # Find minimum residual capacity of the edges along the # path filled by BFS. Or we can say find the maximum flow # through the path found. path_flow = float("Inf") s = sink while s != source: path_flow = min(path_flow, self.graph arent[s[s">.html" ;"title="arent[s">arent[s[s s = parent[s">">arent[s<_a>[s.html" ;"title=".html" ;"title="arent[s">arent[s[s">.html" ;"title="arent[s">arent[s[s s = parent[s # Add path flow to overall flow max_flow += path_flow # update residual capacities of the edges and reverse edges # along the path v = sink while v != source: u = parent[v] self.graph[u][v] -= path_flow self.graph[v][u] += path_flow v = parent[v] return max_flow


See also

* Berge's theorem * Approximate max-flow min-cut theorem * Turn restriction routing *
Dinic's algorithm Dinic's algorithm or Dinitz's algorithm is a strongly polynomial algorithm for computing the maximum flow in a flow network, conceived in 1970 by Israeli (formerly Soviet) computer scientist Yefim Dinitz. The algorithm runs in O(, V, ^2, E, ) time ...


Notes


References

* * * * *


External links


A tutorial explaining the Ford–Fulkerson method to solve the max-flow problem




{{DEFAULTSORT:Ford-Fulkerson Algorithm Articles with example pseudocode Articles with example Python (programming language) code Graph algorithms Network flow problem