From 4b5e999f7c9b1778065abdd23ecf8f87c1d8b004 Mon Sep 17 00:00:00 2001 From: Poorvika Date: Mon, 20 Oct 2025 00:36:23 +0530 Subject: [PATCH 1/5] dijkstras algorithm implemented in java --- .../com/thealgorithms/searches/Dijkstras.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/main/java/com/thealgorithms/searches/Dijkstras.java diff --git a/src/main/java/com/thealgorithms/searches/Dijkstras.java b/src/main/java/com/thealgorithms/searches/Dijkstras.java new file mode 100644 index 000000000000..3a40a3f4b3c3 --- /dev/null +++ b/src/main/java/com/thealgorithms/searches/Dijkstras.java @@ -0,0 +1,55 @@ +package com.thealgorithms.graphs; + +import java.util.*; + +/** + * Implementation of Dijkstra's Algorithm. + * Finds the shortest path from a given source node to all other nodes in a weighted graph. + * + * @author: poorvikaa08 + * @date: 19 October 2025 (Sunday) + * @wiki: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm + */ +public class DijkstrasAlgorithm { + + /** + * Computes the shortest distance from a source vertex to all other vertices. + * + * @param graph adjacency list representing the graph, where graph[u] contains pairs (v, weight) + * @param src the source vertex + * @param V total number of vertices + * @return an array of shortest distances from src to every vertex + */ + public static int[] dijkstra(List> graph, int src, int V) { + int[] dist = new int[V]; + Arrays.fill(dist, Integer.MAX_VALUE); + dist[src] = 0; + + PriorityQueue pq = new PriorityQueue<>(Comparator.comparingInt(n -> n.weight)); + pq.add(new Node(src, 0)); + + while (!pq.isEmpty()) { + Node curr = pq.poll(); + for (Node neighbor : graph.get(curr.vertex)) { + int newDist = dist[curr.vertex] + neighbor.weight; + if (newDist < dist[neighbor.vertex]) { + dist[neighbor.vertex] = newDist; + pq.add(new Node(neighbor.vertex, newDist)); + } + } + } + + return dist; + } + + /** Helper class representing an edge or connection to another vertex */ + public static class Node { + int vertex; + int weight; + + public Node(int vertex, int weight) { + this.vertex = vertex; + this.weight = weight; + } + } +} From 57f13160a5ae267377fb86764e32d6ef6882e5eb Mon Sep 17 00:00:00 2001 From: Poorvika Date: Mon, 20 Oct 2025 01:10:00 +0530 Subject: [PATCH 2/5] renamed the file --- src/main/java/com/thealgorithms/searches/Dijkstras.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/searches/Dijkstras.java b/src/main/java/com/thealgorithms/searches/Dijkstras.java index 3a40a3f4b3c3..788187e74c9c 100644 --- a/src/main/java/com/thealgorithms/searches/Dijkstras.java +++ b/src/main/java/com/thealgorithms/searches/Dijkstras.java @@ -10,7 +10,7 @@ * @date: 19 October 2025 (Sunday) * @wiki: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm */ -public class DijkstrasAlgorithm { +public class Dijkstras { /** * Computes the shortest distance from a source vertex to all other vertices. From 7a0804f15020fe0891bd3490563858d635f7fb82 Mon Sep 17 00:00:00 2001 From: Poorvika Date: Mon, 20 Oct 2025 01:21:08 +0530 Subject: [PATCH 3/5] update --- .../com/thealgorithms/searches/Dijkstras.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/Dijkstras.java b/src/main/java/com/thealgorithms/searches/Dijkstras.java index 788187e74c9c..a937698feca6 100644 --- a/src/main/java/com/thealgorithms/searches/Dijkstras.java +++ b/src/main/java/com/thealgorithms/searches/Dijkstras.java @@ -1,6 +1,9 @@ -package com.thealgorithms.graphs; +package com.thealgorithms.searches; // update to match path; change back if you intend 'com.thealgorithms.graphs' -import java.util.*; +import java.util.List; +import java.util.Arrays; +import java.util.PriorityQueue; +import java.util.Comparator; /** * Implementation of Dijkstra's Algorithm. @@ -10,18 +13,23 @@ * @date: 19 October 2025 (Sunday) * @wiki: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm */ -public class Dijkstras { +public final class Dijkstras { + + // Hide utility-class constructor + private Dijkstras() { + throw new AssertionError("Cannot instantiate utility class"); + } /** * Computes the shortest distance from a source vertex to all other vertices. * * @param graph adjacency list representing the graph, where graph[u] contains pairs (v, weight) * @param src the source vertex - * @param V total number of vertices + * @param v total number of vertices * @return an array of shortest distances from src to every vertex */ - public static int[] dijkstra(List> graph, int src, int V) { - int[] dist = new int[V]; + public static int[] dijkstra(List> graph, int src, int v) { + int[] dist = new int[v]; Arrays.fill(dist, Integer.MAX_VALUE); dist[src] = 0; @@ -52,4 +60,4 @@ public Node(int vertex, int weight) { this.weight = weight; } } -} +} \ No newline at end of file From e8da759e00b7f373d13bebb7d87fdbb9de92ddb9 Mon Sep 17 00:00:00 2001 From: Poorvika Date: Mon, 20 Oct 2025 01:24:20 +0530 Subject: [PATCH 4/5] update path --- .../java/com/thealgorithms/{searches => graph}/Dijkstras.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) rename src/main/java/com/thealgorithms/{searches => graph}/Dijkstras.java (92%) diff --git a/src/main/java/com/thealgorithms/searches/Dijkstras.java b/src/main/java/com/thealgorithms/graph/Dijkstras.java similarity index 92% rename from src/main/java/com/thealgorithms/searches/Dijkstras.java rename to src/main/java/com/thealgorithms/graph/Dijkstras.java index a937698feca6..11d9699ef354 100644 --- a/src/main/java/com/thealgorithms/searches/Dijkstras.java +++ b/src/main/java/com/thealgorithms/graph/Dijkstras.java @@ -1,4 +1,4 @@ -package com.thealgorithms.searches; // update to match path; change back if you intend 'com.thealgorithms.graphs' +package com.thealgorithms.graphs; import java.util.List; import java.util.Arrays; @@ -15,7 +15,6 @@ */ public final class Dijkstras { - // Hide utility-class constructor private Dijkstras() { throw new AssertionError("Cannot instantiate utility class"); } From c110945eb31768201d79a4d5ea40bf3436358986 Mon Sep 17 00:00:00 2001 From: Poorvika Date: Mon, 20 Oct 2025 01:33:01 +0530 Subject: [PATCH 5/5] formatted --- src/main/java/com/thealgorithms/graph/Dijkstras.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/graph/Dijkstras.java b/src/main/java/com/thealgorithms/graph/Dijkstras.java index 11d9699ef354..48cea36cc836 100644 --- a/src/main/java/com/thealgorithms/graph/Dijkstras.java +++ b/src/main/java/com/thealgorithms/graph/Dijkstras.java @@ -1,9 +1,9 @@ -package com.thealgorithms.graphs; +package com.thealgorithms.graph; -import java.util.List; import java.util.Arrays; -import java.util.PriorityQueue; import java.util.Comparator; +import java.util.List; +import java.util.PriorityQueue; /** * Implementation of Dijkstra's Algorithm.