How can the edges on the shortestpath be determined?

1 view (last 30 days)
After using shortestpath (Dijkstra algorithm) on directed and weighted graph, Matlab gives the node sequence on the shortest path.
How can I write the edges on the shortest path?
Thanks
CO

Accepted Answer

Chunru
Chunru on 27 Jun 2022
Edited: Chunru on 28 Jun 2022
s = [1 1 2 3 3 4 4 6 6 7 8 7 5];
t = [2 3 4 4 5 5 6 1 8 1 3 2 8];
G = digraph(s,t);
plt = plot(G)
plt =
GraphPlot with properties: NodeColor: [0 0.4470 0.7410] MarkerSize: 4 Marker: 'o' EdgeColor: [0 0.4470 0.7410] LineWidth: 0.5000 LineStyle: '-' NodeLabel: {'1' '2' '3' '4' '5' '6' '7' '8'} EdgeLabel: {} XData: [0.3082 1.2885 -0.4680 0.3105 -0.5963 -0.7564 1.3653 -1.4518] YData: [0.7918 0.5124 -0.4892 -0.4894 -1.4376 0.2944 1.5685 -0.7508] ZData: [0 0 0 0 0 0 0 0] Show all properties
[P, d, edgepath] = shortestpath(G,7,8)
P = 1×5
7 1 3 5 8
d = 4
edgepath = 1×4
11 2 5 8
E = G.Edges(edgepath, :)
E = 4×1 table
EndNodes ________ 7 1 1 3 3 5 5 8
highlight(plt, 'Edges', edgepath)
  2 Comments
CO
CO on 30 Jun 2022
Chunru, thank you very much for the answer.
In fact I had tried the code in Matlab. But it did not work. By the way, I noticed that my Matlab is Matlab2017.
After that, I installed Matlab2020. It works.
Thanks
CO

Sign in to comment.

More Answers (0)

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!