Skip to content

Commit ba22b1c

Browse files
Update FloydWarshall.java
1 parent 35a4bd3 commit ba22b1c

File tree

1 file changed

+17
-112
lines changed

1 file changed

+17
-112
lines changed
Lines changed: 17 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,173 +1,78 @@
11
import java.util.Scanner;
2-
3-
4-
52
public class FloydWarshall
6-
73
{
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
136
public static final int INFINITY = 999;
14-
15-
16-
177
public FloydWarshall(int numberofvertices)
18-
198
{
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);
2311
this.numberofvertices = numberofvertices;
24-
2512
}
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
3114
{
32-
3315
for (int source = 1; source <= numberofvertices; source++)
34-
3516
{
36-
3717
for (int destination = 1; destination <= numberofvertices; destination++)
38-
3918
{
40-
41-
distancematrix[source][destination] = adjacencymatrix[source][destination];
42-
19+
DistanceMatrix[source][destination] = AdjacencyMatrix[source][destination];
4320
}
44-
4521
}
46-
47-
48-
4922
for (int intermediate = 1; intermediate <= numberofvertices; intermediate++)
50-
5123
{
52-
5324
for (int source = 1; source <= numberofvertices; source++)
54-
5525
{
56-
5726
for (int destination = 1; destination <= numberofvertices; destination++)
58-
5927
{
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];
6932
}
70-
7133
}
72-
7334
}
74-
75-
76-
7735
for (int source = 1; source <= numberofvertices; source++)
78-
7936
System.out.print("\t" + source);
80-
81-
82-
8337
System.out.println();
84-
8538
for (int source = 1; source <= numberofvertices; source++)
86-
8739
{
88-
8940
System.out.print(source + "\t");
90-
9141
for (int destination = 1; destination <= numberofvertices; destination++)
92-
9342
{
94-
95-
System.out.print(distancematrix[source][destination] + "\t");
96-
43+
System.out.print(DistanceMatrix[source][destination] + "\t");
9744
}
98-
9945
System.out.println();
100-
10146
}
102-
10347
}
104-
105-
106-
10748
public static void main(String... arg)
108-
10949
{
110-
111-
int adjacency_matrix[][];
112-
50+
int Adjacency_Matrix[][];
11351
int numberofvertices;
114-
115-
116-
11752
Scanner scan = new Scanner(System.in);
118-
11953
System.out.println("Enter the number of vertices");
120-
12154
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];
12756
System.out.println("Enter the Weighted Matrix for the graph");
128-
12957
for (int source = 1; source <= numberofvertices; source++)
130-
13158
{
132-
13359
for (int destination = 1; destination <= numberofvertices; destination++)
134-
13560
{
136-
137-
adjacency_matrix[source][destination] = scan.nextInt();
138-
61+
Adjacency_Matrix[source][destination] = scan.nextInt();
13962
if (source == destination)
140-
14163
{
142-
143-
adjacency_matrix[source][destination] = 0;
144-
64+
Adjacency_Matrix[source][destination] = 0;
14565
continue;
146-
14766
}
148-
149-
if (adjacency_matrix[source][destination] == 0)
150-
67+
if (Adjacency_Matrix[source][destination] == 0)
15168
{
152-
153-
adjacency_matrix[source][destination] = INFINITY;
154-
69+
Adjacency_Matrix[source][destination] = INFINITY;
15570
}
156-
15771
}
158-
15972
}
160-
161-
162-
16373
System.out.println("The Transitive Closure of the Graph");
164-
16574
FloydWarshall floydwarshall = new FloydWarshall(numberofvertices);
166-
16775
floydwarshall.floydwarshall(adjacency_matrix);
168-
16976
scan.close();
170-
17177
}
172-
17378
}

0 commit comments

Comments
 (0)