How can I find the total distance of the path from the dfsearch on this code?

4 次查看(过去 30 天)
Good evening,
Similar to finding the total distance of the shortest path between two nodes, I want to find the total distance covered in following the path prescribed by the dfsearch on this code. That is, the total distance covered visiting each node at least once.
Can someone help? Here is my code:
% The next three lines of code are used to plot the edges. s = starting
% node; t = terminating node; weights = distance between nodes (weight of
% edge); names = labels for the nodes; g = graph.
s = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 1 16 32 17 33 48 64 49 65 80 97 97 97 97 97 97];
t = [2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 17 32 48 33 49 64 80 65 81 96 1 17 33 49 65 81];
weights = [2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 8.75 6.36 4.74 4.74 6.36 8.75];
names = {'T1' 'T2' 'T3' 'T4' 'T5' 'T6' 'T7' 'T8' 'T9' 'T10' 'T11' 'T12' 'T13' 'T14' 'T15' 'T16' 'T17' 'T18' 'T19' 'T20' 'T21' 'T22' 'T23' 'T24' 'T25' 'T26' 'T27' 'T28' 'T29' 'T30' 'T31' 'T32' 'T33' 'T34' 'T35' 'T36' 'T37' 'T38' 'T39' 'T40' 'T41' 'T42' 'T43' 'T44' 'T45' 'T46' 'T47' 'T48' 'T49' 'T50' 'T51' 'T52' 'T53' 'T54' 'T55' 'T56' 'T57' 'T58' 'T59' 'T60' 'T61' 'T62' 'T63' 'T64' 'T65' 'T66' 'T67' 'T68' 'T69' 'T70' 'T71' 'T72' 'T73' 'T74' 'T75' 'T76' 'T77' 'T78' 'T79' 'T80' 'T81' 'T82' 'T83' 'T84' 'T85' 'T86' 'T87' 'T88' 'T89' 'T90' 'T91' 'T92' 'T93' 'T94' 'T95' 'T96' 'DROPSTATION'};
G = graph(s,t,weights,names);
% The next lines of code specifies the cordinates of each node on the x,y plane
% [P,d] plots the shortest path between two nodes, and highlights it in red
% v dfsearch performs the DFS to simulate movement of workers
x = [-7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -7.5 -4.5 -4.5 -4.5 -4.5 -4.5 -4.5 -4.5 -4.5 -4.5 -4.5 -4.5 -4.5 -4.5 -4.5 -4.5 -4.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 0];
y = [4.5 6.5 8.5 10.5 12.5 14.5 16.5 18.5 20.5 22.5 24.5 26.5 28.5 30.5 32.5 34.5 4.5 6.5 8.5 10.5 12.5 14.5 16.5 18.5 20.5 22.5 24.5 26.5 28.5 30.5 32.5 34.5 4.5 6.5 8.5 10.5 12.5 14.5 16.5 18.5 20.5 22.5 24.5 26.5 28.5 30.5 32.5 34.5 4.5 6.5 8.5 10.5 12.5 14.5 16.5 18.5 20.5 22.5 24.5 26.5 28.5 30.5 32.5 34.5 4.5 6.5 8.5 10.5 12.5 14.5 16.5 18.5 20.5 22.5 24.5 26.5 28.5 30.5 32.5 34.5 4.5 6.5 8.5 10.5 12.5 14.5 16.5 18.5 20.5 22.5 24.5 26.5 28.5 30.5 32.5 34.5 0];
p = plot(G,'XData',x,'YData',y,'EdgeLabel',G.Edges.Weight);
[P,d] = shortestpath(G,22,31)
highlight(p,P,'EdgeColor','r')
v = dfsearch(G,97)

采纳的回答

William Rose
William Rose 2023-3-6
Try
dTot=0;
for i=2:length(v)
dTot=dTot+sqrt((x(v(i))-x(v(i-1)))^2+(y(v(i))-y(v(i-1)))^2);
end
That seems too obvious, so maybe I'm msissing something.
Good luck.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by