|
| 1 | +/** |
| 2 | + * References: |
| 3 | + * https://www.geeksforgeeks.org/greedy-algorithms-set-6-dijkstras-shortest-path-algorithm/ |
| 4 | + * https://www.geeksforgeeks.org/greedy-algorithms-set-7-dijkstras-algorithm-for-adjacency-list-representation/ |
| 5 | + * https://www.geeksforgeeks.org/printing-paths-dijkstras-shortest-path-algorithm/ |
| 6 | + * https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-using-set-in-stl/ |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * All edge weights are non-negative. |
| 11 | + */ |
| 12 | +public class Dijkstra { |
| 13 | + // Adjacency Matrix Representation |
| 14 | + /** |
| 15 | + * Output shortest paths distance from the source to all the vertices. |
| 16 | + */ |
| 17 | + public int[] shortestDistancesFrom(int[][] graph, int src) { |
| 18 | + if (graph == null || graph.length == 0 || graph[0].length == 0) return new int[0]; |
| 19 | + int len = graph.length; |
| 20 | + int[] dist = new int[len]; |
| 21 | + Set<Integer> sptSet = new HashSet[len]; |
| 22 | + |
| 23 | + // initialization |
| 24 | + for (int i = 0; i < len; i++) { |
| 25 | + dist[i] = Integer.MAX_VALUE; |
| 26 | + } |
| 27 | + dist[src] = 0; |
| 28 | + |
| 29 | + while (sptSet.size() < len) { |
| 30 | + int u = extractMin(dist, sptSet); |
| 31 | + sptSet.add(u); |
| 32 | + |
| 33 | + for (int v=0; v<len; v++) { |
| 34 | + // relaxation |
| 35 | + if (!sptSet.contains(v) && graph[u][v] > 0 && |
| 36 | + dist[u] != Integer.MAX_VALUE && |
| 37 | + dist[u] + graph[u][v] < dist[v]) { |
| 38 | + dist[v] = dist[u] + graph[u][v]; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return dist; |
| 44 | + } |
| 45 | + |
| 46 | + // This can be optimized by MinHeap |
| 47 | + private int extractMin(int[] dist, Set<Integer> sptSet) { |
| 48 | + int minIdx = -1; |
| 49 | + int minVal = Integer.MAX_VALUE; |
| 50 | + for (int i=0; i<dist.length; i++) { |
| 51 | + if (!sptSet.contains(i) && dist[i] < minVal) { |
| 52 | + minIdx = i; |
| 53 | + minVal = dist[i]; |
| 54 | + } |
| 55 | + } |
| 56 | + return minIdx; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Output the shortest path distance from the source to the destination. |
| 61 | + */ |
| 62 | + public int shortestDistance(int[][] graph, int src, int dest) { |
| 63 | + if (graph == null || graph.length == 0 || graph[0].length == 0) return new int[0]; |
| 64 | + int len = graph.length; |
| 65 | + int[] dist = new int[len]; |
| 66 | + Set<Integer> sptSet = new HashSet[len]; |
| 67 | + |
| 68 | + // initialization |
| 69 | + for (int i = 0; i < len; i++) { |
| 70 | + dist[i] = Integer.MAX_VALUE; |
| 71 | + } |
| 72 | + dist[src] = 0; |
| 73 | + |
| 74 | + while (sptSet.size() < len) { |
| 75 | + int u = minDistance(dist, sptSet); |
| 76 | + // early return when you fidn the target |
| 77 | + if (u = target) return dist[u]; |
| 78 | + sptSet.add(u); |
| 79 | + |
| 80 | + for (int v=0; v<len; v++) { |
| 81 | + // relaxation |
| 82 | + if (!sptSet.contains(v) && graph[u][v] > 0 && |
| 83 | + dist[u] != Integer.MAX_VALUE && |
| 84 | + dist[u] + graph[u][v] < dist[v]) { |
| 85 | + dist[v] = dist[u] + graph[u][v]; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return -1; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Output shortest path from the source to the destination. |
| 95 | + */ |
| 96 | + public List<Integer> shortestPath(int[][] graph, int src, int dest) { |
| 97 | + if (graph == null || graph.length == 0 || graph[0].length == 0) return new int[0]; |
| 98 | + int len = graph.length; |
| 99 | + int[] dist = new int[len]; |
| 100 | + int[] parent = new int[len]; |
| 101 | + Set<Integer> sptSet = new HashSet[len]; |
| 102 | + |
| 103 | + // initialization |
| 104 | + for (int i = 0; i < len; i++) { |
| 105 | + dist[i] = Integer.MAX_VALUE; |
| 106 | + parent[i] = i; |
| 107 | + } |
| 108 | + dist[src] = 0; |
| 109 | + |
| 110 | + while (sptSet.size() < len) { |
| 111 | + int u = extractMin(dist, sptSet); |
| 112 | + if (u = target) return constructShortestPath(parent, src, dest); |
| 113 | + sptSet.add(u); |
| 114 | + |
| 115 | + for (int v=0; v<len; v++) { |
| 116 | + // relaxation |
| 117 | + if (!sptSet.contains(v) && graph[u][v] > 0 && |
| 118 | + dist[u] != Integer.MAX_VALUE && |
| 119 | + dist[u] + graph[u][v] < dist[v]) { |
| 120 | + dist[v] = dist[u] + graph[u][v]; |
| 121 | + parent[v] = u; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return dist; |
| 127 | + } |
| 128 | + |
| 129 | + private List<Integer> constructShortestPath(int[] parent, int src, int dest) { |
| 130 | + LinkedList<Integer> path = new LinkedList<>(); |
| 131 | + path.add(dest); |
| 132 | + while (path.getFirst() != src) { |
| 133 | + int head = path.getFirst(); |
| 134 | + if (parent[head] == head) return new LinkedList<>(); |
| 135 | + path.addFirst(parent[head]); |
| 136 | + } |
| 137 | + return path; |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments