|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.PriorityQueue; |
| 3 | + |
| 4 | +public class GraphsP3_Dijkstra { |
| 5 | + // Create Weighted Edge --- |
| 6 | + static class Edge { |
| 7 | + int src; |
| 8 | + int dest; |
| 9 | + int wt; |
| 10 | + |
| 11 | + public Edge(int src, int dest, int wt) { |
| 12 | + this.src = src; |
| 13 | + this.dest = dest; |
| 14 | + this.wt = wt; |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + // Create Graph --- |
| 19 | + public static void createGraph(ArrayList<Edge>[] graph) { |
| 20 | + for (int i = 0; i < graph.length; i++) { |
| 21 | + graph[i] = new ArrayList<>(); |
| 22 | + } |
| 23 | + /* |
| 24 | + * (7) |
| 25 | + * 1 ——————————→ 3 |
| 26 | + * ↗ | ↑ \ |
| 27 | + * (2)/ | | \(1) |
| 28 | + * / | | ↘ |
| 29 | + * 0 |(1) (2)| 5 |
| 30 | + * \ | | ↗ |
| 31 | + * (4)\ | | /(5) |
| 32 | + * ↘ ↓ | / |
| 33 | + * 2 ——————————→ 4 |
| 34 | + * (3) |
| 35 | + */ |
| 36 | + graph[0].add(new Edge(0, 1, 2)); |
| 37 | + graph[0].add(new Edge(0, 2, 4)); |
| 38 | + |
| 39 | + graph[1].add(new Edge(1, 2, 1)); |
| 40 | + graph[1].add(new Edge(1, 3, 7)); |
| 41 | + |
| 42 | + graph[2].add(new Edge(2, 4, 3)); |
| 43 | + |
| 44 | + graph[3].add(new Edge(3, 5, 1)); |
| 45 | + |
| 46 | + graph[4].add(new Edge(4, 3, 2)); |
| 47 | + graph[4].add(new Edge(4, 5, 5)); |
| 48 | + } |
| 49 | + |
| 50 | + // Create Pair class to store node & distance in sorted order |
| 51 | + static class Pair implements Comparable<Pair> { |
| 52 | + int n; // node |
| 53 | + int path; // path distance |
| 54 | + |
| 55 | + public Pair(int n, int path) { |
| 56 | + this.n = n; |
| 57 | + this.path = path; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + // deside Which bases to compare |
| 62 | + public int compareTo(Pair p2) { // Compare to p1 - p2 (Sorted order) |
| 63 | + return this.path - p2.path; // path based sorting for my Pair |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Dijkstra Algorithm (Shortest Path Src to All vertices) -------------------------------- |
| 68 | + public static void dijkstra(ArrayList<Edge>[] graph, int src) { |
| 69 | + // Create distances Array & initialize |
| 70 | + int[] dist = new int[graph.length]; // dist[i] = src to i |
| 71 | + for (int i = 0; i < dist.length; i++) { |
| 72 | + if (i != src) { |
| 73 | + dist[i] = Integer.MAX_VALUE; // +infinity |
| 74 | + } |
| 75 | + } |
| 76 | + // Visited Array |
| 77 | + boolean[] visited = new boolean[graph.length]; |
| 78 | + // Create PriorityQueue |
| 79 | + PriorityQueue<Pair> pq = new PriorityQueue<>(); |
| 80 | + pq.add(new Pair(src, 0)); // add src to src |
| 81 | + // BFS loop |
| 82 | + while (!pq.isEmpty()) { |
| 83 | + Pair curr = pq.remove(); |
| 84 | + if (!visited[curr.n]) { |
| 85 | + visited[curr.n] = true; |
| 86 | + // neighbors |
| 87 | + for (int i = 0; i < graph[curr.n].size(); i++) { |
| 88 | + Edge e = graph[curr.n].get(i); |
| 89 | + int u = e.src; // current = e.src ya curr.n |
| 90 | + int v = e.dest; // neighbor |
| 91 | + int wt = e.wt; // weight |
| 92 | + if (dist[u] + wt < dist[v]) { // Update distance of source(u) to vertex(v) |
| 93 | + dist[v] = dist[u] + wt; |
| 94 | + pq.add(new Pair(v, dist[v])); // Add new pair in Priority Queue |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + // Print all source to all vertices Path |
| 100 | + for (int i = 0; i < dist.length; i++) { |
| 101 | + System.out.println("Source " + src + " to distance " + i + " Sorted path : " + dist[i]); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public static void main(String[] args) { |
| 106 | + // Dijkstra Algorithms -------------------------------- |
| 107 | + // Shortest paths from the source to all vertices (Weighted Graph) |
| 108 | + int V = 6; |
| 109 | + @SuppressWarnings("unchecked") |
| 110 | + ArrayList<Edge>[] graph = new ArrayList[V]; |
| 111 | + createGraph(graph); // call for create graph |
| 112 | + int src = 0; |
| 113 | + dijkstra(graph, src); |
| 114 | + |
| 115 | + } |
| 116 | +} |
0 commit comments