|
| 1 | +package com.thealgorithms.backtracking; |
| 2 | + |
| 3 | +// Author: Jivan Jamdar |
| 4 | + |
| 5 | +/* |
| 6 | +Dijkstra's Algorithm |
| 7 | +
|
| 8 | +Problem Statement: |
| 9 | +find the shortest path from a source vertex to all other vertices in a weighted graph with non-negative edge weights. |
| 10 | +
|
| 11 | +|| Graph Structure ||: |
| 12 | +
|
| 13 | + (0) |
| 14 | + / | \ |
| 15 | + 1 4 2 |
| 16 | + / | \ |
| 17 | +(1)---3-->(2) |
| 18 | + | | |
| 19 | + 2 1 |
| 20 | + | | |
| 21 | +(3)------->(4) |
| 22 | +
|
| 23 | +Edges and Weights: |
| 24 | +
|
| 25 | +(0) to (1): weight 1 |
| 26 | +(0) to (2): weight 2 |
| 27 | +(0) to (3): weight 4 |
| 28 | +(1) to (2): weight 3 |
| 29 | +(1) to (3): weight 2 |
| 30 | +(2) to (4): weight 1 |
| 31 | +(3) to (4): weight 1 |
| 32 | +
|
| 33 | +Given the graph above, the algorithm calculates the shortest path distances from Node 0 to all other nodes. |
| 34 | +
|
| 35 | +Result: |
| 36 | +For the given graph, the shortest path distances from Node 0 would be: |
| 37 | +
|
| 38 | +Distance from Node 0 to: |
| 39 | +- Node 0: 0 |
| 40 | +- Node 1: 1 |
| 41 | +- Node 2: 2 |
| 42 | +- Node 3: 3 |
| 43 | +- Node 4: 3 |
| 44 | +*/ |
| 45 | + |
| 46 | +import java.util.ArrayList; |
| 47 | +import java.util.Arrays; |
| 48 | +import java.util.Comparator; |
| 49 | +import java.util.List; |
| 50 | +import java.util.PriorityQueue; |
| 51 | + |
| 52 | +public class Dijkstra { |
| 53 | + |
| 54 | + static class Edge { |
| 55 | + int target; |
| 56 | + int weight; |
| 57 | + |
| 58 | + Edge(int target, int weight) { |
| 59 | + this.target = target; |
| 60 | + this.weight = weight; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Method to perform Dijkstra's algorithm |
| 65 | + public int[] dijkstra(List<List<Edge>> graph, int source) { |
| 66 | + int V = graph.size(); // Number of vertices in the graph |
| 67 | + int[] dist = new int[V]; // Distance array to store shortest path distances |
| 68 | + Arrays.fill(dist, Integer.MAX_VALUE); // Initialize distances to infinity |
| 69 | + dist[source] = 0; |
| 70 | + |
| 71 | + // Min heap priority queue to get the vertex with the smallest distance |
| 72 | + PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0])); |
| 73 | + pq.add(new int[] {0, source}); // Add source to the priority queue |
| 74 | + |
| 75 | + // Dijkstra's algorithm loop |
| 76 | + while (!pq.isEmpty()) { |
| 77 | + int[] current = pq.poll(); |
| 78 | + int u = current[1]; |
| 79 | + |
| 80 | + // Explore all neighboring vertices |
| 81 | + for (Edge edge : graph.get(u)) { |
| 82 | + int v = edge.target; |
| 83 | + int weightUV = edge.weight; |
| 84 | + |
| 85 | + // If a shorter path to vertex v is found, update and push to queue |
| 86 | + if (dist[u] + weightUV < dist[v]) { |
| 87 | + dist[v] = dist[u] + weightUV; |
| 88 | + pq.add(new int[] {dist[v], v}); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return dist; |
| 94 | + } |
| 95 | +} |
0 commit comments