0

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

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Danny_Kim
  • 299
  • 2
  • 18
  • [shortestpathtree](https://www.mathworks.com/help/matlab/ref/graph.shortestpathtree.html)? – rahnema1 Jun 15 '18 at 03:36
  • I want to know what FAST algorithms are most preferred by experienced researchers, is it the `shortestpathtree`? – Danny_Kim Jun 15 '18 at 04:01
  • 2
    So in [CS](https://cs.stackexchange.com) or [math](https://math.stackexchange.com) you may get more relevant feedbacks. – rahnema1 Jun 15 '18 at 04:05
  • @rahnema1 Thanks for giving a direction. – Danny_Kim Jun 15 '18 at 04:45
  • 1
    I edited your question a bit, because "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow". I felt there was enough interesting here to try to avoid the question being closed. – Cris Luengo Jun 15 '18 at 05:37
  • 1
    The MATLAB editor puts red lines underneath some lines of code. If you hover your mouse over these, it will give you hints. Pay attention to these, they will help you speed up your code. For example, you can preallocate arrays in several places to prevent the array being re-allocated with every loop iteration. – Cris Luengo Jun 15 '18 at 05:47
  • 1
    Are you asking for different algorithms or different implementations? The Dijkstra algorithm for shortest paths (for cases where the distance between nodes change is supposed to very quite fast), there are others though but depends a bit on the specifics of the network which might be faster for you. Here in SO I think you can get a more feedback regarding the implementation, but you should ask very specific things, isolating them. Going to your whole algorithm and analysing it is a bit of a lot of work. – myradio Jun 15 '18 at 08:49
  • 1
    @Danny_Kim If you have a complete graph and edge weights are equal to the Euclidean distance, then the shortest path between node `i` and node `1` is always the edge between the two nodes. What am I missing? – beaker Jun 16 '18 at 01:38
  • @beaker I set the weight of each edge to "squared" of the Euclidean distance. Thus, although a complete graph is considered, the shortest routing paths are not just node `i` and node `1`. – Danny_Kim Jun 18 '18 at 07:02
  • 1
    @Danny_Kim Then @rahnema1's suggestion of `shortestpathtree` will likely be faster than hand-written code, unless you get into compiled `.mex` files. One thing that will speed up both your function and `shortestpathtree` is to use the single destination node as the source (reversing the edges if a directed graph) and finding the distances to all of the source nodes. This will remove your main loop. – beaker Jun 18 '18 at 13:54

0 Answers0