栅格法建立环境地图时Dijkstra所用单位距离邻接矩阵的建立及MATLAB实现

本文介绍了在栅格法建立环境地图时,如何使用Dijkstra算法,并探讨了单位距离邻接矩阵的构建。文章通过举例说明了邻接矩阵的性质,以及在MATLAB中实现单位距离邻接矩阵的方法,以减少手动输入的工作量。
摘要由CSDN通过智能技术生成

欢迎来到 < Haoh-Smile > 的博客,觉得受用客官就点个赞评论一下呗!

*栅格法建立环境地图时Dijkstra所用单位距离邻接矩阵的建立

邻接矩阵(Adjacency Matrix):是表示顶点之间相邻关系的矩阵。设G=(V,E)是一个图,其中V={v1,v2,…,vn}。G的邻接矩阵是一个具有下列性质的n阶方阵:
①对无向图而言,邻接矩阵一定是对称的,而且对角线一定为零(在此仅讨论无向简单图),有向图则不一定如此。
②在无向图中,任一顶点i的度为第i列所有元素的和,在有向图中顶点i的出度为第i行所有元素的和,而入度为第i列所有元素的和。
③用邻接矩阵法表示图共需要n^2个空间,由于无向图的邻接矩阵一定具有对称关系,所以扣除对角线为零外,仅需要存储上三角形或下三角形的数据即可,因此仅需要n(n-1)/2个空间。
权值无向图表示以上所示的权值无向图的邻接矩阵为:

  
Dijkstra算法是一种常用的最短路径算法,可以用于栅格地图路径规划。下面是一个简单的MATLAB代码实现,用于在给定的栅格地图中寻找起点到终点的最短路径。 ```matlab function [path, cost] = dijkstra(map, start, goal) % DIJKSTRA Find the shortest path in a grid map using Dijkstra's algorithm % [path, cost] = DIJKSTRA(map, start, goal) returns the shortest path % between the start and goal points in the given map using Dijkstra's % algorithm. The input map is a binary matrix where 0 represents an % obstacle and 1 represents an open space. The input start and goal are % vectors containing the row and column indices of the start and goal % points, respectively. The output path is a matrix where each row % represents a point on the path in the form [row, col]. The output cost % is the total cost of the path. % Initialize variables [nrows, ncols] = size(map); start_node = sub2ind([nrows ncols], start(1), start(2)); goal_node = sub2ind([nrows ncols], goal(1), goal(2)); cost = inf(nrows, ncols); parent = zeros(nrows, ncols); visited = false(nrows, ncols); % Set the cost of the starting node to 0 and add it to the heap cost(start_node) = 0; heap = [start_node]; % Loop until the heap is empty or the goal node is visited while ~isempty(heap) % Get the node with the lowest cost curr_node = heap(1); heap(1) = []; visited(curr_node) = true; % Check if the goal node is reached if curr_node == goal_node break end % Get the neighbors of the current node [row, col] = ind2sub([nrows ncols], curr_node); neighbors = []; if row > 1 neighbors = [neighbors; sub2ind([nrows ncols], row-1, col)]; end if row < nrows neighbors = [neighbors; sub2ind([nrows ncols], row+1, col)]; end if col > 1 neighbors = [neighbors; sub2ind([nrows ncols], row, col-1)]; end if col < ncols neighbors = [neighbors; sub2ind([nrows ncols], row, col+1)]; end % Loop through the neighbors and update their costs for i = 1:length(neighbors) neighbor = neighbors(i); if visited(neighbor) || map(neighbor) == 0 continue end new_cost = cost(curr_node) + 1; if new_cost < cost(neighbor) cost(neighbor) = new_cost; parent(neighbor) = curr_node; heap = [heap neighbor]; end end % Reorder the heap based on the new costs [~, idx] = sort(cost(heap)); heap = heap(idx); end % Reconstruct the path path = []; curr_node = goal_node; while curr_node ~= start_node path = [path; ind2sub([nrows ncols], curr_node)]; curr_node = parent(curr_node); end path = [path; ind2sub([nrows ncols], start_node)]; path = flipud(path); cost = cost(goal_node); end ``` 该代码实现Dijkstra算法的基本步骤,包括初始化变量、设置起点和终点、计算代价、更新代价等。在函数的最后,使用 parent 矩阵从终点到起点重构最短路径。你可以将地图作为二进制矩阵输入,并提供起点和终点的坐标作为输入来使用此函数。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Haoh-Smile

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值