在R中的宽度优先搜索期间更改网络中的节点的属性值 [英] Changing attribute values of nodes in a network during breadth first search in R
问题描述
我创建了一个有100个节点的随机(Erdos-Renyi)网络。我已经为所有100个节点设置了一个属性值为0.我找到了最大程度的节点(最邻近),并将其属性值从0更改为1.然后,使用该节点作为根节点,我做了一个网络上的广度优先搜索(BFS)。
I have created a random (Erdos-Renyi) network that has 100 nodes. I have set an attribute value for all 100 nodes as 0. I find the node with the maximum degree (the most neighbors), and change its attribute value from 0 to 1. Then, using the node as the root node, I do a breadth first search (BFS) on the network.
以下是我的代码:
Here is my code to do this so far:
# 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)
当它通过网络的每个节点时,我希望它改变属性我不确定如何做到这一点。
As it goes through each node of the network, I want it to change the attribute value of the node it's looking at from 0 to 1. I'm not sure how to do this.
任何帮助都将非常感谢。
Any help would be much appreciated.
在此先感谢您。
推荐答案
试试这个:
Try this:
for (i in 1:100) {
if (is.nan(bfs$dist[i]) == FALSE) {
V(graph)$value[i] = 1 - V(graph)$value[i]
}
}
它基本上只是检查节点是否连接到根节点。如果是,它的属性值将更改为1,如果不是,则其属性值保持不变。
It basically just checks if a node is connected to the root node. If it is, its attribute value is changed to 1, and if it isn't, then its attribute value remains unchanged.
这篇关于在R中的宽度优先搜索期间更改网络中的节点的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!