Repository: TheAlgorithms/Java
Pull request #5329 (2024-08-15 21:44:43)

refactor: `Dijkstra algorithm`
Rename the previous file from capital letters to the standard Java name.
Adding tests to this algorithm.
Adding Javadocs.

<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5329?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `96.55172%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 42.74%. Comparing base [(`a84a4a2`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/a84a4a29ed64c6e701859339d0c69d1cd5d4982c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`b9da271`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b9da27172e1b4b894a2d6d83b7a2a07bfdc0ba75?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5329?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...ithms/datastructures/graphs/DijkstraAlgorithm.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5329?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fdatastructures%2Fgraphs%2FDijkstraAlgorithm.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9kYXRhc3RydWN0dXJlcy9ncmFwaHMvRGlqa3N0cmFBbGdvcml0aG0uamF2YQ==) | 96.55% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5329?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5329      +/-   ##
============================================
+ Coverage     42.56%   42.74%   +0.18%     
- Complexity     2640     2655      +15     
============================================
  Files           522      522              
  Lines         15493    15491       -2     
  Branches       2948     2947       -1     
============================================
+ Hits           6594     6622      +28     
+ Misses         8616     8585      -31     
- Partials        283      284       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5329?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/DIJSKSTRAS_ALGORITHM.java b/src/main/java/com/thealgorithms/datastructures/graphs/DIJSKSTRAS_ALGORITHM.java
deleted file mode 100644
index 419da4a9..00000000
--- a/src/main/java/com/thealgorithms/datastructures/graphs/DIJSKSTRAS_ALGORITHM.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
-Refer https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/
-for better understanding
- */
-package com.thealgorithms.datastructures.graphs;
-
-class Dijkstras {
-
-    int k = 9;
-
-    int minDist(int[] dist, Boolean[] set) {
-        int min = Integer.MAX_VALUE;
-        int minIndex = -1;
-
-        for (int r = 0; r < k; r++) {
-            if (!set[r] && dist[r] <= min) {
-                min = dist[r];
-                minIndex = r;
-            }
-        }
-
-        return minIndex;
-    }
-
-    void print(int[] dist) {
-        System.out.println("Vertex \t\t Distance");
-        for (int i = 0; i < k; i++) {
-            System.out.println(i + " \t " + dist[i]);
-        }
-    }
-
-    void dijkstra(int[][] graph, int src) {
-        int[] dist = new int[k];
-        Boolean[] set = new Boolean[k];
-
-        for (int i = 0; i < k; i++) {
-            dist[i] = Integer.MAX_VALUE;
-            set[i] = Boolean.FALSE;
-        }
-
-        dist[src] = 0;
-
-        for (int c = 0; c < k - 1; c++) {
-            int u = minDist(dist, set);
-
-            set[u] = Boolean.TRUE;
-
-            for (int v = 0; v < k; v++) {
-                if (!set[v] && graph[u][v] != 0 && dist[u] != Integer.MAX_VALUE && dist[u] + graph[u][v] < dist[v]) {
-                    dist[v] = dist[u] + graph[u][v];
-                }
-            }
-        }
-
-        print(dist);
-    }
-
-    public static void main(String[] args) {
-        int[][] graph = new int[][] {
-            {0, 4, 0, 0, 0, 0, 0, 8, 0},
-            {4, 0, 8, 0, 0, 0, 0, 11, 0},
-            {0, 8, 0, 7, 0, 4, 0, 0, 2},
-            {0, 0, 7, 0, 9, 14, 0, 0, 0},
-            {0, 0, 0, 9, 0, 10, 0, 0, 0},
-            {0, 0, 4, 14, 10, 0, 2, 0, 0},
-            {0, 0, 0, 0, 0, 2, 0, 1, 6},
-            {8, 11, 0, 0, 0, 0, 1, 0, 7},
-            {0, 0, 2, 0, 0, 0, 6, 7, 0},
-        };
-        Dijkstras t = new Dijkstras();
-        t.dijkstra(graph, 0);
-    } // main
-} // djikstras
-/*
-OUTPUT :
-Vertex   Distance
-0            0
-1            4
-2            12
-3            19
-4            21
-5            11
-6            9
-7            8
-8            14
- */
diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java
new file mode 100644
index 00000000..70699a94
--- /dev/null
+++ b/src/main/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithm.java
@@ -0,0 +1,91 @@
+package com.thealgorithms.datastructures.graphs;
+
+import java.util.Arrays;
+
+/**
+ * Dijkstra's algorithm for finding the shortest path from a single source vertex to all other vertices in a graph.
+ */
+public class DijkstraAlgorithm {
+
+    private final int vertexCount;
+
+    /**
+     * Constructs a Dijkstra object with the given number of vertices.
+     *
+     * @param vertexCount The number of vertices in the graph.
+     */
+    public DijkstraAlgorithm(int vertexCount) {
+        this.vertexCount = vertexCount;
+    }
+
+    /**
+     * Executes Dijkstra's algorithm on the provided graph to find the shortest paths from the source vertex to all other vertices.
+     *
+     * The graph is represented as an adjacency matrix where {@code graph[i][j]} represents the weight of the edge from vertex {@code i}
+     * to vertex {@code j}. A value of 0 indicates no edge exists between the vertices.
+     *
+     * @param graph The graph represented as an adjacency matrix.
+     * @param source The source vertex.
+     * @return An array where the value at each index {@code i} represents the shortest distance from the source vertex to vertex {@code i}.
+     * @throws IllegalArgumentException if the source vertex is out of range.
+     */
+    public int[] run(int[][] graph, int source) {
+        if (source < 0 || source >= vertexCount) {
+            throw new IllegalArgumentException("Incorrect source");
+        }
+
+        int[] distances = new int[vertexCount];
+        boolean[] processed = new boolean[vertexCount];
+
+        Arrays.fill(distances, Integer.MAX_VALUE);
+        Arrays.fill(processed, false);
+        distances[source] = 0;
+
+        for (int count = 0; count < vertexCount - 1; count++) {
+            int u = getMinDistanceVertex(distances, processed);
+            processed[u] = true;
+
+            for (int v = 0; v < vertexCount; v++) {
+                if (!processed[v] && graph[u][v] != 0 && distances[u] != Integer.MAX_VALUE && distances[u] + graph[u][v] < distances[v]) {
+                    distances[v] = distances[u] + graph[u][v];
+                }
+            }
+        }
+
+        printDistances(distances);
+        return distances;
+    }
+
+    /**
+     * Finds the vertex with the minimum distance value from the set of vertices that have not yet been processed.
+     *
+     * @param distances The array of current shortest distances from the source vertex.
+     * @param processed The array indicating whether each vertex has been processed.
+     * @return The index of the vertex with the minimum distance value.
+     */
+    private int getMinDistanceVertex(int[] distances, boolean[] processed) {
+        int min = Integer.MAX_VALUE;
+        int minIndex = -1;
+
+        for (int v = 0; v < vertexCount; v++) {
+            if (!processed[v] && distances[v] <= min) {
+                min = distances[v];
+                minIndex = v;
+            }
+        }
+
+        return minIndex;
+    }
+
+    /**
+     * Prints the shortest distances from the source vertex to all other vertices.
+     *
+     * @param distances The array of shortest distances.
+     */
+    private void printDistances(int[] distances) {
+        System.out.println("Vertex \t Distance");
+        for (int i = 0; i < vertexCount; i++) {
+            System.out.println(i + " \t " + distances[i]);
+        }
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithmTest.java b/src/test/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithmTest.java
new file mode 100644
index 00000000..c5df9acd
--- /dev/null
+++ b/src/test/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithmTest.java
@@ -0,0 +1,64 @@
+package com.thealgorithms.datastructures.graphs;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class DijkstraAlgorithmTest {
+
+    private DijkstraAlgorithm dijkstraAlgorithm;
+    private int[][] graph;
+
+    @BeforeEach
+    void setUp() {
+        graph = new int[][] {
+            {0, 4, 0, 0, 0, 0, 0, 8, 0},
+            {4, 0, 8, 0, 0, 0, 0, 11, 0},
+            {0, 8, 0, 7, 0, 4, 0, 0, 2},
+            {0, 0, 7, 0, 9, 14, 0, 0, 0},
+            {0, 0, 0, 9, 0, 10, 0, 0, 0},
+            {0, 0, 4, 14, 10, 0, 2, 0, 0},
+            {0, 0, 0, 0, 0, 2, 0, 1, 6},
+            {8, 11, 0, 0, 0, 0, 1, 0, 7},
+            {0, 0, 2, 0, 0, 0, 6, 7, 0},
+        };
+
+        dijkstraAlgorithm = new DijkstraAlgorithm(graph.length);
+    }
+
+    @Test
+    void testRunAlgorithm() {
+        int[] expectedDistances = {0, 4, 12, 19, 21, 11, 9, 8, 14};
+        assertArrayEquals(expectedDistances, dijkstraAlgorithm.run(graph, 0));
+    }
+
+    @Test
+    void testGraphWithDisconnectedNodes() {
+        int[][] disconnectedGraph = {
+            {0, 3, 0, 0}, {3, 0, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0} // Node 3 is disconnected
+        };
+
+        DijkstraAlgorithm dijkstraDisconnected = new DijkstraAlgorithm(disconnectedGraph.length);
+
+        // Testing from vertex 0
+        int[] expectedDistances = {0, 3, 4, Integer.MAX_VALUE}; // Node 3 is unreachable
+        assertArrayEquals(expectedDistances, dijkstraDisconnected.run(disconnectedGraph, 0));
+    }
+
+    @Test
+    void testSingleVertexGraph() {
+        int[][] singleVertexGraph = {{0}};
+        DijkstraAlgorithm dijkstraSingleVertex = new DijkstraAlgorithm(1);
+
+        int[] expectedDistances = {0}; // The only vertex's distance to itself is 0
+        assertArrayEquals(expectedDistances, dijkstraSingleVertex.run(singleVertexGraph, 0));
+    }
+
+    @Test
+    void testInvalidSourceVertex() {
+        assertThrows(IllegalArgumentException.class, () -> dijkstraAlgorithm.run(graph, -1));
+        assertThrows(IllegalArgumentException.class, () -> dijkstraAlgorithm.run(graph, graph.length));
+    }
+}