Jump to content

Floyd-Warshall algorithm: Difference between revisions

→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details
m (→‎{{header|REXX}}: added the REXX language.)
(→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details)
Line 623:
=={{header|Kotlin}}==
{{trans|Java}}
<lang scala>// version 1.0.61
 
object FloydWarshall {
fun doCalcs(weights: Array<IntArray>, nVertices: Int) {
val dist = Array(nVertices) { DoubleArray(nVertices) { Double.POSITIVE_INFINITY } }
for (w in weights) dist[w[0] - 1][w[1] - 1] = w[2].toDouble()
val next = Array(nVertices) { IntArray(nVertices) }
for (i in 0 until next.size) {
for (j in 0 until next.size) {
if (i != j) next[i][j] = j + 1
for (k in 0 until nVertices)}
}
for (i in 0 until nVertices)
for (jk in 0 until nVertices) {
for (i in 0 until nVertices) {
for (j in 0 whileuntil (unVertices) != v){
if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j]
next[i][j] = next[i][k]
}
}
}
}
printResult(dist, next)
}
Line 648 ⟶ 653:
var path: String
println("pair dist path")
for (i in 0 until next.size) {
for (j in 0 until next.size) {
if (i != j) {
u = i + 1
v = j + 1
path = ("%d -> %d %2d %s").format(u, v, dist[i][j].toInt(), u)
do {
u = next[u - 1][v - 1]
path += " -> " + u
} while (u != v)
while (u != v)
println(path)
}
}
}
}
}
 
fun main(args: Array<String>) {
val weights = arrayOf(
intArrayOf(1, 3, -2),
intArrayOf(2, 1, 4),
intArrayOf(2, 3, 3),
intArrayOf(3, 4, 2),
intArrayOf(4, 2, -1)
)
val nVertices = 4
FloydWarshall.doCalcs(weights, nVertices)
Cookies help us deliver our services. By using our services, you agree to our use of cookies.