-
Notifications
You must be signed in to change notification settings - Fork 81
/
net-barabasi-albert.py
44 lines (36 loc) · 996 Bytes
/
net-barabasi-albert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import pycxsimulator
from pylab import *
import networkx as nx
m0 = 5 # number of nodes in initial condition
m = 2 # number of edges per new node
def initialize():
global g
g = nx.complete_graph(m0)
g.pos = nx.spring_layout(g)
g.count = 0
def observe():
global g
cla()
nx.draw(g, pos = g.pos)
def pref_select(nds):
global g
r = uniform(0, sum([g.degree(i) for i in nds]))
x = 0
for i in nds:
x += g.degree(i)
if r <= x:
return i
def update():
global g
g.count += 1
if g.count % 20 == 0: # network growth once in every 20 steps
nds = list(g.nodes)
newcomer = max(nds) + 1
for i in range(m):
j = pref_select(nds)
g.add_edge(newcomer, j)
nds.remove(j)
g.pos[newcomer] = (0, 0)
# simulation of node movement
g.pos = nx.spring_layout(g, pos = g.pos, iterations = 3)
pycxsimulator.GUI().start(func=[initialize, observe, update])