基于MATLAB的djikstra算法实现

lyqmath

http://blog.sina.com.cn/lyqmath

简介
dijkstra算法(迪杰斯特拉算法)是一种经典的优化算法。以其应用的广泛性与简便性,值得我们去研究。
Dijkstra算法是典型最短路算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。Dijkstra算法能得出最短路径的最优解,但由于它遍历计算的节点很多,所以效率低。(摘自网络,呵呵)
实例
这里给出一个基于MATLAB的dijkstra算法的实现函数,并给出MATLAB已有的dijkstra算法函数的调用情况。给出一个具体的例子。
路径分布图

基于MATLAB的dijkstra算法及其应用 


结果信息

基于MATLAB的dijkstra算法及其应用 


代码

function [distance,path]=dijkstra(A,s,e)
% [DISTANCE,PATH]=DIJKSTRA(A,S,E)
% returns the distance and path between the start node and the end node.
%
% A: adjcent matrix
% s: start node
% e: end node

% initialize
n=size(A,1); % node number
D=A(s,:); % distance vector
path=[]; % path vector
visit=ones(1,n); % node visibility
visit(s)=0; % source node is unvisible
parent=zeros(1,n); % parent node

% the shortest distance
for i=1:n-1 % BlueSet has n-1 nodes
temp=zeros(1,n);
count=0;
for j=1:n
if visit(j)
temp=[temp(1:count) D(j)];
else
temp=[temp(1:count) inf];
end
count=count+1;
end
[value,index]=min(temp);
j=index; visit(j)=0;
for k=1:n
if D(k)>D(j)+A(j,k)
D(k)=D(j)+A(j,k);
parent(k)=j;
end
end
end
distance=D(e);

% the shortest distance path
if parent(e)==0
return;
end
path=zeros(1,2*n); % path preallocation
t=e; path(1)=t; count=1;
while t~=s && t>0
p=parent(t);
path=[p path(1:count)];
t=p;
count=count+1;
end
if count>=2*n
error(['The path preallocation length is too short.',...
'Please redefine path preallocation parameter.']);
end
path(1)=s;
path=path(1:count);

-------------------------------------------------------------

基于MATLAB的dijkstra算法及其应用 

clc; clear; close all;
%% 载入设置数据
points = load('c:\\niu\\点的数据.txt');
lines = load('c:\\niu\\边数据.txt');
A = ones(size(points, 1))*Inf;
for i = 1 : size(A, 1)
A(i, i) = 0;
end
%% 绘图
figure('NumberTitle', 'off', 'Name', '连接关系', 'Menu', 'None', ...
'Color', 'w', 'units', 'normalized', 'position', [0 0 1 1]);
hold on; axis off;
plot(points(:, 2), points(:, 3), 'r.', 'MarkerSize', 20);
for i = 1 : size(lines, 1)
temp = lines(i, :);
pt1 = points(temp(1), 2 : end);
pt2 = points(temp(2), 2 : end);
len = norm([pt1(1)-pt2(1), pt1(2)-pt2(2)]);
A(temp(1), temp(2)) = len;
plot([pt1(1) pt2(1)], [pt1(2) pt2(2)], 'k-', 'LineWidth', 2);
end
% A就是连接矩阵,其中对角线为0,表示本身
% 有连接关系的就对应线的长度
% 没有连接关系的就对应inf
%% 下面的是dijstra算法,有两种方式可以调用
s = 10; % 起点
e = 100; % 终点
[distance,path0] = dijkstra(A,s,e);
fprintf('\n Use Dijkstra the Min Distance is: %.5f \n', distance);
fprintf('\n Use Dijkstra the Min Distance path is: \n');
disp(path0);
A1 = A;
A1(isinf(A1)) = 0;
[d, p, pred] = graphshortestpath(sparse(A1), s, e);
fprintf('\n Use graphshortestpath the Min Distance is: %.5f \n', d);
fprintf('\n Use graphshortestpath the Min Distance path is: \n');
disp(p);

for i = 1 : length(path0)
if i == length(path0)
temp = [path0(1) path0(i)];
else
temp = [path0(i) path0(i+1)];
end
pt1 = points(temp(1), 2 : end);
pt2 = points(temp(2), 2 : end);
len = norm([pt1(1)-pt2(1), pt1(2)-pt2(2)]);
plot([pt1(1) pt2(1)], [pt1(2) pt2(2)], 'r-', 'LineWidth', 2);
end
总结
dijkstra算法在优化、图像处理、网格处理等相关领域有非常广泛的应用,希望能借此机会了解其实质内容,并灵活应用其到所学领域中。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值