matlab实现又向图与无向图的最短路径
作者:互联网
无向图的最短路径
s = [9 9 1 1 2 2 2 7 7 6 6 5 5 4];
t = [1 7 7 2 8 3 5 8 6 8 5 3 4 3 ];
w = [4 8 3 8 2 7 4 1 6 6 2 14 10 9];%权重
G = graph(s,t,w);%生成稀疏矩阵
plot(G,'EdgeLabel',G.Edges.Weight,'LineWidth',2);
[P,D] = shortestpath(G,9,4);
%%高亮显示路径
myplot = plot(G,'EdgeLabel',G.Edges.Weight,'LineWidth',2);
highlight(myplot,P,'EdgeColor','r');
有向图的最短路径
clc,clear
w = [10,5,2,1,4,6,7,3,9,2];
DG = sparse([1,1,2,2,3,4,4,5,5,5],[2,5,5,3,4,3,1,2,3,4],w);
[dist,path,pred] = graphshortestpath(DG,1,3)
% biograph生成图对象,view显示该图
point_name = ['1','2','3','4','5'];
h = view(biograph(DG,point_name,'ShowWeights','on'))
% 将最短路径的节点和边缘标记为红色并增加线宽
% getedgesbynodeid得到图h的指定边的句柄
% 第一个参数是图,第二个是边的出点,第三个是边的入点
% 句柄确保能找到对应的东西
% get查询图的属性,h.Nodes(path),‘ID’得到图中最短路径
% set函数设置图形属性
edges = getedgesbynodeid(h,get(h.Nodes(path),'ID'));
set(edges,'LineColor',[1 0 0])
set(edges,'LineWidth',3)
标签:set,实现,DG,路径,edges,matlab,path,LineWidth 来源: https://www.cnblogs.com/java-six/p/16182753.html