内容简介:Ford-Fulkerson算法(亦即标号法?)的输入与步骤如下:
Ford-Fulkerson算法(亦即标号法?)的输入与步骤如下:
-
输入
- 给定一个容量为c的图G=(V, E),源点s与汇点(终点)t
-
步骤
- 对图G中每一个边(u, v)的流量f(u, v)进行初始化为0
- 查询过程: 寻找(DFS、深度优先搜索方式)图G中的一条路径p,其中每一条边(u, v) ∈p,都有fc(u, v) = c(u, v) - f(u, v) > 0(c(u, v) 代表当前边的容量,f(u, v) 代表当前边已有的流量,即c(u, v) - f(u, v)代表当前边可用的最大流量,即剩余流量)
-
调整过程
:计算当前路径下每条边的最小剩余容量,cf(p) = min{fc(u, v) : (u, v) ∈p},然后对于每条边进行如下操作:
- f(u, v) = f(u, v) + cf(p) (前向狐)
- f(v, u) = f(v, u) - cf(p) (后向狐)
- 往复上述2与3步骤,直至无法找到路径p为止
#!/bin/python class Edge(object): def __init__(self, u, v, w): self.source = u self.sink = v self.capacity = w def __repr__(self): return "%s->%s:%s" % (self.source, self.sink, self.capacity) class FlowNetwork(object): def __init__(self): self.adj = {} self.flow = {} def add_vertex(self, vertex): self.adj[vertex] = [] def get_edges(self, v): return self.adj[v] def add_edge(self, u, v, w=0): if u == v: raise ValueError("u == v") edge = Edge(u,v,w) redge = Edge(v,u,0) edge.redge = redge redge.redge = edge self.adj[u].append(edge) self.adj[v].append(redge) self.flow[edge] = 0 self.flow[redge] = 0 def find_path(self, source, sink, path): if source == sink: return path for edge in self.get_edges(source): residual = edge.capacity - self.flow[edge] if residual > 0 and edge not in path: result = self.find_path( edge.sink, sink, path + [edge]) if result != None: return result def max_flow(self, source, sink): path = self.find_path(source, sink, []) while path != None: residuals = [edge.capacity - self.flow[edge] for edge in path] flow = min(residuals) for edge in path: self.flow[edge] += flow self.flow[edge.redge] -= flow path = self.find_path(source, sink, []) for edge in self.get_edges(source): return sum(self.flow[edge] for edge in self.get_edges(source)) g = FlowNetwork() [g.add_vertex(v) for v in "sopqrt"] g.add_edge('s','o',3) g.add_edge('s','p',3) g.add_edge('o','p',2) g.add_edge('o','q',3) g.add_edge('p','r',2) g.add_edge('r','t',3) g.add_edge('q','r',4) g.add_edge('q','t',2) print (g.max_flow('s','t')) #5
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- [经典算法]8皇后问题sql求解(回溯算法)
- 缓存的一些问题和一些加密算法【缓存问题】
- 算法 链表相加问题
- 算法面试:数组编码面试问题
- BFPRT 算法(TOP-K问题)
- google经典算法面试题-鸡蛋问题
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Iterative Methods for Sparse Linear Systems, Second Edition
Yousef Saad / Society for Industrial and Applied Mathematics / 2003-04-30 / USD 102.00
Tremendous progress has been made in the scientific and engineering disciplines regarding the use of iterative methods for linear systems. The size and complexity of linear and nonlinear systems arisi......一起来看看 《Iterative Methods for Sparse Linear Systems, Second Edition》 这本书的介绍吧!