简体   繁体   English

如何获得iGraph对象直径的所有最短路径?

[英]How to get all the shortest paths of the length of the diameter for iGraph object?

I want to get all the longest shortest paths for iGraph object. 我想为iGraph对象获取所有最长的最短路径。 There is this function 有这个功能

get.diameter (graph, directed = TRUE, unconnected = TRUE) 

But it returns only one path. 但是它仅返回一条路径。 So if there are many shortest paths of the length of the diameter, then it returns the first one found 因此,如果存在直径长度最短的路径,那么它将返回找到的第一个路径

You can easily extract which nodes are connected at what lengths using the shortest-distance matrix returned by shortest.paths(graph). 您可以使用shortest.paths(graph)返回的最短距离矩阵轻松提取哪些节点以什么长度连接。 In R, you can use which() and arr.ind=TRUE like so: 在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM