matlab 单元最短路 Dijkstra算法 有向图 无向图

W = [2 8 1 1 6 5 1 2 3 6 4 9 3 7 9];
S = [0 0 0 1 1 3 3 3 5 5 6 4 6 2 2];S=S+1;
T = [1 3 2 4 3 4 5 6 4 7 5 7 7 3 6];T=T+1;
IDS={'u0','u1','u2','u3','u4','u5','u6','u7'};
DG = sparse(S,T,W,8,8)%求稀疏矩阵
UG =tril(DG+DG')%取矩阵和转置矩阵和的下三角矩阵。
bg=biograph(DG,IDS,'ShowArrows','off','ShowWeights','on');%构建biograph对象,设置参数
set(bg.nodes,'shape','circle','color',[1,1,0],'linecolor',[0,0,1]);
set(bg.nodes,'textColor',[0,0,0],'lineWidth',2,'fontsize',9);
set(bg,'EdgeType','straight');%设置连线为直线 
get(bg.nodes,'position');%获取节点位置
dolayout(bg);
bg.nodes(1).position=[9,100];
bg.nodes(2).position=[101,182];
bg.nodes(3).position=[101,10];
bg.nodes(4).position=[101,100];
bg.nodes(5).position=[214,182];
bg.nodes(6).position=[214,100];
bg.nodes(7).position=[214,10];
bg.nodes(8).position=[303,100];
dolayout(bg,'pathsonly',true);
t = view(bg)
[dist,path,pred] = graphshortestpath(UG,1,8,'directed',false)%Dijkstra求最短路

猜你喜欢

转载自www.cnblogs.com/zxhyxiao/p/9315917.html