Test directed graph for cycles, write them, and change paths to it become acyclic
1 次查看(过去 30 天)
显示 更早的评论
Hi, I'm totally new to matlab, I need to write an algorithm where directed graph would be tested for cycles, cycles writed down and then their paths directions changed so that the graph becomes acyclic.
Here is my code so far..
clear all
clc
% Number of edges
n = input ('Kiek grafe virsuniu:');
% Create graph
A = round(rand(n));
no_el = n*n; % elementu sk.
dlg = 1:n+1:no_el; % isrinkti pagrindine istrizaine
A(dlg) = 0; % pagrindines istrizaines skaicius paversti nuluikais
testi_viska = 1;
testi = 1;
KelioIlgis = 1;
i = 1;
j = 1;
while i <= n && testi_viska == 1
%KelioIlgis = (KelioIlgis + 1);
Kelias(1,j) = i;
testi = 1;
j = 1;
while j <= n && testi == 1
if A(i,j) == 1
KelioIlgis = (KelioIlgis + 1);
Kelias(1, KelioIlgis) = j;
c = 1;
while c < KelioIlgis
if Kelias(1,c)==Kelias(1,KelioIlgis);
disp('CIKLAS!');
disp(Kelias);
A(i,j) = 0;
%A(j,i) = 1;
Kelias = Kelias - Kelias(1,KelioIlgis);
KelioIlgis = (KelioIlgis - 1);
testi = 1;
elseif Kelias(1,c)~=Kelias(1,KelioIlgis);
disp('CIKLO NĖRA');
testi = 0;
i = j;
c = c + 1;
end
end
end;
end;
end;
It looks like somewhere it becomes endless... Guys please help me out!
1 个评论
Walter Roberson
2012-11-22
Does it display anything endlessly? Have you tried stepping through with the debugger?
回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!