Igraph如何处理重量? [英] How Igraph handle weights?

查看:96
本文介绍了Igraph如何处理重量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我有一个非常简单的问题,因为缺乏术语,我无法找到答案.在r的包装中igraph如何被视为重量?是将它们视为成本,从而减少了边缘的容量,还是确实将它们视为边缘的容量?

I have a really simple question that I wasn't able to find an answer, because of lack of terminology I am afraid. In r's package igraph how are considered weights? Are they considered a cost, therefore reducing the capacity of an edge or are they considered indeed as the capacity of the edge?

非常感谢您

推荐答案

在igraph中,权重是边缘属性,代表在该边缘中行进的摩擦成本.而不是边缘的容量带宽.较低的权重使路径的 weight-sum 较低,并且get.shortest.paths()在运行时返回具有最低权重和的路径,而不会禁用加权图上的权重.

In igraph, weights are edge-attributes that represent the friction or cost of traveling that edge in a parth, not the capacity or bandwidth of the edge. Low weight makes for a low weight-sum of a path and get.shortest.paths() returns the path with the lowest weight-sum when run without disabling weights on a weighted graph.

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

This code example shows a graph with different shortest paths in weighted and unweighted mode, and explains why the path is differently calculated.

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),但越过重量较轻的楔块,重量总和就越低,或者,如果愿意的话,也可以降低成本.

See the shortest unweighted path between the two larger vertices has the length of 4, but travels over rather thickly weighted green edges. The shortest weighted path has the lowest weight-sum and travels more steps (5) but over wedges with lower weight resulting in a lower weight-sum, or lower cost of travelling the parth, if you like.

这篇关于Igraph如何处理重量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆