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


    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
    

  • 相关阅读:
    第五次站立会议
    第四次站立会议
    迪杰斯特拉算法求最短路径问题
    数组课堂作业
    java2
    Java书写add函数
    《大道至简》第二章(是懒人创造了方法)读后感
    大二暑假周进度报告之四
    大二暑假周进度报告之三
    大二暑假周进度报告之二
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6890002.html
Copyright © 2020-2023  润新知