Matlab图论工具箱中shortestpath和graphshortestpath

文章介绍了在MATLAB中使用graphshortestpath函数处理无向图的最短路径问题,当graphshortestpath不可用时,转向使用shortestpath函数,并展示了如何处理有向图的情况,包括自环的处理。
摘要由CSDN通过智能技术生成

无向图 

在司守奎老师都书中,matlab图论工具箱中求最短路径及长度中提到了graphshortestpath函数,但是在某些matlab版本中graphshortestpath函数无法正常使用,原代码是:

clc, clear
a(1,2)=2;a(1,3)=8;a(1,4)=1;
a(2,3)=1;a(2,3)=6;a(2,5)=1;
a(3,4)=7;a(3,5)=5;a(3,6)=1;a(3,7)=2;
a(4,7)=9;
a(5,6)=3;a(5,8)=2;a(5,9)=9;
a(6,7)=4;a(6,9)=6;
a(7,9)=3;a(7,10)=1;
a(8,9)=7;a(8,11)=9;
a(9,10)=1;a(9,11)=2;
a(10,11)=4;
a=a';   %matlab工具箱要求数据是下三角矩阵
[i,j,v]=find(a);
b=sparse(i,j,v,11,11) %构造稀疏矩阵
[x,y,z]=graphshortestpath(b,1,11,'Directed',false) % Directed是标志图为有向或无向的属性,该图是无向图,对应的属性值为false,或0。

运行结果:

 所以我们可以使用shortestpath函数代替

%使用shortestpath函数,你可以计算从一个节点到另一个节点的最短路径。下面是该函数的使用示例:

G = graph(b,'lower'); % 将稀疏矩阵b转换为图对象G
[dist, path] = shortestpath(G, 1, 11); % 计算从节点1到节点11的最短路径

使用lower是因为在前部分操作中将a转换为下三角矩阵,你可以使用graph函数将稀疏矩阵b转换为图对象G。这个函数创建一个无向图,其中节点由稀疏矩阵的行和列索引表示,边由非零元素的位置表示。

 完整代码:

clc, clear
a(1,2)=2;a(1,3)=8;a(1,4)=1;
a(2,3)=1;a(2,3)=6;a(2,5)=1;
a(3,4)=7;a(3,5)=5;a(3,6)=1;a(3,7)=2;
a(4,7)=9;
a(5,6)=3;a(5,8)=2;a(5,9)=9;
a(6,7)=4;a(6,9)=6;
a(7,9)=3;a(7,10)=1;
a(8,9)=7;a(8,11)=9;
a(9,10)=1;a(9,11)=2;
a(10,11)=4;
a=a'   %matlab工具箱要求数据是下三角矩阵
[i,j,v]=find(a);
b=sparse(i,j,v,11,11) %构造稀疏矩阵
G = graph(b,'lower'); % 将稀疏矩阵b转换为图对象G
plot(G);
[dist, path] = shortestpath(G, 1, 11); % 计算从节点1到节点11的最短路径
disp("最短路径:");
disp(path);
disp("路径总权重:");
disp(dist);

运行结果:


最短路径:
    13

路径总权重:
     1     2     5     6     3     7    10     9    11

另外用其他方法可参考另一篇文章https://blog.csdn.net/zhounei/article/details/132044487 

有向图 

 对于有向图,求vs到vt的最短距离,我们可以用digraph函数

clc, clear
a=zeros(7);
a(1,2)=4; a(1,3)=2;
a(2,3)=3; a(2,4)=2; a(2,5)=6;
a(3,4)=5; a(3,6)=4;
a(4,5)=2; a(4,6)=7;
a(5,6)=4; a(5,7)=8;
a(6,7)=3;
b=sparse(a); %构造稀疏矩阵,这里给出构造稀疏矩阵的另一种方法
G = digraph(b,'omitselfloops');
plot(G)
[dist, path] = shortestpath(G, 1, 7); 
disp("最短路径:");
disp(path);
disp("路径总权重:");
disp(dist);

omitselfloops是MATLAB中digraph函数的一个可选参数,用于创建有向图对象时忽略自环(self-loops)。自环是指起点和终点相同的边,即一个节点指向自身的边。

通过在digraph函数中设置'omitselfloops'参数为true,可以在创建有向图对象时忽略自环。默认情况下,该参数为false,即保留自环。

 

 

GrTheory - Graph Theory Toolbox. 内含40个图论问题的matlab代码,包括最短径问题等。对数学建模,2012美赛ICM的帮助尤其大。欢迎下载。 Functions: grBase - find all bases of digraph; grCoBase - find all contrabases of digraph; grCoCycleBasis - find all independent cut-sets for a connected graph; grColEdge - solve the color problem for graph edges; grColVer - solve the color problem for graph vertexes; grComp - find all components of graph; grCycleBasis - find all independent cycles for a connected graph; grDecOrd - solve the problem about decomposition of the digraph to the sections with mutually accessed vertexes (strongly connected components); grDistances - find the distances between any vertexes of graph; grEccentricity - find the (weighted) eccentricity of all vertexes, radius, diameter, center vertexes and the periphery vertexes; grIsEulerian - find the Eulerian cycle of graph; grIsomorph - solve the problem about isomorphism for two graphs; grMaxComSu - solve the maximal complete sugraph problem for the graph; grMaxFlows - solve the maximal flow problem for the digraph; grMaxMatch - solve the maximal matching problem for the graph; grMaxStabSet - solve the maximal stable set problem for the graph; grMinAbsEdgeSet - solve the minimal absorbant set problem for the graph edges; grMinAbsVerSet - solve the minimal absorbant set problem for the graph vertexes; grMinCutSet - solve the minimal cut-set problem for the digraph; grMinEdgeCover - solve the minimal edge cover problem for the graph; grMinSpanTree - solve the minimal spanning tree problem for the graph; grMinVerCover - solve the minimal vertex cover problem for the graph; grPERT - solve the project evaluation research task; grPlot - draw the plot of the graph (digraph); grShortPath - solve the shortest path problem for the digraph; grShortVerPath - for digraph with weighted vertexes solve the problem about the path with minimal weight of verticies; grTranClos - built the transitive closure for the digraph; grTravSale - solve
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值