Skip to content

Commit dee1c0a

Browse files
added Dijkstra Algorithm
1 parent fd14016 commit dee1c0a

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.*;
2+
3+
public class DijkstraShortestPath {
4+
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];
15+
Arrays.fill(distances, Integer.MAX_VALUE);
16+
distances[startNode] = 0;
17+
18+
PriorityQueue<int[]> minHeap = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
19+
minHeap.offer(new int[]{startNode, 0});
20+
21+
while (!minHeap.isEmpty()) {
22+
int[] current = minHeap.poll();
23+
int currentNode = current[0];
24+
int currentDistance = current[1];
25+
26+
// This condition prevents revisiting nodes with a longer distance
27+
if (currentDistance > distances[currentNode]) {
28+
continue;
29+
}
30+
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+
36+
int newDistance = distances[currentNode] + edgeWeight;
37+
if (newDistance < distances[nextNode]) {
38+
distances[nextNode] = newDistance;
39+
minHeap.offer(new int[]{nextNode, newDistance});
40+
}
41+
}
42+
}
43+
44+
return distances;
45+
}
46+
}
47+
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.ArrayList;
6+
import java.util.Map;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class DijkstraShortestPathTest {
11+
12+
private DijkstraShortestPath dijkstra;
13+
14+
@BeforeEach
15+
public void setUp() {
16+
dijkstra = new DijkstraShortestPath();
17+
}
18+
19+
@Test
20+
public void testSinglePath() {
21+
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}));
25+
adjList.put(3, new ArrayList<>());
26+
27+
int[] result = dijkstra.shortestPath(4, adjList, 0);
28+
29+
int[] expected = {0, 1, 2, 3};
30+
assertArrayEquals(expected, result, "Shortest path distances should match.");
31+
}
32+
33+
@Test
34+
public void testDisconnectedGraph() {
35+
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}));
38+
adjList.put(2, new ArrayList<>());
39+
adjList.put(3, new ArrayList<>());
40+
41+
int[] result = dijkstra.shortestPath(4, adjList, 0);
42+
43+
int[] expected = {0, 2, 4, Integer.MAX_VALUE};
44+
assertArrayEquals(expected, result, "Shortest path should indicate unreachable nodes.");
45+
}
46+
47+
@Test
48+
public void testComplexGraph() {
49+
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}));
53+
adjList.put(3, new ArrayList<>());
54+
55+
int[] result = dijkstra.shortestPath(4, adjList, 0);
56+
57+
int[] expected = {0, 3, 1, 4};
58+
assertArrayEquals(expected, result, "Distances should match expected shortest path distances.");
59+
}
60+
61+
@Test
62+
public void testRevisitedNodeWithHigherDistance() {
63+
// This graph is set up to test the condition where a node is revisited with a higher distance.
64+
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}));
68+
adjList.put(3, new ArrayList<>());
69+
70+
int[] result = dijkstra.shortestPath(4, adjList, 0);
71+
72+
int[] expected = {0, 5, 6, 7};
73+
assertArrayEquals(expected, result, "Distances should match expected shortest path distances.");
74+
}
75+
}
76+
//added test cases

0 commit comments

Comments
 (0)