From f725b0fab965acbfebbb14d247e8e3233dbc03cc Mon Sep 17 00:00:00 2001 From: Amit-Verma5262 Date: Tue, 29 Oct 2024 18:24:14 +0530 Subject: [PATCH 1/2] added Dijkstra algorithm --- .../graph/DijkstraShortestPath.java | 45 +++++++++++++ .../graph/DijkstraShortestPathTest.java | 63 +++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 src/main/java/com/thealgorithms/graph/DijkstraShortestPath.java create mode 100644 src/test/java/com/thealgorithms/graph/DijkstraShortestPathTest.java diff --git a/src/main/java/com/thealgorithms/graph/DijkstraShortestPath.java b/src/main/java/com/thealgorithms/graph/DijkstraShortestPath.java new file mode 100644 index 000000000000..dd7f20bd9ad7 --- /dev/null +++ b/src/main/java/com/thealgorithms/graph/DijkstraShortestPath.java @@ -0,0 +1,45 @@ +import java.util.*; + +public class DijkstraShortestPath { + + /** + * Finds the shortest path distances from the startNode to all other nodes in a weighted graph. + * + * @param n The number of nodes in the graph. + * @param adjList The adjacency list where each entry contains pairs of neighboring nodes and edge weights. + * @param startNode The starting node for the shortest path calculation. + * @return An array where each index i contains the shortest distance from startNode to node i. + */ + public int[] shortestPath(int n, Map> adjList, int startNode) { + int[] distances = new int[n]; + Arrays.fill(distances, Integer.MAX_VALUE); + distances[startNode] = 0; + + PriorityQueue minHeap = new PriorityQueue<>(Comparator.comparingInt(a -> a[1])); + minHeap.offer(new int[]{startNode, 0}); + + while (!minHeap.isEmpty()) { + int[] current = minHeap.poll(); + int currentNode = current[0]; + int currentDistance = current[1]; + + if (currentDistance > distances[currentNode]) { + continue; + } + + List neighbors = adjList.getOrDefault(currentNode, new ArrayList<>()); + for (int[] neighbor : neighbors) { + int nextNode = neighbor[0]; + int edgeWeight = neighbor[1]; + + int newDistance = distances[currentNode] + edgeWeight; + if (newDistance < distances[nextNode]) { + distances[nextNode] = newDistance; + minHeap.offer(new int[]{nextNode, newDistance}); + } + } + } + + return distances; + } +} diff --git a/src/test/java/com/thealgorithms/graph/DijkstraShortestPathTest.java b/src/test/java/com/thealgorithms/graph/DijkstraShortestPathTest.java new file mode 100644 index 000000000000..cdcfc7ea8d14 --- /dev/null +++ b/src/test/java/com/thealgorithms/graph/DijkstraShortestPathTest.java @@ -0,0 +1,63 @@ +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import java.util.HashMap; +import java.util.List; +import java.util.ArrayList; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class DijkstraShortestPathTest { + + private DijkstraShortestPath dijkstra; + + @BeforeEach + public void setUp() { + dijkstra = new DijkstraShortestPath(); + } + + @Test + public void testSinglePath() { + // Simple graph where the path is straightforward + Map> adjList = new HashMap<>(); + adjList.put(0, List.of(new int[]{1, 1})); + adjList.put(1, List.of(new int[]{2, 1})); + adjList.put(2, List.of(new int[]{3, 1})); + adjList.put(3, new ArrayList<>()); + + int[] result = dijkstra.shortestPath(4, adjList, 0); + + int[] expected = {0, 1, 2, 3}; + assertArrayEquals(expected, result, "Shortest path distances should match."); + } + + @Test + public void testDisconnectedGraph() { + // Graph where some nodes are unreachable + Map> adjList = new HashMap<>(); + adjList.put(0, List.of(new int[]{1, 2})); + adjList.put(1, List.of(new int[]{2, 2})); + adjList.put(2, new ArrayList<>()); + adjList.put(3, new ArrayList<>()); + + int[] result = dijkstra.shortestPath(4, adjList, 0); + + int[] expected = {0, 2, 4, Integer.MAX_VALUE}; // node 3 is disconnected + assertArrayEquals(expected, result, "Shortest path should indicate unreachable nodes."); + } + + @Test + public void testComplexGraph() { + // Complex graph with multiple paths + Map> adjList = new HashMap<>(); + adjList.put(0, List.of(new int[]{1, 4}, new int[]{2, 1})); + adjList.put(1, List.of(new int[]{3, 1})); + adjList.put(2, List.of(new int[]{1, 2}, new int[]{3, 5})); + adjList.put(3, new ArrayList<>()); + + int[] result = dijkstra.shortestPath(4, adjList, 0); + + int[] expected = {0, 3, 1, 4}; + assertArrayEquals(expected, result, "Distances should match expected shortest path distances."); + } +} From e3539fc307f48635e347033c2385c59d7cbca5eb Mon Sep 17 00:00:00 2001 From: Amit-Verma5262 Date: Tue, 29 Oct 2024 18:31:57 +0530 Subject: [PATCH 2/2] added Dijkstra algorithm and link of wikipedia --- src/main/java/com/thealgorithms/graph/DijkstraShortestPath.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/thealgorithms/graph/DijkstraShortestPath.java b/src/main/java/com/thealgorithms/graph/DijkstraShortestPath.java index dd7f20bd9ad7..a7a1b9f0ff3d 100644 --- a/src/main/java/com/thealgorithms/graph/DijkstraShortestPath.java +++ b/src/main/java/com/thealgorithms/graph/DijkstraShortestPath.java @@ -43,3 +43,5 @@ public int[] shortestPath(int n, Map> adjList, int startNod return distances; } } + +//https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#:~:text=Dijkstra's%20algorithm%20(/%CB%88da%C9%AA,and%20published%20three%20years%20later. \ No newline at end of file