-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplexity.py
211 lines (156 loc) · 6.34 KB
/
complexity.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from Graph import Graph
from MST import MST
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
from timeit import default_timer as timer
import random
# graph = Graph()
# graph.addEdge(0, 1, 4)
# graph.addEdge(0, 7, 12)
# graph.addEdge(1, 2, 8)
# graph.addEdge(1, 7, 11)
# graph.addEdge(2, 3, 7)
# graph.addEdge(2, 8, 2)
# graph.addEdge(2, 5, 4)
# graph.addEdge(3, 4, 9)
# graph.addEdge(3, 5, 14)
# graph.addEdge(4, 5, 10)
# graph.addEdge(5, 6, 2)
# graph.addEdge(6, 7, 1)
# graph.addEdge(6, 8, 6)
# graph.addEdge(7, 8, 7)
# graph.show()
# mst=MST(graph)
# res = mst.Boruvka()
# res.show()
# res = mst.Prim()
# res.show()
def random_mst(n):
# build the mst
graph = Graph()
unvisited = set(range(0,n))
visited = set()
s = unvisited.pop()
visited.add(s)
# create a set contain all edges that not belong to mst
edgeSet=set()
for i in range(0,n):
for j in range(0,i):
edgeSet.add((j,i))
i = 0
while i < n - 1:
# s = random.choice(tuple(unvisited))
s = unvisited.pop()
w = random.randint(0,10)
t = visited.pop()
visited.add(t)
# t = random.choice(tuple(visited))
graph.addEdge(s,t,w)
# unvisited.discard(s)
i = i + 1
visited.add(s)
# else:
# continue
if s > t:
edgeSet.remove((t,s))
else:
edgeSet.remove((s,t))
return graph,edgeSet
# increase the density
def random_graph(mst,m, edgeSet):
'''
each mst has n-1 edges and n vertices, to increase the density of the network, we can add edges to the mst.
The maximum number of edges is m = (n-1)n/2.
'''
cur = mst.number_of_edges()
n = mst.number_of_nodes()
while cur < m*(n*(n-1))/2.0:
#s= random.randint(0,n-1)
#s_degree = sum(1 for _ in mst.neighbors(s))
# check if the degree of s is already n-1
#while s_degree == n-1:
#s = random.randint(0,n-1)
#s_degree = sum(1 for _ in mst.neighbors(s))
# check if t is already connected with s
#t= random.randint(0,n-1)
#while t in mst.neighbors(s):
#t = random.randint(0,n-1)
# connect s & t with weight w
edge = edgeSet.pop()
# edge = random.choice(tuple(edgeSet))
# edgeSet.remove(edge)
w = random.randint(11, 30)
mst.addEdge(edge[0], edge[1], w)
cur += 1
return mst, edgeSet
def func2(x,k,c):
return k * ((x-1)*x/2.0) * np.log2(x) + c
def func1(x,k):
return k * ((x-1)*x/2.0) * np.log2(x)
def func3(x,k,b,c):
return k * ((x-1)*x/2.0) * np.log2(x) + b*x + c
if __name__ == '__main__':
plt.switch_backend('agg')
# some test function
sizes = [100, 200, 400, 800,1600,3200,6400,12800]
# sizes = [200*i for i in range(5,31)]
density = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]
times_Boruvka = []
times_Prim =[]
for n in sizes:
mst,edgeSet= random_mst(n)
for m in density:
graph, edgeSet = random_graph(mst, m, edgeSet)
res = MST(graph)
start = timer()
res.Boruvka()
end = timer()
cost = end - start
times_Boruvka.append(cost)
print("Boruvka: ", "size: ", n, " ", 'density: ', m, 'time:',' ', cost, "s")
start = timer()
res.Prim()
end = timer()
cost = end - start
times_Prim.append(cost)
print("Prim: ", "size: ", n, " ", 'density: ', m, 'time:',' ', cost, "s")
times_Boruvka = np.array(times_Boruvka).reshape((len(sizes), len(density)))
times_Prim = np.array(times_Prim).reshape((len(sizes), len(density)))
for i in range(0, len(density)):
vertices = sizes
plt.figure(figsize=(17,10))
plt.plot(vertices, times_Prim[:,i], 'o', label='Prim')
xdata=np.linspace(vertices[0],vertices[-1],50)
popt1, pcov1 = curve_fit(func1, np.array(vertices), times_Prim[:,i])
dev1 = sum(np.square(func1(np.array(vertices),*popt1) - times_Prim[:,i]))
popt_1=np.append(popt1,dev1)
plt.plot(xdata, func1(xdata, *popt1), 'r-', label = 'Prim: fit: k=%10.8f, dev=%f' % tuple(popt_1))
popt3, pcov3 = curve_fit(func3, np.array(vertices), times_Prim[:,i])
dev3 = sum(np.square(func3(np.array(vertices),*popt3) - times_Prim[:,i]))
popt_3=np.append(popt3,dev3)
plt.plot(xdata, func3(xdata, *popt3), 'g-', label = 'Prim: fit: k=%15.13f, b=%15.13f, c=%5.3f, dev=%f' % tuple(popt_3))
popt2, pcov2 = curve_fit(func2, np.array(vertices), times_Prim[:,i])
dev2 = sum(np.square(func2(np.array(vertices),*popt2) - times_Prim[:,i]))
popt_2=np.append(popt2,dev2)
plt.plot(xdata, func2(xdata, *popt2), 'b-', label = 'Prim: fit: k=%15.13f,c=%5.3f,dev=%f' % tuple(popt_2))
plt.plot(vertices, times_Boruvka[:,i], 'ro',label = 'Boruvka')
popt1, pcov1 = curve_fit(func1, np.array(vertices), times_Boruvka[:,i])
dev1 = sum(np.square(func1(np.array(vertices),*popt1) - times_Boruvka[:,i]))
popt_1=np.append(popt1,dev1)
plt.plot(xdata, func1(xdata, *popt1), 'r-', label = 'Boruvka: fit: k=%10.8f,dev=%f' % tuple(popt_1))
popt3, pcov3 = curve_fit(func3, np.array(vertices), times_Boruvka[:,i])
dev3 = sum(np.square(func3(np.array(vertices),*popt3) - times_Boruvka[:,i]))
popt_3=np.append(popt3,dev3)
plt.plot(xdata, func3(xdata, *popt3), 'g-', label = 'Boruvka: fit: k=%15.13f, b=%15.13f, c=%5.3f,dev=%f' % tuple(popt_3))
popt2, pcov2 = curve_fit(func2, np.array(vertices), times_Boruvka[:,i])
dev2 = sum(np.square(func2(np.array(vertices),*popt2) - times_Boruvka[:,i]))
popt_2=np.append(popt2,dev2)
plt.plot(xdata, func2(xdata, *popt2), 'b-', label = 'Boruvka: fit: k=%15.13f, c=%5.3f,dev=%f' % tuple(popt_2))
plt.xlabel('nodes')
plt.ylabel('time')
plt.legend()
plt.title('density='+str(density[i]))
# plt.savefig('fit/1000-6000nodes/density='+str(density[i])+'.png')
plt.savefig('fit/max12800nodes/density='+str(density[i])+'.png')
plt.close()