From 3555941c15d913826e1a1bdaf7732c37d4a81ce4 Mon Sep 17 00:00:00 2001 From: Jivan <137729176+Jivanjamadar@users.noreply.github.com> Date: Sun, 20 Oct 2024 16:25:33 +0530 Subject: [PATCH 1/2] Add files via upload --- .../backtracking/DijkstraAlgorithm.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/main/java/com/thealgorithms/backtracking/DijkstraAlgorithm.java diff --git a/src/main/java/com/thealgorithms/backtracking/DijkstraAlgorithm.java b/src/main/java/com/thealgorithms/backtracking/DijkstraAlgorithm.java new file mode 100644 index 000000000000..bbd92a800ab5 --- /dev/null +++ b/src/main/java/com/thealgorithms/backtracking/DijkstraAlgorithm.java @@ -0,0 +1,95 @@ +package com.thealgorithms.backtracking; + +// Author: Jivan Jamdar + +/* +Dijkstra's Algorithm + +Problem Statement: +find the shortest path from a source vertex to all other vertices in a weighted graph with non-negative edge weights. + +|| Graph Structure ||: + + (0) + / | \ + 1 4 2 + / | \ +(1)---3-->(2) + | | + 2 1 + | | +(3)------->(4) + +Edges and Weights: + +(0) to (1): weight 1 +(0) to (2): weight 2 +(0) to (3): weight 4 +(1) to (2): weight 3 +(1) to (3): weight 2 +(2) to (4): weight 1 +(3) to (4): weight 1 + +Given the graph above, the algorithm calculates the shortest path distances from Node 0 to all other nodes. + +Result: +For the given graph, the shortest path distances from Node 0 would be: + +Distance from Node 0 to: +- Node 0: 0 +- Node 1: 1 +- Node 2: 2 +- Node 3: 3 +- Node 4: 3 +*/ + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.PriorityQueue; + +public class Dijkstra { + + static class Edge { + int target; + int weight; + + Edge(int target, int weight) { + this.target = target; + this.weight = weight; + } + } + + // Method to perform Dijkstra's algorithm + public int[] dijkstra(List> graph, int source) { + int V = graph.size(); // Number of vertices in the graph + int[] dist = new int[V]; // Distance array to store shortest path distances + Arrays.fill(dist, Integer.MAX_VALUE); // Initialize distances to infinity + dist[source] = 0; + + // Min heap priority queue to get the vertex with the smallest distance + PriorityQueue pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0])); + pq.add(new int[] {0, source}); // Add source to the priority queue + + // Dijkstra's algorithm loop + while (!pq.isEmpty()) { + int[] current = pq.poll(); + int u = current[1]; + + // Explore all neighboring vertices + for (Edge edge : graph.get(u)) { + int v = edge.target; + int weightUV = edge.weight; + + // If a shorter path to vertex v is found, update and push to queue + if (dist[u] + weightUV < dist[v]) { + dist[v] = dist[u] + weightUV; + pq.add(new int[] {dist[v], v}); + } + } + } + + return dist; + } +} From d4d509559d5db85fc0f4094bd79e09fd5e0cb0eb Mon Sep 17 00:00:00 2001 From: Jivan <137729176+Jivanjamadar@users.noreply.github.com> Date: Sun, 20 Oct 2024 16:26:02 +0530 Subject: [PATCH 2/2] Add files via upload --- .../backtracking/DijkstraAlgorithmTest.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/test/java/com/thealgorithms/backtracking/DijkstraAlgorithmTest.java diff --git a/src/test/java/com/thealgorithms/backtracking/DijkstraAlgorithmTest.java b/src/test/java/com/thealgorithms/backtracking/DijkstraAlgorithmTest.java new file mode 100644 index 000000000000..6456fdaf539a --- /dev/null +++ b/src/test/java/com/thealgorithms/backtracking/DijkstraAlgorithmTest.java @@ -0,0 +1,97 @@ +package com.thealgorithms.backtracking; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Test; + +public class DijkstraTest { + + @Test + void testSingleNodeGraph() { + // Test case where graph has a single node + List> graph = new ArrayList<>(); + graph.add(new ArrayList<>()); + + Dijkstra dijkstra = new Dijkstra(); + int[] result = dijkstra.dijkstra(graph, 0); + + // Since it's the only node, the distance to itself is 0 + assertArrayEquals(new int[]{0}, result); + } + + @Test + void testDisconnectedGraph() { + // Test case where graph is disconnected + int V = 3; // 3 nodes in the graph + List> graph = new ArrayList<>(); + for (int i = 0; i < V; i++) { + graph.add(new ArrayList<>()); + } + + Dijkstra dijkstra = new Dijkstra(); + int[] result = dijkstra.dijkstra(graph, 0); + + // Node 0 can only reach itself, others should be unreachable (infinity) + assertArrayEquals(new int[]{0, Integer.MAX_VALUE, Integer.MAX_VALUE}, result); + } + + @Test + void testSimpleGraph() { + // Test case for a simple connected graph + int V = 4; + List> graph = new ArrayList<>(); + for (int i = 0; i < V; i++) { + graph.add(new ArrayList<>()); + } + + // Add edges to the graph + graph.get(0).add(new Dijkstra.Edge(1, 1)); + graph.get(0).add(new Dijkstra.Edge(2, 4)); + graph.get(1).add(new Dijkstra.Edge(2, 2)); + graph.get(1).add(new Dijkstra.Edge(3, 6)); + graph.get(2).add(new Dijkstra.Edge(3, 3)); + + Dijkstra dijkstra = new Dijkstra(); + int[] result = dijkstra.dijkstra(graph, 0); + + // Expected shortest distances from node 0 to all other nodes + assertArrayEquals(new int[]{0, 1, 3, 6}, result); + } + + @Test + void testInvalidSourceNode() { + int V = 3; + List> graph = new ArrayList<>(); + for (int i = 0; i < V; i++) { + graph.add(new ArrayList<>()); + } + + Dijkstra dijkstra = new Dijkstra(); + + assertThrows(IndexOutOfBoundsException.class, () -> { + dijkstra.dijkstra(graph, 5); + }); + } + + @Test + void testCyclicGraph() { + // Test case for a cyclic graph + int V = 3; + List> graph = new ArrayList<>(); + for (int i = 0; i < V; i++) { + graph.add(new ArrayList<>()); + } + + graph.get(0).add(new Dijkstra.Edge(1, 2)); + graph.get(1).add(new Dijkstra.Edge(2, 3)); + graph.get(2).add(new Dijkstra.Edge(0, 4)); + + Dijkstra dijkstra = new Dijkstra(); + int[] result = dijkstra.dijkstra(graph, 0); + + assertArrayEquals(new int[]{0, 2, 5}, result); + } +}