How does shortestpath function work?
5 views (last 30 days)
Show older comments
Riccardo Nebuloni
on 7 Dec 2021
Commented: Christine Tobler
on 7 Dec 2021
Hi everyone. i need a help.
i don't completely understand the "Shortest Path in Multigraph" example in "shortestpath" function (in particular, the edgepth). i don't understand the meaning of the indeces [1 7 9 10]: i thought they were the indeces of the couples of coordinates, but -if i were right- these couples shoud be 1-2/2,4/3,5/3,5 (that are not the right ones, couse in the "highlight" part and in the plot part everithing is ok.
Thx for your help
0 Comments
Accepted Answer
Christine Tobler
on 7 Dec 2021
Every edge has a number, which is the order in which they appear in the Edges table (try displaying g.Edges). The edgepath contains the number of each edge on the path:
G = graph([1 1 1 1 1 2 2 3 3 3 4 4],[2 2 2 2 2 3 4 4 5 5 5 2],[2 4 6 8 10 5 3 1 5 6 8 9])
G.Edges
So the array [1 7 9 10] points to the first, 7th, 9th and 10th rows of the Edges table as the edges on the path (this is relevant for a multigraph because there can be several edges between the same two nodes).
If we index into the Edges table using edgepath, we get just a table of the edges that are on the path (see g.Edges(edgepath, :) in the example).
4 Comments
Christine Tobler
on 7 Dec 2021
I see. The Edges table is always put into a standard format, as this makes it easier to compare graphs and the algorithms are much more efficient this way.
You can maintain the information about the original order of the edges by having the weights of the graph store this original order:
a=[1 1 1 1 1 2 2 3 3 3 4 4];
b= [2 2 2 2 2 3 4 4 5 5 5 2];
G = graph(a, b, 1:length(a));
G.Edges
Here the weight of each edge is giving the order in which it appeared in the original inputs.
More 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!