#################本文为学习《图论算法及其MATLAB实现》的学习笔记#################
算法用途
通过指定两顶点的最短路的求法
算法思想
程序参数说明
W: 图的权值矩阵
k1: 始点
k2: 终点
t1,t2: 指定的两顶点
P: 程序最终得到的路
u: 路的距离
算法的matlab程序详解
其中调用的 n2shortf() 函数详情
%求必须通过指定两个点的最短通路
function [ P,d ] = cn2shorf( W,k1,k2,t1,t2 )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% 输入: W: 图的权值矩阵
%%%%%%%%% k1: 始点
%%%%%%%%% k2: 终点
%%%%%%%%% t1,t2: 指定的两顶点
%%%%%%%%% 输出: P: 程序最终得到的路
%%%%%%%%% u: 路的距离
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 注:路中重复的点记作一个点
[p1 , d1] = n2shorf(W,k1,t1); % 计算k1 ,t1之间的最短路
[p2 , d2] = n2shorf(W,t1,t2); % 计算t1 ,t2之间的最短路
[p3 , d3] = n2shorf(W,t2,k2); % 计算t2 , k2之间的最短路
dt1 = d1 + d2 + d3; % 计算k1,t1,t2,k2顺序的长度
[p4 , d4] = n2shorf(W,k1,t2); % 计算k1 ,t2之间的最短路
[p5 , d5] = n2shorf(W,t2,t1); % 计算t2 ,t1之间的最短路
[p6 , d6] = n2shorf(W,t1,k2); % 计算t1 ,k2之间的最短路
dt2 = d4 + d5 + d6; % 计算k1,t2,t1,k2顺序的长度
%%%%%%%%%%% 比较两个和值 %%%%%%%%%%%
if dt1 < dt2
d = dt1;
P = [p1 p2(2:length(p2)) p3(2:length(p3))];
else
d = dt2;
P = [p4 p5(2:length(p5)) p6(2:length(p6))];
end
P;
d;