資源描述:
《matlab解決最短路徑問題代碼》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、默認(rèn)是Dijkstra算法是有權(quán)的,我想如果把權(quán)都賦1的話,就相當(dāng)于沒權(quán)的了參數(shù)是帶權(quán)的稀疏矩陣及結(jié)點(diǎn)看看這兩個(gè)例子(一個(gè)有向一個(gè)無向),或許你能找到你想知道的%Createadirectedgraphwith6nodesand11edgesW=[.41.99.51.32.15.45.38.32.36.29.21];%這是權(quán)DG=sparse([61223445561],[26354163435],W)%有權(quán)的有向圖h=view(biograph(DG,[],'ShowWeights','on'))
2、%畫圖,這個(gè)好玩%Findshortestpathfrom1to6[dist,path,pred]=graphshortestpath(DG,1,6)%找頂點(diǎn)1到6的最短路徑%Markthenodesandedgesoftheshortestpathset(h.Nodes(path),'Color',[10.40.4])%上色edges=getedgesbynodeid(h,get(h.Nodes(path),'ID'));set(edges,'LineColor',[100])%上色set(edg
3、es,'LineWidth',1.5)%上色下面是無向圖的例子%%Solvingthepreviousproblemforanundirectedgraph%UG=tril(DG+DG')%h=view(biograph(UG,[],'ShowArrows','off','ShowWeights','on'))%%Findtheshortestpathbetweennode1and6%[dist,path,pred]=graphshortestpath(UG,1,6,'directed',false
4、)%%Markthenodesandedgesoftheshortestpath%set(h.Nodes(path),'Color',[10.40.4])%fowEdges=getedgesbynodeid(h,get(h.Nodes(path),'ID'));%revEdges=getedgesbynodeid(h,get(h.Nodes(fliplr(path)),'ID'));%edges=[fowEdges;revEdges];%set(edges,'LineColor',[100])%se
5、t(edges,'LineWidth',1.5)clc;closeall;clear;loaddata;%globalquyu;quyu=[2,3];%一片區(qū)域z_jl=lxjl(jdxx,lxxh);%計(jì)算路線的距離z=qyxz(jdxx,quyu,z_jl);%根據(jù)節(jié)點(diǎn)信息,從z中將y區(qū)域的節(jié)點(diǎn)和路線選出所有點(diǎn)的信息hzlx(z);%繪制Z的圖像[qypt,nqypt]=ptxzm(xjpt,quyu);changdu=length(bhxz(jdxx,1:6));%選出x中y區(qū)的標(biāo)號(hào),只是分區(qū)
6、域,求長度并繪制它tt=z(:,[1,2,end])';k=min(min(tt(1:2,:)));%求兩次最小值t=tt(1:2,:);xsjz=sparse(t(2,:),t(1,:),tt(3,:),changdu,changdu);%產(chǎn)生稀疏矩陣[dist,path,pred]=zdljxz(xsjz,qypt,k);%三個(gè)原包矩陣通過zdljxz計(jì)算得到最短路徑holdonforj=1:nqyptcolors=rand(1,3);%產(chǎn)生隨機(jī)數(shù)并用顏色標(biāo)記hzptxc(path{j},jdx
7、x,colors)endholdoffaxisequal%把坐標(biāo)軸單位設(shè)為相等zjd=jdfgd(path,quyu);functionz=lxjl(x,y)%計(jì)算路線的距離[mn]=size(y);fori=1:myy(i,1:2)=x(y(i,1),2:3);yy(i,3:4)=x(y(i,2),2:3);endz=sqrt((yy(:,3)-yy(:,1)).^2+(yy(:,2)-yy(:,4)).^2);y=sort(y');y=y';z=[yyyz];z=sortrows(z);func
8、tion[zlz]=ptxz(xjpt,y)pt=xjpt(:,2);wei=ismember(xjpt(:,1),y);z=pt(wei);lz=length(z);unctionhzptxc(path,jdxx,colors)n=length(path);%holdonfori=1:nhzptjd(jdxx,path{i},colors)end%holdoffunctionhzptjd(jdxx,x,colors)%m=length(x);%x=x';holdon