词条 | Ford–Fulkerson algorithm | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
释义 |
The Ford–Fulkerson method or Ford–Fulkerson algorithm (FFA) is a greedy algorithm that computes the maximum flow in a flow network. 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[1] or it is specified in several implementations with different running times.[2] It was published in 1956 by L. R. Ford, Jr. and D. R. Fulkerson.[3] 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. AlgorithmLet be a graph, and for each edge from {{mvar|u}} to {{mvar|v}}, let be the capacity and be the flow. We want to find the maximum flow from the source {{mvar|s}} to the sink {{mvar|t}}. 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 to be the network with capacity and no flow. Notice that it can happen that a flow from {{mvar|v}} to {{mvar|u}} is allowed in the residual network, though disallowed in the original network: if and then . {{Algorithm-begin|name=Ford–Fulkerson}}Inputs Given a Network with flow capacity {{mvar|c}}, a source node {{mvar|s}}, and a sink node {{mvar|t}} Output Compute a flow {{mvar|f}} from {{mvar|s}} to {{mvar|t}} of maximum value
The path in step 2 can be found with for example a breadth-first search (BFS) or a depth-first search in . If you use the former, the algorithm is called Edmonds–Karp. When no more paths in step 2 can be found, {{mvar|s}} will not be able to reach {{mvar|t}} in the residual network. If {{mvar|S}} is the set of nodes reachable by {{mvar|s}} in the residual network, then the total capacity in the original network of edges from {{mvar|S}} to the remainder of {{mvar|V}} is on the one hand equal to the total flow we found from {{mvar|s}} to {{mvar|t}}, 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. If the graph has multiple sources and sinks, we act as follows: Suppose that and . Add a new source with an edge from to every node , with capacity . And add a new sink with an edge from every node to , with capacity . Then apply the Ford–Fulkerson algorithm. Also, if a node {{mvar|u}} has capacity constraint , we replace this node with two nodes , and an edge , with capacity . Then apply the Ford–Fulkerson algorithm. ComplexityBy 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 runs forever, the flow might not even 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 (see big O notation), where is the number of edges in the graph and is the maximum flow in the graph. This is because each augmenting path can be found in time and increases the flow by an integer amount of at least , with the upper bound . 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 time. Integral exampleThe following example shows the first steps of Ford–Fulkerson in a flow network with 4 nodes, source and sink . This example shows the worst-case behaviour of the algorithm. In each step, only a flow of is sent across the network. If breadth-first-search were used instead, only two steps would be needed.
Notice how flow is "pushed back" from to when finding the path . Non-terminating exampleConsider the flow network shown on the right, with source , sink , capacities of edges , and respectively , and and the capacity of all other edges some integer . The constant was chosen so, that . We use augmenting paths according to the following table, where , and .
Note that after step 1 as well as after step 5, the residual capacities of edges , and are in the form , and , respectively, for some . This means that we can use augmenting paths , , and 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 . If we continue to use augmenting paths as above, the total flow converges to , while the maximum flow is . In this case, the algorithm never terminates and the flow doesn't even converge to the maximum flow.[4] {{clear}}Python implementation of Edmonds-Karp algorithmimport collections
class Graph: 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[s] = True # Standard BFS Loop while queue: u = queue.popleft() # Get all adjacent vertices's of the dequeued vertex u # If a adjacent has not been visited, then mark it # visited and enqueue it for ind, val in enumerate(self.graph[u]): if (visited[ind] == False) and (val > 0): queue.append(ind) visited[ind] = True parent[ind] = u # If we reached sink in BFS starting from source, then return # true, else false return visited[t] # Returns the maximum flow from s to t in the given graph def EdmondsKarp(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[parent[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 Notes1. ^{{Cite book|title = Electronic Design Automation: Synthesis, Verification, and Test|last = Laung-Terng Wang, Yao-Wen Chang, Kwang-Ting (Tim) Cheng|publisher = Morgan Kaufmann|year = 2009|isbn = 0080922007|location = |pages = 204}} 2. ^{{Cite book|title = Introduction to Algorithms|author1=Thomas H. Cormen |author2=Charles E. Leiserson |author3=Ronald L. Rivest |author4=Clifford Stein |publisher = MIT Press|year = 2009|isbn = 0262258102|location = |pages = 714}} 3. ^{{Cite journal | last1 = Ford | first1 = L. R. | authorlink1 = L. R. Ford, Jr.| last2 = Fulkerson | first2 = D. R. | authorlink2 = D. R. Fulkerson| doi = 10.4153/CJM-1956-045-5 | title = Maximal flow through a network | journal = Canadian Journal of Mathematics| volume = 8 | pages = 399–404 | year = 1956 | pmid = | pmc = | url = http://www.cs.yale.edu/homes/lans/readings/routing/ford-max_flow-1956.pdf }} 4. ^{{cite journal| title = The smallest networks on which the Ford–Fulkerson maximum flow procedure may fail to terminate | first = Uri | last = Zwick|authorlink=Uri Zwick | journal = Theoretical Computer Science | volume = 148 | issue = 1 | date = 21 August 1995 | pages = 165–170 | doi = 10.1016/0304-3975(95)00022-O}} References
| first1 = Thomas H. | last1 = Cormen | authorlink1 = Thomas H. Cormen | first2 = Charles E. | last2 = Leiserson | authorlink2 = Charles E. Leiserson | first3 = Ronald L. | last3 = Rivest | authorlink3 = Ronald L. Rivest | first4 = Clifford | last4 = Stein | authorlink4 = Clifford Stein | title = Introduction to Algorithms | edition = Second | publisher = MIT Press and McGraw–Hill | year = 2001 | isbn = 0-262-03293-7 | chapter = Section 26.2: The Ford–Fulkerson method | pages = 651–664 }}
Pearson Education | year=2006 | chapter=Chapter 7:Extensions to the Maximum-Flow Problem | pages = 378–384 | isbn=0-321-29535-8 }}
See also
External links
3 : Network flow problem|Articles with example pseudocode|Graph algorithms |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。