Dijkstra’s 最短路径算法的 Matlab实现

这篇具有很好参考价值的文章主要介绍了Dijkstra’s 最短路径算法的 Matlab实现。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

随机生成400个点,再去除其中的120个点作为‘路障’。采用dijkstra算法寻找最短路径。

 

matlab在400个点里随机跳出,网络通信,算法,1024程序员节

主函数: main.m

clc,clear all

% Define the size of the map
sideLength = 20;
nodes = sideLength*sideLength;
removed_num = 120;

% Generate the map
[routing_value,mapping] = mapGenerator(sideLength,removed_num)

% Calculate the shorest path
[dist,selectedNode] = Mydijkstra(routing_value,1,nodes)

% Draw the line 
drawLine(selectedNode,mapping)

思路:

step1:定义地图大小/内容

step2:把随机生成的‘视觉地图’信息载入‘矩阵地图’

step3:使用dijkstra原理寻找最短路径

step4:连点成线

生成地图函数: mapGenerator.m

function [routing_value, mapping] = mapGenerator(edge,removed_num)
% edge: the side length of the map

nodes = edge * edge;                    % Total elements in the routing table
routing_value = ones(nodes,nodes)*999;  % Set the initial distance to infinity(999)
mapping = zeros(2,nodes);               % The mapping matrix of map position and routing value table 

% updating the routing value from the (1,1) dot.
for y = 1:edge                          % x. Y represents two axes in the map
    y_original = y;
    for x = 1:edge
        y = y_original;
        x_original = x;
        h = x_original+(y_original-1)*edge;
        mapping(1,h)=x_original;
        mapping(2,h)=y_original;

        % right adjacency
        if(x_original<edge)
            x = x+1;
            k = (y-1)*edge + x;     % calculate the neighbor's number which is the column of routing_value table
            routing_value(h,k) = 1;
            routing_value(k,h) = 1; % because it is adjacent, the reverse is also true
            % h,k
        end

        % upper adjacency
        if(y_original<edge)
            x = x_original;
            y = y+1;
            k = (y-1)*edge + x;     % calculate the neighbor's number which is the column of routing_value table
            routing_value(h,k) = 1;
            routing_value(k,h) = 1; % because it is adjacent, the reverse is also true
            % h,k
        end

        % right upper corner
        % condition: not in the right border and not in the upper border.
        if(x_original<edge) && (y_original<edge)
            x = x+1;
            k = (y-1)*edge + x;       % calculate the neighbor's number which is the column of routing_value table
            routing_value(h,k) = 1.4;
            routing_value(k,h) = 1.4; % because it is adjacent, the reverse is also true
            % h,k
        end

        % left upper corner
        % condition: not in the left border and not in the upper border
        if(x_original>1) && (y_original<edge)
            x=x_original-1;
            k = (y-1)*edge + x;         % calculate the neighbor's number which is the column of routing_value table
            routing_value(h,k) = 1.4;
            routing_value(k,h) = 1.4;   % because it is adjacent, the reverse is also true
            % h,k
        end
    end
end

% !!! step2: draw dots on the map
for i = 1:edge
    x = i;
    for j = 1:edge
        y = j;
        scatter(x,y,'green');
        hold on
    end
end

% !!! step3: remove 3/10 dots on the map and re-calculate the routing table
record = zeros(1,size(routing_value,1)); % put the removed dots in record list to prevent removing more than once.
for i = 1:removed_num
    x = randperm(edge,1);                % the x axis of the dot, x<=edge
    y = randperm(edge,1);
    hk = x+(y-1)*edge;

    % can not remove start point and destination point.
    % and can not remove one point twice
    if(hk ~= nodes && hk ~= 1 && record(hk) ~= 1) 
        scatter(x,y,'red');
        record(hk) = 1;

        % set the removing nodes' value routing to infinite(999) 
        routing_value(hk,:)=999;
        routing_value(:,hk)=999;
    else 
        removed_num = removed_num+1;
    end
end

把 ‘视觉地图’ 信息载入矩阵的原理/逻辑:

如下表格所示,是 ‘视觉地图’ 中 ‘地点’ 的坐标信息,以中心地点的坐标为(x,y)为例,它与八个邻居坐标关系如下。

(x-1, y+1) (x, y+1) (x+1, y+1)
(x-1, y) (x, y) (x+1, y)
(x-1, y-1) (x, y-1) (x+1, y-1)

所以当我们生成‘ 矩阵地图 (用矩阵记录地图中的路径信息) ’时,可以(对总共400个点)从左往右,由下至上地更新每一个点和它邻居的关系。由于两点间路径值一致 (i.e. A-->B的距离=B-->A的距离),所以避免重复,对于当前‘点’,我们只需要写入‘当前点’ 到 ‘当前点之后/ 没有出现过的邻居’ 的距离值。

以中心地点的坐标为(x,y)为例,它之后的邻居为:

(x-1, y+1) (x, y+1) (x+1, y+1)
(x, y) (x+1, y)

(x+1, y-1)

PS: 我们还需要考虑到‘顶点’,如果当前点为顶点,或在边缘线之上,需要补充限制条件,以防程序报错。

Dijkstra’s 算法函数: Mydijkstra.m


function [dist,selectedNode] = Mydijkstra(routing_value,start,dest)

% setting
routers = size(routing_value,1);        % statistic total number of points(the row number of r_v table)
Selected(1) = dest;          % set of selected points
Unselected = 1:routers;        % set of unselected points
Unselected(dest) = [];         
Distance = zeros(2,routers);  % inisialize all the path length
Distance(1,:) = 1:routers;    % A 2*7 matrix to record the distance from every point to the destination
Distance(2,1:routers) = routing_value(dest,1:routers);  % assign the value
new_Distance = Distance;        % A 2*7 matrix used to compare with the original links distance
min_distance = Distance;            % Initialize minimum distance
min_distance(:,dest) = [];          % deleted the length from destination to destination

% initialize path
path = zeros(2,routers);  
path(1,:) = 1:routers;    
path(2,Distance(2,:)~=999) = dest;  % '999' stand for cannot reach
 
% finding the shorest path
while ~isempty(Unselected)  % until the unselected set is empty
    index = find( min_distance(2,:)==min(min_distance(2,:)),1 );  % find the shorest path among the leftover points
    k = min_distance(1,index); 
    % update the point
    Selected = [Selected,k];     % add k to Selected set
    Unselected(Unselected==k) = [];  % deleted from the set of Unselected  
    %calculate the distance
    new_Distance(2,:) = routing_value(k,1:routers)+Distance(2,k); 
    min_distance = min(Distance,new_Distance);  % compare with the former value and select the min
    % update the path
    path( 2, min_distance(2,:)~=Distance(2,:) ) = k;  % path used to record the former 'jump' of the router 
    Distance = min_distance;  
    min_distance(:,Selected) = [];   
end
dist = Distance(2,start);  
% output
selectedNode=[];
fprintf('the path is:');
while start ~= dest    % end when reach the destination
    selectedNode = [selectedNode,start];
    fprintf('%d-->',start);  
    next = path(2,start);   
    start = next;           
end
fprintf('%d\n',dest); % print out the destination
selectedNode = [selectedNode,dest];
selectedNode
fprintf('The length of the shorest path:%f\n',dist);
end
 

此部分参考:

乐观的lishan:最短路径 Dijkstra算法的Matlab代码实现_乐观的lishan的博客-CSDN博客_dijkstra matlab

连点成线函数:drawLine.m



function [] = drawLine(selectedNode,mapping)

% step5: draw the line

pathlength = size(selectedNode,2)

for i=1:pathlength-1
    line([mapping(1,selectedNode(i)),mapping(1,selectedNode(i+1))], ...
        [mapping(2,selectedNode(i)),mapping(2,selectedNode(i+1))], ...
        'linewidth',3,'color','b')
end

设置线段的颜色,粗细。在‘视觉地图’中呈现出最短路径。文章来源地址https://www.toymoban.com/news/detail-724710.html

到了这里,关于Dijkstra’s 最短路径算法的 Matlab实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Dijkstra算法和Floyd算法详解(MATLAB代码)

    Dijkstra 算法是由 E.W.Dijkstra 于 1959 年提出,又叫迪杰斯特拉算法,它应用了贪心算法模式,是目前公认的最好的求解最短路径的方法。算法解决的是有向图中单个源点到其他顶点的最短路径问题,其主要特点是每次迭代时选择的下一个顶点是标记点之外距离源点最近的顶点。

    2024年02月16日
    浏览(23)
  • java实现迪杰斯特拉(Dijkstra)算法求解最短路问题

    迪杰斯特拉(Dijkstra)算法是由荷兰计算机科学家狄克斯特拉于1959年提出的。是寻找从一个顶点到其余各顶点的最短路径算法,可用来解决最短路径问题。 迪杰斯特拉算法采用贪心算法的策略,将所有顶点分为已标记点和未标记点两个集合,从起始点开始,不断在未标记点中寻

    2024年02月12日
    浏览(32)
  • 数据结构 -最短路径dijkstra(迪杰斯特拉)算法讲解及代码实现

            迪杰斯特拉算法是一种广义的贪心算法,求出局部最优解,再去求全局最优解 举例图:(起始点为1) 辅助数组: s:记录了目标顶点到其他顶点的最短路径是否求得(求得为1,否则为0) p:目标顶点到其他顶点的最短路径的前驱节点 (如,求得1-7-5的最短路径,那

    2024年02月11日
    浏览(25)
  • 使用邻接矩阵实现有向图最短路径Dijkstra算法 题目编号:1136

    用邻接矩阵存储有向图,实现最短路径Dijkstra算法,图中边的权值为整型,顶点个数少于10个。 部分代码提示: #include #include using namespace std; const int MaxSize = 10; const int INF = 32767; class MGraph { public: MGraph(char a[], int n, int e); private: char vertex[MaxSize]; int arc[MaxSize][MaxSize]; int vertexNum,

    2024年02月01日
    浏览(66)
  • 数据结构与算法 —— 最短路径Dijkstra算法(迪杰斯特拉)详细图解以及python实现

    目录 前言 1. 介绍 2. 加权图 2.1 概念 3. 最短路径 -- Dijkstra 算法 3.1 历史 3.2 Dijkstra 算法的基本思路 3.3 Dijkstra 算法图解 4.  python中dijkstra算法的实现 5. 总结  前两章我们讲到了关于图的基本知识,和广度/深度优先搜索。 本章,我们将介绍 加权图 和 最短路径 的相关知识。 最

    2024年02月12日
    浏览(41)
  • python实现Dijkstra算法求解最短路径问题(Shortest Path Problem)

    最短路问题(Shortest Path Problem,SPP)是 图论 的一个经典问题,其目的在于寻找网络中任意两个节点间的最短路径,这里的最短可以衍生为距离最短、费用最小、时间最短等一系列度量。当前的涌现了很多最短路径的变种,如带资源约束的最短路径问题(Shortest Path Problem wit

    2024年02月09日
    浏览(24)
  • 【数据结构】最短路径算法实现(Dijkstra(迪克斯特拉),FloydWarshall(弗洛伊德) )

    最短路径问题 :从在带权有向图G中的某一顶点出发,找出一条通往另一顶点的最短路径,最短也就是沿路径各边的权值总和达到最小。 单源最短路径问题:给定一个图G = ( V , E ) G=(V,E)G=(V,E),求源结点s ∈ V s∈Vs∈V到图 中每个结点v ∈ V v∈Vv∈V的最短路径 针对一个带权

    2024年02月04日
    浏览(39)
  • 【MATLAB】最短路径Floyd算法

    1.1适用范围 ∙ bullet ∙ 求每队顶点的最短路径 ∙ bullet ∙ 有向图、无向图和混合图 1.2算法思想 直接在图的带权邻接矩阵中用插入顶点的方法依次递推地构造出n个矩阵D(1),D(2)…D(n)( 每次加入一个点然后更新最短路径矩阵D ),D(n)是图的最短距离矩阵,同时引入一个后继

    2024年02月16日
    浏览(21)
  • Dijkstra算法实现求有向图中一顶点到其余各个顶点的最短路径

    一、文章说明: C++语言实现; 有向图的存储结构为: 邻接矩阵; 这篇文章的代码是我根据B站UP主 懒猫老师 所写的,能成功运行,VS里面显示有很多警告。而且还可能存在有未调试出的BUG,请多多指教。 观看懒猫老师的视频后,才方便理解博主代码,不然可能理解起来会很吃

    2024年02月08日
    浏览(37)
  • 数据结构与算法细节篇之最短路径问题:Dijkstra和Floyd算法详细描述,java语言实现。

    最短路径的算法有两个, Dijkstra算法 和 Floyd算法 。 Dijkstra算法 解决的是 单源 最短路径问题 。 Floyd算法解决的是 多源 最短路径问题,并且可以处理负权图 。 今天要讲的就是Dijkstra算法。 加: feng--Insist (大写的i),进java交流群讨论互联网+技术。可索要PPT等资料。 其他资料

    2024年02月11日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包