From b04295308f8ba09dee4062c71dbc79b21234a88e Mon Sep 17 00:00:00 2001 From: Roua91 <165356652+Roua91@users.noreply.github.com> Date: Thu, 20 Jun 2024 13:49:26 +0800 Subject: [PATCH] Create Dijkstra.java Dijkstra's algorithm in Java using priority queue --- .../05. Graphs/04. Dijkstra/Dijkstra.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 02. Algorithms/05. Graphs/04. Dijkstra/Dijkstra.java diff --git a/02. Algorithms/05. Graphs/04. Dijkstra/Dijkstra.java b/02. Algorithms/05. Graphs/04. Dijkstra/Dijkstra.java new file mode 100644 index 00000000..30fc024b --- /dev/null +++ b/02. Algorithms/05. Graphs/04. Dijkstra/Dijkstra.java @@ -0,0 +1,81 @@ +//Implementation of dijkstra's algorithm in Java using priority queue + +import java.util.*; + +class Dijkstra { + static class Edge { + int vertex, weight; + Edge(int v, int w) { + vertex = v; + weight = w; + } + } + + static class Graph { + int V; + LinkedList[] adj; + + Graph(int V) { + this.V = V; + adj = new LinkedList[V]; + for (int i = 0; i < V; i++) { + adj[i] = new LinkedList<>(); + } + } + + void addEdge(int u, int v, int weight) { + adj[u].add(new Edge(v, weight)); + adj[v].add(new Edge(u, weight)); + } + + void shortestPath(int src) { + PriorityQueue pq = new PriorityQueue<>(V, Comparator.comparingInt(edge -> edge.weight)); + int[] dist = new int[V]; + Arrays.fill(dist, Integer.MAX_VALUE); + pq.add(new Edge(src, 0)); + dist[src] = 0; + + while (!pq.isEmpty()) { + Edge edge = pq.poll(); + int u = edge.vertex; + + for (Edge e : adj[u]) { + int v = e.vertex; + int weight = e.weight; + + if (dist[v] > dist[u] + weight) { + dist[v] = dist[u] + weight; + pq.add(new Edge(v, dist[v])); + } + } + } + + System.out.println("Vertex Distance from Source"); + for (int i = 0; i < V; i++) { + System.out.println(i + " \t\t " + dist[i]); + } + } + } + + public static void main(String[] args) { + int V = 9; + Graph g = new Graph(V); + + g.addEdge(0, 1, 4); + g.addEdge(0, 7, 8); + g.addEdge(1, 2, 8); + g.addEdge(1, 7, 11); + g.addEdge(2, 3, 7); + g.addEdge(2, 8, 2); + g.addEdge(2, 5, 4); + g.addEdge(3, 4, 9); + g.addEdge(3, 5, 14); + g.addEdge(4, 5, 10); + g.addEdge(5, 6, 2); + g.addEdge(6, 7, 1); + g.addEdge(6, 8, 6); + g.addEdge(7, 8, 7); + + g.shortestPath(0); + } +}