What am I doing wrong when returning the value from the function to my loop?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
I have wriiten the program in c++ and verified that it works but am struggling with the loop part when converting it to MATLAB.
In c++ I have
while (int sent = dfs(s, t, INF))
but in MATLAB I do not know how to do this? Also for computing the paths from s->t in my dfs function I have the following if statment in c++
if (int sent = dfs (i, t, min (minimum, flow_capacity))) {
// adjust the capacity
Flow[s][i] += sent;
Flow[i][s] -= sent;
return sent;
How can I do this in MATLAB in where I have tried to implement the last parts but obviously have done so incorrectly? below is all my code.
Also I am aware that MATLAB has a built in function for these sorts of problems but I need to implement my own. Sorry if its something basic and trivial Im new to MATLAB.
Thanks in advance.
function ff
clear;
%adjacency matrix representing capacities
Cap = [0, 4, 5, 0, 0, 0;
0, 0, 2, 1, 4, 0;
0, 0, 0, 0, 3, 0;
0, 0, 0, 0, 0, 2;
0, 0, 0, 2, 0, 6;
0, 0, 0, 0, 0, 0;];
% source and sink
INF=999999;
s = 1;
t = 6;
max_flow=0;
len = length(Cap);
sent = 0;
Flow =zeros([len,len]);
visited=boolean(zeros(1,len));
%sent = dfs(s,t,INF);
sent = dfs(s,t,INF);
while sent==dfs(s,t,INF);
max_flow =+sent;
visited=boolean(zeros(1,len));
end
disp('Residual graph:');
disp(Flow);
disp(['Max flow is ' num2str(max_flow)]);
%dfs
function F = dfs(s,t,minimum)
visited(s) = true;
if s==t
F = minimum;
end
for i = 1:len
flow_cap = Cap(s,i) - Flow(s,i);
if ~visited(i) && flow_cap > 0
if sent == dfs(i,t,min(minimum,flow_cap))
Flow(s,i) = Flow(s,i)+sent;
Flow(i,s) = Flow(i,s)-sent ;
F=+sent;
end
end
end
end
end
0 Commenti
Risposta accettata
Jan
il 5 Apr 2022
Modificato: Jan
il 5 Apr 2022
% C++:
% while (int sent = dfs(s, t, INF))
sent = dfs(s, t, INF);
while sent ~= 0
sent = dfs(s, t, Inf);
end
And:
% if (int sent = dfs (i, t, min (minimum, flow_capacity))) {
% // adjust the capacity
% Flow[s][i] += sent;
% Flow[i][s] -= sent;
% return sent;
sent = dfs(i, t, min(minimum, flow_capacity));
if sent
Flow(s,i) = Flow(s,i) + sent;
Flow(i,s) = Flow(i,s) - sent;
end
Omit the useless "clear;"
Replace
Flow =zeros([len,len]);
visited=boolean(zeros(1,len));
by
Flow = zeros(len, len);
visited = false(1, len);
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!