Hello Mat

 找回密码
 立即注册
查看: 5517|回复: 1

dijkstra最短路算法

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2019-10-16 22:33:21 | 显示全部楼层 |阅读模式
dijkstra最短路算法
  1. clc % 清屏
  2. clear all;                                 % 删除workplace变量
  3. close all;                                 % 关掉显示图形窗口
  4. clc,clear,close all
  5. W=[0 1 2 Inf 7 4 8 Inf ;     % 带权邻接矩阵
  6.    1 0 2 5 Inf Inf 7 Inf ;
  7.    2 2 0 1 5 Inf Inf Inf;
  8.    Inf 5 1 0 3 Inf Inf 6 ;
  9.    7 Inf 5 3 0 3 Inf 4 ;
  10.    4 Inf Inf Inf 3 0 2 6;
  11.    8 7 Inf Inf Inf 2 0 4;
  12.    Inf Inf Inf 6 4 6 4 0];
  13. [r_path, r_cost] = dijkstra(1, 8, W)  % 从1到8节点的最短路径
复制代码
dijkstra函数如下:
  1. function [r_path, r_cost] = dijkstra(pathS, pathE, transmat)
  2. % The Dijkstra's algorithm, Implemented by Yi Wang, 2005
  3. %   pathS: 所求最短路径的起点
  4. %   pathE :所求最短路径的终点
  5. %   transmat: 图的转移矩阵或者邻接矩阵,应为方阵
  6. if ( size(transmat,1) ~= size(transmat,2) )
  7.   error( 'detect_cycles:Dijkstra_SC', ...
  8.          'transmat has different width and heights' );
  9. end
  10. % 初始化:
  11. %  noOfNode-图中的顶点数
  12. %  parent(i)-节点i的父节点
  13. %  distance(i)-从起点pathS的最短路径的长度
  14. %  queue-图的广度遍历
  15. noOfNode = size(transmat, 1);
  16. for i = 1:noOfNode
  17.   parent(i) = 0;       % 初值操作
  18.   distance(i) = Inf;    % 初值操作
  19. end
  20. queue = [];           % 队列

  21. % 由路径开始最短路计算
  22. for i=1:noOfNode
  23.   if transmat(pathS, i)~=Inf
  24.     distance(i) = transmat(pathS, i);
  25.     parent(i)   = pathS;  % 当前路径
  26.     queue     = [queue i];
  27.   end
  28. end

  29. % 对图进行广度遍历
  30. while length(queue) ~= 0
  31.   hopS  = queue(1);
  32.   queue = queue(2:end);
  33.   
  34.   for hopE = 1:noOfNode
  35.     if distance(hopE) > distance(hopS) + transmat(hopS,hopE)   % 如果当前距离大于转换后的距离
  36.       distance(hopE) = distance(hopS) + transmat(hopS,hopE);  % 更新
  37.       parent(hopE)   = hopS;
  38.       queue         = [queue hopE];
  39.     end
  40.   end
  41.   
  42. end

  43. % 回溯进行最短路径的查找
  44. r_path = [pathE];   
  45. i = parent(pathE);

  46. while i~=pathS && i~=0
  47.   r_path = [i r_path];
  48.   i      = parent(i);
  49. end
  50. if i==pathS
  51.   r_path = [i r_path];   % 记录
  52. else
  53.   r_path = []          % 清空
  54. end
  55. % 返回最短路径的权和
  56. r_cost = distance(pathE);
复制代码
输出:
  1. [r_path, r_cost] = dijkstra(1, 8, W)  % 从1到8节点的最短路径
  2. r_path =

  3.      1     3     4     8


  4. r_cost =

  5.      9
复制代码
  1. [r_path, r_cost] = dijkstra(2, 8, W)  % 从1到8节点的最短路径
  2. r_path =

  3.      2     3     4     8


  4. r_cost =

  5.      9
复制代码
  1. [r_path, r_cost] = dijkstra(3, 7, W)  % 从1到8节点的最短路径

  2. r_path =

  3.      3     1     6     7


  4. r_cost =

  5.      8
复制代码













算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-20 03:04 , Processed in 0.203238 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表