栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

生成具有一定程度分布的图?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

生成具有一定程度分布的图?

如果要使用配置模型,则应在NetworkX中使用以下方法:

import random import networkx as nxz=[int(random.gammavariate(alpha=9.0,beta=2.0)) for i in range(100)]G=nx.configuration_model(z)

您可能需要根据伽马分布中的参数调整序列z的平均值。同样z不需要是图形的(您将获得多图),但是它确实需要一个偶数和,因此您可能必须尝试一些随机序列(或加1)…

关于configuration_model的NetworkX文档注释提供了另一个示例,参考以及如何删除平行边和自循环:

Notes-----As described by Newman [1]_.A non-graphical degree sequence (not realizable by some simplegraph) is allowed since this function returns graphs with selfloops and parallel edges.  An exception is raised if the degreesequence does not have an even sum.This configuration model construction process can lead toduplicate edges and loops.  You can remove the self-loops andparallel edges (see below) which will likely result in a graphthat doesn't have the exact degree sequence specified.  This"finite-size effect" decreases as the size of the graph increases.References----------.. [1] M.E.J. Newman, "The structure and function       of complex networks", SIAM REVIEW 45-2, pp 167-256, 2003.Examples-------->>> from networkx.utils import powerlaw_sequence>>> z=nx.create_degree_sequence(100,powerlaw_sequence)>>> G=nx.configuration_model(z)To remove parallel edges:>>> G=nx.Graph(G)To remove self loops:>>> G.remove_edges_from(G.selfloop_edges())

这是一个类似于http://networkx.lanl.gov/examples/drawing/degree_histogram.html上的示例的示例,该示例绘制的图形包括最大连接组件的图形布局:

#!/usr/bin/env pythonimport randomimport matplotlib.pyplot as pltimport networkx as nxdef seq(n):    return [random.gammavariate(alpha=2.0,beta=1.0) for i in range(100)]    z=nx.create_degree_sequence(100,seq)nx.is_valid_degree_sequence(z)G=nx.configuration_model(z)  # configuration modeldegree_sequence=sorted(nx.degree(G).values(),reverse=True) # degree sequenceprint "Degree sequence", degree_sequencedmax=max(degree_sequence)plt.hist(degree_sequence,bins=dmax)plt.title("Degree histogram")plt.ylabel("count")plt.xlabel("degree")# draw graph in inset plt.axes([0.45,0.45,0.45,0.45])Gcc=nx.connected_component_subgraphs(G)[0]pos=nx.spring_layout(Gcc)plt.axis('off')nx.draw_networkx_nodes(Gcc,pos,node_size=20)nx.draw_networkx_edges(Gcc,pos,alpha=0.4)plt.savefig("degree_histogram.png")plt.show()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/617041.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号