简体   繁体   English

Java中的Dijkstra算法

[英]Dijkstra Algorithm in Java

I have some troubles implementing Dijkstra algorithm in Java. 我在用Java实现Dijkstra算法时遇到了一些麻烦。
I use this (first) pseudocode: http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm 我使用此(第一个)伪代码: http : //en.wikipedia.org/wiki/Dijkstra%27s_algorithm
Line 15 you need to get the vertex with the lowest distance. 第15行需要获得最短距离的顶点。 But how can I save the distance with the according distance. 但是我该如何用相应的距离保存距离。
Note: vertex is defined as an Integer. 注意:顶点被定义为整数。

My solutions that didn't work properly: 我的解决方案无法正常工作:

  1. Map with K = vertex, V = distance, Problem: long searching to get min dist 映射,其中K =顶点,V =距离,问题:长时间搜索以获取最小距离
  2. SortedMap with K = distance, V = vertex, Problem: almost every distance is defined as Integer.MAX_VALUE SortedMap,其中K =距离,V =顶点,问题:几乎每个距离都定义为Integer.MAX_VALUE

So I am looking for a fast way to save a vertex to a distance and it should be easy to get the vertex with min dist. 因此,我正在寻找一种将顶点保存到一定距离的快速方法,并且应该容易获得具有最小距离的顶点。

Try using a PriorityQueue. 尝试使用PriorityQueue。 That way, you can simply remove the head, as it would have the minimum distance of all vertices. 这样,您可以简单地移除头部,因为它具有所有顶点的最小距离。

See more information about PriorityQueue here: http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html 在此处查看有关PriorityQueue的更多信息: http : //docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM