MCPcopy Create free account
hub / github.com/PrajaktaSathe/Java / AllPairShortestPath

Class AllPairShortestPath

Programs/Floyd_Warshall_Algorithm.java:7–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5
6
7class AllPairShortestPath
8{
9 final static int INF = 99999, V = 4;
10
11 void floydWarshall(int graph[][])
12 {
13 int dist[][] = new int[V][V];
14 int i, j, k;
15
16 for (i = 0; i < V; i++)
17 for (j = 0; j < V; j++)
18 dist[i][j] = graph[i][j];
19
20 for (k = 0; k < V; k++)
21 {
22 for (i = 0; i < V; i++)
23 {
24 for (j = 0; j < V; j++)
25 {
26 if (dist[i][k] + dist[k][j] < dist[i][j])
27 dist[i][j] = dist[i][k] + dist[k][j];
28 }
29 }
30 }
31
32 printSolution(dist);
33 }
34
35 void printSolution(int dist[][])
36 {
37 System.out.println("The following matrix shows the shortest "+
38 "distances between every pair of vertices");
39 for (int i=0; i<V; ++i)
40 {
41 for (int j=0; j<V; ++j)
42 {
43 if (dist[i][j]==INF)
44 System.out.print("INF ");
45 else
46 System.out.print(dist[i][j]+" ");
47 }
48 System.out.println();
49 }
50 }
51
52 public static void main (String[] args)
53 {
54
55 int graph[][] = { {0, 5, INF, 10},
56 {INF, 0, 3, INF},
57 {INF, INF, 0, 1},
58 {INF, INF, INF, 0}
59 };
60 AllPairShortestPath a = new AllPairShortestPath();
61
62 a.floydWarshall(graph);
63 }
64}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected