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