在 r 中打印中间顶点序列
Printing the intermediate vertex sequences in r
我有一个网络,我在其中找到了所有节点之间的成对最短路径。而且我还找到了中间最短路径序列。这些中间最短路径序列是那些序列,例如从节点 B 到节点 D 的最短路径序列是 B C A D,然后中间节点序列是 C A。我得到的中间最短路径序列结果不是 ID 的形式,而是数字形式.为了获取顶点序列,我尝试了以下方法,但在顶点序列中,我得到的是字符 (0),因为 output.My 所需的输出是 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 然后我想将中间节点作为我的结果,除了最短路径函数中来自和到的两个节点。
我稍微修改了你的代码(并将输入环图减少到 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
returns 顶点 from
到顶点列表之间的所有最短路径。所以我摆脱了你的内部循环。
get.all.shortest.paths
returns 也是关系,即如果两个顶点之间有两条或更多等效的最短路径,则返回它们(例如检查 A
到 D
, B
到 E
等。在上面的例子中).
编辑:
将循环结果存储到列表中并最终计算最频繁的节点。
我有一个网络,我在其中找到了所有节点之间的成对最短路径。而且我还找到了中间最短路径序列。这些中间最短路径序列是那些序列,例如从节点 B 到节点 D 的最短路径序列是 B C A D,然后中间节点序列是 C A。我得到的中间最短路径序列结果不是 ID 的形式,而是数字形式.为了获取顶点序列,我尝试了以下方法,但在顶点序列中,我得到的是字符 (0),因为 output.My 所需的输出是 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 然后我想将中间节点作为我的结果,除了最短路径函数中来自和到的两个节点。
我稍微修改了你的代码(并将输入环图减少到 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
returns 顶点from
到顶点列表之间的所有最短路径。所以我摆脱了你的内部循环。get.all.shortest.paths
returns 也是关系,即如果两个顶点之间有两条或更多等效的最短路径,则返回它们(例如检查A
到D
,B
到E
等。在上面的例子中).
编辑:
将循环结果存储到列表中并最终计算最频繁的节点。