我创建了一个有100个节点的随机(Erdos-Renyi)网络。我已将所有100个节点的属性值设置为0.我找到具有最大度数(最多邻居)的节点,并将其属性值从0更改为1.然后,使用该节点作为根节点,我执行了网络上的广度优先搜索(BFS)。
到目前为止,这是我的代码:
# Loads the igraph package
library(igraph)
# Creates a random (Erdos-Renyi) network with 100 nodes and edges with p = 0.2
graph <- erdos.renyi.game(100, 0.2, type = c("gnp", "gnm"), directed = FALSE,
loops = FALSE)
# Sets the attributes of all the nodes to 0
graph <- set.vertex.attribute(graph, "value", value = 0)
# Determines the maximum degree
max_deg <- max(degree(graph))
# The node with the maximum degree becomes the root node for the BFS, and changes
# its value from 0 to 1
root_node <- which(degree(graph) %in% c(max_deg))
V(graph)$value[root_node] = 1 - V(graph)$value[root_node]
# BFS on the network
bfs <- graph.bfs(graph, root = root_node, unreachable = FALSE, order = TRUE,
dist = TRUE)
当它通过网络的每个节点时,我希望它将它正在查看的节点的属性值从0更改为1.我不知道该怎么做。
非常感谢任何帮助。
提前致谢。
答案 0 :(得分:1)
试试这个:
for (i in 1:100) {
if (is.nan(bfs$dist[i]) == FALSE) {
V(graph)$value[i] = 1 - V(graph)$value[i]
}
}
它基本上只检查节点是否连接到根节点。如果是,则其属性值更改为1,如果不是,则其属性值保持不变。