Skip to content

Commit f725b0f

Browse files
added Dijkstra algorithm
1 parent 5e9d1dc commit f725b0f

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
if (currentDistance > distances[currentNode]) {
27+
continue;
28+
}
29+
30+
List<int[]> neighbors = adjList.getOrDefault(currentNode, new ArrayList<>());
31+
for (int[] neighbor : neighbors) {
32+
int nextNode = neighbor[0];
33+
int edgeWeight = neighbor[1];
34+
35+
int newDistance = distances[currentNode] + edgeWeight;
36+
if (newDistance < distances[nextNode]) {
37+
distances[nextNode] = newDistance;
38+
minHeap.offer(new int[]{nextNode, newDistance});
39+
}
40+
}
41+
}
42+
43+
return distances;
44+
}
45+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
// Simple graph where the path is straightforward
22+
Map<Integer, List<int[]>> adjList = new HashMap<>();
23+
adjList.put(0, List.of(new int[]{1, 1}));
24+
adjList.put(1, List.of(new int[]{2, 1}));
25+
adjList.put(2, List.of(new int[]{3, 1}));
26+
adjList.put(3, new ArrayList<>());
27+
28+
int[] result = dijkstra.shortestPath(4, adjList, 0);
29+
30+
int[] expected = {0, 1, 2, 3};
31+
assertArrayEquals(expected, result, "Shortest path distances should match.");
32+
}
33+
34+
@Test
35+
public void testDisconnectedGraph() {
36+
// Graph where some nodes are unreachable
37+
Map<Integer, List<int[]>> adjList = new HashMap<>();
38+
adjList.put(0, List.of(new int[]{1, 2}));
39+
adjList.put(1, List.of(new int[]{2, 2}));
40+
adjList.put(2, new ArrayList<>());
41+
adjList.put(3, new ArrayList<>());
42+
43+
int[] result = dijkstra.shortestPath(4, adjList, 0);
44+
45+
int[] expected = {0, 2, 4, Integer.MAX_VALUE}; // node 3 is disconnected
46+
assertArrayEquals(expected, result, "Shortest path should indicate unreachable nodes.");
47+
}
48+
49+
@Test
50+
public void testComplexGraph() {
51+
// Complex graph with multiple paths
52+
Map<Integer, List<int[]>> adjList = new HashMap<>();
53+
adjList.put(0, List.of(new int[]{1, 4}, new int[]{2, 1}));
54+
adjList.put(1, List.of(new int[]{3, 1}));
55+
adjList.put(2, List.of(new int[]{1, 2}, new int[]{3, 5}));
56+
adjList.put(3, new ArrayList<>());
57+
58+
int[] result = dijkstra.shortestPath(4, adjList, 0);
59+
60+
int[] expected = {0, 3, 1, 4};
61+
assertArrayEquals(expected, result, "Distances should match expected shortest path distances.");
62+
}
63+
}

0 commit comments

Comments
 (0)