Skip to content

Commit 98bee26

Browse files
authored
refactor: Dijkstra algorithm (#5329)
1 parent e32cab3 commit 98bee26

File tree

3 files changed

+155
-86
lines changed

3 files changed

+155
-86
lines changed

src/main/java/com/thealgorithms/datastructures/graphs/DIJSKSTRAS_ALGORITHM.java

-86
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Dijkstra's algorithm for finding the shortest path from a single source vertex to all other vertices in a graph.
7+
*/
8+
public class DijkstraAlgorithm {
9+
10+
private final int vertexCount;
11+
12+
/**
13+
* Constructs a Dijkstra object with the given number of vertices.
14+
*
15+
* @param vertexCount The number of vertices in the graph.
16+
*/
17+
public DijkstraAlgorithm(int vertexCount) {
18+
this.vertexCount = vertexCount;
19+
}
20+
21+
/**
22+
* Executes Dijkstra's algorithm on the provided graph to find the shortest paths from the source vertex to all other vertices.
23+
*
24+
* The graph is represented as an adjacency matrix where {@code graph[i][j]} represents the weight of the edge from vertex {@code i}
25+
* to vertex {@code j}. A value of 0 indicates no edge exists between the vertices.
26+
*
27+
* @param graph The graph represented as an adjacency matrix.
28+
* @param source The source vertex.
29+
* @return An array where the value at each index {@code i} represents the shortest distance from the source vertex to vertex {@code i}.
30+
* @throws IllegalArgumentException if the source vertex is out of range.
31+
*/
32+
public int[] run(int[][] graph, int source) {
33+
if (source < 0 || source >= vertexCount) {
34+
throw new IllegalArgumentException("Incorrect source");
35+
}
36+
37+
int[] distances = new int[vertexCount];
38+
boolean[] processed = new boolean[vertexCount];
39+
40+
Arrays.fill(distances, Integer.MAX_VALUE);
41+
Arrays.fill(processed, false);
42+
distances[source] = 0;
43+
44+
for (int count = 0; count < vertexCount - 1; count++) {
45+
int u = getMinDistanceVertex(distances, processed);
46+
processed[u] = true;
47+
48+
for (int v = 0; v < vertexCount; v++) {
49+
if (!processed[v] && graph[u][v] != 0 && distances[u] != Integer.MAX_VALUE && distances[u] + graph[u][v] < distances[v]) {
50+
distances[v] = distances[u] + graph[u][v];
51+
}
52+
}
53+
}
54+
55+
printDistances(distances);
56+
return distances;
57+
}
58+
59+
/**
60+
* Finds the vertex with the minimum distance value from the set of vertices that have not yet been processed.
61+
*
62+
* @param distances The array of current shortest distances from the source vertex.
63+
* @param processed The array indicating whether each vertex has been processed.
64+
* @return The index of the vertex with the minimum distance value.
65+
*/
66+
private int getMinDistanceVertex(int[] distances, boolean[] processed) {
67+
int min = Integer.MAX_VALUE;
68+
int minIndex = -1;
69+
70+
for (int v = 0; v < vertexCount; v++) {
71+
if (!processed[v] && distances[v] <= min) {
72+
min = distances[v];
73+
minIndex = v;
74+
}
75+
}
76+
77+
return minIndex;
78+
}
79+
80+
/**
81+
* Prints the shortest distances from the source vertex to all other vertices.
82+
*
83+
* @param distances The array of shortest distances.
84+
*/
85+
private void printDistances(int[] distances) {
86+
System.out.println("Vertex \t Distance");
87+
for (int i = 0; i < vertexCount; i++) {
88+
System.out.println(i + " \t " + distances[i]);
89+
}
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class DijkstraAlgorithmTest {
10+
11+
private DijkstraAlgorithm dijkstraAlgorithm;
12+
private int[][] graph;
13+
14+
@BeforeEach
15+
void setUp() {
16+
graph = new int[][] {
17+
{0, 4, 0, 0, 0, 0, 0, 8, 0},
18+
{4, 0, 8, 0, 0, 0, 0, 11, 0},
19+
{0, 8, 0, 7, 0, 4, 0, 0, 2},
20+
{0, 0, 7, 0, 9, 14, 0, 0, 0},
21+
{0, 0, 0, 9, 0, 10, 0, 0, 0},
22+
{0, 0, 4, 14, 10, 0, 2, 0, 0},
23+
{0, 0, 0, 0, 0, 2, 0, 1, 6},
24+
{8, 11, 0, 0, 0, 0, 1, 0, 7},
25+
{0, 0, 2, 0, 0, 0, 6, 7, 0},
26+
};
27+
28+
dijkstraAlgorithm = new DijkstraAlgorithm(graph.length);
29+
}
30+
31+
@Test
32+
void testRunAlgorithm() {
33+
int[] expectedDistances = {0, 4, 12, 19, 21, 11, 9, 8, 14};
34+
assertArrayEquals(expectedDistances, dijkstraAlgorithm.run(graph, 0));
35+
}
36+
37+
@Test
38+
void testGraphWithDisconnectedNodes() {
39+
int[][] disconnectedGraph = {
40+
{0, 3, 0, 0}, {3, 0, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0} // Node 3 is disconnected
41+
};
42+
43+
DijkstraAlgorithm dijkstraDisconnected = new DijkstraAlgorithm(disconnectedGraph.length);
44+
45+
// Testing from vertex 0
46+
int[] expectedDistances = {0, 3, 4, Integer.MAX_VALUE}; // Node 3 is unreachable
47+
assertArrayEquals(expectedDistances, dijkstraDisconnected.run(disconnectedGraph, 0));
48+
}
49+
50+
@Test
51+
void testSingleVertexGraph() {
52+
int[][] singleVertexGraph = {{0}};
53+
DijkstraAlgorithm dijkstraSingleVertex = new DijkstraAlgorithm(1);
54+
55+
int[] expectedDistances = {0}; // The only vertex's distance to itself is 0
56+
assertArrayEquals(expectedDistances, dijkstraSingleVertex.run(singleVertexGraph, 0));
57+
}
58+
59+
@Test
60+
void testInvalidSourceVertex() {
61+
assertThrows(IllegalArgumentException.class, () -> dijkstraAlgorithm.run(graph, -1));
62+
assertThrows(IllegalArgumentException.class, () -> dijkstraAlgorithm.run(graph, graph.length));
63+
}
64+
}

0 commit comments

Comments
 (0)