필터 지우기
필터 지우기

How can the edges on the shortestpath be determined?

조회 수: 2 (최근 30일)
CO
CO 2022년 6월 27일
댓글: Steven Lord 2022년 6월 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
Steven Lord
Steven Lord 2022년 6월 30일
That makes sense. The third output argument from shortestpath was introduced in release R2018a.

댓글을 달려면 로그인하십시오.

추가 답변 (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