Question

I have created an undirected random (Erdos-Renyi) network in R using the igraph library. It consists of 100 nodes and the probability p that an edge exists between two nodes is 0.2.

This is my code to create the random network:

original <- erdos.renyi.game(100, 0.2, type = c("gnp", "gnm"), directed = FALSE,
    loops = FALSE)

So the original network can look something like this:

      O
      |                    Please note: O represent nodes, and the lines
      |                                 represent the edges between the nodes
O-----O-----O
|     | \
|     |  \
O-----O   O

I need to split this random network into two networks (called net1 and net2), so that if a random number '1' is chosen, an edge between two nodes from the original network will exist between the same two nodes of net1 (or if a random number '2' is chosen, the edge will exist in net2, again between the same two nodes).

Then net1 and net2 can look something like this, depending on the random numbers ('1' and '2') for their edges:

      O                   O
                          |
                          |
O-----O     O       O     O-----O
      | \           |
      |  \          |
O     O   O         O-----O   O
     net1                net2

So if net1 has an edge between two nodes that were originally in the original network, net2 wouldn't have that edge between the same two nodes (and same with if the edge is in net2, it shouldn't be in net1 as well).

I'm not sure how to go about splitting the original network into the two networks and creating the edges between the same nodes of the two networks, as the original network, according to a random number ('1' or '2') being chosen.

Sorry if this is a bit confusing, it's difficult to explain.

Any help would be much appreciated. Many thanks in advance.

Was it helpful?

Solution

For some igraph random network object a

# what is the order of a?
n <- length(V(a))

# random network
# we're only interested in this for that fact that it provides a
# random symmetric matrix with approx. half of its edges present
ran <- erdos.renyi.game(n, .5, type="gnp", directed=F)

# network 1 (as an igraph graph object)
g1 <- graph.adjacency(a[,] * ran[,])

# network 2 (also as an igraph graph object)
g2 <- graph.adjacency(a[,] * !ran[,])

The resulting objects will be igraph graph representations of your split network. You can easily convert them to whatever form you need. Without graph.adjacency() these last two calculations result in sparse matrix representations, for example.

The essential logic here is that a random matrix (differentiated from the random network) of 0s and 1s (approximately 50% of each) is element-wise multiplied by the random network to result in one network of, on average, a random selection of half of the edges in the original random network. Then the complement of the random matrix is multiplied by the random network resulting in a second network with the remaining ties of the original network.

If you were to take the two results in matrix format and element-wise add them, you'd have the original network again (in a matrix format).

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