-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindShortestPath.asv
68 lines (56 loc) · 1.57 KB
/
FindShortestPath.asv
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
65
66
function shortestPath = FindShortestPath(...
positions, ...
cost, ...
source_node_id, ...
target_node_id)
% Function: FindShortestPath
% Description: 运用Dijkstra算法寻找source到destination的最短路径
% Input: positions: 所有节点的位置
% cost: 边的权重矩阵
% source_node_id: 源节点id
% target_node_id: 目的节点id
% Output: shortestPath: 最短路径编号序列
% Author: Zihao Zhou, eezihaozhou@gmail.com
% Updated at: 2024/4/6
numNodes = size(positions, 1);
distance = inf(1, numNodes); % 初始化到source的距离
prevNode = zeros(1, numNodes); % 初始化前驱节点列表
visited = false(1, numNodes); % 初始化访问列表
distance(source_node_id) = 0; % 初始化源节点到自身的距离为0
for i = 1:numNodes
% 从还没有visted过的node中找最小distance
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