Skip to content

Feature 1: Add-on of DijkstraAlgorithm in backtracking folder #5912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<List<Edge>> 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<int[]> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<List<Dijkstra.Edge>> 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<List<Dijkstra.Edge>> 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<List<Dijkstra.Edge>> 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<List<Dijkstra.Edge>> 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<List<Dijkstra.Edge>> 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);
}
}
Loading