|
1 | 1 | import java.util.Scanner;
|
2 |
| - |
3 |
| - |
4 |
| - |
5 | 2 | public class FloydWarshall
|
6 |
| - |
7 | 3 | {
|
8 |
| - |
9 |
| - private int distancematrix[][]; |
10 |
| - |
11 |
| - private int numberofvertices; |
12 |
| - |
| 4 | + private int DistanceMatrix[][]; |
| 5 | + private int numberofvertices;//number of vertices in the graph |
13 | 6 | public static final int INFINITY = 999;
|
14 |
| - |
15 |
| - |
16 |
| - |
17 | 7 | public FloydWarshall(int numberofvertices)
|
18 |
| - |
19 | 8 | {
|
20 |
| - |
21 |
| - distancematrix = new int[numberofvertices + 1][numberofvertices + 1]; |
22 |
| - |
| 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); |
23 | 11 | this.numberofvertices = numberofvertices;
|
24 |
| - |
25 | 12 | }
|
26 |
| - |
27 |
| - |
28 |
| - |
29 |
| - public void floydwarshall(int adjacencymatrix[][]) |
30 |
| - |
| 13 | + public void floydwarshall(int AdjacencyMatrix[][])//calculates all the distances from source to destination vertex |
31 | 14 | {
|
32 |
| - |
33 | 15 | for (int source = 1; source <= numberofvertices; source++)
|
34 |
| - |
35 | 16 | {
|
36 |
| - |
37 | 17 | for (int destination = 1; destination <= numberofvertices; destination++)
|
38 |
| - |
39 | 18 | {
|
40 |
| - |
41 |
| - distancematrix[source][destination] = adjacencymatrix[source][destination]; |
42 |
| - |
| 19 | + DistanceMatrix[source][destination] = AdjacencyMatrix[source][destination]; |
43 | 20 | }
|
44 |
| - |
45 | 21 | }
|
46 |
| - |
47 |
| - |
48 |
| - |
49 | 22 | for (int intermediate = 1; intermediate <= numberofvertices; intermediate++)
|
50 |
| - |
51 | 23 | {
|
52 |
| - |
53 | 24 | for (int source = 1; source <= numberofvertices; source++)
|
54 |
| - |
55 | 25 | {
|
56 |
| - |
57 | 26 | for (int destination = 1; destination <= numberofvertices; destination++)
|
58 |
| - |
59 | 27 | {
|
60 |
| - |
61 |
| - if (distancematrix[source][intermediate] + distancematrix[intermediate][destination] |
62 |
| - |
63 |
| - < distancematrix[source][destination]) |
64 |
| - |
65 |
| - distancematrix[source][destination] = distancematrix[source][intermediate] |
66 |
| - |
67 |
| - + distancematrix[intermediate][destination]; |
68 |
| - |
| 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]; |
69 | 32 | }
|
70 |
| - |
71 | 33 | }
|
72 |
| - |
73 | 34 | }
|
74 |
| - |
75 |
| - |
76 |
| - |
77 | 35 | for (int source = 1; source <= numberofvertices; source++)
|
78 |
| - |
79 | 36 | System.out.print("\t" + source);
|
80 |
| - |
81 |
| - |
82 |
| - |
83 | 37 | System.out.println();
|
84 |
| - |
85 | 38 | for (int source = 1; source <= numberofvertices; source++)
|
86 |
| - |
87 | 39 | {
|
88 |
| - |
89 | 40 | System.out.print(source + "\t");
|
90 |
| - |
91 | 41 | for (int destination = 1; destination <= numberofvertices; destination++)
|
92 |
| - |
93 | 42 | {
|
94 |
| - |
95 |
| - System.out.print(distancematrix[source][destination] + "\t"); |
96 |
| - |
| 43 | + System.out.print(DistanceMatrix[source][destination] + "\t"); |
97 | 44 | }
|
98 |
| - |
99 | 45 | System.out.println();
|
100 |
| - |
101 | 46 | }
|
102 |
| - |
103 | 47 | }
|
104 |
| - |
105 |
| - |
106 |
| - |
107 | 48 | public static void main(String... arg)
|
108 |
| - |
109 | 49 | {
|
110 |
| - |
111 |
| - int adjacency_matrix[][]; |
112 |
| - |
| 50 | + int Adjacency_Matrix[][]; |
113 | 51 | int numberofvertices;
|
114 |
| - |
115 |
| - |
116 |
| - |
117 | 52 | Scanner scan = new Scanner(System.in);
|
118 |
| - |
119 | 53 | System.out.println("Enter the number of vertices");
|
120 |
| - |
121 | 54 | numberofvertices = scan.nextInt();
|
122 |
| - |
123 |
| - |
124 |
| - |
125 |
| - adjacency_matrix = new int[numberofvertices + 1][numberofvertices + 1]; |
126 |
| - |
| 55 | + Adjacency_Matrix = new int[numberofvertices + 1][numberofvertices + 1]; |
127 | 56 | System.out.println("Enter the Weighted Matrix for the graph");
|
128 |
| - |
129 | 57 | for (int source = 1; source <= numberofvertices; source++)
|
130 |
| - |
131 | 58 | {
|
132 |
| - |
133 | 59 | for (int destination = 1; destination <= numberofvertices; destination++)
|
134 |
| - |
135 | 60 | {
|
136 |
| - |
137 |
| - adjacency_matrix[source][destination] = scan.nextInt(); |
138 |
| - |
| 61 | + Adjacency_Matrix[source][destination] = scan.nextInt(); |
139 | 62 | if (source == destination)
|
140 |
| - |
141 | 63 | {
|
142 |
| - |
143 |
| - adjacency_matrix[source][destination] = 0; |
144 |
| - |
| 64 | + Adjacency_Matrix[source][destination] = 0; |
145 | 65 | continue;
|
146 |
| - |
147 | 66 | }
|
148 |
| - |
149 |
| - if (adjacency_matrix[source][destination] == 0) |
150 |
| - |
| 67 | + if (Adjacency_Matrix[source][destination] == 0) |
151 | 68 | {
|
152 |
| - |
153 |
| - adjacency_matrix[source][destination] = INFINITY; |
154 |
| - |
| 69 | + Adjacency_Matrix[source][destination] = INFINITY; |
155 | 70 | }
|
156 |
| - |
157 | 71 | }
|
158 |
| - |
159 | 72 | }
|
160 |
| - |
161 |
| - |
162 |
| - |
163 | 73 | System.out.println("The Transitive Closure of the Graph");
|
164 |
| - |
165 | 74 | FloydWarshall floydwarshall = new FloydWarshall(numberofvertices);
|
166 |
| - |
167 | 75 | floydwarshall.floydwarshall(adjacency_matrix);
|
168 |
| - |
169 | 76 | scan.close();
|
170 |
| - |
171 | 77 | }
|
172 |
| - |
173 | 78 | }
|
0 commit comments