From e712d4a64cd55d99ddc0d09011832fcc5ab87491 Mon Sep 17 00:00:00 2001 From: SUZUB Date: Wed, 17 Dec 2025 10:52:31 +0530 Subject: [PATCH] chore: clean up directory for fresh start --- desktop.ini | 2 + .../graphs/package com.thealgorithms.java | 77 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 desktop.ini create mode 100644 src/test/java/com/thealgorithms/graphs/package com.thealgorithms.java diff --git a/desktop.ini b/desktop.ini new file mode 100644 index 000000000000..8671558cf098 --- /dev/null +++ b/desktop.ini @@ -0,0 +1,2 @@ +[.ShellClassInfo] +LocalizedResourceName=@dijkstra-priority-queue,0 diff --git a/src/test/java/com/thealgorithms/graphs/package com.thealgorithms.java b/src/test/java/com/thealgorithms/graphs/package com.thealgorithms.java new file mode 100644 index 000000000000..71d15b2c0b85 --- /dev/null +++ b/src/test/java/com/thealgorithms/graphs/package com.thealgorithms.java @@ -0,0 +1,77 @@ +package com.thealgorithms.graphs; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.PriorityQueue; + +/** + * Dijkstra's algorithm finds the shortest path from a source vertex to all other vertices + * in a weighted graph. This implementation uses a PriorityQueue for optimal performance. + * + * Applications: GPS routing (Google Maps), Network routing (OSPF protocol). + * + * Time Complexity: O((V + E) log V) + * Space Complexity: O(V) + */ +public class DijkstraPriorityQueue { + + public static class Edge { + int target; + int weight; + + public Edge(int target, int weight) { + this.target = target; + this.weight = weight; + } + } + + /** + * Finds the shortest paths from the source to all other vertices. + * + * @param source the starting vertex + * @param graph the adjacency list representation of the graph + * @param numVertices total number of vertices in the graph + * @return an array of shortest distances from source + * @throws IllegalArgumentException if any edge weight is negative + */ + public int[] runDijkstra(int source, Map> graph, int numVertices) { + if (numVertices <= 0) { + return new int[0]; + } + + // Min-priority queue based on distance (int[1]) + PriorityQueue pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1])); + int[] dist = new int[numVertices]; + Arrays.fill(dist, Integer.MAX_VALUE); + + dist[source] = 0; + pq.add(new int[] {source, 0}); + + while (!pq.isEmpty()) { + int[] current = pq.poll(); + int u = current[0]; + int d = current[1]; + + // If current distance is already greater than stored distance, skip + if (d > dist[u]) { + continue; + } + + for (Edge edge : graph.getOrDefault(u, new ArrayList<>())) { + // Dijkstra's algorithm does not support negative weights + if (edge.weight < 0) { + throw new IllegalArgumentException("Graph contains negative weight edge: " + edge.weight); + } + + if (dist[u] + edge.weight < dist[edge.target]) { + dist[edge.target] = dist[u] + edge.weight; + pq.add(new int[] {edge.target, dist[edge.target]}); + } + } + } + return dist; + } +} \ No newline at end of file