python networkx模块,python复杂网络处理模块networkx

最近开始认真的学习发现一个 python 好玩的模块

以下内容为网上的文章整合

networkx在02年5月产生,是用python语言编写的软件包,便于用户对复杂网络进行创建、操作和学习。利用networkx可以以标准化和非标准化的数据格式存储网络、生成多种随机网络和经典网络、分析网络结构、建立网络模型、设计新的网络算法、进行网络绘制等。

NetworkX提供了4种常见网络的建模方法,分别是:规则图,ER随机图,WS小世界网络和BA无标度网络。

一. 规则图

规则图差不多是最没有复杂性的一类图,random_graphs.random_regular_graph(d, n)方法可以生成一个含有n个节点,每个节点有d个邻居节点的规则图。

下面一段示例代码,生成了包含20个节点、每个节点有3个邻居的规则图:

import networkx as nx

import matplotlib.pyplot as plt

# regular graphy

# generate a regular graph which has 20 nodes & each node has 3 neghbour nodes.

RG = nx.random_graphs.random_regular_graph(3, 20)

# the spectral layout

pos = nx.spectral_layout(RG)

# draw the regular graphy

nx.draw(RG, pos, with_labels = False, node_size = 30)

plt.show()

0818b9ca8b590ca3270a3433284dd417.png

二、ER随机图

ER随机图是早期研究得比较多的一类“复杂”网络,模型的基本思想是以概率p连接N个节点中的每一对节点。用random_graphs.erdos_renyi_graph(n,p)方法生成一个含有n个节点、以概率p连接的ER随机图:

import networkx as nx

import matplotlib.pyplot as plt

# erdos renyi graph

# generate a graph which has n=20 nodes, probablity p = 0.2.

ER = nx.random_graphs.erdos_renyi_graph(20, 0.2)

# the shell layout

pos = nx.shell_layout(ER)

nx.draw(ER, pos, with_labels = False, node_size = 30)

plt.show()

0818b9ca8b590ca3270a3433284dd417.png

三、WS小世界网络

用random_graphs.watts_strogatz_graph(n, k, p)方法生成一个含有n个节点、每个节点有k个邻居、以概率p随机化重连边的WS小世界网络。

下面是一个例子:

import networkx as nx

import matplotlib.pyplot as plt

# WS network

# generate a WS network which has 20 nodes,

# each node has 4 neighbour nodes,

# random reconnection probability was 0.3.

WS = nx.random_graphs.watts_strogatz_graph(20, 4, 0.3)

# circular layout

pos = nx.circular_layout(WS)

nx.draw(WS, pos, with_labels = False, node_size = 30)

plt.show()

0818b9ca8b590ca3270a3433284dd417.png

四、BA无标度网络

用random_graphs.barabasi_albert_graph(n, m)方法生成一个含有n个节点、每次加入m条边的BA无标度网络。

下面是一个例子:

import networkx as nx

import matplotlib.pyplot as plt

# BA scale-free degree network

# generalize BA network which has 20 nodes, m = 1

BA = nx.random_graphs.barabasi_albert_graph(20, 1)

# spring layout

pos = nx.spring_layout(BA)

nx.draw(BA, pos, with_labels = False, node_size = 30)

plt.show()

0818b9ca8b590ca3270a3433284dd417.png

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值