matlab dijkstra算法求解最短路径例题

合集下载

Dijkstra求最短路的MATLAB程序(含注释)

Dijkstra求最短路的MATLAB程序(含注释)

Dijkstra求解最短路(含注释)function [P,D]=dijkstra_pt(A,sv)%Dijkstra法求解最短路%A为邻接矩阵;%sv为寻求最短路的起始点%P为所有点的P标号,即路权值%D为sv到所有结点的最短路径矩阵%2010年8月28日凌晨1:41[n,n]=size(A);s=sv;T=inf.*ones(1,n);%T标号初始化P=inf.*ones(1,n);%P标号初始化Tv=1:1:n;%具有T标号的点,初始时,所有点均为T标号v=zeros(1,n);%结点的前驱,初始时,均为0Tm=zeros(n,n);%所有点从P标号变为T标号的过程矩阵P(s)=0;for i=1:n%将所有结点从P标号变为T标号的过程Pv(i)=s;%Pv具有P标号的结点Tv=Tmark(Tv,s);%删去具有P标号的结点Tm(s,:)=A(s,:);for k=Pv%将具有P标号的点赋值无穷大,从而不影响后面的程序取最小值Tm(s,k)=inf;T(k)=inf;endfor k=Tv%一次修改P标号点所对应的T标号点的T标号Tm(s,k)=Tm(s,k)+P(s);endfor k=Tv[x,val]=min([T(k),Tm(s,k)]);T(k)=x;%二次修改P标号点所对应的T标号点的T标号if val==2v(k)=s;%修改P标号点所对应的T标号点的前驱endend[x,val]=min(T);%寻找P标号点if x==infbreak;ends=val;P(s)=x;%修改P标号end%下面求解从sv到各点的最短路矩阵aad=zeros(1,n);%最短路临时存储向量for i=n:-1:1w=i;for k=1:n%将sv到i点的最短路倒序存储在aad中if w==0break;endaad(k)=w;w=v(w);if w==svaad(k+1)=w;break;endendfor l=1:n%将sv到i点的最短路顺序存储在D中if aad(l)==svk=1;for j=l:-1:1D(i,k)=aad(j);k=k+1;endendendaad=zeros(1,n);end[g,h]=size(D);for i=1:g%将与最短路径无关的点赋值NaNfor j=1:h%con由上面计算得到if D(i,j)==0D(i,j)=NaN;endendend%下面为在T标号结点集合中删除P标号的子函数function Tvad=Tmark(Tv,vm)tg=length(Tv);for i=1:tgif Tv(i)==vm;wd=i;break;endendTvad=[Tv(1,1:wd-1),Tv(1,wd+1:tg)];。

最短路dijkstra算法Matlab程序

最短路dijkstra算法Matlab程序

function [c0,c,path0,path]=dijkstra(s,t,C,flag)% Use the Dijkstra's algorithm to find the shortest path from% s to t and can also find the shortest path between s and all% the other points.% Reference: Graph Theory with Applications by J. A. Bondy and% U. S. R. Murty.% Input -- s is the starting point and also is the point s.% -- t is the given terminal point and is the point t.% -- C \in R^{n \times n}is the cost matrix, where% C(i,j)>=0 is the cost from point i to point j.% If there is no direct connection between point i and% j, C(i,j)=inf.% -- flag: if flag=1, the function just reports the% shortest path between s and t; if flag~=1, the% function reports the shortest path between s and t,% and the shortest paths between s and other points.% Output -- c0 is the minimal cost from s to t.% -- path0 denotes the shortest path form s to t.% -- c \in R{1\times n} in which the element i is the% minimal cost from s to point i.% -- path \in R^{n \times n} in which the row i denotes% the shortest path from s to point i.% Copyright by MingHua Xu(徐明华), Changhzou University, 27 Jan. 2014. s=floor(s);t=floor(t);n=size(C,1);if s<1 || t < 1 || s > n || t > nerror(' The starting point and the terminal point exceeds the valid range');endif t==sdisp('The starting point and the terminal point are the same points');endlabel=ones(1,n)*inf;label(s)=0;S=[s];Sbar=[1:s-1,s+1:n];c0=0;path=zeros(n,n);path(:,1)=s;c=ones(1,n)*inf;parent=zeros(1,n);i=1; % number of points in point set S.while i<n% for each point in Sbar, replace label(Sbar(j)) by% min(label(Sbar(j)),label(S(k))+C(S(k),Sbar(j)))for j=1:n-ifor k=1:iif label(Sbar(j)) > label(S(k))+C(S(k),Sbar(j))label(Sbar(j))=label(S(k))+C(S(k),Sbar(j));parent(Sbar(j))=S(k);endendend% Find the minmal label(j), j \in Sbar.temp=label(Sbar(1));son=1;for j=2:n-iif label(Sbar(j))< temptemp=label(Sbar(j));son=j;endend% update the point set S and SbarS=[S,Sbar(son)];Sbar=[Sbar(1:son-1),Sbar(son+1:n-i)];i=i+1;% if flag==1, just output the shortest path between s and t.if flag==1 && S(i)==tson=t;temp_path=[son];if son~=swhile parent(son)~=sson=parent(son);temp_path=[temp_path,son];endtemp_path=[temp_path,s];endtemp_path=fliplr(temp_path);m=size(temp_path,2);path0(1:m)=temp_path;c_temp=0;for j=1:m-1c_temp=c_temp+C(temp_path(j),temp_path(j+1));endc0=c_temp;path(t,1:m)=path0;c(t)=c0;returnendend% Form the output resultsfor i=1:nson=i;temp_path=[son];if son~=swhile parent(son)~=sson=parent(son);temp_path=[temp_path,son];endtemp_path=[temp_path,s];endtemp_path=fliplr(temp_path);m=size(temp_path,2);path(i,1:m)=temp_path;c_temp=0;for j=1:m-1c_temp=c_temp+C(temp_path(j),temp_path(j+1));endc(i)=c_temp;c0=c(t);path0=path(t,:);endreturn。

图论算法及matlab程序的三个案例

图论算法及matlab程序的三个案例

图论实验三个案例单源最短路径问题 1.1 Dijkstra 算法Dijkstra 算法是解单源最短路径问题的一个贪心算法。

其基本思想是,设置 一个顶点集合S 并不断地作贪心选择来扩充这个集合。

一个顶点属于集合S 当且 仅当从源到该顶点的最短路径长度已知。

设 v 是图中的一个顶点,记l(v)为顶点 v 到源点V 1的最短距离,V i,V jV ,若(V i,V j)E ,记“到百的权w 。

Dijkstra 算法:① S {V J I(V J 0 ; V V {可 1(V ) i i S V {V J ;J7JJJ7②S,停止,否则转③;l(v) min{ l(v) , d(V j ,v)}V j S④ 存在Vi 1,使l (V i l) min{l(V)},V S ;⑤SSU{v i 1}S S {v i 1}i i 1实际上,Dijkstra 算法也是最优化原理的应用:如果V 1V 2LV n1Vn是从V1到Vn的最短路径,贝UV 1V 2L Vn1也必然是从V1到Vn 1的最优路径。

在下面的MATLA 实现代码中,我们用到了距离矩阵,矩阵第 i 行第j 行元 素表示顶点Vi到Vj的权Wj,若v 到V j无边,则W ijrealmax,其中realmax 是 MATLA 常量,表示最大的实数(1.7977e+308)function re=Dijkstra(ma)%用Dijkstra 算法求单源最短路径%俞入参量ma是距离矩阵%输出参量是一个三行n 列矩阵,每列表示顶点号及顶点到源的最短距离和前顶点n=size(ma,1);% 得到距离矩阵的维数s=ones(1,n);s(1)=0;% 标记集合S和S 的补r=zeros(3,n);r(1,:)=1:n;r(2,2:end)=realmax;% 初始化for i=2:n;% 控制循环次数mm=realmax;for j=find(s==0);% 集合S中的顶点for k=find(s==1);% 集合S补中的顶点if(r(2,j)+ma(j,k)<r(2,k))r(2,k)=r(2,j)+ma(j,k);r(3,k)=j;endif(mm>r(2,k))mm=r(2,k);t=k;endendends(1,t)=0;%找到最小的顶点加入集合Send re=r;1.2动态规划求解最短路径动态规划是美国数学家 Richard Bellman 在1951年提出来的分析一类多阶 段决策过程的最优化方法,在工程技术、工业生产、经济管理、军事及现代化控 制工程等方面均有着广泛的应用。

利用MATLAB实现Dijkstra算法

利用MATLAB实现Dijkstra算法

利用计算机语言编程实现D算法一:实验目的本实验课程主要目的是让学生够熟练掌握图论中的D算法。

二:实验方法选择MATLAB语言编程实现D算法。

三:实验要求1.输入必要参数,包括:节点个数、节点间路径长度、给定节点;2.输出给定节点到其它各节点的最短路径、径长;3.节点间路径长度用矩阵形式表示。

四:实验内容无向图共有7个节点,如下图所示。

v1457计算机输入的节点间路径长度为7×7矩阵:12345671 2 3 4 5 6 7012310620543045407 6408780⎡⎤∞∞∞⎢⎥∞∞∞∞⎢⎥⎢⎥∞∞∞⎢⎥∞∞∞∞⎢⎥⎢⎥∞∞∞⎢⎥∞∞∞⎢⎥⎢⎥∞∞∞∞⎣⎦v v v v v v vvvvvvvv若1v为指定节点,则1v到其它各节点的最短路径及径长的计算机计算结果为:提示:不相邻的两个节点间∞可以用相对较大的数代替(如输入100表示∞)五:实验原理1. D 算法原理已知图G=(V,E),将其节点集分为两组:置定节点集p G 和未置定节点集p G G -。

其中p G 内的所有置定节点,是指定点s v 到这些节点的路径为最短(即已完成最短路径的计算)的节点。

而p G G -内的节点是未置定节点,即s v 到未置定节点距离是暂时的,随着算法的下一步将进行不断调整,使其成为最短径。

在调整各未置定节点的最短径时,是将p G 中的节点作为转接点。

具体地说,就是将p G 中的节点作为转接点,计算(s v ,j v )的径长(j p v G G ∈-),若该次计算的径长小于上次的值,则更新径长,否则,径长不变。

计算后取其中径长最短者,之后将j v 划归到p G 中。

当(p G G -)最终成为空集,同时p G G =,即求得s v 到所有其他节点的最短路径。

j w 表示s v 与其他节点的距离。

在p G 中,i w 表示上一次划分到p G 中的节点iv 到s v 得最短路径。

在 p G G -中,表示s v 到j v (j p v G G ∈-)仅经过p G 中的节点作为转接点所求得的该次的最短路径的长度。

matlab最短路径案例

matlab最短路径案例

matlab最短路径案例在实际生活和工作中,我们经常会遇到需要找到最短路径的问题,例如在物流配送中,我们需要计算货物从出发地到目的地的最短路线,以提高效率和节约成本。

在这种情况下,MATLAB是一种非常有效的工具,可以帮助我们快速计算出最短路径。

最短路径问题是计算图中两个节点之间最短路径的问题。

在MATLAB中,我们可以使用Graph和Dijkstra算法来实现最短路径的计算。

首先,我们需要构建一个图,用来表示节点和边。

在MATLAB中,我们可以使用Graph对象来表示图,并且可以使用addnode和addedge函数来添加节点和边。

G = graph();G = addnode(G, 5); % 添加5个节点G = addedge(G, 1, 2, 10); % 添加边,每条边都有一个权重G = addedge(G, 1, 3, 15);G = addedge(G, 2, 3, 8);G = addedge(G, 2, 4, 2);G = addedge(G, 3, 4, 6);G = addedge(G, 4, 5, 12);上面的代码创建了一个图,其中包含5个节点和6条边。

每条边都有一个权重,代表两个节点之间的距离。

接下来,我们可以使用dijkstra函数来计算最短路径。

这个函数需要指定图、起始节点和目标节点。

[start_node, end_node, shortest_dist] = shortestpath(G, 1, 5);上面的代码计算了图G中从节点1到节点5的最短路径,并且返回了起始节点、终止节点和最短路径的长度。

最后,我们可以使用plot函数将最短路径可视化。

plot(G, 'EdgeLabel', G.Edges.Weight) % 可视化图highlight(G, shortest_path, 'EdgeColor', 'r') % 高亮显示最短路径通过以上步骤,我们可以使用MATLAB计算并可视化最短路径。

MATLAB解决最短路径问题代码

MATLAB解决最短路径问题代码

默认是Dijkstra 算法是有权的, 我想如果把权都赋1的话, 就相当于没权的了参数是带权的稀疏矩阵及结点看看这两个例子(一个有向一个无向), 或许你能找到你想知道的% Create a directed graph with 6 nodes and 11 edgesW = [.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,[],'ShowWeights','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 pathset(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',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 shortest 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;quyu = [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,:),tt(3,:),changdu,changdu);%产生稀疏矩阵[dist, path, pred] = zdljxz(xsjz, qypt, k );%三个原包矩阵通过zdljxz计算得到最短路径hold onfor j = 1:nqyptcolors = rand(1,3);%产生随机数并用颜色标记hzptxc(path{j},jdxx,colors)endhold offaxis equal%把坐标轴单位设为相等zjd = jdfgd( path, quyu);function z = lxjl(x, y)%计算路线的距离[m n] = size(y);for i = 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 = [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,colors)n = length(path);% hold onfor i = 1:nhzptjd(jdxx, path{i},colors)end% hold offunction hzptjd(jdxx,x,colors)% m = length(x);% x = x';hold onplot(jdxx(x,2),jdxx(x,3),'o','LineStyle' ,'-' ,...'Color',colors,'MarkerEdgeColor',colors)plot(jdxx(x(1),2),jdxx(x(1),3),'*','MarkerFaceColor',colors)hold offfunction hzlx(x)%绘制x的图像[m n] = size(x);hold onfor i = 1:mplot([x(i,3) x(i,5)],[x(i,4) x(i,6)],'k:')endhold offfunction z = bhxz(x,y)%选出x中y区的标号,只是分区域xzq = x(:,4);xzr = ismember(xzq,y);z = x(xzr,:);z = z(:,1);。

最短路dijkstra算法Matlab程序

最短路dijkstra算法Matlab程序

function [c0,c,path0,path]=dijkstra(s,t,C,flag)% Use the Dijkstra's algorithm to find the shortest path from% s to t and can also find the shortest path between s and all% the other points.% Reference: Graph Theory with Applications by J. A. Bondy and% U. S. R. Murty.% Input -- s is the starting point and also is the point s.% -- t is the given terminal point and is the point t.% -- C \in R^{n \times n}is the cost matrix, where% C(i,j)>=0 is the cost from point i to point j.% If there is no direct connection between point i and% j, C(i,j)=inf.% -- flag: if flag=1, the function just reports the% shortest path between s and t; if flag~=1, the% function reports the shortest path between s and t,% and the shortest paths between s and other points.% Output -- c0 is the minimal cost from s to t.% -- path0 denotes the shortest path form s to t.% -- c \in R{1\times n} in which the element i is the% minimal cost from s to point i.% -- path \in R^{n \times n} in which the row i denotes% the shortest path from s to point i.% Copyright by MingHua Xu(徐明华), Changhzou University, 27 Jan. 2014. s=floor(s);t=floor(t);n=size(C,1);if s<1 || t < 1 || s > n || t > nerror(' The starting point and the terminal point exceeds the valid range');endif t==sdisp('The starting point and the terminal point are the same points');endlabel=ones(1,n)*inf;label(s)=0;S=[s];Sbar=[1:s-1,s+1:n];c0=0;path=zeros(n,n);path(:,1)=s;c=ones(1,n)*inf;parent=zeros(1,n);i=1; % number of points in point set S.while i<n% for each point in Sbar, replace label(Sbar(j)) by% min(label(Sbar(j)),label(S(k))+C(S(k),Sbar(j)))for j=1:n-ifor k=1:iif label(Sbar(j)) > label(S(k))+C(S(k),Sbar(j))label(Sbar(j))=label(S(k))+C(S(k),Sbar(j));parent(Sbar(j))=S(k);endendend% Find the minmal label(j), j \in Sbar.temp=label(Sbar(1));son=1;for j=2:n-iif label(Sbar(j))< temptemp=label(Sbar(j));son=j;endend% update the point set S and SbarS=[S,Sbar(son)];Sbar=[Sbar(1:son-1),Sbar(son+1:n-i)];i=i+1;% if flag==1, just output the shortest path between s and t.if flag==1 && S(i)==tson=t;temp_path=[son];if son~=swhile parent(son)~=sson=parent(son);temp_path=[temp_path,son];endtemp_path=[temp_path,s];endtemp_path=fliplr(temp_path);m=size(temp_path,2);path0(1:m)=temp_path;c_temp=0;for j=1:m-1c_temp=c_temp+C(temp_path(j),temp_path(j+1));endc0=c_temp;path(t,1:m)=path0;c(t)=c0;returnendend% Form the output resultsfor i=1:nson=i;temp_path=[son];if son~=swhile parent(son)~=sson=parent(son);temp_path=[temp_path,son];endtemp_path=[temp_path,s];endtemp_path=fliplr(temp_path);m=size(temp_path,2);path(i,1:m)=temp_path;c_temp=0;for j=1:m-1c_temp=c_temp+C(temp_path(j),temp_path(j+1));endc(i)=c_temp;c0=c(t);path0=path(t,:);endreturn。

D_i_j_k_s_t_r_a最短路算法MATLAB程序_

D_i_j_k_s_t_r_a最短路算法MATLAB程序_

从起点sb到终点db通用的Dijkstra标号算法程序function [mydistance,mypath]=mydijkstra(a,sb,db);% 输入:a—邻接矩阵,a(i,j)是指i到j之间的距离,可以是有向的% sb—起点的标号, db—终点的标号% 输出:mydistance—最短路的距离, mypath—最短路的路径n=size(a,1); visited(1:n) = 0;distance(1:n) = inf; distance(sb) = 0; %起点到各顶点距离的初始化visited(sb)=1; u=sb; %u为最新的P标号顶点parent(1:n) = 0; %前驱顶点的初始化for i = 1: n-1id=find(visited==0); %查找未标号的顶点for v = idif a(u, v) + distance(u) < distance(v)distance(v) = distance(u) + a(u, v); %修改标号值parent(v) = u;endendtemp=distance;temp(visited==1)=inf; %已标号点的距离换成无穷[t, u] = min(temp); %找标号值最小的顶点visited(u) = 1; %标记已经标号的顶点endmypath = [];if parent(db) ~= 0 %如果存在路!t = db; mypath = [db];while t ~= sb %从终点db开始回溯p = parent(t);mypath = [p mypath];t = p;endendmydistance = distance(db);例题:运筹学教材P205 第七题D=[0 3 6 1 inf inf inf inf;inf 0 2 inf 4 6 inf inf;inf inf 0 inf inf 5 inf inf;inf inf 4 0 inf 3 6 inf;inf inf inf inf 0 inf inf 7;inf inf inf inf inf 0 7 11;inf inf inf inf inf inf 0 8;inf inf inf inf inf inf inf 0]在command window输入:[mydistance,mypath]=mydijkstra(D,1,8);。

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

matlab dijkstra算法求解最短路径例题
摘要:
一、Dijkstra 算法简介
1.Dijkstra 算法背景
2.Dijkstra 算法原理
二、MATLAB 实现Dijkstra 算法求解最短路径
1.创建图对象
2.计算最短路径
3.可视化结果
三、Dijkstra 算法应用示例
1.例题描述
2.解题步骤
3.结果分析
正文:
一、Dijkstra 算法简介
Dijkstra 算法是一种经典的图论算法,用于计算图中两个节点之间的最短路径。

它是由荷兰计算机科学家Edsger W.Dijkstra 于1956 年提出的,其基本思想是以起始点为中心向外层层扩展,直到扩展到终点为止。

Dijkstra 算法能得出最短路径的最优解,但由于它遍历计算的节点很多,所以效率低。

可以用堆优化来提高效率。

二、MATLAB 实现Dijkstra 算法求解最短路径
1.创建图对象
首先,我们需要使用MATLAB 的graph 函数创建一个图对象,指定节点和边的信息。

例如,我们创建一个简单的图,包含4 个节点和3 条边:```matlab
G = graph(4, 3);
```
其中,4 表示图中有4 个节点,3 表示图中有3 条边。

2.计算最短路径
接下来,我们可以使用MATLAB 的shortestpath 函数计算两个节点之间的最短路径。

例如,我们计算节点1 到节点3 的最短路径:```matlab
SP = shortestpath(G, 1, 3);
```
3.可视化结果
最后,我们可以使用MATLAB 的plot 函数将最短路径可视化。

例如,我们绘制节点和边以及最短路径:
```matlab
plot(G, SP);
```
三、Dijkstra 算法应用示例
以下是一个使用Dijkstra 算法求解最短路径的例题:
在一个图中,有4 个节点和3 条边,如下所示:
```
1 --
2 -- 3
| /
| /
| /
| /
|/
4
```
请问,节点1 到节点4 的最短路径是多少?。

相关文档
最新文档