Skip to content

Commit 35a4bd3

Browse files
used to find the shorthest paths among all pairs of nodes in a graph,
Floyd-Warshall algorithm is a procedure, which is used to find the shorthest (longest) paths among all pairs of nodes in a graph, which does not contain any cycles of negative lenght. The main advantage of Floyd-Warshall algorithm is its simplicity.
1 parent 8b359c7 commit 35a4bd3

File tree

1 file changed

+173
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)