-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscalefreequantum.py
166 lines (102 loc) · 3.17 KB
/
scalefreequantum.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# -*- coding: utf-8 -*-
"""
Created on Sun May 28 01:18:22 2023
@author: ektop
"""
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 2 19:01:57 2021
@author: cosmi
"""
import matplotlib
matplotlib.use('TkAgg')
from pylab import *
import networkx as nx
import numpy as np
from pygsp import graphs
import matplotlib.pyplot as plt
m0 = 4 # number of nodes in initial condition
m = 2 # number of edges per new node
global grid2d
counter = 0
def initialize():
global g, nextg, counter, grid2d
g = nx.complete_graph(m0)
g.pos = nx.spring_layout(g)
nextg = g.copy()
xdata = []
ydata = []
grid2d = []
def observe1():
global g, nextg, counter, grid2d
subplot(1,2,1)
cla()
nx.draw(g)
#subplot(1,2,2)
#cla()
#plot(xdata, ydata,'o',alpha = 0.05)
#axis('image')
def observe2():
global g, nextg, counter, grid2d
subplot(1,2,2)
grid2d = graphs.Graph.from_networkx(g)
plt.imshow(grid2d.A.todense())
axis('image')
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, nextg, counter, grid2d
counter += 1
if counter % 20 == 0:
nds = g.nodes()
newcomer = max(nds) + 1
for i in range(m):
j = pref_select(nds)
g.add_edge(newcomer, j)
unsaturated_b = g.nodes()
list(unsaturated_b).remove(j)
xdata.append(g.degree(i))
ccs = nx.connected_components(g)
ydata.append(max(len(cc) for cc in ccs))
#xdata.append(g.degree(i)); ydata.append(g.degree(j))
#xdata.append(g.degree(j)); ydata.append(g.degree(i))
#g.pos[newcomer] = (0, 0) # simulation of node movement
g, nextg = nextg, g
grid2d = graphs.Graph.from_networkx(g)
#g.pos = nx.spring_layout(pos = g.pos, iterations = 5)
import pycxsimulator2plots
pycxsimulator2plots.GUI().start(func=[initialize, observe1, observe2, update])
# for percolation search at end of run
pycxsimulator2plots.GUI().quitGUI
print(grid2d.W.toarray())
print(grid2d.signals)
print(grid2d)
grid2d.compute_fourier_basis()
grid2d.set_coordinates()
grid2d.plot()
plt.imshow(grid2d.A.todense())
# plot spectrum
fig, ax = plt.subplots(1, 1, figsize=(7,7))
ax.plot(grid2d.e)
ax.set_xlabel('eigenvalue index (i)')
ax.set_ylabel('eigenvalue ($\lambda_{i}$)')
ax.set_title('2D-grid spectrum');
#fiedler vector highlighted graph
grid2d.plot_signal(grid2d.U[:,1])
#plot all eigenvectors as network graph frames
fig, axes = plt.subplots(2, 3, figsize=(10, 6.6))
count = 0
for j in range(2):
for i in range(3):
grid2d.plot_signal(grid2d.U[:, count*1], ax=axes[j,i],colorbar=False)
axes[j,i].set_xticks([])
axes[j,i].set_yticks([])
axes[j,i].set_title(f'Eigvec {count*1+1}')
count+=1
fig.tight_layout()