Created
February 25, 2019 16:01
-
-
Save evgeth/0f117e0107ebda650d4da29b9347ead9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib | |
matplotlib.use('TkAgg') | |
from pylab import * | |
import networkx as nx | |
m0 = 5 # number of nodes in initial condition | |
m = 2 # number of edges per new node | |
def set_m(val=2): | |
global m | |
m = int(val) | |
return m | |
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 = 5) | |
import pycxsimulator | |
pycxsimulator.GUI(parameterSetters=[set_m]).start(func=[initialize, observe, update]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment