Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About the betweenness function in igraph

Tags:

r

igraph

When we use the 'betweenness' function betweenness(g,weights=NULL,directed = FALSE), if the graph has the weight attribute, even if we write the weights=NULL, the function will still calculate the betweenness using the weight attribute. But I want to calculate the betweenness without the weight attribute. So I think this function seems strange. Why does it still use the weight attribute when we write the weights=NULL?

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
}
like image 758
Ben Avatar asked Jan 24 '26 20:01

Ben


1 Answers

The weight option is not about ignoring and not using the weights. It is a about providing the option for the user to supply their own weight vector.

From the doc

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

So if weights=NULL the function will use the E(g)$weight by default.

On way to do this yourself would be to remove the weights or set them to 1 e.g.

E(g)$weight <- 1
like image 155
user1317221_G Avatar answered Jan 27 '26 12:01

user1317221_G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!