11package com .thealgorithms .datastructures .graphs ;
22
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+ */
517public class FloydWarshall {
618
719 private int [][] distanceMatrix ;
8- private int numberofvertices ; // number of vertices in the graph
20+ private int numberofvertices ;
921 public static final int INFINITY = 999 ;
1022
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+ */
1129 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 ];
1431 // The matrix is initialized with 0's by default
1532 this .numberofvertices = numberofvertices ;
1633 }
1734
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.
1944 for (int source = 1 ; source <= numberofvertices ; source ++) {
2045 for (int destination = 1 ; destination <= numberofvertices ; destination ++) {
2146 distanceMatrix [source ][destination ] = adjacencyMatrix [source ][destination ];
@@ -24,19 +49,29 @@ public void floydwarshall(int[][] adjacencyMatrix) { // calculates all the dista
2449 for (int intermediate = 1 ; intermediate <= numberofvertices ; intermediate ++) {
2550 for (int source = 1 ; source <= numberofvertices ; source ++) {
2651 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 ]) {
3154 distanceMatrix [source ][destination ] = distanceMatrix [source ][intermediate ] + distanceMatrix [intermediate ][destination ];
3255 }
3356 }
3457 }
3558 }
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
3669 for (int source = 1 ; source <= numberofvertices ; source ++) {
3770 System .out .print ("\t " + source );
3871 }
3972 System .out .println ();
73+
74+ // Print the distance matrix
4075 for (int source = 1 ; source <= numberofvertices ; source ++) {
4176 System .out .print (source + "\t " );
4277 for (int destination = 1 ; destination <= numberofvertices ; destination ++) {
@@ -46,27 +81,7 @@ public void floydwarshall(int[][] adjacencyMatrix) { // calculates all the dista
4681 }
4782 }
4883
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 ;
7186 }
7287}
0 commit comments