Floyd-Warshall代码段不断在Javascript中返回空矩阵

时间:2018-09-21 23:46:18

标签: javascript adjacency-matrix floyd-warshall

我一直在尝试使用Floyd-Marshall算法来解决运动问题,但是它一直仅返回零矩阵。这是代码:

// Adjacency matrix

let am = [[0,0,0,1,1,1], 
          [0,0,1,0,1,0], 
          [0,1,0,1,0,1], 
          [1,0,1,0,0,0], 
          [1,1,0,0,0,0], 
          [1,0,1,0,0,0]]


// Floyd-Warshall algorithm

for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
        for (k = 0; k < n; k++) {

            if (am[i][j] > am[i][k] + am[k][j]) {
                am[i][j] = am[i][k] + am[k][j];
            }

        }
    }
}

return am;

返回

 [[0,0,0,0,0,0], 
  [0,0,0,0,0,0], 
  [0,0,0,0,0,0], 
  [0,0,0,0,0,0], 
  [0,0,0,0,0,0], 
  [0,0,0,0,0,0]]

我在做什么错了?

0 个答案:

没有答案