[英]How to get all the shortest paths of the length of the diameter for iGraph object?
我想為iGraph對象獲取所有最長的最短路徑。 有這個功能
get.diameter (graph, directed = TRUE, unconnected = TRUE)
但是它僅返回一條路徑。 因此,如果存在直徑長度最短的路徑,那么它將返回找到的第一個路徑
您可以使用shortest.paths(graph)返回的最短距離矩陣輕松提取哪些節點以什么長度連接。 在R中,您可以這樣使用which()
和arr.ind=TRUE
:
longest.shortest.paths <- function(graph){
# Return edgelist of all node-pairs between which the shortest path
# in a graph are the longest shortest path observed in that graph.
# Get all the shortest paths of a graph
shortest.paths = shortest.paths(graph)
# Make sure that there are no Inf-values caused by isolates in the graph
shortest.paths[shortest.paths == Inf] <- 0
# What nodes in the distance matrix are linked by longest shortest paths?
el <- which(shortest.paths==max(shortest.paths), arr.ind=TRUE)
colnames(el) <- c("i","j")
(el)
}
graph <- erdos.renyi.game(100, 140, "gnm", directed=FALSE)
longest.shortest.paths(graph)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.