Changing the values ​​of the attributes of nodes in the network at the first width search in R

I created a random (Erdos-Renyi) network with 100 nodes. I set the attribute value for all 100 nodes to 0. I find the node with the maximum degree (most neighbors) and change its attribute value from 0 to 1. Then, using node as the root of the node, I do the first width search (BFS) in 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 value of the node attribute that it was looking at from 0 to 1. I'm not sure how to do this.

Any help would be greatly appreciated.

Thanks in advance.

+1
source share
1 answer

Try the following:

for (i in 1:100) {
    if (is.nan(bfs$dist[i]) == FALSE) {
        V(graph)$value[i] = 1 - V(graph)$value[i]
    }
}

, node node. , 1, , .

+1

All Articles