R 中迭代多维数组并比较其元素的正确方法是什么?

What is the right way in R to iterate over a multidimensional array and compare it's elements?

我正在尝试在 R 中实现 Floyd Warshall 的算法。当我 运行 程序时,出现以下错误: if ((graph[i][k] + graph[k][j]) < graph[i][j]) { 错误: 参数的长度为零

我知道这与图形数组的迭代有关。迭代图形数组的正确方法是什么?谢谢。

图表:

       10
   (0)------->(3)
    |         /|\
  5 |          |
    |          | 1
   \|/         |
   (1)------->(2)
        3    

代码:

inf <- 99999 
graph <- array(c(0, inf, inf, inf, 5, 0, inf, inf, inf, 3, 0, inf, 10, inf, 1, 0), dim = c(4, 4, 1))

V <- 4

new.floyd <- function(graph){
  k <- 0
  i <- 0
  j <- 0
  
  while(k < V){ 
    while(i < V){
      while(j < V){
        if((graph[i][k] + graph[k][j]) < graph[i][j]){
          graph[i][j] <- (graph[i][k] + graph[k][j])
        }
        j <- j + 1
      }
      j <- 0
      i <- i + 1
    }
    i <- 0
    k <- k + 1
}
inf <- 99999 
graph <- array(c(0, inf, inf, inf, 5, 0, inf, inf, inf, 3, 0, inf, 10, inf, 1, 0), dim = c(4, 4, 1))

V <- 4

new.floyd <- function(graph){
  k <- 1
  i <- 1
  j <- 1
  
  while(k <= V){ 
    while(i <= V){
      while(j <= V){
        if((graph[i, k,] + graph[k,j,]) < graph[i,j,]){
          graph[i,j,] <- (graph[i,k,] + graph[k,j,])
        }
        j <- j + 1
      }
      j <- 1
      i <- i + 1
    }
    i <- 1
    k <- k + 1
}
 
  print(graph)

  
}

new.floyd(graph)