Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating random graph in r

Tags:

graph

r

I would like to generate a grandom graph in R using any of the packages.

The desired output would be a two column matrix with the first column listing agents and the second column their connections of the following form:

1 3
1 4
1 6
1 7
2 2
2 5
3 9
3 11
3 32
3 43
3 2
4 5

I would like to be able to specify the average degree and minimum and maximum number of contacts.

What is the easiest way of doing this?

like image 400
user1723765 Avatar asked Jan 14 '23 14:01

user1723765


1 Answers

Since you don't specify the need for anything other than just a graph we ca do this very simply:

actor     <- sample(1:4, 10, replace=TRUE)
receiver  <- sample(3:43, 10, replace=TRUE)
graph     <- cbind(actor,receiver)

if you want something more specific have a look at igraph for instance

library(igraph)
graph <- erdos.renyi.game(21, 0.3, type=c("gnp", "gnm"),
              directed = FALSE, loops = FALSE)

# here the 0.3 is the probability of ties and 21 is the number of nodes
# this is a one mode network

or using package bipartite which focuses specifically on two mode networks:

library(bipartite)
web <- genweb(N1 = 5, N2 = 10, dens = 2)
web2edges(web,return=TRUE)

# here N1 is the number of nodes in set 1 and N2 the number of nodes in set 2
# and dens the average number of ties per node

There are many things to take into account, for instance if you want to constrain the degree distribution, probablity of ties between agents etc.

like image 61
user1317221_G Avatar answered Jan 22 '23 23:01

user1317221_G