I'm trying to create a PriorityQueue of keys that establishes sorting using a custom comparator based on values corresponding to the given keys within a map. However, changing any value within the map does not affect the priority of its corresponding index in the queue. Why is that the case?
thorough description of the problem
I am implementing Dijkstra's Graph Traversal algorithm and need a priority-queue-like structure to store the non-visited nodes. However, the pre-defined structure uses an edge list as graph representation, so what I have is simply indices from 1 to n corresponding to the nodes.
To keep the up-to-date distance from a given starting point I create a map with the node indices as keys and distances to each index as corresponding values (at first all, except for the starting one itself, are infinity).
Map<Integer, Integer> distances = new HashMap();
for(int i = 1; i <= n; i++) {
distances.put(i, Integer.MAX_VALUE);
}
distances.put(s, 0);
To be able to easily move on to the next node during iteration, I declare a PriorityQueue that would contain the indices of the nodes (sorted based on their distance to the starting point).
Queue<Integer> queue = new PriorityQueue((a, b) -> distances.get(a) - distances.get(b));
for(int i = 1; i <= n; i++) {
queue.offer(i);
}
However, when I have to update the distance, as the algorithm finds a shorted one to a given node, the priority queue does not adapt. Thus, I am bound to use remove and then add.
queue.remove(i);
distances.put(i, newDistance);
queue.offer(i);