-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBellmanFinal.m
More file actions
58 lines (47 loc) · 1.88 KB
/
Copy pathBellmanFinal.m
File metadata and controls
58 lines (47 loc) · 1.88 KB
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
[conn1,cM,rD]= ImportData();
[weights numEdges] = makeWeights(cM, rD);
conn = importdata('NDConn.csv');
N=size(weights,1); % number of nodes
weightVals = zeros(1,numEdges);
startPointVals = zeros(1,numEdges);
endPointVals = zeros(1,numEdges);
index = 1;
for i= 1:N
for j = 1:N
if(weights(i,j)~=0)
weightVals(index) = weights(i,j);
startPointVals(index) = i;
endPointVals(index) = j;
index = index + 1;
end
end
end
points = [startPointVals;endPointVals]
s2=1; % Initialization of source node
namesBank2={'A','B','C','D','E','F','G','H','L','M','N','O','P','Q','R','S','T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'BB','CC','DD','EE','FF','GG','HH','LL','MM','NN','OO','PP','QQ','RR','SS','TT', 'UU', 'VV', 'WW', 'XX', 'YY', 'ZZ'};
names2 = namesBank2(1:N);
G=graph(startPointVals,endPointVals,weightVals)
title('West and East Hall Step Graph')
axis off
h2=plot(G,'EdgeLabel',G.Edges.Weight,'Nodelabel',names2,'EdgeColor','b','NodeColor','r', 'Layout','force')
masterMatrix = [startPointVals, endPointVals, weightVals];
%Setting inital distances to Inf (source node starts at 0)
dists = zeros(N);
dists(1:N) = Inf;
dists(1) = 0;
parentNodes(1:N) = 0;
for i = 1:N %For every node
for j = 1:numEdges %For every edge
distance = dists(startPointVals(j)); %Calculate the new distance
if distance < dists(endPointVals(j))
%Update the Distance
dists(endPointVals(j)) = distance;
parentNodes(endPointVals(j)) = startPointVals(j);
end
end
end
destin=input('Where do you want to go?');
totalStepsTaken = dists(destin)
path=shortestpathtree(G,1,destin);
p=plot(G,'EdgeLabel',G.Edges.Weight,'Nodelabel',names2,'EdgeColor','k','NodeColor','b', 'Layout','circle')
highlight(p,path,'EdgeColor','g','LineWidth',5);