简体   繁体   English

使用get.shortest.paths()与第二个变量的距离矩阵

[英]Distance Matrix from second variable using get.shortest.paths()

I would like to go one step further with this question ( Find total of second variable related to the distance of route from get.shortest.paths() ). 我想进一步解决这个问题( 从get.shortest.paths()查找与路径距离有关的第二个变量的总和 )。 How does one get the matrix of distances between nodes, when the 'shortest' path is found using the newcost variable? 当使用newcost变量找到“最短”路径时,如何获得节点之间的距离矩阵?

(My experience with igraph is very limited). (我对igraph的经验非常有限)。

      df2 = rbind(c(234,235,21.6,75),
      c(234,326,11.0,35),
      c(235,241,14.5,78),
      c(326,241,8.2,98),
      c(241,245,15.3,75),
      c(234,245,38.46,65))

      df2 = as.data.frame(df2)
      names(df2) = c("start_id","end_id","newcost","distance")

      require(igraph)
      g2 <- graph.data.frame(df2, directed=FALSE)
      tmp2 = shortest.paths(g2,weights=E(g2)$newcost)
      tmp2 #this gives the matrix of newcost-weighted shortest distances

Where I could use help is how to find all paths, say using optimal.path <- get.shortest.paths , and using sum(E(g2, path = optimal.path)$distance) to create the matrix of distances 我可以使用帮助的地方是如何找到所有路径,例如,使用optimal.path <- get.shortest.paths ,并使用sum(E(g2, path = optimal.path)$distance)创建距离矩阵

what I would really like is an edgelist of distances for all node pairs, like: 我真正想要的是所有节点对的距离边缘列表,例如:

      startid endid shortestdist
      234     235     75
      234     245     208

What is tricky about this problem is that newcost is used to find the shortest paths, but what I want is the sum of another variable - the distance variable on each shortest path between node pairs. 关于此问题的棘手问题是,使用newcost来查找最短路径,但是我想要的是另一个变量的总和-节点对之间每个最短路径上的距离变量。

Ok, first let me make it clear that I am not an igraph user myself. 好的,首先让我明确说明我自己不是igraph用户。 Nevertheless I thought the question was interesting so I thought I'd take a look. 不过,我认为这个问题很有趣,所以我想看看。 And I too could find no easy solution to the problem you are having. 而且我也找不到解决您所遇到问题的简便方法。 I ended up making some helper functions to make the process possible. 最后,我做了一些辅助功能以使该过程成为可能。 There is a good chance that i've re-coded the functionality inside igraph already but I could not find it. 我很有可能已经在igraph重新编码了功能,但找不到。

Let me first define get.shortest.paths.for.all which will not just return the shortest path lengths for a given attribute, but will also return the shortest path themselves for all vertices in the graph. 让我首先定义get.shortest.paths.for.all ,它不仅会返回给定属性的最短路径长度,还将自己返回图中所有顶点的最短路径。 Here's that code 这是代码

get.shortest.paths.for.all<-function(graph, attr) {
    paths<-lapply(1:(vcount(graph)-1), function(i) {
        get.all.shortest.paths(
            graph, 
            weights=get.edge.attribute(g2,attr),
            from = V(graph)[i],
            to = V(graph)[(i+1):vcount(graph)]
        )$res
    })
    unsplit(paths, rep.int(seq_along(paths), sapply(paths, length)))
}

Now let me define get.path.dist.matrix will takes a graph and a list of paths (like the one returned by get.shortest.paths.for.all ) and will calculate the distance for a given attribute between each of those paths 现在让我定义get.path.dist.matrix将获取一个图形和一个路径列表(例如get.shortest.paths.for.all返回的get.shortest.paths.for.all ),并计算每个路径之间给定属性的距离

get.path.dist.matrix<-function(graph, path, attr) {
    dd<-get.adjacency(graph, attr=attr)
    uniqs <- numeric(0)
    if (is.list(path)) {
        starts<-sapply(path, head, 1)
        ends<-sapply(path, tail, 1)
        uniqs <- unique(sort(c(starts,ends)))
    } else {
        uniqs <- c(head(path,1), tail(path,1))
    }
    m<-matrix(0, nrow=length(uniqs), ncol=length(uniqs),
        dimnames=list(V(graph)$name[uniqs],V(graph)$name[uniqs]))
    for(pp in path) {
        m[pp[1], pp[length(pp)]]<-sum(dd[embed(pp,2)])
    }
    m+t(m)
}

Using your sample data, I use them like this 使用您的样本数据,我像这样使用它们

paths <- get.shortest.paths.for.all(g2, "newcost")
get.path.dist.matrix(g2, paths,"distance")

#     234 235 326 241 245
# 234   0  75  35 133 208
# 235  75   0 176  78 153
# 326  35 176   0  98 173
# 241 133  78  98   0  75
# 245 208 153 173  75   0

Which seems reasonable and distinct from shortest.paths(g2,weights=E(g2)$distance) . 这似乎是合理的,并且与shortest.paths(g2,weights=E(g2)$distance) To try to test my functions, I see that 为了测试我的功能,我看到

all(tmp2==get.path.dist.matrix(g2, paths,"newcost"))

So feel free to try these out and let me know if you see any problems or possible improvements. 因此,请随时尝试这些,如果您发现任何问题或可能的改进,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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