Dijkstras.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import java.util.*;
  2. class Dijkstras {
  3. // Construct adjacency list using ArrayList of ArrayList
  4. static ArrayList<ArrayList<ArrayList<Integer>>>
  5. constructAdj(int[][] edges, int V) {
  6. // Initialize the adjacency list
  7. ArrayList<ArrayList<ArrayList<Integer>>>
  8. adj = new ArrayList<>();
  9. for (int i = 0; i < V; i++) {
  10. adj.add(new ArrayList<>());
  11. }
  12. // Fill the adjacency list from edges
  13. for (int[] edge : edges) {
  14. int u = edge[0];
  15. int v = edge[1];
  16. int wt = edge[2];
  17. // Add edge from u to v
  18. ArrayList<Integer> e1 = new ArrayList<>();
  19. e1.add(v);
  20. e1.add(wt);
  21. adj.get(u).add(e1);
  22. // Since the graph is undirected, add edge from v to u
  23. ArrayList<Integer> e2 = new ArrayList<>();
  24. e2.add(u);
  25. e2.add(wt);
  26. adj.get(v).add(e2);
  27. }
  28. return adj;
  29. }
  30. // Returns shortest distances from src to all other vertices
  31. static int[] dijkstra(int V, int[][] edges, int src) {
  32. // Create adjacency list
  33. ArrayList<ArrayList<ArrayList<Integer>>> adj =
  34. constructAdj(edges, V);
  35. // PriorityQueue to store vertices to be processed
  36. // Each element is a pair: [distance, node]
  37. PriorityQueue<ArrayList<Integer>> pq =
  38. new PriorityQueue<>(Comparator.comparingInt(a -> a.get(0)));
  39. // Create a distance array and initialize all distances as infinite
  40. int[] dist = new int[V];
  41. Arrays.fill(dist, Integer.MAX_VALUE);
  42. // Insert source with distance 0
  43. dist[src] = 0;
  44. ArrayList<Integer> start = new ArrayList<>();
  45. start.add(0);
  46. start.add(src);
  47. pq.offer(start);
  48. // Loop until the priority queue is empty
  49. while (!pq.isEmpty()) {
  50. // Get the node with the minimum distance
  51. ArrayList<Integer> curr = pq.poll();
  52. int d = curr.get(0);
  53. int u = curr.get(1);
  54. // Traverse all adjacent vertices of the current node
  55. for (ArrayList<Integer> neighbor : adj.get(u)) {
  56. int v = neighbor.get(0);
  57. int weight = neighbor.get(1);
  58. // If there is a shorter path to v through u
  59. if (dist[v] > dist[u] + weight) {
  60. // Update distance of v
  61. dist[v] = dist[u] + weight;
  62. // Add updated pair to the queue
  63. ArrayList<Integer> temp = new ArrayList<>();
  64. temp.add(dist[v]);
  65. temp.add(v);
  66. pq.offer(temp);
  67. }
  68. }
  69. }
  70. // Return the shortest distance array
  71. return dist;
  72. }
  73. // Driver program to test methods of graph class
  74. public static void main(String[] args) {
  75. int V = 5;
  76. int src = 0;
  77. // Edge list format: {u, v, weight}
  78. int[][] edges = {
  79. {0, 1, 4}, {0, 2, 8}, {1, 4, 6},
  80. {2, 3, 2}, {3, 4, 10}
  81. };
  82. // Get shortest path distances
  83. int[] result = dijkstra(V, edges, src);
  84. // Print shortest distances in one line
  85. for (int d : result)
  86. System.out.print(d + " ");
  87. }
  88. }