How can the edges on the shortestpath be determined?

2 次查看(过去 30 天)
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

采纳的回答

Chunru
Chunru 2022-6-27
编辑:Chunru 2022-6-28
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 个评论
CO
CO 2022-6-30
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by