【问题标题】:How to get the longest flow matrix of a graph with r-igraph package?如何使用 r-igraph 包获得图的最长流矩阵?
【发布时间】:2020-04-13 16:49:28
【问题描述】:

假设我使用 igraph 包创建了一个随机图:

n=6  # number of vertices 
F <- erdos.renyi.game(n, p.or.m=0.7, directed=FALSE)
m=ecount(F)
min = 1    # 1 km
max = 50   # 50 km 
F <- set.edge.attribute(F, name="distance", value=runif(m , min , max))
plot(F, layout=layout.fruchterman.reingold)
distances(F)
distances(F, weights = E(F)$distance)
distances(F, v = 1, to = 6, weights = E(F)$distance)
get.all.shortest.paths(F, 1, to = V(F)) 

我们知道distances(F, weights = E(F)$distance) 给出了与图中最短路径相关的流的三次矩阵。

我想要与以下行相同:

distances(F, weights = E(F)$distance) # matrix of shortests paths flows between any two vertices
distances(F, v = 1, to = 6, weights = E(F)$distance)
get.all.shortest.paths(F, 1, to = V(F)) # gives the shortests paths between 1 and other vertices

这次我需要矩阵中关联流的最长路径。 我不知道“igraph”是否可以做到这一点。

感谢您的帮助!

【问题讨论】:

    标签: r


    【解决方案1】:

    如果首先计算最大距离,然后从最大值中减去距离作为权重,则算法将计算最长路径。

    以下两种方法是等价的。

    library(igraph)
    
    max_dist <- max(E(G)$distance)
    all_shortest_paths(G, from = 1, to = V(G), weights = max_dist - E(G)$distance)
    get.all.shortest.paths(G, 1, to = V(G), weights = max_dist - E(G)$distance)
    

    注意事项:

    1. 我已将图形对象的名称更改为G,因为F 也是FALSE 的符号。
    2. 重新创建图表,这次使用 RNG 集,以使结果可重现。

    图表创建代码。

    set.seed(1234)
    
    n <- 6  # number of vertices 
    G <- erdos.renyi.game(n, p.or.m = 0.7, directed = FALSE)
    m <- ecount(G)
    min <- 1    # 1 km
    max <- 50   # 50 km 
    G <- set.edge.attribute(G, name = "distance", value = runif(m , min , max))
    

    【讨论】:

    • 我不认为是因为max_dist&lt;- max(E(G)$distance) 在所有可能对的所有最短路径之间提供了最大流量。如果我们考虑所有对之间所有可能的最长路径,那么这些流的最小值可能大于max_dist。就我目前所知,除了使用像 simpex 这样的优化算法之外,没有这样的技术来定义对偶性!
    • 我想我可以使用负边权重的同一个图,通过反转所有边权重来找到最长的路径。
    • @mouad2020 权重不能为负数,这就是为什么我通过从不小于最大边权重的数字中减去来反转。
    • @mouad2020 至于第一条评论,使用max_dist并不意味着最长路径小于该值,它只是使用权重的反转。
    • @mouad2020 您还可以在[0, 1] 中缩放到数字d 并使用weights = 1 - d
    猜你喜欢
    • 2018-07-03
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多