这是我的伪代码,我想降低这段代码的时间复杂度。我是 Java 和算法的新手。请帮助我。
Func1(int n, int W[1..n, 1..n])
{
array d[1..n, 1..n]
for i = 1 to n do
{ // initialize
for j = 1 to n do {
d[i,j] = W[i,j]
pred[i,j] = null
}
}
for k = 1 to n do // use intermediates {1..k}
for i = 1 to n do // ...from i
for j = 1 to n do // ...to j
if (d[i,k] + d[k,j]) < d[i,j])
{
d[i,j] = d[i,k] + d[k,j] // new shorter path length
pred[i,j] = k // new path is through k
}
return d // matrix of final distances
}