-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloydWarshall.java
More file actions
38 lines (34 loc) · 844 Bytes
/
FloydWarshall.java
File metadata and controls
38 lines (34 loc) · 844 Bytes
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
public class FloydWarshall {
public static void floydWarshall(int[][] graph){
int N = graph.length;
int dist[][] = new int[N][N];
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
dist[i][j] = graph[i][j];
}
}
for(int k=0;k<N;k++){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(dist[i][j]!=-999)
dist[i][j] = Math.max(dist[i][k]+dist[k][j],dist[i][j]);
}
}
}
for(int i=0;i<N;i++){
System.out.println();
for(int j=0;j<N;j++){
System.out.print(" "+dist[i][j]+" ");;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int graph[][] = {{-999,-999,-999,-999},
{3,4,5,-999},
{40,8,9,10},
{-999,1,2,3}
};
floydWarshall(graph);
}
}