Ben
Ben

Reputation: 675

About the betweenness function in 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
}

Upvotes: 2

Views: 2309

Answers (1)

user1317221_G
user1317221_G

Reputation: 15461

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

Upvotes: 6

Related Questions