Skip to content

Commit 0316d74

Browse files
Create FloydWarshall.java
1 parent 1af0c0b commit 0316d74

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.io.*;
2+
import java.lang.*;
3+
import java.util.*;
4+
5+
class AllPairShortestPath {
6+
final static int INF = 99999, V = 4;
7+
8+
void floydWarshall(int dist[][])
9+
{
10+
11+
int i, j, k;
12+
13+
14+
for (k = 0; k < V; k++) {
15+
for (i = 0; i < V; i++) {
16+
for (j = 0; j < V; j++) {
17+
if (dist[i][k] + dist[k][j]
18+
< dist[i][j])
19+
dist[i][j]
20+
= dist[i][k] + dist[k][j];
21+
}
22+
}
23+
}
24+
25+
// Print the shortest distance matrix
26+
printSolution(dist);
27+
}
28+
29+
void printSolution(int dist[][])
30+
{
31+
System.out.println(
32+
"The following matrix shows the shortest "
33+
+ "distances between every pair of vertices");
34+
for (int i = 0; i < V; ++i) {
35+
for (int j = 0; j < V; ++j) {
36+
if (dist[i][j] == INF)
37+
System.out.print("INF ");
38+
else
39+
System.out.print(dist[i][j] + " ");
40+
}
41+
System.out.println();
42+
}
43+
}
44+
45+
// Driver's code
46+
public static void main(String[] args)
47+
{
48+
int graph[][] = { { 0, 5, INF, 10 },
49+
{ INF, 0, 3, INF },
50+
{ INF, INF, 0, 1 },
51+
{ INF, INF, INF, 0 } };
52+
AllPairShortestPath a = new AllPairShortestPath();
53+
54+
// Function call
55+
a.floydWarshall(graph);
56+
}
57+
}
58+

0 commit comments

Comments
 (0)