Iteration related to Dijkstra function issue

2 views (last 30 days)
So, I used a function, similiar to Dijkstra's to calculate the shortest paths in relation to a certain matrix. One of the output variable I got was an array with the previous vertices in the shortest paths.
For example, starting from the first node: previous = [ 0 1 1 3 6 3 ]. Being the first index the previous node before reaching node 1, which is 0 as it's the node itself. The second index, the previous node before reaching node two, which is node 1.
The result I'm looking for is the paths it takes to reach each node. Something like this: For node 2 -> [1 2], for node 3 -> [1 3], for node 4 it'd be [1 3 4], for node 5 [ 1 3 6 5] and for node 6 [1 3 6].
So, to obtain that result, right now the only solution I see, is get the indexes for each previous path and then see which index has that value. For that I'd need to use the function "find()" but since some of the indexes have the same numbers I can't really do that.
Any other solution?

Accepted Answer

Steven Lord
Steven Lord on 12 Dec 2016
Consider using the shortestpathtree method for graph and digraph objects. Specifying the 'Method' as 'positive' creates the tree using "Dijkstra algorithm that requires all edge weights to be nonnegative."
  2 Comments
Nuno Palma
Nuno Palma on 12 Dec 2016
Sadly i cant use this function on my project. Any other suggestion?
Christine Tobler
Christine Tobler on 13 Dec 2016
The method shortestpathtree has options to both compute the vector you described, or a cell array containing the paths you are trying to retrieve. You could take a look at the MATLAB code for how one is translated to the other, using edit shortestpathtree. See the subfunction constructTree.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!