最近開始認真的學習發現一個 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()
二、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()
三、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()
四、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()
來源: http://www.cnblogs.com/forstudy/archive/2012/03/20/2407954.html
還有使用筆記給出鏈接 http://blog.sciencenet.cn/blog-404069-337442.html