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% augmentingPath 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 EndEnd
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Realization of Ford_fulkerson maximum flow minimum cut algorithm based on DFS in MATLAB