Question

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.

Thanks in advance.

Was it helpful?

Solution

Try this:

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top