gpt4 book ai didi

r - 关于igraph中的介数函数

转载 作者:行者123 更新时间:2023-12-04 11:46:10 26 4
gpt4 key购买 nike

当我们使用'betweenness'函数betweenness(g,weights=NULL,directed = FALSE)时,如果图有权重属性,即使我们写weights=NULL,该函数仍然会使用权重属性。但是我想计算没有权重属性的介数。所以我觉得这个功能看起来很奇怪。为什么写weights=NULL的时候还是用weight属性?

function (graph, v = V(graph), directed = TRUE, weights = NULL, 
nobigint = TRUE, normalized = FALSE)
{
if (!is.igraph(graph)) {
stop("Not a graph object")
}
v <- as.igraph.vs(graph, v)
if (is.null(weights) && "weight" %in% list.edge.attributes(graph)) {
weights <- E(graph)$weight
}
if (!is.null(weights) && any(!is.na(weights))) {
weights <- as.numeric(weights)
}
else {
weights <- NULL
}
on.exit(.Call("R_igraph_finalizer", PACKAGE = "igraph"))
res <- .Call("R_igraph_betweenness", graph, v - 1, as.logical(directed),
weights, as.logical(nobigint), PACKAGE = "igraph")
if (normalized) {
vc <- vcount(graph)
res <- 2 * res/(vc * vc - 3 * vc + 2)
}
if (getIgraphOpt("add.vertex.names") && is.named(graph)) {
names(res) <- V(graph)$name[v]
}
res
}

最佳答案

权重选项不是忽略和不使用权重。这是关于为用户提供提供他们自己的权重向量的选项。

来自doc

weight - Optional positive weight vector for calculating weighted betweenness. If the graph has a weight edge attribute, then this is used by default.

因此,如果 weights=NULL,函数将默认使用 E(g)$weight

您自己执行此操作的方法是删除权重或将它们设置为 1,例如

E(g)$weight <- 1

关于r - 关于igraph中的介数函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16129233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com