Skip to content

power_centrality() should support edge weights #903

@davidskalinder

Description

@davidskalinder

Describe the bug

power_centrality() cannot handle weighted graphs and so returns different values for a weighted graph and for an equivalent graph with parallel edges.

sna::bonpow(), on which igraph::power_centrality() is based, does not have this problem since it accepts a weighted adjacency matrix as an input, but it's not possible to do this with the igraph function since it uses ensure_igraph() to only allow an igraph object as input. As a result, the only workaround to work with a weighted igraph is to convert the graph to something like an adjacency matrix (with weights in its cells) and then back into a graph with parallel edges instead of weights, which can then be passed to power_centrality() as in the reprex below.

The reason for this behavior is that this line in the function for dense matrices and this one in the function for sparse matrices use as_adj() to convert the igraph to an adjacency matrix, but they do not pass any arguments to as_adj(). The bug could be easily fixed by allowing the name of the weight attribute to be passed to as_adj()'s attr argument, but I'm hesitant to submit a PR since it's not clear to me how the fix would/should affect downstream compatibility. At a minimum, this unexpected behavior should be better documented.

To reproduce

library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union

incidence <- matrix(c(1, 0, 1, 0, 1, 1, 1, 1, 1), nrow = 3, ncol = 3)
bipartite <- graph_from_incidence_matrix(incidence)
gph_weighted <- bipartite_projection(bipartite, which = TRUE)
adj_weighted <- as_adj(gph_weighted, attr = "weight")
gph_unweighted <- graph_from_adjacency_matrix(adj_weighted)
adj_unweighted <- as_adj(gph_unweighted)

identical(adj_weighted, adj_unweighted)
#> [1] TRUE

identical(power_centrality(gph_weighted), power_centrality(gph_unweighted))
#> [1] FALSE

Version information

  • R/igraph version: 1.5.1
  • R version: R version 4.1.2 (2021-11-01)
  • Operating system: Ubuntu 20.04.6 LTS
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] igraph_1.5.1

loaded via a namespace (and not attached):
 [1] compiler_4.1.2    Matrix_1.6-1.1    magrittr_2.0.3    cli_3.6.1        
 [5] tools_4.1.2       rstudioapi_0.15.0 unix_1.5.5        grid_4.1.2       
 [9] rlang_1.1.1       pkgconfig_2.0.3   lattice_0.20-45 

Metadata

Metadata

Assignees

No one assigned

    Labels

    wishlistFeature request that has not been chosen for implementation yet; vote or comment to prioritize it!

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions