When there are 100 nodes labeled '1', '2', ..., '100', I want to make a code that gives shortest paths from some nodes to node '1'. For example, if S is defined by {node '10', node '15', node '30', node '99'}
, then I want to get some paths from node '10' to node '1', from node '15' to node '1', from node '30' to node '1', and from node '99' to node '1'.
In my case, each node has its n-dimensional position value, and each weight of edge between node 'i' and node 'j' is defined by the squared of the Euclidean distance between them. For example, if node 'i' and node 'j' have position values (0, 0, 0, 0) and (100, 200, 0, 300), respectively, then the weight of edge between them is defined by sqrt(100^2+200^2+300^2)=374
.
Actually, I made my own code using the Dijkstra algorithm as follows:
function PATHs = getPATH(D_Node, Rest_Nodes, S_Nodes)
all_Nodes = [D_Node; Rest_Nodes; S_Nodes];
D_Node_index = 1;
numRN = size(Rest_Nodes,1);
numSN = size(S_Nodes,1);
for m = 1:numSN
predecessor = nan(1+numRN,1);
oldCost = Inf(1+numRN,1);
settled = zeros(1+numRN,1);
dists = sqrt(sum((repmat(S_Nodes(m,:),1+numRN,1)-all_Nodes(1:1+numRN,:)).^2, 2));
newCost = dists.^2;
predecessor(newCost < oldCost) = 1+numRN+m;
[minCost, target_idx] = min(newCost);
settled(target_idx) = true;
while ~settled(D_Node_index)
oldCost = newCost;
dists = sqrt(sum((repmat(all_Nodes(target_idx,:),1+numRN,1)-all_Nodes(1:1+numRN,:)).^2, 2));
tempCost = dists.^2;
tempCost = tempCost + minCost;
newCost = min(tempCost,newCost);
newCost(settled > 0) = NaN;
predecessor(newCost < oldCost) = target_idx;
[minCost, target_idx] = min(newCost);
settled(target_idx) = true;
end
temp_path = [D_Node_index];
pre = predecessor(D_Node_index);
while pre ~= 1+numRN+m
temp_path = [pre temp_path];
pre = predecessor(pre);
end
path{m} = [1+numRN+m temp_path];
end
PATHs = path;
For more detail, D_Node
is 1 by 3 matrix that represents the position of destination node, S_Nodes
is m
by 3 matrix where each row represents the position of source node, and Rest_Nodes
is n
by 3 matrix where each row represents the position of common node (neither selected as destination node nor source node). That is, I consider a complete graph with 1+m+n
nodes.
The output of my function, getPATH
, is m
cells. The i-th cell represents the path from i-th node in S_Nodes
to D_Node
. Note that I have labeled D_Node
to '1', Rest_Nodes
to ['2', '3', ..., 'n+1']
, and S_Nodes
to ['n+2', 'n+3', ..., 'n+m+1']
.
Actually, my code runs well, but it is slow.
My outer loop algorithm runs this getPATH
function a lot.
Thus, my whole algorithm takes a very long time to finish.
Hence, I am trying to reduce the execution time of this getPATH
algorithm, and I decide to use an existing function (e.g., graph
, graphallshortestpaths
, shortestpathtree
, etc.) because the existing functions given by MATLAB are almost always faster than my own code (sadly).
Are there any widely-used implementations to find the shortest paths? If so, please let me know the function's name. If not, please help me speed up my function.
Also, I want to know a good way to map from a position matrix to a variable of type graph