|
| 1 | +// Java implementation of Dijkstra's Algorithm |
| 2 | +// using Priority Queue |
| 3 | +import java.util.*; |
| 4 | +public class DPQ { |
| 5 | + private int dist[]; |
| 6 | + private Set<Integer> settled; |
| 7 | + private PriorityQueue<Node> pq; |
| 8 | + private int V; // Number of vertices |
| 9 | + List<List<Node> > adj; |
| 10 | + |
| 11 | + public DPQ(int V) |
| 12 | + { |
| 13 | + this.V = V; |
| 14 | + dist = new int[V]; |
| 15 | + settled = new HashSet<Integer>(); |
| 16 | + pq = new PriorityQueue<Node>(V, new Node()); |
| 17 | + } |
| 18 | + |
| 19 | + // Function for Dijkstra's Algorithm |
| 20 | + public void dijkstra(List<List<Node> > adj, int src) |
| 21 | + { |
| 22 | + this.adj = adj; |
| 23 | + |
| 24 | + for (int i = 0; i < V; i++) |
| 25 | + dist[i] = Integer.MAX_VALUE; |
| 26 | + |
| 27 | + // Add source node to the priority queue |
| 28 | + pq.add(new Node(src, 0)); |
| 29 | + |
| 30 | + // Distance to the source is 0 |
| 31 | + dist[src] = 0; |
| 32 | + while (settled.size() != V) { |
| 33 | + |
| 34 | + // remove the minimum distance node |
| 35 | + // from the priority queue |
| 36 | + int u = pq.remove().node; |
| 37 | + |
| 38 | + // adding the node whose distance is |
| 39 | + // finalized |
| 40 | + settled.add(u); |
| 41 | + |
| 42 | + e_Neighbours(u); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Function to process all the neighbours |
| 47 | + // of the passed node |
| 48 | + private void e_Neighbours(int u) |
| 49 | + { |
| 50 | + int edgeDistance = -1; |
| 51 | + int newDistance = -1; |
| 52 | + |
| 53 | + // All the neighbors of v |
| 54 | + for (int i = 0; i < adj.get(u).size(); i++) { |
| 55 | + Node v = adj.get(u).get(i); |
| 56 | + |
| 57 | + // If current node hasn't already been processed |
| 58 | + if (!settled.contains(v.node)) { |
| 59 | + edgeDistance = v.cost; |
| 60 | + newDistance = dist[u] + edgeDistance; |
| 61 | + |
| 62 | + // If new distance is cheaper in cost |
| 63 | + if (newDistance < dist[v.node]) |
| 64 | + dist[v.node] = newDistance; |
| 65 | + |
| 66 | + // Add the current node to the queue |
| 67 | + pq.add(new Node(v.node, dist[v.node])); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Driver code |
| 73 | + public static void main(String arg[]) |
| 74 | + { |
| 75 | + int V = 5; |
| 76 | + int source = 0; |
| 77 | + |
| 78 | + // Adjacency list representation of the |
| 79 | + // connected edges |
| 80 | + List<List<Node> > adj = new ArrayList<List<Node> >(); |
| 81 | + |
| 82 | + // Initialize list for every node |
| 83 | + for (int i = 0; i < V; i++) { |
| 84 | + List<Node> item = new ArrayList<Node>(); |
| 85 | + adj.add(item); |
| 86 | + } |
| 87 | + |
| 88 | + // Inputs for the DPQ graph |
| 89 | + adj.get(0).add(new Node(1, 9)); |
| 90 | + adj.get(0).add(new Node(2, 6)); |
| 91 | + adj.get(0).add(new Node(3, 5)); |
| 92 | + adj.get(0).add(new Node(4, 3)); |
| 93 | + |
| 94 | + adj.get(2).add(new Node(1, 2)); |
| 95 | + adj.get(2).add(new Node(3, 4)); |
| 96 | + |
| 97 | + // Calculate the single source shortest path |
| 98 | + DPQ dpq = new DPQ(V); |
| 99 | + dpq.dijkstra(adj, source); |
| 100 | + |
| 101 | + // Print the shortest path to all the nodes |
| 102 | + // from the source node |
| 103 | + System.out.println("The shorted path from node :"); |
| 104 | + for (int i = 0; i < dpq.dist.length; i++) |
| 105 | + System.out.println(source + " to " + i + " is " |
| 106 | + + dpq.dist[i]); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// Class to represent a node in the graph |
| 111 | +class Node implements Comparator<Node> { |
| 112 | + public int node; |
| 113 | + public int cost; |
| 114 | + |
| 115 | + public Node() |
| 116 | + { |
| 117 | + } |
| 118 | + |
| 119 | + public Node(int node, int cost) |
| 120 | + { |
| 121 | + this.node = node; |
| 122 | + this.cost = cost; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public int compare(Node node1, Node node2) |
| 127 | + { |
| 128 | + if (node1.cost < node2.cost) |
| 129 | + return -1; |
| 130 | + if (node1.cost > node2.cost) |
| 131 | + return 1; |
| 132 | + return 0; |
| 133 | + } |
| 134 | +} |
0 commit comments