forked from ivanbrugere/matlab-networks-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.m
executable file
·58 lines (46 loc) · 1.66 KB
/
dijkstra.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
%##################################################################
% Dijkstra's algorithm.
%
% INPUTS: adj - adjacency matrix (nxn), s - source node, target - target node
% OUTPUTS: distance, d and path, P (from s to target)
%
% Note: if target==[], then dist and P include all distances and paths from s
% Other routines used: adj2adjL.m
% GB: last updated, Oct 5, 2012
%##################################################################
function [dist,P]=dijkstra(adj,s,target)
n=length(adj); % number of nodes
adjL=adj2adjL(adj); % list of neighbors
dist=inf(1,n); % initialize distances
dist(s)=0;
previous=[1:n; inf(1,n)]'; % {i: inf}, i=1:n, inf -> not assigned
S=cell(1,n); % initialize shortest path sequence
Q=[1:n]; % all unvisited vertices, entire graph
while length(Q)>0 % while not empty
% get min dist member among unvisited vertices
[mindist,min_ind]=min(dist(Q));
u=Q(min_ind);
% termination condition - save "source-u" path
S{u}=[];
t=u;
while not(isempty(find(previous(:,1)==t))) % t in previous.keys():
% insert u at the beginning of S
S{u}=[t S{u}];
t=previous(t,2);
end
if length(target)>0 & u==target
dist=dist(u); P=S{u};
return
end
% =====================================================
Q = setdiff(Q,u); % remove u from Q
for v=1:length(adjL{u}) % across all neighbors of u
v=adjL{u}(v);
alt=dist(u)+adj(u,v);
if alt < dist(v) % update the distance and path
dist(v)=alt;
previous(v,2)=u;
end
end
end
P=S;