|
| 1 | +package com.packt.datastructuresandalg.lesson6.dijkstra; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.PriorityQueue; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +public class DijkstraWithPQ { |
| 9 | + private class Edge { |
| 10 | + int v; |
| 11 | + int weight; |
| 12 | + |
| 13 | + Edge(int v, int weight) { |
| 14 | + this.v = v; |
| 15 | + this.weight = weight; |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + private class Vertex { |
| 20 | + int u; |
| 21 | + int distance; |
| 22 | + boolean visited; |
| 23 | + |
| 24 | + Vertex(int u, int distance) { |
| 25 | + this.distance = distance; |
| 26 | + this.u = u; |
| 27 | + this.visited = false; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public class Path { |
| 32 | + int from; |
| 33 | + int to; |
| 34 | + int distance; |
| 35 | + List<Integer> path; |
| 36 | + |
| 37 | + Path(int from, int to, int distance, List<Integer> path) { |
| 38 | + this.from = from; |
| 39 | + this.to = to; |
| 40 | + this.distance = distance; |
| 41 | + this.path = path; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public class Node implements Comparable<Node> { |
| 46 | + int u; |
| 47 | + int distance; |
| 48 | + |
| 49 | + Node(int u, int distance) { |
| 50 | + this.distance = distance; |
| 51 | + this.u = u; |
| 52 | + } |
| 53 | + |
| 54 | + public int compareTo(Node o) { |
| 55 | + return (int) Math.signum(distance - o.distance); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + List<Edge> adj[]; |
| 60 | + |
| 61 | + public DijkstraWithPQ(int nodes) { |
| 62 | + this.adj = new ArrayList[nodes]; |
| 63 | + for (int i = 0; i < nodes; i++) |
| 64 | + this.adj[i] = new ArrayList<>(); |
| 65 | + } |
| 66 | + |
| 67 | + public void addEdge(int u, int v, int weight) { |
| 68 | + adj[u].add(new Edge(v, weight)); |
| 69 | + } |
| 70 | + |
| 71 | + public Path[] dijkstra(int source) { |
| 72 | + Vertex vertices[] = new Vertex[adj.length]; |
| 73 | + int parent[] = new int[adj.length]; |
| 74 | + for (int i = 0; i < adj.length; i++) { |
| 75 | + Vertex v = new Vertex(i, Integer.MAX_VALUE); |
| 76 | + vertices[i] = v; |
| 77 | + parent[i] = -1; |
| 78 | + } |
| 79 | + |
| 80 | + vertices[source].distance = 0; |
| 81 | + PriorityQueue<Node> pq = new PriorityQueue<>(); |
| 82 | + pq.add(new Node(source, 0)); |
| 83 | + while (!pq.isEmpty()) { |
| 84 | + Node v = pq.remove(); |
| 85 | + if (!vertices[v.u].visited) { |
| 86 | + vertices[v.u].visited = true; |
| 87 | + for (Edge e : adj[v.u]) { |
| 88 | + Vertex next = vertices[e.v]; |
| 89 | + if (v.distance + e.weight < next.distance) { |
| 90 | + next.distance = v.distance + e.weight; |
| 91 | + parent[next.u] = v.u; |
| 92 | + pq.add(new Node(next.u, next.distance)); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + Path[] paths = new Path[adj.length]; |
| 99 | + |
| 100 | + for (int i = 0; i < adj.length; i++) { |
| 101 | + List<Integer> path = new ArrayList<>(); |
| 102 | + Path p = new Path(source, i, vertices[i].distance, path); |
| 103 | + int j = i; |
| 104 | + while (parent[j] != -1) { |
| 105 | + path.add(0, j); |
| 106 | + j = parent[j]; |
| 107 | + } |
| 108 | + paths[i] = p; |
| 109 | + } |
| 110 | + |
| 111 | + return paths; |
| 112 | + } |
| 113 | + |
| 114 | + public static void main(String [] args) { |
| 115 | + DijkstraWithPQ d = new DijkstraWithPQ(5); |
| 116 | + d.addEdge(0, 1, 10); |
| 117 | + d.addEdge(0, 3, 5); |
| 118 | + d.addEdge(1, 3, 2); |
| 119 | + d.addEdge(1, 2, 1); |
| 120 | + d.addEdge(2, 4, 4); |
| 121 | + d.addEdge(3, 1, 3); |
| 122 | + d.addEdge(3, 2, 9); |
| 123 | + d.addEdge(3, 4, 2); |
| 124 | + d.addEdge(4, 2, 6); |
| 125 | + |
| 126 | + Path[] paths = d.dijkstra(0); |
| 127 | + for (int i = 0; i < paths.length; i++) { |
| 128 | + Path p = paths[i]; |
| 129 | + System.out.print("Path from " + p.from + " to " + p.to + " (" + p.distance + ")"); |
| 130 | + for (int j = 0; j < p.path.size(); j++) { |
| 131 | + System.out.print(" " + p.path.get(j)); |
| 132 | + } |
| 133 | + System.out.println(); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments