Skip to content

Commit dcce83e

Browse files
authored
commit5-floydwarshall
1 parent b9dca29 commit dcce83e

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

FloydWarshall.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
public class FloydWarshall {
2+
3+
public static void floydWarshall(int[][] graph){
4+
int N = graph.length;
5+
int dist[][] = new int[N][N];
6+
for(int i=0;i<N;i++){
7+
for(int j=0;j<N;j++){
8+
dist[i][j] = graph[i][j];
9+
}
10+
}
11+
12+
for(int k=0;k<N;k++){
13+
for(int i=0;i<N;i++){
14+
for(int j=0;j<N;j++){
15+
if(dist[i][j]!=-999)
16+
dist[i][j] = Math.max(dist[i][k]+dist[k][j],dist[i][j]);
17+
}
18+
}
19+
}
20+
21+
for(int i=0;i<N;i++){
22+
System.out.println();
23+
for(int j=0;j<N;j++){
24+
System.out.print(" "+dist[i][j]+" ");;
25+
}
26+
}
27+
}
28+
public static void main(String[] args) {
29+
// TODO Auto-generated method stub
30+
int graph[][] = {{-999,-999,-999,-999},
31+
{3,4,5,-999},
32+
{40,8,9,10},
33+
{-999,1,2,3}
34+
};
35+
floydWarshall(graph);
36+
}
37+
38+
}

0 commit comments

Comments
 (0)