igraph-help
[Top][All Lists]

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

 From: mario rossi Subject: Re: [igraph] how to change attributes to a randomly selected nodes of a network in igraph Date: Thu, 11 May 2017 22:50:07 +0200

Thank you very much, Chris!
Can you help me to better understand how it works in the second part?
For example, why
"  i <- 3                                                                                                                                                                                             cnames <- c(LETTERS, letters)
while (sum(V(g)\$compartment == 'A') > 3  "?

And what is "  round(0.15 * sum(V(g)\$compartment == 'A')))  "? Is there a third final parenthesis, it is right? Is it to saple at least the 15%, but regard the only integer part? (Or, maybe, the "next integer")?
And what it do in the two final lines of the code?

As usual, thank you in advance.
Mario

2017-05-11 19:26 GMT+02:00 Chris Watson :
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]
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 wrote:

Hi, R community!

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

sample(vcount(g),10)

[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)

End

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

And sorry for my bad English.

Mario Rossi

_______________________________________________
igraph-help mailing list
https://lists.nongnu.org/mailman/listinfo/igraph-help

_______________________________________________
igraph-help mailing list