Get shortest paths and distances among nodes without loop for (and possibly in a faster way)
1 次查看(过去 30 天)
显示 更早的评论
How to get shortest paths and distances among nodes (selected from a list) without a loop for, and possibly in a faster way than what shown in the following example ?
% Input 1 (the graph)
s = [1 1 1 1 2 2 2 2 2 8 8 12 14 14 1 14];
t = [3 5 4 2 6 10 7 9 8 11 12 13 7 6 8 15];
G = graph(s,t);
plot(G)
% Input 2 (list of nodes, among which, both shortest paths and distances will be
% calculated)
list = [3 7 8 9 10 14];
% Calculation
tic
for i = 1 :length(list)
for j = 1 : length(list)
[p,d] = shortestpath(G,list(i),list(j));
nodes_path{i,j} = p;
nodes_distances{i,j} = d;
end
end
toc
% Outputs
nodes_path,
nodes_distances,
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!