-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplementing-floyd-warshal.java
39 lines (36 loc) · 1.05 KB
/
implementing-floyd-warshal.java
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
// Problem link - https://www.geeksforgeeks.org/problems/implementing-floyd-warshall2042/1
class Solution
{
public void shortest_distance(int[][] mat)
{
int n = mat.length;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(mat[i][j] == -1){
mat[i][j] = Integer.MAX_VALUE;
}
if(i == j){
mat[i][j] = 0;
}
}
}
for(int v = 0; v < n; v++){
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++){
if(mat[i][v] != Integer.MAX_VALUE && mat[v][j] != Integer.MAX_VALUE){
mat[i][j] = Math.min(mat[i][j], (mat[i][v] + mat[v][j]));
}
}
}
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(mat[i][j] == Integer.MAX_VALUE)
mat[i][j] = -1;
}
}
}
}