Skip to content

Commit 00339c7

Browse files
refactor: unify Dijkstra on efficient PriorityQueue implementation (#7486)
1 parent 0fe06ac commit 00339c7

3 files changed

Lines changed: 26 additions & 154 deletions

File tree

src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.datastructures.graphs;
22

33
import java.util.Arrays;
4+
import java.util.PriorityQueue;
45

56
/**
67
* Dijkstra's algorithm for finding the shortest path from a single source vertex to all other vertices in a graph.
@@ -18,6 +19,21 @@ public DijkstraAlgorithm(int vertexCount) {
1819
this.vertexCount = vertexCount;
1920
}
2021

22+
private static class Node implements Comparable<Node> {
23+
int id;
24+
int distance;
25+
26+
Node(int id, int distance) {
27+
this.id = id;
28+
this.distance = distance;
29+
}
30+
31+
@Override
32+
public int compareTo(Node other) {
33+
return Integer.compare(this.distance, other.distance);
34+
}
35+
}
36+
2137
/**
2238
* Executes Dijkstra's algorithm on the provided graph to find the shortest paths from the source vertex to all other vertices.
2339
*
@@ -36,18 +52,25 @@ public int[] run(int[][] graph, int source) {
3652

3753
int[] distances = new int[vertexCount];
3854
boolean[] processed = new boolean[vertexCount];
55+
PriorityQueue<Node> unprocessed = new PriorityQueue<>();
3956

4057
Arrays.fill(distances, Integer.MAX_VALUE);
41-
Arrays.fill(processed, false);
4258
distances[source] = 0;
59+
unprocessed.add(new Node(source, 0));
60+
61+
while (!unprocessed.isEmpty()) {
62+
Node current = unprocessed.poll();
63+
int u = current.id;
4364

44-
for (int count = 0; count < vertexCount - 1; count++) {
45-
int u = getMinDistanceVertex(distances, processed);
65+
if (processed[u]) {
66+
continue;
67+
}
4668
processed[u] = true;
4769

4870
for (int v = 0; v < vertexCount; v++) {
4971
if (!processed[v] && graph[u][v] != 0 && distances[u] != Integer.MAX_VALUE && distances[u] + graph[u][v] < distances[v]) {
5072
distances[v] = distances[u] + graph[u][v];
73+
unprocessed.add(new Node(v, distances[v]));
5174
}
5275
}
5376
}
@@ -56,27 +79,6 @@ public int[] run(int[][] graph, int source) {
5679
return distances;
5780
}
5881

59-
/**
60-
* Finds the vertex with the minimum distance value from the set of vertices that have not yet been processed.
61-
*
62-
* @param distances The array of current shortest distances from the source vertex.
63-
* @param processed The array indicating whether each vertex has been processed.
64-
* @return The index of the vertex with the minimum distance value.
65-
*/
66-
private int getMinDistanceVertex(int[] distances, boolean[] processed) {
67-
int min = Integer.MAX_VALUE;
68-
int minIndex = -1;
69-
70-
for (int v = 0; v < vertexCount; v++) {
71-
if (!processed[v] && distances[v] <= min) {
72-
min = distances[v];
73-
minIndex = v;
74-
}
75-
}
76-
77-
return minIndex;
78-
}
79-
8082
/**
8183
* Prints the shortest distances from the source vertex to all other vertices.
8284
*

src/main/java/com/thealgorithms/datastructures/graphs/DijkstraOptimizedAlgorithm.java

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/test/java/com/thealgorithms/datastructures/graphs/DijkstraOptimizedAlgorithmTest.java

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)