diff --git a/Dijkstra b/Dijkstra new file mode 100644 index 0000000..a1412fd --- /dev/null +++ b/Dijkstra @@ -0,0 +1,92 @@ +import java.util.*; +public class Dijkstra_Algo { + /* + So what is Dijkstra's Algorithm? + --> When the source and destination is given and we have to find out the shortest path in the directed weighted graph + Dijkstra come into picture. (IMPORTANT - It is not possible in negative weight graphs + + How to implement it ? + We have to use min heap Priority Queue so that shortest distance get saved in the top + + Time Complexity - E log V + Now lets code it + + */ + public static void main(String[] args) { + int V = 5; + int source = 0; + + // Adjacency list representation of the + // connected edges by declaring List class object + // Declaring object of type List + ArrayList > adj + = new ArrayList >(); + + // Initialize list for every node + for (int i = 0; i < V; i++) { + ArrayList item = new ArrayList(); + adj.add(item); + } + + // Inputs for the GFG(dpq) graph + adj.get(0).add(new Node(1, 9)); + adj.get(0).add(new Node(2, 6)); + adj.get(0).add(new Node(3, 5)); + adj.get(0).add(new Node(4, 3)); + + adj.get(2).add(new Node(1, 2)); + adj.get(2).add(new Node(3, 4)); + + ShortestPath ob = new ShortestPath(); + int[] dist = ob.dijkstra(V,adj,source); + for (int i:dist) + { + System.out.print(i+ " "); + } + + + } +} + + +public class ShortestPath { + + public static int[] dijkstra(int V, ArrayList> adj, int S) + { + int dist[] = new int[V]; + Arrays.fill(dist,(int)1e9);//At first fill the array with infinite value(large value) then check shortest path + dist[S] = 0; + + PriorityQueue pq = new PriorityQueue<>((x, y)->x.dist-y.dist); + pq.add(new Node(S,0)); + + while (!pq.isEmpty()) + { + Node n = pq.poll(); + int node = n.node; + int distance = n.dist; + + for(Node it: adj.get(node)) + { + int weight = it.dist; + int adjacent_node = it.node; + if(distance + weight < dist[adjacent_node]) + { + dist[adjacent_node] = distance + weight; + pq.add(new Node(adjacent_node,distance+weight)); + } + } + + } return dist; + } +} + + +class Node{ + int node,dist; + public Node(int node , int dist) + { + this.dist = dist; + this.node = node; + } +}