Igraph 如何处理权重?

How Igraph handle weights?

大家好。

我有一个非常简单的问题,我找不到答案,恐怕是因为缺乏术语。在 r 的包 igraph 中如何考虑权重?它们是否被视为成本,因此减少了边缘的容量,或者它们确实被视为边缘的容量?

非常感谢

如果您查看 R igraph 包中的官方文档 - https://igraph.org/r/doc/strength.html

您会发现权重被称为:

"Weight vector. If the graph has a weight edge attribute, then this is used by default. If the graph does not have a weight edge attribute and this argument is NULL, then a warning is given and degree is called."

还有:

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

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

在 igraph 中,权重是边属性,表示在部分中通过该边的 摩擦成本,而不是 [边缘的=21=]容量或带宽。低权重使得路径的 权重总和 较低,并且 get.shortest.paths() returns 在 运行 没有禁用权重的情况下具有最低权重总和的路径在加权图上。

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

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) 但越过具有较低权重的楔形导致较低的权重和,或者 较低的成本 行进部分,如果你喜欢