This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 179
/
Floyd.kt
129 lines (101 loc) · 3.31 KB
/
Floyd.kt
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.util.*
class Floyd {
fun shortestpath(adj: Array<IntArray>, path: Array<IntArray>): Array<IntArray> {
val n = adj.size
val ans = Array(n) { IntArray(n) }
// Implement algorithm on a copy matrix so that the adjacency isn't
// destroyed.
copy(ans, adj)
// Compute successively better paths through vertex k.
for (k in 0 until n) {
// Do so between each possible pair of points.
for (i in 0 until n) {
for (j in 0 until n) {
if (ans[i][k] + ans[k][j] < ans[i][j]) {
ans[i][j] = ans[i][k] + ans[k][j]
path[i][j] = path[k][j]
}
}
}
}
// Return the shortest path matrix.
return ans
}
// Copies the contents of array b into array a. Assumes that both a and
// b are 2D arrays of identical dimensions.
fun copy(a: Array<IntArray>, b: Array<IntArray>) {
for (i in a.indices)
for (j in 0 until a[0].size)
a[i][j] = b[i][j]
}
// Returns the smaller of a and b.
fun min(a: Int, b: Int): Int {
return if (a < b)
a
else
b
}
fun main(args: Array<String>) {
val stdin = Scanner(System.`in`)
// Tests out algorithm with graph shown in class.
val m = Array(5) { IntArray(5) }
m[0][0] = 0
m[0][1] = 3
m[0][2] = 8
m[0][3] = 10000
m[0][4] = -4
m[1][0] = 10000
m[1][1] = 0
m[1][2] = 10000
m[1][3] = 1
m[1][4] = 7
m[2][0] = 10000
m[2][1] = 4
m[2][2] = 0
m[2][3] = 10000
m[2][4] = 10000
m[3][0] = 2
m[3][1] = 10000
m[3][2] = -5
m[3][3] = 0
m[3][4] = 10000
m[4][0] = 10000
m[4][1] = 10000
m[4][2] = 10000
m[4][3] = 6
m[4][4] = 0
val shortpath: Array<IntArray>
val path = Array(5) { IntArray(5) }
// Initialize with the previous vertex for each edge. -1 indicates
// no such vertex.
for (i in 0..4)
for (j in 0..4)
if (m[i][j] == 10000)
path[i][j] = -1
else
path[i][j] = i
// This means that we don't have to go anywhere to go from i to i.
for (i in 0..4)
path[i][i] = i
shortpath = shortestpath(m, path)
// Prints out shortest distances.
for (i in 0..4) {
for (j in 0..4)
System.out.print(shortpath[i][j].toString() + " ")
System.out.println()
}
System.out.println("From where to where do you want to find the shortest path?(0 to 4)")
val start = stdin.nextInt()
var end = stdin.nextInt()
// The path will always end at end.
var myPath = end + ""
// Loop through each previous vertex until you get back to start.
while (path[start][end] != start) {
myPath = path[start][end].toString() + " -> " + myPath
end = path[start][end]
}
// Just add start to our string and print.
myPath = start + " -> " + myPath
System.out.println("Here's the path $myPath")
}
}