【问题标题】:How to get the a list of the edge attributes when performing shortest.paths in igraph (R)在igraph(R)中执行shortest.paths时如何获取边缘属性列表
【发布时间】:2013-04-10 14:42:43
【问题描述】:

我需要计算图中两个顶点之间最短路径的边属性的乘积。

例如:

data<-as.data.frame(cbind(c(1,2,3,4,5,1),c(4,3,4,5,6,5),c(0.2,0.1,0.5,0.7,0.8,0.2)))
G<-graph.data.frame(data, directed=FALSE)
set.edge.attribute(G, "V3", index=E(G), data$V3)

如果我根据属性计算最短路径我有两个可能性,第一个告诉我步骤:

get.shortest.paths (G, 2, 6, weights=E(G)$V3)

2 3 4 1 5 6

第二个告诉我沿路径的属性总和。

shortest.paths (G, 2, 6, weights=E(G)$V3)

1.8

由于我需要制作产品,因此我需要在路径的节点之间有一个边缘属性向量。在这个例子中,我应该得到 0.8 0.2 0.2 0.5 0.1,其乘积为 0.0016。 谁能建议我怎么做?

【问题讨论】:

    标签: r igraph


    【解决方案1】:

    使用get.shortest.pathsoutput 参数:

    library(igraph)
    data <- data.frame(from  =c(1,  2,  3,  4,  5,  1),
                       to    =c(4,  3,  4,  5,  6,  5),
                       weight=c(0.2,0.1,0.5,0.7,0.8,0.2))
    G <- graph.data.frame(data, directed=FALSE)
    
    esp26 <- get.shortest.paths(G, 2, 6, output="epath")[[1]]
    esp26
    # [1] 2 3 1 6 5
    
    prod(E(G)$weight[esp26])
    # [1] 0.0016
    
    plot(G, edge.label=paste("Id:", 1:ecount(G), "\n", "W:",
              E(G)$weight, sep=""))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 2014-04-13
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多