具体程序见附件
程序一:
%求网络最短路径的dijkstra算法
%用法:
% 首先输入矩阵:
% map=[起点1 终点1 边长1;起点2 终点2 边长2;............;起点n 终点n 边长n]
% 和u1,u2
% 注意:这里map为无向图。
% 再用[p,v]=dijkstra(map,u1,u2)求最短路径
%参数说明
% map----3列邻接矩阵,每行表示一条边.
% 第一列表示起点,第二列表示终点,第三列表示边长
% u1---所求路径起点
% u2---所求路径终点
% p---输出最短路径
% v---最短路径的总长度
%例如
%map=[1 2 6;1 3 1;2 3 6;2 4 3;2 5 4;2 6 1;3 5 2;3 6 7;4 5 5;5 6 2];
% [p,v]=dijkstra(map,2,5)
%
%本算法调用由VC++6.0程序dijk.c生成的MEX文件dijk.dll求得最短路径
% 表示无穷大的数值上界(默认10000)
%
%See also KRUSKAL,LPINT,DP,BNBGUI,BNB18
程序二:
%图与网络论中求最短路径的Dijkstra算法 M-函数
%格式 [S,D]=minroute(i,m,W)
% i为最短路径的起始点,m为图顶点数,W为图的带权邻接矩阵,
% 不构成边的两顶点之间的权用inf表示。显示结果为:S的每
% 一列从上到下记录了从始点到终点的最短路径所经顶点的序号;
% D是一行向量,记录了S中所示路径的大小;
%例如
% clear;w=inf*ones(6);w(1,3)=10;w(1,5)=30;
% w(1,6)=100;w(2,3)=5;w(3,4)=50;w(4,6)=10;
% w(5,4)=20;w(5,6)=60;
% i=1;[s,d]=minroute(i,6,w)
程序三:
%DIJKSTRA Calculates the shortest distance and path between points on a map
% using Dijkstra's Shortest Path Algorithm
%
% [DIST, PATH] = DIJKSTRA(NODES, SEGMENTS, SID, FID)
% Calculates the shortest distance and path between start and finish nodes SID and FID
%
% [DIST, PATH] = DIJKSTRA(NODES, SEGMENTS, SID)
% Calculates the shortest distances and paths from the starting node SID to all
% other nodes in the map
%
% Note:
% DIJKSTRA is set up so that an example is created if no inputs are provided,
% but ignores the example and just processes the inputs if they are given.
%
% Inputs:
% NODES should be an Nx3 or Nx4 matrix with the format [ID X Y] or [ID X Y Z]
% where ID is an integer, and X, Y, Z are cartesian position coordinates)
% SEGMENTS should be an Mx3 matrix with the format [ID N1 N2]
% where ID is an integer, and N1, N2 correspond to node IDs from NODES list
% such that there is an [undirected] edge/segment between node N1 and node N2
% SID should be an integer in the node ID list corresponding with the starting node
% FID (optional) should be an integer in the node ID list corresponding with the finish
%
% Outputs:
% DIST is the shortest Euclidean distance
% If FID was specified, DIST will be a 1x1 double representing the shortest
% Euclidean distance between SID and FID along the map segments. DIST will have
% a value of INF if there are no segments connecting SID and FID.
% If FID was not specified, DIST will be a 1xN vector representing the shortest
% Euclidean distance between SID and all other nodes on the map. DIST will have
% a value of INF for any nodes that cannot be reached along segments of the map.
% PATH is a list of nodes containing the shortest route
% If FID was specified, PATH will be a 1xP vector of node IDs from SID to FID.
% NAN will be returned if there are no segments connecting SID to FID.
% If FID was not specified, PATH will be a 1xN cell of vectors representing the
% shortest route from SID to all other nodes on the map. PATH will have a value
% of NAN for any nodes that cannot be reached along the segments of the map.
%
% Example:
% dijkstra; % calculates shortest path and distance between two nodes
% % on a map of randomly generated nodes and segments
%
% Example:
% nodes = [(1:10); 100*rand(2,10)]';
% segments = [(1:17); floor(1:0.5:9); ceil(2:0.5:10)]';
% figure; plot(nodes(:,2), nodes(:,3),'k.');
% hold on;
% for s = 1:17
% if (s <= 10) text(nodes(s,2),nodes(s,3),[' ' num2str(s)]); end
% plot(nodes(segments(s,2:3)',2),nodes(segments(s,2:3)',3),'k');
% end
% [d, p] = dijkstra(nodes, segments, 1, 10)
% for n = 2:length(p)
% plot(nodes(p(n-1:n),2),nodes(p(n-1:n),3),'r-.','linewidth',2);
% end
% hold off;
%
% Author: Joseph Kirk
% Email: jdkirk630 at gmail dot com
% Release: 1.3
% Release Date: 5/18/07