-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBestPath.m
77 lines (47 loc) · 1.57 KB
/
BestPath.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
65
66
67
68
69
70
71
72
73
74
75
function [rows,columns,elevations] = BestPath(heights)
%Finds the best path from west to east across the heightfield heights
%uses dijkstra's algorithm: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
[height, width] = size(heights);
width = width + 2;
siz = [height width]; %fore use in ind2sub
%heightfield with extra edges added
dheights = [[1; zeros(height-1,1)], heights,[1; zeros(height-1,1)]];
%tracking
unvisited = ones(height,width);
unvisited = logical(unvisited);
cost = inf(height, width);
prevnode = -1 * ones(height, width);
cost(1,1) = 0;
finished = false;
unvisited(:,1) = 0;
unvisited(1,1) = 1;
while ~finished
%find unvisited node with least distance TO CURRENT NODE
tcost = cost(:);
tcost(~unvisited) = inf;
[~,leasts] = min(tcost);
[row,col] = ind2sub(siz,leasts(1));
unvisited(row,col) = 0;
[rows_n, cols_n, costs_n] = getneighbours(row,col,dheights,height,width); %get closest neighbour
if col == width - 1
finished = true;
break
else
for x = 1:length(rows_n)
t_cost = cost(row,col) + costs_n(x);
if t_cost < cost(rows_n(x), cols_n(x))
cost(rows_n(x), cols_n(x)) = t_cost;
prev(rows_n(x), cols_n(x)) = sub2ind(siz,row,col);
end
end
end
end
disp(prev)
disp(cost)
[~,step_m] = min(cost(:,width-1));
for x = width-2:-1:1
rows(x) = step_m;
columns(x) = x;
elevations(x) = heights(step_m,x);
[~, step_m] = ind2sub(siz,prev(step_m,x));
end