Azzera filtri
Azzera filtri

How can the edges on the shortestpath be determined?

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Chunru
Chunru il 27 Giu 2022
Modificato: Chunru il 28 Giu 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 Commenti
CO
CO il 30 Giu 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
Steven Lord
Steven Lord il 30 Giu 2022
That makes sense. The third output argument from shortestpath was introduced in release R2018a.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graph and Network Algorithms in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by