简体   繁体   中英

R / igraph Removing the edge between specific nodes that fulfill a certain requirement?

I currently have a code like this:

library(igraph)
set.seed(123); g <- erdos.renyi.game(30, 151 , type = "gnm" , directed = F , loops = F) %>%
  set_vertex_attr("a", value = 0) %>%
  set_vertex_attr("b", value = 0) 

V(g)$b <- sample(c(0, .6, .7), vcount(g), replace = TRUE, prob = c(0.3, .4, .3))


repeat{
mean <- mean(V(g)$a == 1)
prev_value <- mean(v(g)$a == 1)
V(g)$a[V(g)$b <= mean & V(g)$a == 0] <- 1
curr_value <- mean(v(g)$a == 1)

if(prev_value == curr_value){




break

}
}

I want to add something to the if code that will randomly delete an edge that has certain "trait". In this case I want the trait to be that the two connected nodes both have values of 0 for "a".

I want to [...] randomly delete an edge [where] the two connected nodes both have values of 0 for "a".

You could try

# trait:
candidates <- which(head_of(g, E(g))$a==0 & tail_of(g, E(g))$a==0)
# random: 20% prob for deletion
idx <- sample(c(TRUE, FALSE), length(candidates), prob = c(.2,.8), repl=T)
delete_edges(g, candidates[idx])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM