英文:
Generate a directed graph with k inputs in j outputs and n nodes in networkx
问题
I'm trying to generate a directed graph using networkx in Python with the following characteristics: specified k inputs, j outputs, and n nodes. Ideally I would also be able to specify the connectivity of the graph through an additional parameter. I've played around with the various graph generators in networkx, but I can't seem to find one that fits this specific description. The closest I've found is the random_k_out_graph()
function, but I don't think that lets you specify the number of inputs as well. Any help is appreciated!
英文:
I'm trying to generate a directed graph using networkx in Python with the following characteristics: specified k inputs, j outputs, and n nodes. Ideally I would also be able to specify the connectivity of the graph through an additional parameter. I've played around with the various graph generators in networkx, but I can't seem to find one that fits this specific description. The closest I've found is the random_k_out_graph()
function, but I don't think that lets you specify the number of inputs as well. Any help is appreciated!
答案1
得分: 1
这部分代码的翻译如下:
这似乎运行得很好,生成了各种连接程度,并指定输入和输出的数量都为1:
def gen_degree_distribution(poisson_par, number_nodes):
### 生成度分布
in_degree = poisson.rvs(mu=poisson_par, size=number_nodes) + 1 # 加1是因为我们不想要额外的零节点
in_degree_zero = np.insert(in_degree, 0, 0) # 在第一个入度前插入零,表示有一个原始节点
out_degree_zero = np.insert(random.sample(in_degree.tolist(),
len(in_degree)),
len(in_degree), 0) # 在最后一个节点后插入零,表示有一个退出节点
return [in_degree_zero, out_degree_zero]
degree_out = gen_degree_distribution(1, number_nodes=20)
in_degree_zero = degree_out[0]
out_degree_zero = degree_out[1]
D = nx.directed_havel_hakimi_graph(in_deg_sequence=in_degree_zero,
out_deg_sequence=out_degree_zero)
pos = nx.spring_layout(D) # 节点的位置
nx.draw(D, pos, with_labels=True, arrows=True)
plt.show()
希望对你有所帮助。
英文:
This seems to work okay, generating various degrees of connectivity and specifying the number of inputs and outputs to both be 1:
def gen_degree_distribution(poisson_par, number_nodes):
### generate degree distribution
in_degree = poisson.rvs(mu=poisson_par, size=number_nodes) + 1 # added one because we don't want any additional zero nodes
in_degree_zero = np.insert(in_degree, 0, 0) # add a zero to the first in degree to say there is one seminal node
out_degree_zero = np.insert(random.sample(in_degree.tolist(),
len(in_degree)),
len(in_degree), 0)# add a zero to the last node to say there is one exit node
return [in_degree_zero, out_degree_zero]
degree_out = gen_degree_distribution(1, number_nodes = 20)
in_degree_zero = degree_out[0]
out_degree_zero = degree_out[1]
D = nx.directed_havel_hakimi_graph(in_deg_sequence=in_degree_zero,
out_deg_sequence = out_degree_zero)
pos = nx.spring_layout(D) # Positions of the nodes
nx.draw(D, pos, with_labels = True, arrows = True)
plt.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论