Professional Documents
Culture Documents
Bellman Ford Dan Disjkarta
Bellman Ford Dan Disjkarta
NAMA
NIM
Outputnya :
visited(1:n) = 0;
distance(1:n) = inf;
parent(1:n) = 0;
distance(s) = 0;
for i = 1:(n-1),
temp = [];
for h = 1:n,
if visited(h) == 0
temp=[temp distance(h)];
else
temp=[temp inf];
end
end;
[t, u] = min(temp);
distance to the source;
visited(u) = 1;
for v = 1:n,
if ( ( netCostMatrix(u, v) + distance(u)) < distance(v) )
distance(v) = distance(u) + netCostMatrix(u, v);
parent(v) = u;
end;
end;
end;
path = [];
if parent(d) ~= 0
t = d;
path = [d];
while t ~= s
p = parent(t);
path = [p path];
if netCostMatrix(t, farthestPreviousHop(t)) <
netCostMatrix(t, p)
farthestPreviousHop(t) = p;
end;
if netCostMatrix(p, farthestNextHop(p)) < netCostMatrix(p,
t)
farthestNextHop(p) = t;
end;
t = p;
end;
end;
totalCost = distance(d);
return;
path
totalCost
if length(path) ~= 0
for i = 1:(length(path)-1)
line([netXloc(path(i)) netXloc(path(i+1))],
[netYloc(path(i)) netYloc(path(i+1))], 'Color', 'r', 'LineWidth',
1.50, 'LineStyle', '-.');
end;
end;
hold off;
return;
Outputnya :