MATLAB解决最短路径问题代码.docx

上传人:牧羊曲112 文档编号:3161307 上传时间:2023-03-11 格式:DOCX 页数:4 大小:37.76KB
返回 下载 相关 举报
MATLAB解决最短路径问题代码.docx_第1页
第1页 / 共4页
MATLAB解决最短路径问题代码.docx_第2页
第2页 / 共4页
MATLAB解决最短路径问题代码.docx_第3页
第3页 / 共4页
MATLAB解决最短路径问题代码.docx_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《MATLAB解决最短路径问题代码.docx》由会员分享,可在线阅读,更多相关《MATLAB解决最短路径问题代码.docx(4页珍藏版)》请在三一办公上搜索。

1、MATLAB解决最短路径问题代码默认是Dijkstra 算法 是有权的, 我想如果把权都赋1的话, 就相当于没权的了 参数是带权的稀疏矩阵及结点 看看这两个例子(一个有向一个无向), 或许你能找到你想知道的 % Create a directed graph with 6 nodes and 11 edges W = .41 .99 .51 .32 .15 .45 .38 .32 .36 .29 .21; %这是权 DG = sparse(6 1 2 2 3 4 4 5 5 6 1,2 6 3 5 4 1 6 3 4 3 5,W) %有权的有向图 h = view(biograph(DG,Sh

2、owWeights,on) %画图, 这个好玩 % Find shortest path from 1 to 6 dist,path,pred = graphshortestpath(DG,1,6) %找顶点1到6的最短路径 % Mark the nodes and edges of the shortest path set(h.Nodes(path),Color,1 0.4 0.4) %上色 edges = getedgesbynodeid(h,get(h.Nodes(path),ID); set(edges,LineColor,1 0 0) %上色 set(edges,LineWidth

3、,1.5) %上色 下面是无向图的例子 % % Solving the previous problem for an undirected graph % UG = tril(DG + DG) % h = view(biograph(UG,ShowArrows,off,ShowWeights,on) % % Find the shortest path between node 1 and 6 % dist,path,pred = graphshortestpath(UG,1,6,directed,false) % % Mark the nodes and edges of the shor

4、test path % set(h.Nodes(path),Color,1 0.4 0.4) % fowEdges = getedgesbynodeid(h,get(h.Nodes(path),ID); % revEdges = getedgesbynodeid(h,get(h.Nodes(fliplr(path),ID); % edges = fowEdges;revEdges; % set(edges,LineColor,1 0 0) % set(edges,LineWidth,1.5) clc;close all; clear; load data; % global quyu; quy

5、u = 2,3;%一片区域 z_jl = lxjl(jdxx,lxxh);%计算路线的距离 z = qyxz(jdxx,quyu,z_jl); % 根据节点信息,从z中将y区域的节点和路线选出所有点的信息 hzlx(z); %绘制Z的图像 qypt, nqypt = ptxzm(xjpt,quyu); changdu = length(bhxz(jdxx,1:6);%选出x中y区的标号,只是分区域,求长度并 绘制它 tt = z(:,1,2,end); k = min(min(tt(1:2,:); %求两次最小值 t = tt(1:2,:) ; xsjz = sparse(t(2,:),t(1

6、,:),tt(3,:),changdu,changdu); %产生稀疏矩阵 dist, path, pred = zdljxz(xsjz, qypt, k ); %三个原包矩阵通过zdljxz计算得到最短路径 hold on for j = 1:nqypt colors = rand(1,3); %产生随机数并用颜色标记 hzptxc(pathj,jdxx,colors) end hold off axis equal %把坐标轴单位设为相等 zjd = jdfgd( path, quyu); function z = lxjl(x, y) %计算路线的距离 m n = size(y); fo

7、r i = 1:m yy(i,1:2) = x(y(i,1),2:3); yy(i,3:4) = x(y(i,2),2:3); end z = sqrt(yy(:,3) - yy(:,1).2 + (yy(:,2) - yy(:,4).2); y = sort(y); y = y; z = y yy z; z = sortrows(z); function z lz = ptxz(xjpt,y) pt = xjpt(:,2); wei = ismember(xjpt(:,1),y); z = pt(wei); lz = length(z); unction hzptxc(path,jdxx,c

8、olors) n = length(path); % hold on for i = 1:n hzptjd(jdxx, pathi,colors) end % hold off unction hzptjd(jdxx,x,colors) % m = length(x); % x = x; hold on plot(jdxx(x,2),jdxx(x,3),o,LineStyle ,- ,. Color,colors,MarkerEdgeColor,colors) plot(jdxx(x(1),2),jdxx(x(1),3),*,MarkerFaceColor,colors) hold off function hzlx(x) %绘制x的图像 m n = size(x); hold on for i = 1:m plot(x(i,3) x(i,5),x(i,4) x(i,6),k:) end hold off function z = bhxz(x,y) %选出x中y区的标号,只是分区域 xzq = x(:,4); xzr = ismember(xzq,y); z = x(xzr,:); z = z(:,1);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号