Shortest path connecting all nodes
5 views (last 30 days)
Show older comments
Dear community,
I would like to travel through a three-dimensional point grid with multi-axis kinematics and save as much time as possible and thus travel through the nodes in the best possible order. For this purpose, I performed a Delaunay triangulation and created a disordered graph. Unfortunately I am relatively new to graph theory and would like to ask you to briefly skim the code below.
%% Create Samples
M=rand([20,3]);
%% Perform Delaunay
tri = delaunayTriangulation(M);
figure(1);
hold on
CC=parula(1000);
dist=pdist2(M,M);
dist=linspace(min(dist(dist>0)),max(dist(:)),1000);
for kk=1:size(tri.ConnectivityList,1)
Pts=M(tri.ConnectivityList(kk,:),:);
idx=nchoosek(1:1:size(Pts,1),2);
for i=1:size(idx,1)
P1=Pts(idx(i,1),:);
P2=Pts(idx(i,2),:);
[~,idx_CC]=min(abs(vecnorm(P1-P2,2,2)-dist));
line([P1(1) P2(1)],[P1(2) P2(2)],[P1(3) P2(3)],'Color',CC(idx_CC,:));
end
end
for kk=1:size(tri.Points,1)
text(tri.Points(kk,1),tri.Points(kk,2),tri.Points(kk,3),num2str(kk));
end
pcshow(M, [0, 0.4470, 0.7410],'MarkerSize',100);
% tetramesh(tri,[.5 .5 .5],'FaceAlpha',0.1);
hold off
cbar=colorbar;
cbar.Label.String='Weight, Euclidean Distance';
caxis([dist(1) dist(end)]);
%% Create Graph
AdjMat = false(size(M,1));
for kk = 1:size(tri,1)
idx=nchoosek(1:1:size(tri(kk,:),2),2);
for i=1:size(idx,1)
AdjMat(tri(kk,idx(i,1)), tri(kk,idx(i,2))) = true;
end
end
AdjMat = AdjMat | AdjMat';
G=graph(AdjMat);
weight=vecnorm(tri.Points(G.Edges.EndNodes(:,1),:)-tri.Points(G.Edges.EndNodes(:,2),:),2,2);
G.Edges.Weight=weight;
figure(2);
h=plot(G);
labeledge(h,1:1:numedges(G),weight);
In the next step I formed an MST and thus reduced the number of edges to the minimum. The problem is that this tree is also disordered and the sequence of the nodes does not make sense in the context of the problem. Unfortunately, it is not clear to me how I can get ahead at this point. I have been working on the 'shortestpathtree' function. But the starting node is irrelevant to me and the problem is that the tree structure of the resulting graph implicitly adds further edges, which have actually already been removed by the MST. Can anyone help me with adjusting the top lines of the code below?
%% Find Shortest Path
T_min = minspantree(G,'Method','sparse');
T = shortestpathtree(T_min,1);
figure(3);
h=plot(T);
% labelnode(h,1:1:numnodes(G),1:1:numnodes(G));
figure(4);
pcshow(M,[0, 0.4470, 0.7410],'MarkerSize',100);
hold on
CC=parula(size(T.Edges,1));
for i=1:size(T.Edges,1)
idx=(T.Edges(i,1).EndNodes);
P1=M(idx(1),:);
P2=M(idx(2),:);
line([P1(1) P2(1)],[P1(2) P2(2)],[P1(3) P2(3)],'Color',CC(i,:));
end
for kk=1:size(tri.Points,1)
text(tri.Points(kk,1),tri.Points(kk,2),tri.Points(kk,3),num2str(kk));
end
hold off
cbar=colorbar;
cbar.Label.String='Edge Index';
caxis([1 size(T.Edges,1)]);
Best wishes
Lennart
0 Comments
Answers (0)
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!