-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphs-2:Dijkstra's Algorithm
53 lines (50 loc) · 1.69 KB
/
Graphs-2:Dijkstra's Algorithm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int V = s.nextInt();
int E = s.nextInt();
int[][] matrix=new int[V][V];
for(int i=0;i<E;i++){
int sv=s.nextInt();
int ev=s.nextInt();
int weight=s.nextInt();
matrix[sv][ev]=weight;
matrix[ev][sv]=weight;
}
dijkstra(matrix);
}
public static void dijkstra(int[][] matrix)
{
boolean visited[]=new boolean[matrix.length];
int distance[]=new int[matrix.length];
distance[0]=0;
for(int i=1;i<distance.length;i++){
distance[i]=Integer.MAX_VALUE;
}
for(int i=0;i<matrix.length-1;i++)
{
int mindistancevertex=findmindistancevertex(distance,visited);
visited[mindistancevertex]=true;
for(int j=0;j<matrix.length;j++){
if(matrix[mindistancevertex][j]!=0 && !visited[j] ){
if(distance[j]>distance[mindistancevertex]+matrix[mindistancevertex][j])
{
distance[j]=distance[mindistancevertex]+matrix[mindistancevertex][j];
}
}
}
}
for(int i=0;i<matrix.length;i++){
System.out.println(i+" "+distance[i]);
}
}
public static int findmindistancevertex(int[] distance,boolean[] visited){
int minvertex=-1;
for(int i=0;i<distance.length;i++){
if(!visited[i] && (minvertex==-1 || distance[i]<distance[minvertex]))
minvertex=i;
}
return minvertex;
}
}