8.1 R function CombAttack
We implement an R function called CombAttack
to calculate attackness for individual nodes and nodes in combination (see the function parameter combine
) in a network (see the function parameter ig
).
## ig: an igraph object
## combine: a list containing individual nodes or nodes combined for combinatorial attack
CombAttack <- function(ig, combine)
{
max.comp.orig <- max(igraph::components(ig)$csize)
n <- vcount(ig)
res <- NULL
if(!is.null(combine)){
m <- length(combine)
max.comp.removed <- rep(max.comp.orig, m)
nodes.removed <- rep(max.comp.orig, m)
removed.pct <- rep(max.comp.orig, m)
pb <- dplyr::progress_estimated(m)
for(i in seq_len(m)){
pb$tick()$print()
ind <- match(V(ig)$name, combine[[i]])
v <- V(ig)$name[!is.na(ind)]
nodes.removed[i] <- paste(v,collapse=',')
g.manual <- igraph::delete_vertices(ig, v)
max.comp.removed[i] <- max(igraph::components(g.manual)$csize)
removed.pct[i] <- 1 - igraph::vcount(g.manual) / n
}
comp.pct <- max.comp.removed/max.comp.orig
res <- tibble::tibble(frac.disconnected=1-comp.pct, frac.removed=removed.pct, nodes.removed=nodes.removed)
}
return(res)
}