From 8426d22e8105064dac7b462d229ae55ebd3d10ce Mon Sep 17 00:00:00 2001 From: Mayank Kumar Jha Date: Mon, 24 Apr 2017 19:13:31 +0530 Subject: [PATCH 1/4] Add files via upload --- Dijkshtra.java | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Dijkshtra.java diff --git a/Dijkshtra.java b/Dijkshtra.java new file mode 100644 index 000000000..93c58d670 --- /dev/null +++ b/Dijkshtra.java @@ -0,0 +1,38 @@ +public static void main(String[] args) throws IOException { + Reader in=new Reader(); + int t1=in.nextInt(); + for(int gj=0;gjcmp){ + w[x][y]=cmp; w[y][x]=cmp; + } + } + 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 Date: Mon, 24 Apr 2017 19:24:24 +0530 Subject: [PATCH 2/4] Dijkshtra's Algorithm --- Dijkshtra.java | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/Dijkshtra.java b/Dijkshtra.java index 93c58d670..ed05ef343 100644 --- a/Dijkshtra.java +++ b/Dijkshtra.java @@ -1,20 +1,28 @@ public static void main(String[] args) throws IOException { Reader in=new Reader(); int t1=in.nextInt(); - for(int gj=0;gjcmp){ - w[x][y]=cmp; w[y][x]=cmp; + if(w[x][y]>cmp){ //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++){ @@ -30,9 +38,11 @@ public static void main(String[] args) throws IOException { min=(int) w[src][t.elementAt(i)];loc=i;} } p.push(t.elementAt(loc));t.removeElementAt(loc);} + + //Printing shortest path from the given source src for(int i=1;i<=n;i++){ if(i!=src && w[src][i]!=1000000l){System.out.print(w[src][i]+" ");} - else if(i!=src){System.out.print("-1"+" ");} - }System.out.println(); + else if(i!=src){System.out.print("-1"+" ");} //Printing -1 if there is no path b/w given pair of edges } + } From 1857fac133de94ce5003cef025e0efaa16284a7c Mon Sep 17 00:00:00 2001 From: Mayank Kumar Jha Date: Mon, 24 Apr 2017 19:25:18 +0530 Subject: [PATCH 3/4] Dijkshtra.java --- Dijkshtra.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Dijkshtra.java b/Dijkshtra.java index ed05ef343..7b4f19738 100644 --- a/Dijkshtra.java +++ b/Dijkshtra.java @@ -1,7 +1,6 @@ public static void main(String[] args) throws IOException { - Reader in=new Reader(); - int t1=in.nextInt(); - + 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 From a7cef0c4e4ce9e32cf0f6bf6197fde683c5f3452 Mon Sep 17 00:00:00 2001 From: Mayank Kumar Jha Date: Mon, 24 Apr 2017 19:27:05 +0530 Subject: [PATCH 4/4] Dijkshtra.java --- Dijkshtra.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Dijkshtra.java b/Dijkshtra.java index 7b4f19738..377c487ee 100644 --- a/Dijkshtra.java +++ b/Dijkshtra.java @@ -1,3 +1,11 @@ +/* +@author : Mayank K Jha + +*/ + + +public class Solution { + public static void main(String[] args) throws IOException { Scanner in =new Scanner(System.in); @@ -45,3 +53,4 @@ public static void main(String[] args) throws IOException { } } +}