Search code examples
pythonnetworkxgraph-theorysocial-networking

Simulate graph model in networkx


I have a very specific graph problem in networkx:

My directed graph has two different type of nodes ( i will call them I and T) and it is built with edges only between I-T and T-I (so T doesn't connect with other T and the same with I)

Now I need to simulate a new graph with the same behavior: i have a certain number of I and T and the edge between I-T exists with a certain probability (also for T-I but with different probability, let's call them p_i and p_o).

So my problem is that i can't iterate with for loops both for I and then for T because both are quite big (the data I'm analyzing right now are 5000 T's and 5000 I's but the will probably increase up to 300000 each) and my pc can't handle that.

What is the best way to create a graph in this situation?


Solution

  • This is the solution that @ravenspoint makes me reach with his comment.

    For T=5000 and I=5000 it works by doing for loops in I and one in T and by using np.random.binomial(1, pi, nI) and np.random.binomial(1, po, nO) from numpy, where nO and nI are the length of O and I in the real graph and creating edges if these arrays are 1.

    If po=pi (as it happens in my example) also @Stef solution works and you can use nx.bipartite.random_graph(countadd, countt, p, seed=None,directed=True)