[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] how to change attributes to a randomly selected nodes of a

From: Chris Watson
Subject: Re: [igraph] how to change attributes to a randomly selected nodes of a network in igraph
Date: Thu, 11 May 2017 12:26:36 -0500

This should get you started. It's not very fast but I think should do what you want it to do. I used an Erdos-Renyi graph instead of star because your neighbor constraint (#7) wouldn't have made sense then, as they are all neighbors of one node. If the stopping point is when compartment "A" is empty, and you want 15% of the nodes in "A" changed, the loop won't stop (unless I did something wrong.

g <- erdos.renyi.game(100, 0.2)                                                                                                                                                                        
V(g)$compartment <- 'A'                                                                                                                                                                                
x <- sample(vcount(g), 10)                                                                                                                                                                             
V(g)[x]$compartment <- 'B'                                                                                                                                                                             
i <- 3                                                                                                                                                                                                 
cnames <- c(LETTERS, letters)                                                                                                                                                                         
while (sum(V(g)$compartment == 'A') > 3) {                                                                                                                                                             
  x <- sample(which(V(g)$compartment == 'A'), round(0.15 * sum(V(g)$compartment == 'A')))                                                                                                              
  V(g)[x]$compartment <- cnames[i]                                                                                                                                                                    
  nghbrs <- unique(unlist(adjacent_vertices(g, x)))                                                                                                                                                    
  y <- sample(nghbrs, round(0.12 * length(nghbrs)))                                                                                                                                                    
  if (length(y) > 0) V(g)[y]$compartment <- cnames[i + 1]                                                                                                                                             
  i <- i + 2                                                                                                                                                                                           

On Thu, May 11, 2017 at 9:40 AM, mario rossi <address@hidden> wrote:

Hi, R community!

As you can read in the headline, i have a question for you about igraph.

I need to make an algorithm  that works as follow:

1) I start from a given network (for example a star: "g <- make_star(100)");

2) then, I assign to all nodes the same attribute (for example, all the nodes in the same compartment, that I call "A": "v(g)$compartment <- rep("A", 100)" );

3) I select a random number of nodes (for example: "sample(vcount(g), 10)");

No problem with 1), 2) and 3)! But, please, can you help me with the following part of the algorithm?

4) I have to change the attribute to the randomly selected nodes (for example, if the result of the random sample of 10 nodes is


 [1] 18 11 26 28 44 67 86 89 52 78,

I have to change the compartment of nodes 18, 11, 26, ..., 78 from "A" to "B");

5) I have to randomly sampling, within the nodes that still in the compartment "A" (and not in "B") a given PERCENTAGE (and not a given number) of the nodes (in this first iteration, there are 90 nodes in the compartment "A", and I want to select, for example 15% of the 90 nodes).

6) I have to change the compartment of the just randomly selected nodes from "A" to a new compartment, called "C"; (Note that this will be a part of a "repeat until" loop, so I need to obtain an algorithm that automatically turn the compartment of the randomly selected nodes from "A" to "C").

7) I need to randomly sampling, AMONG THE NODES THAT ARE ADJACENT to those in compartment "C", a given percentage of them, for example 12%, and turn these just selected nodes into a new compartment, called "D".

8) If compartment "A" is empty, the algorithm stops. Otherwise, it return at point 5)



Thank you in advance, if you have time to help me.

And sorry for my bad English.

Mario Rossi

igraph-help mailing list

reply via email to

[Prev in Thread] Current Thread [Next in Thread]