Skip to content

Commit 9f88de2

Browse files
added Dijkstra Algorithm in graph
1 parent dee1c0a commit 9f88de2

File tree

2 files changed

+23
-41
lines changed

2 files changed

+23
-41
lines changed

src/main/java/com/thealgorithms/graph/DijkstraShortestPath.java

+9-22
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,36 @@
22

33
public class DijkstraShortestPath {
44

5-
/**
6-
* Finds the shortest path distances from the startNode to all other nodes in a weighted graph.
7-
*
8-
* @param n The number of nodes in the graph.
9-
* @param adjList The adjacency list where each entry contains pairs of neighboring nodes and edge weights.
10-
* @param startNode The starting node for the shortest path calculation.
11-
* @return An array where each index i contains the shortest distance from startNode to node i.
12-
*/
13-
public int[] shortestPath(int n, Map<Integer, List<int[]>> adjList, int startNode) {
14-
int[] distances = new int[n];
5+
public int[] shortestPath(int numNodes, Map<Integer, List<int[]>> adjList, int startNode) {
6+
int[] distances = new int[numNodes];
157
Arrays.fill(distances, Integer.MAX_VALUE);
168
distances[startNode] = 0;
179

1810
PriorityQueue<int[]> minHeap = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
19-
minHeap.offer(new int[]{startNode, 0});
11+
minHeap.offer(new int[] {startNode, 0});
2012

2113
while (!minHeap.isEmpty()) {
2214
int[] current = minHeap.poll();
2315
int currentNode = current[0];
2416
int currentDistance = current[1];
2517

26-
// This condition prevents revisiting nodes with a longer distance
2718
if (currentDistance > distances[currentNode]) {
2819
continue;
2920
}
3021

31-
List<int[]> neighbors = adjList.getOrDefault(currentNode, new ArrayList<>());
32-
for (int[] neighbor : neighbors) {
33-
int nextNode = neighbor[0];
34-
int edgeWeight = neighbor[1];
35-
22+
for (int[] edge : adjList.getOrDefault(currentNode, Collections.emptyList())) {
23+
int nextNode = edge[0];
24+
int edgeWeight = edge[1];
3625
int newDistance = distances[currentNode] + edgeWeight;
3726
if (newDistance < distances[nextNode]) {
3827
distances[nextNode] = newDistance;
39-
minHeap.offer(new int[]{nextNode, newDistance});
28+
minHeap.offer(new int[] {nextNode, newDistance});
4029
}
4130
}
4231
}
43-
4432
return distances;
4533
}
4634
}
4735

48-
//wikipedia link for algorithm
49-
50-
//https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#:~:text=Dijkstra's%20algorithm%20(/%CB%88da%C9%AA,and%20published%20three%20years%20later.
36+
// wikipedia link for algorithm
37+
// https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#:~:text=Dijkstra's%20algorithm%20(/%CB%88da%C9%AA,and%20published%20three%20years%20later.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22

3+
import java.util.ArrayList;
34
import java.util.HashMap;
45
import java.util.List;
5-
import java.util.ArrayList;
66
import java.util.Map;
77
import org.junit.jupiter.api.BeforeEach;
88
import org.junit.jupiter.api.Test;
@@ -19,41 +19,38 @@ public void setUp() {
1919
@Test
2020
public void testSinglePath() {
2121
Map<Integer, List<int[]>> adjList = new HashMap<>();
22-
adjList.put(0, List.of(new int[]{1, 1}));
23-
adjList.put(1, List.of(new int[]{2, 1}));
24-
adjList.put(2, List.of(new int[]{3, 1}));
22+
adjList.put(0, List.of(new int[] {1, 1}));
23+
adjList.put(1, List.of(new int[] {2, 1}));
24+
adjList.put(2, List.of(new int[] {3, 1}));
2525
adjList.put(3, new ArrayList<>());
2626

2727
int[] result = dijkstra.shortestPath(4, adjList, 0);
28-
2928
int[] expected = {0, 1, 2, 3};
30-
assertArrayEquals(expected, result, "Shortest path distances should match.");
29+
assertArrayEquals(expected, result, "Distances should match expected shortest path distances.");
3130
}
3231

3332
@Test
3433
public void testDisconnectedGraph() {
3534
Map<Integer, List<int[]>> adjList = new HashMap<>();
36-
adjList.put(0, List.of(new int[]{1, 2}));
37-
adjList.put(1, List.of(new int[]{2, 2}));
35+
adjList.put(0, List.of(new int[] {1, 2}));
36+
adjList.put(1, List.of(new int[] {2, 2}));
3837
adjList.put(2, new ArrayList<>());
3938
adjList.put(3, new ArrayList<>());
4039

4140
int[] result = dijkstra.shortestPath(4, adjList, 0);
42-
4341
int[] expected = {0, 2, 4, Integer.MAX_VALUE};
44-
assertArrayEquals(expected, result, "Shortest path should indicate unreachable nodes.");
42+
assertArrayEquals(expected, result, "Distances should match expected shortest path distances.");
4543
}
4644

4745
@Test
4846
public void testComplexGraph() {
4947
Map<Integer, List<int[]>> adjList = new HashMap<>();
50-
adjList.put(0, List.of(new int[]{1, 4}, new int[]{2, 1}));
51-
adjList.put(1, List.of(new int[]{3, 1}));
52-
adjList.put(2, List.of(new int[]{1, 2}, new int[]{3, 5}));
48+
adjList.put(0, List.of(new int[] {1, 4}, new int[] {2, 1}));
49+
adjList.put(1, List.of(new int[] {3, 1}));
50+
adjList.put(2, List.of(new int[] {1, 2}, new int[] {3, 5}));
5351
adjList.put(3, new ArrayList<>());
5452

5553
int[] result = dijkstra.shortestPath(4, adjList, 0);
56-
5754
int[] expected = {0, 3, 1, 4};
5855
assertArrayEquals(expected, result, "Distances should match expected shortest path distances.");
5956
}
@@ -62,15 +59,13 @@ public void testComplexGraph() {
6259
public void testRevisitedNodeWithHigherDistance() {
6360
// This graph is set up to test the condition where a node is revisited with a higher distance.
6461
Map<Integer, List<int[]>> adjList = new HashMap<>();
65-
adjList.put(0, List.of(new int[]{1, 5}));
66-
adjList.put(1, List.of(new int[]{2, 1}));
67-
adjList.put(2, List.of(new int[]{0, 3}, new int[]{3, 1}));
62+
adjList.put(0, List.of(new int[] {1, 5}));
63+
adjList.put(1, List.of(new int[] {2, 1}));
64+
adjList.put(2, List.of(new int[] {0, 3}, new int[] {3, 1}));
6865
adjList.put(3, new ArrayList<>());
6966

7067
int[] result = dijkstra.shortestPath(4, adjList, 0);
71-
7268
int[] expected = {0, 5, 6, 7};
7369
assertArrayEquals(expected, result, "Distances should match expected shortest path distances.");
7470
}
7571
}
76-
//added test cases

0 commit comments

Comments
 (0)