|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.Arrays; |
| 3 | +import java.util.Comparator; |
| 4 | +import java.util.List; |
| 5 | +import java.util.PriorityQueue; |
| 6 | + |
| 7 | +public class DijkstraSingleSourceShortestPath { |
| 8 | + |
| 9 | + public Integer[][] singleSourceShortestPath(Integer[][] weight,int source){ |
| 10 | + //auxiliary constants |
| 11 | + final int SIZE = weight.length; |
| 12 | + final int EVE = -1;//to indicate no predecessor |
| 13 | + final int INFINITY = Integer.MAX_VALUE; |
| 14 | + |
| 15 | + //declare and initialize pred to EVE and minDist to INFINITY |
| 16 | + Integer[] pred = new Integer[SIZE]; |
| 17 | + Integer[] minDist = new Integer[SIZE]; |
| 18 | + Arrays.fill(pred, EVE); |
| 19 | + Arrays.fill(minDist, INFINITY); |
| 20 | + |
| 21 | + //set minDist[source] =0 because source is 0 distance from itself. |
| 22 | + minDist[source] = 0; |
| 23 | + |
| 24 | + PriorityQueue<Integer[]> pq = createPriorityQueue(minDist); |
| 25 | + |
| 26 | + while (!pq.isEmpty()) { |
| 27 | + updatePriorityQueue(pq); |
| 28 | + int v = pq.remove()[0]; |
| 29 | + for (Integer[] XD : adjacency(weight, pq, v)) { |
| 30 | + Integer x = XD[0]; |
| 31 | + //triangle inequality |
| 32 | + if (null != x && minDist[x] > minDist[v] + weight[v][x]) { |
| 33 | + minDist[x] = minDist[v] + weight[v][x]; |
| 34 | + pred[XD[0]] = v; |
| 35 | + XD[1] = minDist[x];//update pq. |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + Integer[][] result = {pred, minDist}; |
| 40 | + return result; |
| 41 | + } |
| 42 | + |
| 43 | + /********************************************************************* |
| 44 | + * Create a priority queue and load it with the vertices sorted by |
| 45 | + * minDist. |
| 46 | + ********************************************************************/ |
| 47 | + private PriorityQueue<Integer[]> createPriorityQueue(Integer[] dist) { |
| 48 | + PriorityQueue<Integer[]> pq = new PriorityQueue<Integer[]>(11, |
| 49 | + new Comparator<Integer[]>() { |
| 50 | + public int compare(Integer[] A, Integer[] B) { |
| 51 | + return A[1] < B[1] ? -1 : 1; |
| 52 | + } |
| 53 | + }); |
| 54 | + for (int v = 0; v < dist.length; v++) { |
| 55 | + pq.add(new Integer[]{v, dist[v]}); |
| 56 | + } |
| 57 | + return pq; |
| 58 | + } |
| 59 | + |
| 60 | + /****************************************************************** |
| 61 | + * Retrieve all the neighbors of vertex v that are |
| 62 | + * in the priority queue pq. |
| 63 | + *****************************************************************/ |
| 64 | + private List<Integer[]> adjacency(Integer[][] G, |
| 65 | + PriorityQueue<Integer[]> pq, int v) { |
| 66 | + List<Integer[]> result = new ArrayList<Integer[]>(); |
| 67 | + for (Integer[] ent : pq) {// {u,key[u]} |
| 68 | + int u = ent[0]; |
| 69 | + if (G[v][u] != null) { |
| 70 | + result.add(ent); |
| 71 | + } |
| 72 | + } |
| 73 | + return result; |
| 74 | + } |
| 75 | + |
| 76 | + /***************************************************************** |
| 77 | + * Re-prioritize the queue based on changes in the |
| 78 | + * minDist array. |
| 79 | + * |
| 80 | + * Technical Details: Dijktra's algorithm requires a priority queue |
| 81 | + * that changes continuously to reflect changes in minDist. |
| 82 | + * For Java it does not suffice to simply pass new values to |
| 83 | + * the array objects that constitute the queue. The |
| 84 | + * PriorityQueue data structure in Java only checks its structure |
| 85 | + * when it is adding or removing elements. It is unaware of any |
| 86 | + * direct changes to the objects it comprises. Therefore to force |
| 87 | + * the queue to re-prioritize, an element is removed and then |
| 88 | + * immediately added back. |
| 89 | + * |
| 90 | + *****************************************************************/ |
| 91 | + private void updatePriorityQueue(PriorityQueue<Integer[]> pq) { |
| 92 | + pq.add(pq.remove()); |
| 93 | + } |
| 94 | +} |
0 commit comments