1
1
/*
2
- * Floyd-Warshall algorithm in Java
3
- * All pairs shortest path algorithm
4
- * Time Complexity: O(n³)
5
- * Space Complexity: O(n²)
2
+ * Floyd-Warshall algorithm in Java
3
+ * All pairs shortest path algorithm
4
+ * Time Complexity: O(n³)
5
+ * Space Complexity: O(n²)
6
6
*/
7
7
8
8
public class FloydWarshall {
9
9
10
- public static void showMatrix (long [][] matriz , int nroVertices ) {
11
- for (int i = 0 ; i < nroVertices ; i ++) {
12
- for (int j = 0 ; j < nroVertices ; j ++) {
13
- if (matriz [i ][j ] < 10 ) {
14
- System .out .print (" " + matriz [i ][j ] + " " );
15
- } else {
16
- System .out .print (matriz [i ][j ] + " " );
17
- }
18
- }
19
- System .out .println ();
10
+ public static void showMatrix (long [][] matriz , int nroVertices ) {
11
+ for (int i = 0 ; i < nroVertices ; i ++) {
12
+ for (int j = 0 ; j < nroVertices ; j ++) {
13
+ if (matriz [i ][j ] < 10 ) {
14
+ System .out .print (" " + matriz [i ][j ] + " " );
15
+ } else {
16
+ System .out .print (matriz [i ][j ] + " " );
20
17
}
21
- System .out .println ();
18
+ }
19
+ System .out .println ();
22
20
}
21
+ System .out .println ();
22
+ }
23
23
24
- public static void main (String [] args ) {
25
- int nroVertices = 5 ;
26
- long [][] matriz = {
27
- { 0 , 2 , 10 , 5 , 7 },
28
- { 2 , 0 , 3 , 3 , 1 },
29
- { 10 , 3 , 0 , 1 , 2 },
30
- { 5 , 3 , 1 , 0 , Long .MAX_VALUE },
31
- { 7 , 1 , 2 , 2 , 0 }
32
- };
24
+ public static void main (String [] args ) {
25
+ int nroVertices = 5 ;
26
+ long [][] matriz = {
27
+ { 0 , 2 , 10 , 5 , 7 },
28
+ { 2 , 0 , 3 , 3 , 1 },
29
+ { 10 , 3 , 0 , 1 , 2 },
30
+ { 5 , 3 , 1 , 0 , Long .MAX_VALUE },
31
+ { 7 , 1 , 2 , 2 , 0 }
32
+ };
33
33
34
- // Display the original matrix
35
- System .out .println ("Original matrix:" );
36
- showMatrix (matriz , nroVertices );
34
+ // Display the original matrix
35
+ System .out .println ("Original matrix:" );
36
+ showMatrix (matriz , nroVertices );
37
37
38
- floydWarshall (matriz , nroVertices );
38
+ floydWarshall (matriz , nroVertices );
39
39
40
- // Display the updated matrix
41
- System .out .println ("Updated matrix:" );
42
- showMatrix (matriz , nroVertices );
40
+ // Display the updated matrix
41
+ System .out .println ("Updated matrix:" );
42
+ showMatrix (matriz , nroVertices );
43
43
44
- // Show all shortest paths
45
- System .out .println ();
46
- for (int i = 0 ; i < nroVertices ; i ++) {
47
- for (int x = 0 ; x < nroVertices ; x ++) {
48
- System .out .println ("Shortest distance from " + i + " to " + x + " = " + matriz [x ][i ] + "." );
49
- }
50
- }
51
- System .out .println ();
44
+ // Show all shortest paths
45
+ System .out .println ();
46
+ for (int i = 0 ; i < nroVertices ; i ++) {
47
+ for (int x = 0 ; x < nroVertices ; x ++) {
48
+ System .out .println ("Shortest distance from " + i + " to " + x + " = " + matriz [x ][i ] + "." );
49
+ }
52
50
}
51
+ System .out .println ();
52
+ }
53
53
54
- public static void floydWarshall (long [][] matriz , int n ) {
55
- for (int x = 0 ; x < n ; x ++) { // Intermediary vertex
56
- for (int y = 0 ; y < n ; y ++) { // Origin vertex
57
- for (int z = 0 ; z < n ; z ++) { // Destination vertex
58
- if (matriz [y ][z ] > (matriz [y ][x ] + matriz [x ][z ])) {
59
- matriz [y ][z ] = matriz [y ][x ] + matriz [x ][z ];
60
- }
61
- }
62
- }
54
+ public static void floydWarshall (long [][] matriz , int n ) {
55
+ for (int x = 0 ; x < n ; x ++) { // Intermediary vertex
56
+ for (int y = 0 ; y < n ; y ++) { // Origin vertex
57
+ for (int z = 0 ; z < n ; z ++) { // Destination vertex
58
+ if (matriz [y ][z ] > (matriz [y ][x ] + matriz [x ][z ])) {
59
+ matriz [y ][z ] = matriz [y ][x ] + matriz [x ][z ];
60
+ }
63
61
}
62
+ }
64
63
}
65
- }
64
+ }
65
+ }
0 commit comments