码迷,mamicode.com
首页 > 编程语言 > 详细

matlab实现基于DFS的Ford_Fulkerson最大流最小割算法

时间:2015-08-15 11:59:02      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:

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.
    
    if isempty(stk)
        % not found. max flow get.
        S = setdiff(1:n, unvisited);
        V = unvisited;
        break;
        
    else
        % Augmenting path found.
        %in: pre, Df
        maxf = maxf + Df(sink);
        %update arc.
        t = sink;
        while t ~= src
            % pre(t)-t
            if C(pre(t), t) ~= 0
                % forward arc.
                F(pre(t), t) = F(pre(t), t) + Df(sink);
            else
                % backward arc.
                F(t, pre(t)) = F(t, pre(t)) - Df(sink); 
            end
            
            t = pre(t);
        end
    end
    
end

end

版权声明:本文为博主原创文章,未经博主允许不得转载。

matlab实现基于DFS的Ford_Fulkerson最大流最小割算法

标签:

原文地址:http://blog.csdn.net/qq_21555605/article/details/47680319

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!