forked from The-Open-Source-Society/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Floyd_Warshall_Algorithm.java
68 lines (57 loc) · 1.73 KB
/
Floyd_Warshall_Algorithm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Created by jude on 21/10/16.
*/
public class Floyd_Warshall_Algorithm {
public static int Infinity = Integer.MAX_VALUE;
static int[][] graph = { {0, 8, Infinity, 15},
{Infinity, 0, 7, Infinity},
{Infinity, Infinity, 0, 7},
{Infinity, Infinity, Infinity, 0}
};
public static void main(String[] args)
{
FloydWarshall(graph);
}
public static void FloydWarshall(int[][] graph)
{
for(int k=0;k<4;k++)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(graph[i][k] != Infinity && graph[k][j] != Infinity && graph[i][k]+graph[k][j] < graph[i][j])
{
graph[i][j] = graph[i][k]+graph[k][j];
}
}
}
}
printSolution(graph);
}
private static void printSolution(int[][] graph) {
System.out.println("Shortest distances between each pair of vertices");
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(graph[i][j] == Infinity)
{
System.out.println("INFINITY \t");
}
else
{
System.out.println(graph[i][j]);
}
}
System.out.println("\n");
}
}
}
/* OUTPUT
The matrix below shows the shortest distances between each pair of vertices
0 8 15 15
INFINITY 0 7 14
INFINITY INFINITY 0 7
INFINITY INFINITY INFINITY 0
*/