|
| 1 | +package dataStructures.graphs; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +import dataStructures.graphs.CustomGraph.Vertex; |
| 6 | + |
| 7 | +public class WeightedGraph { |
| 8 | + private Map<Vertex, List<Edge>> adjVertices; |
| 9 | + |
| 10 | + public static class Edge { |
| 11 | + Vertex destination; |
| 12 | + int weight; |
| 13 | + |
| 14 | + public Edge(Vertex destination, int weight) { |
| 15 | + this.destination = destination; |
| 16 | + this.weight = weight; |
| 17 | + } |
| 18 | + |
| 19 | + @Override |
| 20 | + public String toString() { |
| 21 | + return "Edge{to=" + destination + ", weight=" + weight + '}'; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public Map<Vertex, Integer> dijkstra(Vertex source) { |
| 26 | + Map<Vertex, Integer> distances = new HashMap<>(); |
| 27 | + Map<Vertex, Vertex> previous = new HashMap<>(); |
| 28 | + PriorityQueue<VertexDistance> pq = new PriorityQueue<>(Comparator.comparingInt(vd -> vd.distance)); |
| 29 | + |
| 30 | + for (Vertex vertex : adjVertices.keySet()) { |
| 31 | + distances.put(vertex, Integer.MAX_VALUE); |
| 32 | + } |
| 33 | + distances.put(source, 0); |
| 34 | + pq.add(new VertexDistance(source, 0)); |
| 35 | + |
| 36 | + while (!pq.isEmpty()) { |
| 37 | + VertexDistance current = pq.poll(); |
| 38 | + Vertex u = current.vertex; |
| 39 | + |
| 40 | + for (Edge edge : adjVertices.getOrDefault(u, Collections.emptyList())) { |
| 41 | + Vertex v = edge.destination; |
| 42 | + int newDist = distances.get(u) + edge.weight; |
| 43 | + if (newDist < distances.get(v)) { |
| 44 | + distances.put(v, newDist); |
| 45 | + previous.put(v, u); |
| 46 | + pq.add(new VertexDistance(v, newDist)); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return distances; |
| 52 | + } |
| 53 | + |
| 54 | + private static class VertexDistance { |
| 55 | + Vertex vertex; |
| 56 | + int distance; |
| 57 | + |
| 58 | + public VertexDistance(Vertex vertex, int distance) { |
| 59 | + this.vertex = vertex; |
| 60 | + this.distance = distance; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // demo |
| 65 | + |
| 66 | + public static void main(String[] args) { |
| 67 | + Vertex a = new Vertex("A"); |
| 68 | + Vertex b = new Vertex("B"); |
| 69 | + Vertex c = new Vertex("C"); |
| 70 | + Vertex d = new Vertex("D"); |
| 71 | + |
| 72 | + WeightedGraph graph = new WeightedGraph(); |
| 73 | + graph.adjVertices = new HashMap<>(); |
| 74 | + |
| 75 | + graph.adjVertices.put(a, Arrays.asList(new Edge(b, 1), new Edge(c, 4))); |
| 76 | + graph.adjVertices.put(b, Arrays.asList(new Edge(c, 2), new Edge(d, 5))); |
| 77 | + graph.adjVertices.put(c, Arrays.asList(new Edge(d, 1))); |
| 78 | + graph.adjVertices.put(d, new ArrayList<>()); |
| 79 | + |
| 80 | + Map<Vertex, Integer> distances = graph.dijkstra(a); |
| 81 | + for (Map.Entry<Vertex, Integer> entry : distances.entrySet()) { |
| 82 | + System.out.println("Distance from A to " + entry.getKey().label + " = " + entry.getValue()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments