![](/img/trans.png)
[英]How to find shortest paths between two nodes based on certain edge attribute using igraph in R?
[英]R / igraph Removing the edge between specific nodes that fulfill a certain requirement?
我目前有這樣的代碼:
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
}
}
我想在if代碼中添加一些內容,以隨機刪除具有某些“特征”的邊緣。 在這種情況下,我希望特征是兩個連接的節點的“ a”值都為0。
我想隨機刪除兩個連接的節點的邊,其中“ a”的值均為0。
你可以試試
# 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])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.