具有最小优先级队列的Dijkstra算法

编程入门 行业动态 更新时间:2024-10-08 22:13:43
本文介绍了具有最小优先级队列的Dijkstra算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图用优先级队列实现dijkstra算法,但我无法理解它是如何工作的。我在网上阅读了很多指南,但是我根本无法理解这个算法。

我的问题是:每个节点的优先级是多少?我认为这是最小值的传入边缘的权重,但我不确定。这是真的吗?

第二个问题,当我提取队列的根时,如果这个节点不是没有访问节点的邻接关系,那么这个节点是如何工作的?

解决方案您应该使用优先级队列,其中顶点与最初距离顶点的最短距离将获得最高优先级。最初,所有顶点将具有最短的无限距离,并且起始顶点将具有最短距离0。 p>

首先插入所有顶点(使用它的边)从 PQ 中的图表。从 PQ 中删除​​ vertex 并探索所有的边缘。将最短距离与所有相邻的顶点进行比较,如果任何距离小于当前顶点的最短距离,则更新相邻顶点 PQ 内的最短距离。继续,而 PQ 不为空。 没有边缘的顶点将会以无限远的最短距离完成,因为它不可能从'get to them'起点顶点。但是,它们仍会从 PQ 中移除。

$ b

伪代码

初始化图初始化pq pq.insertAll(graph.getVertices()) (pq不为空){ vertex = pq.remove() edges = vertex.getEdges() 用于所有边{ destination = edge .getDestination() newDistance = edge.getLength()+ vertex.getDistance() if(newDistance< destination.getDistance()){ destination.setShortestDistance(newDistance) pq.update(destination)} } }

麻省理工学院开放式课件链接: 路径问题概述 Dijkstra

I'm trying to implement the dijkstra algorithm with priority queue, but I can't understand how it works. I read many guide on the web but I can't understand this algorithm at all.

My question are: what is the priority for each node? I think that it is the weight of the incoming edge with minimun value, but I'm not sure. Is this true?

Second question, when I extract the root of the queue, how works if this node is not adjacency with no one of the visited nodes?

解决方案

You should use priority queue where the vertex with the shortest distance from the starting vertex will get the highest priority. Initially, all vertices will have the shortest distance of infinity and the starting vertex will have the shortest distance 0.

Start by inserting of all vertices (with its edges) from the graph inside the PQ. Remove vertex from the PQ and explore all its edges. Compare the shortest distances with all adjacent vertices and if any distance is less than the shortest distance on the current vertex, update adjacent vertex shortest distance inside the PQ. Continue while PQ is not empty. Vertices which got no edges will finish with the shortest distance of infinity because it is not possible 'get to them' from the starting vertex. However, they will be still removed from the PQ.

Pseudocode

initialize graph initialize pq pq.insertAll(graph.getVertices()) while (pq is not empty) { vertex = pq.remove() edges = vertex.getEdges() for all edges { destination = edge.getDestination() newDistance = edge.getLength() + vertex.getDistance() if (newDistance < destination.getDistance()) { destination.setShortestDistance(newDistance) pq.update(destination) } } }

MIT OpenCourseWare Links: Path problems overview Dijkstra

更多推荐

具有最小优先级队列的Dijkstra算法

本文发布于:2023-11-29 06:08:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645558.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:优先级   队列   算法   最小   Dijkstra

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!