Igraph 如何处理权重?

S. *_*rtz 1 r igraph cran

祝大家有个美好的一天。

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

非常感谢

nJG*_*JGL 6

在 igraph 中,权重是边属性,表示在一个部分中移动该边的摩擦成本,而不是边的容量带宽。低权重使路径的权重和较低,并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)
           )
)
Run Code Online (Sandbox Code Playgroud)

看到两个较大顶点之间的最短未加权路径的长度为 4,但经过相当厚的加权绿色边。最短的加权路径具有最低的权重总和,并且经过更多的步骤 (5),但越过权重较低的楔形,导致较低的权重总和,或者如果您愿意,可以降低通过该部分的成本

在此处输入图片说明