Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

TUGAS JARINGAN KOMPUTER

ALGORITMA DIJKSTRA DAN BELLMAN FORD

NAMA

NIM

ANDRE FEBRIAN OTNIEL


BAGUSTY VIRGONY
FARHANI
DESTIO R.

DBC 113 094


DBC 113 060
DBC 113 124
DBC 113 114

JURUSAN TEKNIK INFORMATIKA


FAKULTAS TEKNIK
UNIVERSITAS PALANGKA RAYA
2015

Algoritma Mencari Jalur Terpendek dengan Dijkstra dan Bellman Ford


Source Code Algoritma Bellman Ford dengan 100 node (tujuan node 1 ke 9) :
W = [.31 .40 .61 .12 .35 .45 .50 .78 .56 .28 .49 ];
DG = sparse([6 1 2 7 100 8 4 5 9 6 1],[2 6 3 4 1 7 100 8 4 9 5],W)
h = view(biograph(DG,[],'ShowWeights','on'))
[dist,path,pred] = graphshortestpath(DG,1,9)
set(h.Nodes(path),'Color',[1 0.4 0.4])
edges = getedgesbynodeid(h,get(h.Nodes(path),'ID'));
set(edges,'LineColor',[1 0 0])
set(edges,'LineWidth',1.5)
UG = tril(DG + DG')
h = view(biograph(UG,[],'ShowArrows','off','ShowWeights','on'))
[dist,path,pred] = graphshortestpath(UG,1,9,'directed',false)
set(h.Nodes(path),'Color',[1 0.4 0.4])
fowEdges = getedgesbynodeid(h,get(h.Nodes(path),'ID'));
revEdges = getedgesbynodeid(h,get(h.Nodes(fliplr(path)),'ID'));
edges = [fowEdges;revEdges];
set(edges,'LineColor',[1 0 0])
set(edges,'LineWidth',1.5)

Outputnya :

Source Code Algoritma Dijkstra :


noOfNodes = 100;
rand('state', 0);
figure(1);
clf;
hold on;
L = 2000;
R = 300;
netXloc = rand(1,noOfNodes)*L;
netYloc = rand(1,noOfNodes)*L;
for i = 1:noOfNodes
plot(netXloc(i), netYloc(i), '.');
text(netXloc(i), netYloc(i), num2str(i));
for j = 1:noOfNodes
distance = sqrt((netXloc(i) - netXloc(j))^2 + (netYloc(i) netYloc(j))^2);
if distance <= R
matrix(i, j) = 1;
% there is a link;
line([netXloc(i) netXloc(j)], [netYloc(i) netYloc(j)],
'LineStyle', ':');
else
matrix(i, j) = inf;
end;
end;
end;
activeNodes = [];
for i = 1:noOfNodes,
farthestPreviousHop(i) = i;
farthestNextHop(i) = i;
end;
n = noOfNodes;
netCostMatrix = matrix;
s = 34;
d = 72;

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 :

You might also like