[英]igraph in R: error with get.shortest.paths but not with shortest.paths
[英]Returning individual vertices of get.shortest.paths() in R
我使用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)
我發現圖的中心節點和葉節點是這樣的:
centralNode <- which(degree(graph) %in% c(max(degree(graph))))
leafNodes <- which(degree(graph) %in% c(1))
我發現從中央節點到第一個葉節點的最短路徑是這樣的:
sp <- get.shortest.paths(graph, centralNode, leafNodes[1])
並可以得到類似的信息(如果1是centralNode
而4是leafNodes[1]
:
[[1]]
[1] 1 2 9 4
我希望能夠訪問從centralNode
到leafNodes[1]
的最短路徑中的每個頂點。
我嘗試這樣做,但不斷出現這些錯誤:
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$["
我不確定如何單獨返回每個頂點,或者只選擇其中一個。 我希望這是有道理的。
任何幫助將非常感激。 謝謝
sp
只是一個標准列表。 因此, sp[[1]]
等將只獲取向量。
但是,子集圖形對象可能更有意義。 像這樣:
V(graph)[sp[[1]]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.