ダイクストラアルゴリズム分析(matlab)



Dijkstra Algorithm Analysis



最短経路を計算するダイクストラアルゴリズム

注:当面は、matlab関数を直接呼び出して解決します。アルゴリズムプロセスは含まれません。
動作環境:Matlab 2017b
コードは次のとおりです。

clear clc n=9 M =[ 0 4 0 0 0 0 3 1 0 4 0 3 0 0 0 0 0 2 0 3 0 0 0 0 0 0 2 0 0 0 0 0 4 6 0 4 0 0 0 0 0 6 5 5 0 0 0 0 4 6 0 0 2 0 3 0 0 6 5 0 0 0 0 1 0 0 0 5 2 0 0 0 0 2 2 4 0 0 0 0 0] a=[]b=[]c=[] for i=1:n for j=1:n if M(i,j)~=0 a=[a,i] b=[b,j] c=[c,M(i,j)] end end end g=digraph(a,b,c) [path,d]=shortestpath(g,1,5) pp=num2str(path(1)) for i=2:length(path) pp=[pp,'->',num2str(path(i))] end disp('Shortest path') disp(pp) disp('Shortest cost') disp(num2str(d))

得られた結果:

最短経路
1-> 8-> 5
最小コスト
6