-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloyd.java
67 lines (61 loc) · 2.08 KB
/
Floyd.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
import java.util.Scanner;
public class Floyd {
private static int[][] tableDist;
private static int[][] tablePath;
private static int vertNum;
public static void main(String[] args)
{
System.out.print("Number of vertices: ");
Scanner sysin = new Scanner( System.in );
vertNum = sysin.nextInt();
tableDist = new int[vertNum][vertNum];
tablePath = new int[vertNum][vertNum];
for(int i = 0; i < vertNum; i++)
{
System.out.println("Row weights (use space to separate): ");
for(int j = 0; j < vertNum; j++)
tableDist[i][j] = sysin.nextInt();
}
floyds();
}
private static void print(int[][] table1, int[][] table2)
{
for(int i = 0; i < table1.length && i < table2.length; i++)
{
for(int k = 0; k < table1[i].length && k < table2[i].length; k++)
{
if(table1[i][k] == -1)
System.out.print("-" + " (" + table2[i][k] + ")");
else
System.out.print(table1[i][k] + " (" + table2[i][k] + ")");
System.out.print("\t|\t");
}
System.out.println();
}
}
private static void floyds()
{
System.out.println("\n ITERATION: (0) ");
print(tableDist, tablePath);
// Floyd's
// k = iteration #
// i = row #
// j = column #
for(int k = 0; k < vertNum; k++)
{
for (int i = 0; i < vertNum; i++)
{
for (int j = 0; j < vertNum; j++)
{
if ((tableDist[i][j] > (tableDist[i][k] + tableDist[k][j])|| tableDist[i][j] < 0)&& tableDist[k][j] > 0 && tableDist[i][k] > 0)
{
tableDist[i][j] = tableDist[i][k] + tableDist[k][j];
tablePath[i][j] = k+1;
}
}
}
System.out.println("\n ITERATION:(" + (k+1) + ") ");
print(tableDist, tablePath);
}
}
}