Igraph如何处理重量?

时间:2019-01-17 09:33:10

标签: r igraph cran

大家好。

我有一个非常简单的问题,我担心找不到答案,因为缺少术语。在r的包装中igraph如何考虑重量?是将它们视为成本,从而降低了边缘的容量,还是确实将它们视为边缘的容量?

非常感谢您

2 个答案:

答案 0 :(得分:0)

如果您查看R igraph软件包-https://igraph.org/r/doc/strength.html

中的官方文档

您会发现权重被称为:

“权重向量。如果图形具有权重边缘属性,则默认使用此属性。如果图形不具有权重边缘属性,并且此参数为NULL,则给出警告并调用度。”

而且:

https://igraph.org/r/doc/edge_attr.html

说明边缘是作为权重属性给出的

答案 1 :(得分:0)

在igraph中,权重是边缘属性,而不是 capability 边缘的带宽。低权重使路径的 weight-sum 低,并且get.shortest.paths()在运行时返回权重和最低的路径,而不会禁用加权图的权重。

此代码示例显示了在加权和未加权模式下具有不同最短路径的图,并解释了为什么计算路径的方式有所不同。

library(igraph)

# Colours for the weighted edges
N <- 22
set.seed(7890123)

# Make a random graph with randomly weighted edges coloured in gray
g <- erdos.renyi.game(N, .2, type="gnp", directed=F, loops=F, weighted=T)
E(g)$weight <- sample(1:40, length(E(g)), replace=T)
#E(g)$weight <- E(g)$weight/10
E(g)$color <- "gray"
V(g)$size <- 4
V(g)$size[c(1,N)] <- 12

# Look how the shortest path is calculated differently when taken the graph weihgt into acocunt
(weighted.path <- unlist(get.shortest.paths(g, 1, N)$vpath) )
(unweighted.path <- unlist(get.shortest.paths(g, 1, N, weights=NA)$vpath) )

# Set weights and colours of shortest paths to visualise them
E(g, path=weighted.path)$color <- "red"
E(g, path=unweighted.path)$color <- "green"

# plot the graph with red shortest weighted path, and green shortest path
same.sahpe <- layout_with_fr(g)
plot(g, vertex.color="white", vertex.label=NA, edge.weight=2, edge.width=(E(g)$weight/5), layout=same.sahpe)

# The two paths look like this. Even though path-length might be longer, a weighted
# shortest path is determined using the sum of path-weights. As with path-lengths, the
# lowest value is the path most easily travelled by. In this case, the weighted alternative
# has a longer path but with lower friction.
data.frame(path.length=c(length(weighted.path),
                        length(unweighted.path)
                        ),
           weight.sum=c(sum(E(g, path=unlist(weighted.path))$weight),
                        sum(E(g, path=unlist(unweighted.path))$weight)
           )
)

看到两个较大顶点之间的最短未加权路径的长度为4,但在加权较重的绿色边缘上移动。最短的加权路径具有最低的权重总和,并经过更多的步长(5),但越过重量较轻的楔形块,重量总和就越低,或者,如果愿意,也可以降低成本

enter image description here