Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/centrality.R
Original file line number Diff line number Diff line change
Expand Up @@ -2018,7 +2018,7 @@ alpha.centrality.dense <- function(
attr <- NULL
} else if (is.character(weights) && length(weights) == 1) {
## name of an edge attribute, nothing to do
attr <- "weight"
attr <- weights
} else if (any(!is.na(weights))) {
## weights != NULL and weights != rep(NA, x)
graph <- set_edge_attr(graph, "weight", value = as.numeric(weights))
Expand Down Expand Up @@ -2065,7 +2065,7 @@ alpha.centrality.sparse <- function(
attr <- NULL
} else if (is.character(weights) && length(weights) == 1) {
## name of an edge attribute, nothing to do
attr <- "weight"
attr <- weights
} else if (any(!is.na(weights))) {
## weights != NULL and weights != rep(NA, x)
graph <- set_edge_attr(graph, "weight", value = as.numeric(weights))
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-centrality.R
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,22 @@ test_that("weighted sparse alpha_centrality() works", {
expect_equal(ac3, c(vcount(star), 1, 1, 1, 1, 1, 1, 1, 1, 1))
})

test_that("alpha_centrality() works with custom weight attribute names", {
star <- make_star(10)
E(star)$myweight <- sample(ecount(star))

# Test sparse version with custom attribute name
ac_sparse <- alpha_centrality(star, weights = "myweight", sparse = TRUE)
expect_equal(ac_sparse, c(46, 1, 1, 1, 1, 1, 1, 1, 1, 1))

# Test dense version with custom attribute name
ac_dense <- alpha_centrality(star, weights = "myweight", sparse = FALSE)
expect_equal(ac_dense, c(46, 1, 1, 1, 1, 1, 1, 1, 1, 1))

# Ensure both versions give the same result
expect_equal(ac_sparse, ac_dense)
})

test_that("undirected alpha_centrality() works, #653", {
g <- make_ring(10)

Expand Down
Loading