-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindShortestPath.m
66 lines (54 loc) · 1.39 KB
/
FindShortestPath.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
59
60
61
62
63
64
function shortestPath = FindShortestPath(...
positions, ...
cost, ...
source_node_id, ...
target_node_id)
% Function: FindShortestPath
% Description: use Dijstra's algorithm to find the shortest path
% Input: positions:
% cost
% source_node_id
% target_node_id
% Output: shortestPath: sequence (id) of relay nodes
% Author: Zihao Zhou, eezihaozhou@gmail.com
% Updated at: 2024/4/6
numNodes = size(positions, 1);
distance = inf(1, numNodes);
prevNode = zeros(1, numNodes);
visited = false(1, numNodes);
distance(source_node_id) = 0;
for i = 1:numNodes
u = FindMinDistanceNode(distance, visited);
visited(u) = true;
for v = 1:numNodes
if ~visited(v) && cost(u, v) > 0 && cost(u, v) ~= inf
alt = distance(u) + cost(u, v);
if alt < distance(v)
distance(v) = alt;
prevNode(v) = u;
end
end
end
end
path = [];
currentNode = target_node_id;
while currentNode ~= 0
path = [currentNode path];
currentNode = prevNode(currentNode);
end
if length(path)==1
shortestPath = [];
else
shortestPath = path;
end
end
function minNode = FindMinDistanceNode(dist, visited)
minDist = inf;
minNode = 1;
for i = 1:length(dist)
if ~visited(i) && dist(i) < minDist
minDist = dist(i);
minNode = i;
end
end
end