1
1
package com .thealgorithms .datastructures .graphs ;
2
2
3
- import java .util .Scanner ;
4
-
3
+ /**
4
+ * The {@code FloydWarshall} class provides an implementation of the Floyd-Warshall algorithm
5
+ * to compute the shortest paths between all pairs of vertices in a weighted graph.
6
+ * It handles both positive and negative edge weights but does not support negative cycles.
7
+ * The algorithm is based on dynamic programming and runs in O(V^3) time complexity,
8
+ * where V is the number of vertices in the graph.
9
+ *
10
+ * <p>
11
+ * The distance matrix is updated iteratively to find the shortest distance between any two vertices
12
+ * by considering each vertex as an intermediate step.
13
+ * </p>
14
+ *
15
+ * Reference: <a href="https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm">Floyd-Warshall Algorithm</a>
16
+ */
5
17
public class FloydWarshall {
6
18
7
19
private int [][] distanceMatrix ;
8
- private int numberofvertices ; // number of vertices in the graph
20
+ private int numberofvertices ;
9
21
public static final int INFINITY = 999 ;
10
22
23
+ /**
24
+ * Constructs a Floyd-Warshall instance for a graph with the given number of vertices.
25
+ * Initializes the distance matrix for the graph.
26
+ *
27
+ * @param numberofvertices The number of vertices in the graph.
28
+ */
11
29
public FloydWarshall (int numberofvertices ) {
12
- distanceMatrix = new int [numberofvertices + 1 ][numberofvertices + 1 ]; // stores the value of distance from all the possible path form the source
13
- // vertex to destination vertex
30
+ distanceMatrix = new int [numberofvertices + 1 ][numberofvertices + 1 ];
14
31
// The matrix is initialized with 0's by default
15
32
this .numberofvertices = numberofvertices ;
16
33
}
17
34
18
- public void floydwarshall (int [][] adjacencyMatrix ) { // calculates all the distances from source to destination vertex
35
+ /**
36
+ * Executes the Floyd-Warshall algorithm to compute the shortest path between all pairs of vertices.
37
+ * It uses an adjacency matrix to calculate the distance matrix by considering each vertex as an intermediate point.
38
+ *
39
+ * @param adjacencyMatrix The weighted adjacency matrix representing the graph.
40
+ * A value of 0 means no direct edge between the vertices, except for diagonal elements which are 0 (distance to self).
41
+ */
42
+ public void floydwarshall (int [][] adjacencyMatrix ) {
43
+ // Initialize the distance matrix with the adjacency matrix.
19
44
for (int source = 1 ; source <= numberofvertices ; source ++) {
20
45
for (int destination = 1 ; destination <= numberofvertices ; destination ++) {
21
46
distanceMatrix [source ][destination ] = adjacencyMatrix [source ][destination ];
@@ -24,19 +49,29 @@ public void floydwarshall(int[][] adjacencyMatrix) { // calculates all the dista
24
49
for (int intermediate = 1 ; intermediate <= numberofvertices ; intermediate ++) {
25
50
for (int source = 1 ; source <= numberofvertices ; source ++) {
26
51
for (int destination = 1 ; destination <= numberofvertices ; destination ++) {
27
- if (distanceMatrix [source ][intermediate ] + distanceMatrix [intermediate ][destination ] < distanceMatrix [source ][destination ]) { // calculated distance it get replaced as
28
- // new shortest distance // if the new
29
- // distance calculated is less then the
30
- // earlier shortest
52
+ // Update distance if a shorter path through the intermediate vertex exists.
53
+ if (distanceMatrix [source ][intermediate ] + distanceMatrix [intermediate ][destination ] < distanceMatrix [source ][destination ]) {
31
54
distanceMatrix [source ][destination ] = distanceMatrix [source ][intermediate ] + distanceMatrix [intermediate ][destination ];
32
55
}
33
56
}
34
57
}
35
58
}
59
+
60
+ printDistanceMatrix ();
61
+ }
62
+
63
+ /**
64
+ * Prints the distance matrix representing the shortest paths between all pairs of vertices.
65
+ * The rows and columns correspond to the source and destination vertices.
66
+ */
67
+ private void printDistanceMatrix () {
68
+ // Print header for vertices
36
69
for (int source = 1 ; source <= numberofvertices ; source ++) {
37
70
System .out .print ("\t " + source );
38
71
}
39
72
System .out .println ();
73
+
74
+ // Print the distance matrix
40
75
for (int source = 1 ; source <= numberofvertices ; source ++) {
41
76
System .out .print (source + "\t " );
42
77
for (int destination = 1 ; destination <= numberofvertices ; destination ++) {
@@ -46,27 +81,7 @@ public void floydwarshall(int[][] adjacencyMatrix) { // calculates all the dista
46
81
}
47
82
}
48
83
49
- public static void main (String ... arg ) {
50
- Scanner scan = new Scanner (System .in );
51
- System .out .println ("Enter the number of vertices" );
52
- int numberOfVertices = scan .nextInt ();
53
- int [][] adjacencyMatrix = new int [numberOfVertices + 1 ][numberOfVertices + 1 ];
54
- System .out .println ("Enter the Weighted Matrix for the graph" );
55
- for (int source = 1 ; source <= numberOfVertices ; source ++) {
56
- for (int destination = 1 ; destination <= numberOfVertices ; destination ++) {
57
- adjacencyMatrix [source ][destination ] = scan .nextInt ();
58
- if (source == destination ) {
59
- adjacencyMatrix [source ][destination ] = 0 ;
60
- continue ;
61
- }
62
- if (adjacencyMatrix [source ][destination ] == 0 ) {
63
- adjacencyMatrix [source ][destination ] = INFINITY ;
64
- }
65
- }
66
- }
67
- System .out .println ("The Transitive Closure of the Graph" );
68
- FloydWarshall floydwarshall = new FloydWarshall (numberOfVertices );
69
- floydwarshall .floydwarshall (adjacencyMatrix );
70
- scan .close ();
84
+ public Object [] getDistanceMatrix () {
85
+ return distanceMatrix ;
71
86
}
72
87
}
0 commit comments