首页 > 其他分享 >Dijkstra的matlab实现

Dijkstra的matlab实现

时间:2023-02-15 21:24:08浏览次数:37  
标签:ix end jy point 实现 start Dijkstra matlab target

任务描述:生成20x20个点,使用Dijkstra算法让找出点(1, 1)到(20, 20)的最短路径,期中有随机的120个点是不通的,这120个点不包括起点和终点

随机五次的实验结果图,代码在后面

1. Figure 1

2. Figure 2

3. Figure 3

4. Figure 4

5. Figure 5

Code

  • there are 4 parts, the major parts are part2: Initialization and part3: Loop like the ppt shown during class
  • the algorithm complexity is O(n^2)
  • the least-cost-path tree can be seen in cell F
  • the cost from start point to any other can be seen in cell D
  • The set N contains the points added at each step
  • you can edit the start_x、start_y、target_x、target_y to set the start position and target position when the points you tend to set are not removed and when it sure will have a feasible path
%%
%Part1 define the variables
clc;clear;

start_x = 1; % start point
start_y = 1;

target_x = 20; % target pont
target_y = 20;

w = 20; %columns
h = 20; %Rows

remove_amount = 120;
remain_amount = h * w - remove_amount;
total_pont = h * w;

D = zeros(h, w); %Used to record the distance from the point to the starting point
P = ones(1,total_pont); %Whether the location was removed
remove = randperm(total_pont - 2, remove_amount);

F = cell(h, w); % form the least-cost-path tree 

for i = 1:size(remove,2)
    P(remove(i)+1) = 0; % +1 Make sure the starting point is not removed
end



N = cell(1, remain_amount);
N{1} = [start_x start_y]; %Add point u to N set

ix = 1; %scanning point
jy = 1;

x = 1; %the position of a
y = 1;

x_p = target_x; %path
y_p = target_y;

%%
%Part2 Initialization

for i = 1:h
    for j = 1:w
        if P((i-1)*w + j) == 1 %if a point has been removed
            Cub  = sqrt((i-start_x)^2+(j-start_y)^2); %Calculate Cb
            if(Cub < 2 && Cub > 0) %to judge if it is around u
                D(i,j) = Cub;
                F{i,j} = [start_x,start_y];
            else
                D(i,j) = 999;
                F{i,j} = [999,999];
            end
        else
            F{i,j} = [999,999];
            D(i,j) = 999;
        end
    end
end

%%
%Part3 Loop here

for p = 1:remain_amount %steps needed

    Da = D;
    for i = 1:p
        Da(N{i}(1),N{i}(2)) = 999; %Exclude points in set N when looking for a
    end
    [x,y] = find(Da==min(min(Da)));
    N{p+1} = [x(1) y(1)];

    for q = 1:total_pont
        if(jy > w)
            jy = 1;
            ix = ix + 1;
        end
        if P((ix-1)*h + jy) == 1
            Cab = sqrt((ix-x(1))^2+(jy-y(1))^2); %Calculate Cab     
            a = cellfun(@(x)isequal(x,[ix jy]),N); %to judge whether a point is in N set
            if(Cab < 2 && ~ismember(1, a) && Cab > 0)
                D(ix,jy) = min(D(ix,jy),D(x(1),y(1)) + Cab);
                if(D(ix,jy) < (D(x(1),y(1)) + Cab))
                    F{ix,jy} = F{ix,jy}; %record the point for the least-cost-path tree 
                else
                    F{ix,jy} = [x(1) y(1)];
                end
            end
        end
        jy = jy + 1;
    end
    ix = 1;
    jy = 1;
    p %Observe the number of steps
end

%%
%Part4 plot here

for i = 1:h
    for j = 1:w
        if(P((i-1)*w+j) == 1)
            plot(i,j,'go','MarkerFaceColor','g');
            hold on;
        else
            plot(i,j,'ro','MarkerFaceColor','r');
            hold on;
        end
    end
end

% Query the forwarding tree and record the coordinates of each pair of coordinate points for drawing
while(~(target_x == start_x && target_y == start_y))
    x_p = [x_p F{target_x,target_y}(1)];
    y_p = [y_p F{target_x,target_y}(2)];
    temp = target_x;
    target_x = F{target_x,target_y}(1);
    target_y = F{temp,target_y}(2);
end

axis([0, h + 1, 0, w + 1]);
plot(x_p,y_p,'b-');


标签:ix,end,jy,point,实现,start,Dijkstra,matlab,target
From: https://www.cnblogs.com/pange698/p/17124651.html

相关文章

  • 11.6DMA可以实现短时间内传送大量数据
       在了解I/O输入输出及中断处理的同时,还希望大家记住另外一个机制,这就是DMA(DirectMemoryAccess)。DMA是指在不通过CPU的情况下,外围设备直接和主内存进行......
  • 11.5 用中断来实现实时处理
    在主程序运行的过程中,中断发生的频率有多大呢?实际上,大部分的外围设备,都会频繁地发出中断请求。其原因就是为了实时处理从外围设备输入的数据。虽然不利用中断也可以从外围......
  • 11.5用中断来实现实时处理
       由于外围设备有很多个,因此就有必要按照顺序来调查。按照顺序调查多个外围设备的状态称为轮询。对几乎不产生中断的系统来说,轮询是比较合适的处理。不过,对计算机来......
  • 11.6 DMA可以实现短时间内传送大量数据
    DMA机制是指在不通过CPU的情况下,外围设备直接和主内存进行数据传送。磁盘等都用到了这个DMA机制。通过利用DMA,大量数据就可以在短时间内转送到主内存。之所以这么快速,是因......
  • 异步请求池的实现
    今天分享一个异步请求池的例子。首先我们先看一个正常的阻塞的DNS解析的例子。#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#inc......
  • 接口实现多态
    title:接口实现多态date:2023-02-0614:59:36tags:Golang接口使得程序更具有灵活性和拓展性的主要原因是它实现了多态性。多态性指的是不同的类型可以实现相同的接......
  • 富文本编辑器实现从ppt中复制图片
    ​ 项目需求可发布文章需求涉及到富文本编辑器经过查阅我选择了较为简便不需要后端支持可独立完成的tinymce框架官方文档也是相当完整虽然都是全英文但是有强大的......
  • 前端报表如何实现无预览打印解决方案或静默打印
    在前端开发中,除了将数据呈现后,我们往往需要为用户提供,打印,导出等能力,导出是为了存档或是二次分析,而打印则因为很多单据需要打印出来作为主要的单据来进行下一环节的票据支......
  • vue实现图片蒙层效果(带收藏、分享功能)
    实现如下效果:当鼠标经过会出现蒙层并且有对应需求思路:蒙层采用定位鼠标经过给蒙层元素display设置为'block'鼠标移开display设置为'none',具体看代码这是渲染的图片......
  • 860~808 复杂条件查询分析,代码实现
    复杂条件查询分析:  代码实现packagecom.example.web.servlet;importcom.example.domain.PageBean;importcom.example.domain.User;importcom.example.servi......