I am trying to work with Networkx to create a graph that is composed of a fixed number of communities in which there are different probabilities for adding edges for nodes inside a given community and adding edges for nodes between different communities. I have looked at the generator for directed scale_free_graph. Here is an example of how to create three disconnected graphs using it:
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
N_tot = 100 # total number of nodes
pos_frac = 0.1 # fraction of positive nodes
neg_frac = 0.1 # fraction of negative nodes
N_p = int(pos_frac*N_tot) # number of positive nodes
N_n = int(neg_frac*N_tot) # number of negative nodes
N_0 = int(N_tot - (N_p + N_n)) # number of neutral nodes
G1 = nx.scale_free_graph(N_p, seed=3)
G2 = nx.scale_free_graph(N_n, seed=2)
G3 = nx.scale_free_graph(N_0, seed=1)
nx.set_node_attributes(G1,"blue","color")
nx.set_node_attributes(G2,"red","color")
nx.set_node_attributes(G3,"green","color")
G1adj=nx.convert_matrix.to_numpy_matrix(G1,weight=None)
G2adj=nx.convert_matrix.to_numpy_matrix(G2,weight=None)
G3adj=nx.convert_matrix.to_numpy_matrix(G3,weight=None)
mappingG2 = {}
for i in range(G2.number_of_nodes()):
mappingG2[i]=i+G1.number_of_nodes()
G2 = nx.relabel_nodes(G2, mappingG2)
mappingG3 = {}
for i in range(G3.number_of_nodes()):
mappingG3[i]=i+G1.number_of_nodes()+G2.number_of_nodes()
G3 = nx.relabel_nodes(G3, mappingG3)
fig,ax=plt.subplots(1,3)
ax[0].matshow(G1adj)
ax[1].matshow(G2adj)
ax[2].matshow(G3adj)
Someone has an idea of how to create the links between those three graphs while maintaining the scale free property of the whole graph?
from Networkx: create a directed scale free graph with a given number of communities
No comments:
Post a Comment