最短路径问题matlab求解详尽版

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

最短路径问题m a t l a b

求解详尽版

Document serial number【UU89WT-UU98YT-UU8CB-UUUT-UUT108】

MATLAB 求最短路径

利用graphshortestpath 可以求最短路径,具体用法参考MATLAB帮助Examples:

S=[1 1 2 2 3 3 4 4 4 4 5 6 6 7 8]; %起始节点向量

E=[2 3 5 4 4 6 5 7 8 6 7 8 9 9 9]; %终止节点向量

W=[1 2 12 6 3 4 4 15 7 2 7 7 15 3 10]; %边权值向量,有向图,

G(9,9)=0; 9个节点

G=sparse(S,E,W); %关联矩阵的稀疏矩阵表示

G(9,9)=0;

P=biograph(G,[],'ShowWeights','on');%建立有向图对象P

H=view(P);%显示各个路径权值

[Dist,Path]=graphshortestpath(G,1,9,'Method','Dijkstra') %求节点1到节点9的最短路径

set(Path),'Color',[1 ]);%以下三条语句用红色修饰最短路径edges=getedgesbynodeid(H,get(Path),'ID'));

set(edges,'LineColor',[1 0 0]);

set(edges,'LineWidth',;

%以下是运行结果,节点1到节点9的最短路径为19

Dist =

19

Path =

1 3 4 5 7 9

利用graphallshortestpaths可以求出所有最短路径Dists=graphallshortestpaths(G) %求所有最短路径Dists =

0 1 2 5 9 6 16 12 19

Inf 0 Inf 6 10 8 17 13 20

Inf Inf 0 3 7 4 14 10 17

Inf Inf Inf 0 4 2 11 7 14

Inf Inf Inf Inf 0 Inf 7 Inf 10

Inf Inf Inf Inf Inf 0 Inf 7 15

Inf Inf Inf Inf Inf Inf 0 Inf 3

Inf Inf Inf Inf Inf Inf Inf 0 10

Inf Inf Inf Inf Inf Inf Inf Inf 0

相关文档
最新文档