networkx--四種網絡模型


  NetworkX提供了4種常見網絡的建模方法,分別是:規則圖,ER隨機圖,WS小世界網絡和BA無標度網絡。

 

一. 規則圖

  規則圖差不多是最沒有復雜性的一類圖,random_graphs.random_regular_graph(d, n)方法可以生成一個含有n個節點,每個節點有d個鄰居節點的規則圖。

  下面一段示例代碼,生成了包含20個節點、每個節點有3個鄰居的規則圖:

 1 import networkx as nx
2 import matplotlib.pyplot as plt
3
4 # regular graphy
5 # generate a regular graph which has 20 nodes & each node has 3 neghbour nodes.
6 RG = nx.random_graphs.random_regular_graph(3, 20)
7 # the spectral layout
8 pos = nx.spectral_layout(RG)
9 # draw the regular graphy
10 nx.draw(RG, pos, with_labels = False, node_size = 30)
11 plt.show()

 

 



二、ER隨機圖

  ER隨機圖是早期研究得比較多的一類“復雜”網絡,模型的基本思想是以概率p連接N個節點中的每一對節點。用random_graphs.erdos_renyi_graph(n,p)方法生成一個含有n個節點、以概率p連接的ER隨機圖:

 1 import networkx as nx
2 import matplotlib.pyplot as plt
3
4 # erdos renyi graph
5 # generate a graph which has n=20 nodes, probablity p = 0.2.
6 ER = nx.random_graphs.erdos_renyi_graph(20, 0.2)
7 # the shell layout
8 pos = nx.shell_layout(ER)
9 nx.draw(ER, pos, with_labels = False, node_size = 30)
10 plt.show()

 

三、WS小世界網絡

  用random_graphs.watts_strogatz_graph(n, k, p)方法生成一個含有n個節點、每個節點有k個鄰居、以概率p隨機化重連邊的WS小世界網絡。

  下面是一個例子:

 1 import networkx as nx
2 import matplotlib.pyplot as plt
3
4 # WS network
5
6 # generate a WS network which has 20 nodes,
7 # each node has 4 neighbour nodes,
8 # random reconnection probability was 0.3.
9 WS = nx.random_graphs.watts_strogatz_graph(20, 4, 0.3)
10 # circular layout
11 pos = nx.circular_layout(WS)
12 nx.draw(WS, pos, with_labels = False, node_size = 30)
13 plt.show()

四、BA無標度網絡

  用random_graphs.barabasi_albert_graph(n, m)方法生成一個含有n個節點、每次加入m條邊的BA無標度網絡。

  下面是一個例子:

 1 import networkx as nx
2 import matplotlib.pyplot as plt
3
4 # BA scale-free degree network
5 # generalize BA network which has 20 nodes, m = 1
6 BA = nx.random_graphs.barabasi_albert_graph(20, 1)
7 # spring layout
8 pos = nx.spring_layout(BA)
9 nx.draw(BA, pos, with_labels = False, node_size = 30)
10 plt.show()







免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM