Skip to content

Commit 08d5cc5

Browse files
author
Priyanshu1303d
committed
[FEAT] Implement Dials Algorithm(Graph)
1 parent 9484c7e commit 08d5cc5

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Set;
8+
9+
/**
10+
* An implementation of Dial's Algorithm for the single-source shortest path problem.
11+
* This algorithm is an optimization of Dijkstra's algorithm and is particularly
12+
* efficient for graphs with small, non-negative integer edge weights.
13+
*
14+
* It uses a bucket queue (implemented here as a List of HashSets) to store vertices,
15+
* where each bucket corresponds to a specific distance from the source. This is more
16+
* efficient than a standard priority queue when the range of edge weights is small.
17+
*
18+
* Time Complexity: O(E + W * V), where E is the number of edges, V is the number
19+
* of vertices, and W is the maximum weight of any edge.
20+
*
21+
* @see <a href="https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Dial's_algorithm">Wikipedia - Dial's Algorithm</a>
22+
*/
23+
public final class DialsAlgorithm {
24+
/**
25+
* Private constructor to prevent instantiation of this utility class.
26+
*/
27+
private DialsAlgorithm() {
28+
}
29+
/**
30+
* Represents an edge in the graph, connecting to a destination vertex with a given weight.
31+
*/
32+
public static class Edge {
33+
private final int destination;
34+
private final int weight;
35+
36+
public Edge(int destination, int weight) {
37+
this.destination = destination;
38+
this.weight = weight;
39+
}
40+
41+
public int getDestination() {
42+
return destination;
43+
}
44+
45+
public int getWeight() {
46+
return weight;
47+
}
48+
}
49+
/**
50+
* Finds the shortest paths from a source vertex to all other vertices in a weighted graph.
51+
*
52+
* @param graph The graph represented as an adjacency list.
53+
* @param source The source vertex to start from (0-indexed).
54+
* @param maxEdgeWeight The maximum weight of any single edge in the graph.
55+
* @return An array of integers where the value at each index `i` is the
56+
* shortest distance from the source to vertex `i`. Unreachable vertices
57+
* will have a value of Integer.MAX_VALUE.
58+
* @throws IllegalArgumentException if the source vertex is out of bounds.
59+
*/
60+
public static int[] run(List<List<Edge>> graph, int source, int maxEdgeWeight) {
61+
int numVertices = graph.size();
62+
if (source < 0 || source >= numVertices) {
63+
throw new IllegalArgumentException("Source vertex is out of bounds.");
64+
}
65+
66+
// Initialize distances array
67+
int[] distances = new int[numVertices];
68+
Arrays.fill(distances, Integer.MAX_VALUE);
69+
distances[source] = 0;
70+
71+
// The bucket queue. Size is determined by the max possible path length.
72+
int maxPathWeight = maxEdgeWeight * (numVertices > 0 ? numVertices - 1 : 0);
73+
List<Set<Integer>> buckets = new ArrayList<>(maxPathWeight + 1);
74+
for (int i = 0; i <= maxPathWeight; i++) {
75+
buckets.add(new HashSet<>());
76+
}
77+
78+
// Add the source vertex to the first bucket
79+
buckets.get(0).add(source);
80+
81+
// Process buckets in increasing order of distance
82+
for (int d = 0; d <= maxPathWeight; d++) {
83+
// Process all vertices in the current bucket
84+
while (!buckets.get(d).isEmpty()) {
85+
// Get and remove a vertex from the current bucket
86+
int u = buckets.get(d).iterator().next();
87+
buckets.get(d).remove(u);
88+
89+
// If we've found a shorter path already, skip
90+
if (d > distances[u]) {
91+
continue;
92+
}
93+
94+
// Relax all adjacent edges
95+
for (Edge edge : graph.get(u)) {
96+
int v = edge.getDestination();
97+
int weight = edge.getWeight();
98+
99+
// If a shorter path to v is found
100+
if (distances[u] != Integer.MAX_VALUE && distances[u] + weight < distances[v]) {
101+
// If v was already in a bucket, remove it from the old one
102+
if (distances[v] != Integer.MAX_VALUE) {
103+
buckets.get(distances[v]).remove(v);
104+
}
105+
// Update distance and move v to the new bucket
106+
distances[v] = distances[u] + weight;
107+
buckets.get(distances[v]).add(v);
108+
}
109+
}
110+
}
111+
}
112+
return distances;
113+
}
114+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 java.util.ArrayList;
7+
import java.util.List;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.DisplayName;
10+
import org.junit.jupiter.api.Test;
11+
12+
final class DialsAlgorithmTest {
13+
14+
private List<List<DialsAlgorithm.Edge>> graph;
15+
private static final int numVertices = 6;
16+
private static final int maxEdgeWeight = 10;
17+
18+
@BeforeEach
19+
void setUp() {
20+
graph = new ArrayList<>();
21+
for (int i = 0; i < numVertices; i++) {
22+
graph.add(new ArrayList<>());
23+
}
24+
}
25+
26+
private void addEdge(int u, int v, int weight) {
27+
graph.get(u).add(new DialsAlgorithm.Edge(v, weight));
28+
}
29+
30+
@Test
31+
@DisplayName("Test with a simple connected graph")
32+
void testSimpleGraph() {
33+
// Build graph from a standard example
34+
addEdge(0, 1, 2);
35+
addEdge(0, 2, 4);
36+
addEdge(1, 2, 1);
37+
addEdge(1, 3, 7);
38+
addEdge(2, 4, 3);
39+
addEdge(3, 5, 1);
40+
addEdge(4, 3, 2);
41+
addEdge(4, 5, 5);
42+
43+
int[] expectedDistances = {0, 2, 3, 8, 6, 9};
44+
int[] actualDistances = DialsAlgorithm.run(graph, 0, maxEdgeWeight);
45+
assertArrayEquals(expectedDistances, actualDistances);
46+
}
47+
48+
@Test
49+
@DisplayName("Test with a disconnected node")
50+
void testDisconnectedNode() {
51+
addEdge(0, 1, 5);
52+
addEdge(1, 2, 5);
53+
// Node 3, 4, 5 are disconnected
54+
55+
int[] expectedDistances = {0, 5, 10, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE};
56+
int[] actualDistances = DialsAlgorithm.run(graph, 0, maxEdgeWeight);
57+
assertArrayEquals(expectedDistances, actualDistances);
58+
}
59+
60+
@Test
61+
@DisplayName("Test with source as destination")
62+
void testSourceIsDestination() {
63+
addEdge(0, 1, 10);
64+
int[] expectedDistances = {0, 10, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE};
65+
// Run with source 0
66+
int[] actualDistances = DialsAlgorithm.run(graph, 0, maxEdgeWeight);
67+
assertArrayEquals(expectedDistances, actualDistances);
68+
}
69+
70+
@Test
71+
@DisplayName("Test graph with multiple paths to a node")
72+
void testMultiplePaths() {
73+
addEdge(0, 1, 10);
74+
addEdge(0, 2, 3);
75+
addEdge(2, 1, 2); // Shorter path to 1 is via 2 (3+2=5)
76+
77+
int[] expectedDistances = {0, 5, 3, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE};
78+
int[] actualDistances = DialsAlgorithm.run(graph, 0, maxEdgeWeight);
79+
assertArrayEquals(expectedDistances, actualDistances);
80+
}
81+
82+
@Test
83+
@DisplayName("Test with an invalid source vertex")
84+
void testInvalidSource() {
85+
assertThrows(IllegalArgumentException.class, () -> DialsAlgorithm.run(graph, -1, maxEdgeWeight));
86+
assertThrows(IllegalArgumentException.class, () -> DialsAlgorithm.run(graph, numVertices, maxEdgeWeight));
87+
}
88+
}

0 commit comments

Comments
 (0)