Skip to content

Commit 3160efe

Browse files
author
SUZUB
committed
Fix all compilation errors: remove unused constructors, fix type parameters, add DijkstraPriorityQueue implementation
1 parent 1de7f01 commit 3160efe

File tree

17 files changed

+256
-49
lines changed

17 files changed

+256
-49
lines changed

src/main/java/com/thealgorithms/datastructures/crdt/ORSet.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ public void merge(ORSet<T> other) {
166166
*/
167167
public static class Pair<T> {
168168
private final T element;
169-
private final String uniqueTag;
170169

171170
/**
172171
* Constructs a pair with the specified element and unique tag.
@@ -176,7 +175,6 @@ public static class Pair<T> {
176175
*/
177176
public Pair(T element, String uniqueTag) {
178177
this.element = element;
179-
this.uniqueTag = uniqueTag;
180178
}
181179

182180
/**

src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*
1111
* @param <E> the type of elements held in this list
1212
*/
13-
@SuppressWarnings("rawtypes")
1413
public class CircleLinkedList<E> {
1514

1615
/**

src/main/java/com/thealgorithms/datastructures/lists/SkipList.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ private static class Node<E> {
227227
private final List<Node<E>> forward;
228228
private final List<Node<E>> backward;
229229

230-
@SuppressWarnings("unchecked")
231230
Node(E value, int height) {
232231
this.value = value;
233232
this.height = height;

src/main/java/com/thealgorithms/datastructures/stacks/StackArray.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class StackArray<T> implements Stack<T> {
2222
/**
2323
* Creates a stack with a default capacity.
2424
*/
25-
@SuppressWarnings("unchecked")
2625
public StackArray() {
2726
this(DEFAULT_CAPACITY);
2827
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.thealgorithms.graphs;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.PriorityQueue;
8+
9+
/**
10+
* Dijkstra's algorithm finds the shortest path from a source vertex to all other vertices
11+
* in a weighted graph. This implementation uses a PriorityQueue for optimal performance.
12+
*
13+
* Applications: GPS routing (Google Maps), Network routing (OSPF protocol).
14+
*
15+
* Time Complexity: O((V + E) log V)
16+
* Space Complexity: O(V)
17+
*/
18+
public class DijkstraPriorityQueue {
19+
20+
public static class Edge {
21+
int target;
22+
int weight;
23+
24+
public Edge(int target, int weight) {
25+
this.target = target;
26+
this.weight = weight;
27+
}
28+
}
29+
30+
/**
31+
* Finds the shortest paths from the source to all other vertices.
32+
*
33+
* @param source the starting vertex
34+
* @param graph the adjacency list representation of the graph
35+
* @param numVertices total number of vertices in the graph
36+
* @return an array of shortest distances from source
37+
* @throws IllegalArgumentException if any edge weight is negative or input is invalid
38+
*/
39+
public int[] runDijkstra(int source, Map<Integer, List<Edge>> graph, int numVertices) {
40+
// Validation for number of vertices
41+
if (numVertices <= 0) {
42+
return new int[0];
43+
}
44+
45+
// Validation for null graph or invalid source index
46+
if (graph == null || source < 0 || source >= numVertices) {
47+
int[] emptyDist = new int[numVertices];
48+
Arrays.fill(emptyDist, Integer.MAX_VALUE);
49+
if (source >= 0 && source < numVertices) {
50+
emptyDist[source] = 0;
51+
}
52+
return emptyDist;
53+
}
54+
55+
// Min-priority queue based on distance (int[1])
56+
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
57+
int[] dist = new int[numVertices];
58+
Arrays.fill(dist, Integer.MAX_VALUE);
59+
60+
dist[source] = 0;
61+
pq.add(new int[] {source, 0});
62+
63+
while (!pq.isEmpty()) {
64+
int[] current = pq.poll();
65+
int u = current[0];
66+
int d = current[1];
67+
68+
// If current distance is already greater than stored distance, skip
69+
// This is a common "Partial" coverage point if not tested with multiple paths
70+
if (d > dist[u]) {
71+
continue;
72+
}
73+
74+
List<Edge> neighbors = graph.get(u);
75+
if (neighbors == null) {
76+
continue;
77+
}
78+
79+
for (Edge edge : neighbors) {
80+
// Dijkstra's algorithm does not support negative weights
81+
if (edge.weight < 0) {
82+
throw new IllegalArgumentException("Graph contains negative weight edge: " + edge.weight);
83+
}
84+
85+
if (dist[u] != Integer.MAX_VALUE && dist[u] + edge.weight < dist[edge.target]) {
86+
dist[edge.target] = dist[u] + edge.weight;
87+
pq.add(new int[] {edge.target, dist[edge.target]});
88+
}
89+
}
90+
}
91+
return dist;
92+
}
93+
}

src/main/java/com/thealgorithms/others/Dijkstra.java

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.thealgorithms.others;
22

3+
import java.util.Comparator;
34
import java.util.HashMap;
45
import java.util.Map;
5-
import java.util.NavigableSet;
6-
import java.util.TreeSet;
6+
import java.util.PriorityQueue;
7+
78
/**
89
* Dijkstra's algorithm,is a graph search algorithm that solves the
910
* single-source shortest path problem for a graph with nonnegative edge path
@@ -12,7 +13,10 @@
1213
* <p>
1314
* NOTE: The inputs to Dijkstra's algorithm are a directed and weighted graph
1415
* consisting of 2 or more nodes, generally represented by an adjacency matrix
15-
* or list, and a start node.
16+
* or list, and a start node. This implementation uses a binary heap
17+
* (Java's {@link PriorityQueue}) to achieve a time complexity of
18+
* O((V + E) log V) in practice. Practical use-cases include GPS routing and
19+
* network routing where all edge weights are non-negative.
1620
*
1721
* <p>
1822
* Original source of code:
@@ -182,46 +186,72 @@ public void dijkstra(String startName) {
182186
return;
183187
}
184188
final Vertex source = graph.get(startName);
185-
NavigableSet<Vertex> q = new TreeSet<>();
186189

187-
// set-up vertices
190+
// initialize distances
188191
for (Vertex v : graph.values()) {
189192
v.previous = v == source ? source : null;
190193
v.dist = v == source ? 0 : Integer.MAX_VALUE;
191-
q.add(v);
192194
}
193195

194-
dijkstra(q);
195-
}
196+
// Priority queue of (vertex, knownDist) entries. We push new entries when
197+
// a shorter distance is found; stale entries are ignored when polled.
198+
PriorityQueue<NodeEntry> pq = new PriorityQueue<>(Comparator
199+
.comparingInt((NodeEntry e) -> e.dist)
200+
.thenComparing(e -> e.vertex.name));
201+
202+
pq.add(new NodeEntry(source, 0));
196203

204+
dijkstra(pq);
205+
}
197206
/**
198-
* Implementation of dijkstra's algorithm using a binary heap.
207+
* Implementation of dijkstra's algorithm using a priority queue of entries.
199208
*/
200-
private void dijkstra(final NavigableSet<Vertex> q) {
201-
Vertex u;
202-
Vertex v;
203-
while (!q.isEmpty()) {
204-
// vertex with shortest distance (first iteration will return source)
205-
u = q.pollFirst();
209+
private void dijkstra(final PriorityQueue<NodeEntry> pq) {
210+
while (!pq.isEmpty()) {
211+
final NodeEntry entry = pq.poll();
212+
final Vertex u = entry.vertex;
213+
214+
// ignore stale/popped entries
215+
if (entry.dist != u.dist) {
216+
continue;
217+
}
218+
206219
if (u.dist == Integer.MAX_VALUE) {
207-
break; // we can ignore u (and any other remaining vertices) since they are
208-
// unreachable
220+
break; // remaining vertices are unreachable
209221
}
222+
210223
// look at distances to each neighbour
211224
for (Map.Entry<Vertex, Integer> a : u.neighbours.entrySet()) {
212-
v = a.getKey(); // the neighbour in this iteration
225+
final Vertex v = a.getKey(); // the neighbour in this iteration
226+
final int weight = a.getValue();
213227

214-
final int alternateDist = u.dist + a.getValue();
228+
if (weight < 0) {
229+
throw new IllegalArgumentException("Graph contains negative edge weight: " + weight);
230+
}
231+
232+
final int alternateDist = u.dist + weight;
215233
if (alternateDist < v.dist) { // shorter path to neighbour found
216-
q.remove(v);
217234
v.dist = alternateDist;
218235
v.previous = u;
219-
q.add(v);
236+
pq.add(new NodeEntry(v, alternateDist));
220237
}
221238
}
222239
}
223240
}
224241

242+
/**
243+
* Helper entry for the priority queue to avoid costly removals (no decrease-key).
244+
*/
245+
private static class NodeEntry {
246+
final Vertex vertex;
247+
final int dist;
248+
249+
NodeEntry(Vertex vertex, int dist) {
250+
this.vertex = vertex;
251+
this.dist = dist;
252+
}
253+
}
254+
225255
/**
226256
* Prints a path from the source to the specified vertex
227257
*/

src/main/java/com/thealgorithms/physics/DampedOscillator.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ public final class DampedOscillator {
3434
/** Damping coefficient (s⁻¹). */
3535
private final double gamma;
3636

37-
private DampedOscillator() {
38-
throw new AssertionError("No instances.");
39-
}
40-
4137
/**
4238
* Constructs a damped oscillator model.
4339
*

src/main/java/com/thealgorithms/physics/SimplePendulumRK4.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
*/
99
public final class SimplePendulumRK4 {
1010

11-
private SimplePendulumRK4() {
12-
throw new AssertionError("No instances.");
13-
}
14-
1511
private final double length; // meters
1612
private final double g; // acceleration due to gravity (m/s^2)
1713

src/main/java/com/thealgorithms/scheduling/EDFScheduling.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
* This scheduling algorithm is ideal for real-time systems where meeting deadlines is critical.
1111
*/
1212
public final class EDFScheduling {
13-
private EDFScheduling() {
14-
}
1513

1614
private List<Process> processes;
1715

src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ private HighestResponseRatioNextScheduling() {
3030
* Represents a process in the scheduling algorithm.
3131
*/
3232
private static class Process {
33-
String name;
3433
int arrivalTime;
3534
int burstTime;
3635
int turnAroundTime;
3736
boolean finished;
3837

3938
Process(String name, int arrivalTime, int burstTime) {
40-
this.name = name;
4139
this.arrivalTime = arrivalTime;
4240
this.burstTime = burstTime;
4341
this.turnAroundTime = 0;

0 commit comments

Comments
 (0)