我想在带有n个顶点的R中生成随机运输图。
图应该是两部分的,并且相互连接(通常在2个顶点之间有链接/方式,通常不是直接的顶点),且不带“循环”。
此外,边缘评估应该是随机且严格为正的。
我尝试执行以下操作:
n=6 # number of vertices
F <- erdos.renyi.game(n, p.or.m=0.5, 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)
问题是我无法在图形距离矩阵中得到所需的距离:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0 2 1 1 2 1
[2,] 2 0 2 1 1 1
[3,] 1 2 0 1 1 1
[4,] 1 1 1 0 2 1
[5,] 2 1 1 2 0 1
[6,] 1 1 1 1 1 0
通常,我应该获得1到50之间的统一随机值。
另外我不确定如果将来我再次执行此代码,该图是否将不包含循环。
我想我的问题很清楚!谢谢您的帮助!
解决方案 :
n=6 # number of vertices
F <- erdos.renyi.game(n, p.or.m=0.5, directed=FALSE) # We create the graph F
m=ecount(F) # number of edges we obtained using erdos.renyi.game()
min = 1 # 1 km # min and max values for the edges flows
max = 50 # 50 km
F <- set.edge.attribute(F, name="distance", value=runif(m , min , max)) # The flows are drawn in random
plot(F, layout=layout.fruchterman.reingold) # we plot the graph F
distances(F) # matrix of lentghs of the shortest paths between i & j
distances(F, weights = E(F)$distance) # matrix of flows of the shortest paths
distances(F, v = 1, to = 6, weights = E(F)$distance) # same if we want a specific minimum path between two vertices ( ex : 1--->6 )
get.all.shortest.paths(F, 1, to = V(F)) # all shortest paths list
graph.maxflow(F, 1, 6) # the maximum possible flow of any route between 1--->6
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句