Skip to content

Commit d437d58

Browse files
authored
Remove print & main methods (#5584)
1 parent ecd75c0 commit d437d58

File tree

3 files changed

+81
-32
lines changed

3 files changed

+81
-32
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@
709709
* [BoruvkaAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithmTest.java)
710710
* [DijkstraAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/DijkstraAlgorithmTest.java)
711711
* [EdmondsBlossomAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/EdmondsBlossomAlgorithmTest.java)
712+
* [FloydWarshallTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/FloydWarshallTest.java)
712713
* [FordFulkersonTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/FordFulkersonTest.java)
713714
* [HamiltonianCycleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/HamiltonianCycleTest.java)
714715
* [KosarajuTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java)
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,46 @@
11
package 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+
*/
517
public 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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class FloydWarshallTest {
8+
9+
@Test
10+
void testSmallGraph() {
11+
int[][] adjacencyMatrix = {{0, 0, 0, 0}, // Ignored row (0 index)
12+
{0, 0, 3, FloydWarshall.INFINITY}, {0, FloydWarshall.INFINITY, 0, 1}, {0, FloydWarshall.INFINITY, FloydWarshall.INFINITY, 0}};
13+
14+
FloydWarshall fw = new FloydWarshall(3);
15+
fw.floydwarshall(adjacencyMatrix);
16+
17+
int[][] expectedDistanceMatrix = {{0, 0, 0, 0}, {0, 0, 3, 4}, {0, FloydWarshall.INFINITY, 0, 1}, {0, FloydWarshall.INFINITY, FloydWarshall.INFINITY, 0}};
18+
19+
assertArrayEquals(expectedDistanceMatrix, fw.getDistanceMatrix());
20+
}
21+
22+
@Test
23+
void testLargerGraph() {
24+
int[][] adjacencyMatrix = {{0, 0, 0, 0, 0}, {0, 0, 1, FloydWarshall.INFINITY, 2}, {0, FloydWarshall.INFINITY, 0, 4, FloydWarshall.INFINITY}, {0, FloydWarshall.INFINITY, FloydWarshall.INFINITY, 0, 3}, {0, FloydWarshall.INFINITY, FloydWarshall.INFINITY, FloydWarshall.INFINITY, 0}};
25+
26+
FloydWarshall fw = new FloydWarshall(4);
27+
fw.floydwarshall(adjacencyMatrix);
28+
29+
int[][] expectedDistanceMatrix = {{0, 0, 0, 0, 0}, {0, 0, 1, 5, 2}, {0, FloydWarshall.INFINITY, 0, 4, 7}, {0, FloydWarshall.INFINITY, FloydWarshall.INFINITY, 0, 3}, {0, FloydWarshall.INFINITY, FloydWarshall.INFINITY, FloydWarshall.INFINITY, 0}};
30+
31+
assertArrayEquals(expectedDistanceMatrix, fw.getDistanceMatrix());
32+
}
33+
}

0 commit comments

Comments
 (0)