-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstraChain.m
More file actions
47 lines (36 loc) · 1009 Bytes
/
dijkstraChain.m
File metadata and controls
47 lines (36 loc) · 1009 Bytes
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
% This function calculates the shortest path from each given location in the
% adjacency matrix A to the target node, using Dijkstra's Algorithm for graph
% searching/traversal
% dist(i) is the distance from the ith node to the target
% next(i) is the next node in the shortest path from the ith node to the target
function [next,dist] = dijkstraChain(A,target)
for i = 1:length(A)
dist(i) = inf(1,1);
next(i) = -1;
end
dist(target) = 0;
for i = 1 :length(A)
status(i) = 0;
end
checked = 0;
while (length(A) > checked)
minElem = inf;
for j = 1:length(A)
if (status(j) ~= 1 && dist(j) < minElem)
minElem = dist(j);
mindex = j;
end
end
for j = 1:length(A)
if (A(mindex,j) > 0 && j ~= mindex)
alt = dist(mindex) + A(mindex,j)
if alt < dist(j)
dist(j) = alt
next(j) = mindex;
end
end
end
status(mindex) = 1;
checked = checked + 1;
end
end