在r

时间:2015-07-19 11:26:45

标签: r shortest-path

我有一个网络,在其中我找到了所有节点之间的成对最短路径。我还发现了中间最短路径序列。这些中间最短路径序列是那些序列,例如从节点B到节点D的最短路径序列是BCAD,然后中间节点序列是C A.我得到的中间最短路径序列结果不是以ids的形式而是以数字形式。为了获得顶点序列我尝试了以下但在顶点序列中我得到字符(0)作为输出。我想要的输出是C A.

library(igraph)
g <- graph.ring(10)
V(g)$name <- LETTERS[1:10]
LETTERS[1:10]
tm <- get.all.shortest.paths(g,"B","D")
tm$res[[1]]
print(V(g)[tm$res[[1]]]) # printing the vertex sequences



for(i in 1:length(V(g))) {
for(k in 1:length(V(g)))
{   
temp <- get.all.shortest.paths(g,i,k) # finding the shortest paths between all nodes
print(temp$res[[1]]) #printing the result of shortest paths
for(j in 1:length(temp$res)) {
  d <- head(tail(temp$res[[j]], length(temp$res[[j]])-1),-1) # Show me only intermediate nodes
  print(d)
  e <- head(tail(V(g)[temp$res[[1]]], length(V(g)[temp$res[[1]]])-1),-1) # Show me only intermediate vertex sequences
  print(e)
}

}
}

在我的脚本中的变量e的结果中,我希望有一个中间最短路径的顶点序列。对于从节点A到C的最短路径,最短路径序列是A B D C然后我希望将中间节点作为我的结果,除了两个节点,它们来自最短路径函数。

1 个答案:

答案 0 :(得分:1)

我略微修改了你的代码(并将输入环图缩小为6个顶点):

library(igraph)
g <- graph.ring(6)
V(g)$name <- LETTERS[1:6]

intermediates <- list() # list to keep the result
for(i in 1:length(V(g))){
  from <- V(g)[i]
  temp <- get.all.shortest.paths(g,from=from,V(g))
  for(el in temp$res){
    to <- V(g)[el[length(el)]]

    # this "if" excludes paths from A to A, B to B etc...
    if(to != from){ 
      intermediate <- tail(head(el,-1),-1)

      # this "if" excludes shortest-paths without intermediate vertices e.g.
      # A -> B, B -> C etc...
      if(length(intermediate) > 0){ 
        intermediates[[length(intermediates)+1]] <- V(g)$name[intermediate] # append this intermediate vertices to list 
        cat('Intermediated vertices of shortest path from ',from$name,' to ',to$name,': ', paste(V(g)$name[intermediate],collapse=','), '\n', sep='')
      }

    }
  }
}

# find the most frequent (N.B. table() creates a table of occurrencies by element)
tbl <- table(unlist(intermediates))
most.freq <- names(tbl)[tbl == max(tbl)]
cat('Most frequent vertex (or vertices in case of ties): ', paste(most.freq,collapse=','),'\n')

控制台输出:

Intermediated vertices of shortest path from A to C: B
Intermediated vertices of shortest path from A to D: F,E
Intermediated vertices of shortest path from A to D: B,C
Intermediated vertices of shortest path from A to E: F
Intermediated vertices of shortest path from B to D: C
Intermediated vertices of shortest path from B to E: C,D
Intermediated vertices of shortest path from B to E: A,F
Intermediated vertices of shortest path from B to F: A
Intermediated vertices of shortest path from C to A: B
Intermediated vertices of shortest path from C to E: D
Intermediated vertices of shortest path from C to F: D,E
Intermediated vertices of shortest path from C to F: B,A
Intermediated vertices of shortest path from D to A: E,F
Intermediated vertices of shortest path from D to A: C,B
Intermediated vertices of shortest path from D to B: C
Intermediated vertices of shortest path from D to F: E
Intermediated vertices of shortest path from E to A: F
Intermediated vertices of shortest path from E to B: F,A
Intermediated vertices of shortest path from E to B: D,C
Intermediated vertices of shortest path from E to C: D
Intermediated vertices of shortest path from F to B: A
Intermediated vertices of shortest path from F to C: E,D
Intermediated vertices of shortest path from F to C: A,B
Intermediated vertices of shortest path from F to D: E

Most frequent vertex (or vertices in case of ties):  A,B,C,D,E,F 

一些注意事项:

  • get.all.shortest.paths返回顶点from与顶点列表之间的所有最短路径。所以我摆脱了你的内心循环。
  • get.all.shortest.paths还返回绑定,即如果两个顶点之间有两条或更多条等效的最短路径则返回它们(例如检查ADB在上面的示例中为E等。

编辑:

将循环结果存储到列表中并最终计算最频繁的节点。