从起点到终点的路径搜索(MATLAB)

来源:百度知道 编辑:UC知道 时间:2024/06/04 19:12:41
function [path,short_distance]=ShortPath_Dijkstra2(Input_weight,start,endpoint)
% Input parameters:
% Input_weight-------the input node weight!
% start--------the start node number;
% endpoint------the end node number;
% Output parameters:
% path-----the shortest lenght path from the start node to end node;
% short_distance------the distance of the shortest lenght path from the
% start node to end node.
[row,col]=size(Input_weight);

%input detection
if row~=col
error('input matrix is not a square matrix,input error ' );
end
if endpoint>row
error('input parameter endpoint exceed the maximal point number');
end
%initialization
s_path=[start];
distance=inf*ones(1,row);
distance(start)=0;
flag(start)=start;
temp=start;
while length(s_path)<row
pos=find(Input_weight(temp, : )~=inf);
n=length(pos);
for i=1:n

为什么要随机找呢?

for i=1:n
if length(find(s_path==pos(i)))
i=i+1;

else
break
end
end
temp_2=pos(i);

我建模时用的dijkstra算法,贴给你看看。。。。

function [p,v]=dijkstra(map,u1,u2)
%求网络最短路径的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---最短路径的总长度
%
%
%例如
% clear;map=[1 2 30;2 4 5;2 5 50;3 2 6;4 3 1;1 4 20;1 5 3]
% [p,v]=dijkstra(map,2,5)
%
%本算法调用由VC++6.0程序dijk.c生成的MEX文件dijk.dll求得最短路径
% 表示无穷大的数值上界(默认10000)
%
%See also KRUSKAL,LPINT,DP,BNBGUI,BNB18,

%By W. Z. Li, 2000

[m,n]=size(map);
mx=max(max