简体   繁体   中英

Dijkstra Algorithm in Java

I have some troubles implementing Dijkstra algorithm in Java.
I use this (first) pseudocode: http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
Line 15 you need to get the vertex with the lowest distance. 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
  2. SortedMap with K = distance, V = vertex, Problem: almost every distance is defined as 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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