diff --git a/Dijkshtra.java b/Dijkshtra.java new file mode 100644 index 000000000000..377c487ee35a --- /dev/null +++ b/Dijkshtra.java @@ -0,0 +1,56 @@ +/* +@author : Mayank K Jha + +*/ + + +public class Solution { + +public static void main(String[] args) throws IOException { + Scanner in =new Scanner(System.in); + + int n=in.nextInt(); //n = Number of nodes or vertices + int m=in.nextInt(); //m = Number of Edges + long w[][]=new long [n+1][n+1]; //Adjacency Matrix + + //Initializing Matrix with Certain Maximum Value for path b/w any two vertices + for (long[] row: w) + Arrays.fill(row, 1000000l); + //From above,we Have assumed that,initially path b/w any two Pair of vertices is Infinite such that Infinite = 1000000l + //For simplicity , We can also take path Value = Long.MAX_VALUE , but i have taken Max Value = 1000000l . + + //Taking Input as Edge Location b/w a pair of vertices + for(int i=0;icmp){ //Comparing previous edge value with current value - Cycle Case + w[x][y]=cmp; w[y][x]=cmp; + } + } + + //Implementing Dijkshtra's Algorithm + + Stack t=new Stack(); + int src=in.nextInt(); + for(int i=1;i<=n;i++){ + if(i!=src){t.push(i);}} + Stack p=new Stack(); + p.push(src); + w[src][src]=0; + while(!t.isEmpty()){int min=989997979,loc=-1; + for(int i=0;i