vendredi 30 octobre 2015

NetworkX: how to build an Erdos-Renyi graph from a set of predetermined positions?

I am new to NetworkX. I need to build something like an Erdos-Renyi model (random graph):

enter image description here

I need to create it from a dictionary of node positions that is generated by a deterministic function. This means that I cannot allow Python to randomly decide where each node goes to, as I want to decide it. The function is:

pos = dict( (n, n) for n in G.nodes() ).

I was thinking of creating an adjacency matrix first, in order to randomly generate something similar to pairs of (start, endpoint) of each edge, like this:

G=np.random.randint(0, 1, 25).reshape(5, 5)

Then I was thinking of somehow turning the matrix into my list of edges, like this:

G1=nx.grid_2d_graph(G)

but of course it does not work since this function takes 2 args and I am only giving it 1.

My questions:

  1. How to create this kind of graph in NetworkX?
  2. How to make sure that all nodes are connected?
  3. How to make sure that, upon assigning the 1 in the matrix, each pair of nodes has the same probability of landing a 1?

Example for point 3. Imagine we created the regular grid of points which positions are determined according to pos. When we start connecting the network and we select the first node, we want to make sure that the endpoint of this first edge is one of the N-1 nodes left in the network (except the starting node itself). Anyhow, we want to make sure that all N-1 nodes have the same probability of being connected to the node we first analyze.

Thanks a lot!




Aucun commentaire:

Enregistrer un commentaire