Skip to content

Instantly share code, notes, and snippets.

@ivanchoff
Created September 14, 2015 05:31
Show Gist options
  • Save ivanchoff/f78798ca31939ac1cddf to your computer and use it in GitHub Desktop.
Save ivanchoff/f78798ca31939ac1cddf to your computer and use it in GitHub Desktop.
implementation of dijkstra algorithm in octave
function[dist previo] = dijkstraOctave(Adj,inicio)
n = size(Adj,1);
nodos_visitados = 0;
dist(1:n) = inf;
visited(1:n) = 0;
previo(1:n) = -1;
Q(1:n) = inf;
dist(inicio) = 0;
Q(inicio) = 0;
while sum(visited)~=n
[x posx] = min(Q);
%if visited(posx)==0
% continue;
%end
visited(posx) = 1;
for i=1:n
if Adj(posx,i)>0 && visited(i)==0
if dist(posx) + Adj(posx,i) < dist(i)
dist(i) = dist(posx) + Adj(posx,i);
previo(i) = posx;
Q(i) = dist(i);
end
end
end
Q(posx) = inf;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment