[英]Returning individual vertices of get.shortest.paths() in R
I have created a random (Erdos-Renyi) graph with p = 0.2 and 10 nodes in R using the igraph package like this: 我使用igraph包创建了一个随机(Erdos-Renyi)图,其中p = 0.2,R中有10个节点,如下所示:
library(igraph)
graph <- erdos.renyi.game(10, 0.2, type = c("gnp", "gnm"), directed = FALSE,
loops = FALSE)
I find the central node and leaf nodes of the graph like this: 我发现图的中心节点和叶节点是这样的:
centralNode <- which(degree(graph) %in% c(max(degree(graph))))
leafNodes <- which(degree(graph) %in% c(1))
I find the shortest path from the central node to the first leaf node like this: 我发现从中央节点到第一个叶节点的最短路径是这样的:
sp <- get.shortest.paths(graph, centralNode, leafNodes[1])
And can get something like this (if 1 is the centralNode
and 4 is the leafNodes[1]
: 并可以得到类似的信息(如果1是
centralNode
而4是leafNodes[1]
:
[[1]]
[1] 1 2 9 4
I want to be able to access each of the vertices in the shortest path from the centralNode
to the leafNodes[1]
. 我希望能够访问从
centralNode
到leafNodes[1]
的最短路径中的每个顶点。
I tried doing it like this but keep getting these errors: 我尝试这样做,但不断出现这些错误:
sp$2
Error: unexpected numeric constant in "sp$2"
sp$[[1]][2]
Error: unexpected '[[' in "sp$[["
sp$1[2]
Error: unexpected numeric constant in "sp$1"
sp$[1][2]
Error: unexpected '[' in "sp$["
I'm not sure how to return each vertex individually, or just pick one of them. 我不确定如何单独返回每个顶点,或者只选择其中一个。 I hope this makes sense.
我希望这是有道理的。
Any help would be much appreciated. 任何帮助将非常感激。 Thanks
谢谢
sp
is just a standard list. sp
只是一个标准列表。 So sp[[1]]
etc will work to just get a vector. 因此,
sp[[1]]
等将只获取向量。
However, it probably makes more sense for you to be subsetting the graph object. 但是,子集图形对象可能更有意义。 Something like this:
像这样:
V(graph)[sp[[1]]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.