生成一个在networkx中具有k个输入和j个输出以及n个节点的有向图。

huangapple go评论79阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2023年5月18日 00:08:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274095.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定