From 8ffe287502fa8594e2e5cff45b2c250f4d69bf41 Mon Sep 17 00:00:00 2001 From: Anshul-9923 Date: Sat, 26 Oct 2024 22:20:12 +0530 Subject: [PATCH 1/2] Added Dijkstra Graph Algorithm --- .../graph/DijkstraAlgorithm.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/main/java/com/thealgorithms/graph/DijkstraAlgorithm.java diff --git a/src/main/java/com/thealgorithms/graph/DijkstraAlgorithm.java b/src/main/java/com/thealgorithms/graph/DijkstraAlgorithm.java new file mode 100644 index 000000000000..25eddbff4450 --- /dev/null +++ b/src/main/java/com/thealgorithms/graph/DijkstraAlgorithm.java @@ -0,0 +1,69 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.PriorityQueue; + +/** + * Finds the shortest paths from a source node to all other nodes in a graph using Dijkstra's algorithm. + * + * @param adjList The adjacency list representation of the graph where each edge has a weight. + * @param n The number of nodes in the graph. + * @param source The starting node for finding the shortest path. + * @return An array where the value at each index represents the shortest distance from the source to that node. + */ +public class DijkstraAlgorithm { + + static class Pair { + int node; + int weight; + + Pair(int node, int weight) { + this.node = node; + this.weight = weight; + } + } + + public int[] dijkstra(HashMap> adjList, int n, int source) { + int[] distances = new int[n]; + Arrays.fill(distances, Integer.MAX_VALUE); + distances[source] = 0; + + PriorityQueue pq = new PriorityQueue<>((a, b) -> a.weight - b.weight); + pq.add(new Pair(source, 0)); + + while (!pq.isEmpty()) { + Pair current = pq.poll(); + int currentNode = current.node; + int currentWeight = current.weight; + + List neighbors = adjList.get(currentNode); + if (neighbors != null) { + for (Pair neighbor : neighbors) { + int newDist = currentWeight + neighbor.weight; + if (newDist < distances[neighbor.node]) { + distances[neighbor.node] = newDist; + pq.add(new Pair(neighbor.node, newDist)); + } + } + } + } + return distances; + } + + public static void main(String[] args) { + HashMap> adjList = new HashMap<>(); + adjList.put(0, Arrays.asList(new Pair(1, 4), new Pair(2, 1))); + adjList.put(1, Arrays.asList(new Pair(3, 1))); + adjList.put(2, Arrays.asList(new Pair(1, 2), new Pair(3, 5))); + adjList.put(3, new ArrayList<>()); + + DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(); + int[] distances = dijkstra.dijkstra(adjList, 4, 0); + + System.out.println("Shortest distances from source 0:"); + for (int i = 0; i < distances.length; i++) { + System.out.println("To node " + i + ": " + distances[i]); + } + } +} \ No newline at end of file From 6bfc96c2d20ed16e182a3f12d58a315f11dc2181 Mon Sep 17 00:00:00 2001 From: Anshul-9923 Date: Sat, 26 Oct 2024 22:29:30 +0530 Subject: [PATCH 2/2] Updated according to checks --- .../java/com/thealgorithms/graph/DijkstraAlgorithm.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/graph/DijkstraAlgorithm.java b/src/main/java/com/thealgorithms/graph/DijkstraAlgorithm.java index 25eddbff4450..bc46f598d09c 100644 --- a/src/main/java/com/thealgorithms/graph/DijkstraAlgorithm.java +++ b/src/main/java/com/thealgorithms/graph/DijkstraAlgorithm.java @@ -8,8 +8,8 @@ * Finds the shortest paths from a source node to all other nodes in a graph using Dijkstra's algorithm. * * @param adjList The adjacency list representation of the graph where each edge has a weight. - * @param n The number of nodes in the graph. - * @param source The starting node for finding the shortest path. + * @param n The number of nodes in the graph. + * @param source The starting node for finding the shortest path. * @return An array where the value at each index represents the shortest distance from the source to that node. */ public class DijkstraAlgorithm { @@ -66,4 +66,4 @@ public static void main(String[] args) { System.out.println("To node " + i + ": " + distances[i]); } } -} \ No newline at end of file +}