|
| 1 | + import java.util.Scanner; |
| 2 | + public class FloydWarshall |
| 3 | + { |
| 4 | + private int DistanceMatrix[][]; |
| 5 | + private int numberofvertices;//number of vertices in the graph |
| 6 | + public static final int INFINITY = 999; |
| 7 | + public FloydWarshall(int numberofvertices) |
| 8 | + { |
| 9 | + DistanceMatrix = new int[numberofvertices + 1][numberofvertices + 1];//stores the value of distance from all the possible path form the source vertex to destination vertex |
| 10 | + Arrays.fill(DistanceMatrix, 0); |
| 11 | + this.numberofvertices = numberofvertices; |
| 12 | + } |
| 13 | + public void floydwarshall(int AdjacencyMatrix[][])//calculates all the distances from source to destination vertex |
| 14 | + { |
| 15 | + for (int source = 1; source <= numberofvertices; source++) |
| 16 | + { |
| 17 | + for (int destination = 1; destination <= numberofvertices; destination++) |
| 18 | + { |
| 19 | + DistanceMatrix[source][destination] = AdjacencyMatrix[source][destination]; |
| 20 | + } |
| 21 | + } |
| 22 | + for (int intermediate = 1; intermediate <= numberofvertices; intermediate++) |
| 23 | + { |
| 24 | + for (int source = 1; source <= numberofvertices; source++) |
| 25 | + { |
| 26 | + for (int destination = 1; destination <= numberofvertices; destination++) |
| 27 | + { |
| 28 | + if (DistanceMatrix[source][intermediate] + DistanceMatrix[intermediate][destination] |
| 29 | + < DistanceMatrix[source][destination])//if the new distance calculated is less then the earlier shortest calculated distance it get replaced as new shortest distance |
| 30 | + DistanceMatrix[source][destination] = DistanceMatrix[source][intermediate] |
| 31 | + + DistanceMatrix[intermediate][destination]; |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + for (int source = 1; source <= numberofvertices; source++) |
| 36 | + System.out.print("\t" + source); |
| 37 | + System.out.println(); |
| 38 | + for (int source = 1; source <= numberofvertices; source++) |
| 39 | + { |
| 40 | + System.out.print(source + "\t"); |
| 41 | + for (int destination = 1; destination <= numberofvertices; destination++) |
| 42 | + { |
| 43 | + System.out.print(DistanceMatrix[source][destination] + "\t"); |
| 44 | + } |
| 45 | + System.out.println(); |
| 46 | + } |
| 47 | + } |
| 48 | + public static void main(String... arg) |
| 49 | + { |
| 50 | + int Adjacency_Matrix[][]; |
| 51 | + int numberofvertices; |
| 52 | + Scanner scan = new Scanner(System.in); |
| 53 | + System.out.println("Enter the number of vertices"); |
| 54 | + numberofvertices = scan.nextInt(); |
| 55 | + Adjacency_Matrix = new int[numberofvertices + 1][numberofvertices + 1]; |
| 56 | + System.out.println("Enter the Weighted Matrix for the graph"); |
| 57 | + for (int source = 1; source <= numberofvertices; source++) |
| 58 | + { |
| 59 | + for (int destination = 1; destination <= numberofvertices; destination++) |
| 60 | + { |
| 61 | + Adjacency_Matrix[source][destination] = scan.nextInt(); |
| 62 | + if (source == destination) |
| 63 | + { |
| 64 | + Adjacency_Matrix[source][destination] = 0; |
| 65 | + continue; |
| 66 | + } |
| 67 | + if (Adjacency_Matrix[source][destination] == 0) |
| 68 | + { |
| 69 | + Adjacency_Matrix[source][destination] = INFINITY; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + System.out.println("The Transitive Closure of the Graph"); |
| 74 | + FloydWarshall floydwarshall = new FloydWarshall(numberofvertices); |
| 75 | + floydwarshall.floydwarshall(adjacency_matrix); |
| 76 | + scan.close(); |
| 77 | + } |
| 78 | + } |
0 commit comments