function [F, maxf, V, S] = Ford_Fulkerson(C, src, sink)
n = size(C, 1);
F = zeros(n);
maxf = 0;
V = [];
S = [];
while true
% in: ResNet.
ResNet = C - F + F'; % residual network.
% out: pre, Df
pre = ones(1, n) * NaN;
Df = ones(1, n) * inf;
% DFS to find augmenting path.
stk = [ src ];
unvisited = setdiff(1:n, src);
while ~isempty(stk)
if stk(1) == sink
break;
end
% pop
from = stk(1);
stk(1) = [];
% fot v in adj(u)
[~, to] = find(ResNet(from, unvisited) > 0);
tovisit = unvisited(unique(to));
% visit
pre(tovisit) = from;
Df(tovisit) = min(Df(from), ResNet(from, tovisit));
% push
stk = [tovisit, stk];
unvisited = setdiff(unvisited, tovisit);
end
% DFS end.
matlab实现基于DFS的Ford_Fulkerson最大流最小割算法
最新推荐文章于 2023-02-04 00:34:40 发布
该博客介绍了如何使用MATLAB实现基于深度优先搜索(DFS)的Ford-Fulkerson最大流最小割算法。通过构建残差网络并进行DFS搜索找到增广路径,逐步更新流量,直至找不到增广路径为止,达到求解最大流的目的。
摘要由CSDN通过智能技术生成