-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
159 lines (110 loc) · 3.83 KB
/
main.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
import numpy as np
from collections import deque
def calculateRadius(nodes, degree, dist):
if dist == "Square":
return np.sqrt(degree / (np.pi * nodes))
return -1
def generatePoints(dist, nodes):
if dist == "Square":
return np.random.rand(nodes, 2)
return []
def findEdges(nodes, rad, alg="Brute"):
num_buckets = int(1/rad) - 1
if alg == "Brute":
edges = []
for idx, x in enumerate(nodes):
e = []
for idy, y in enumerate(nodes):
if np.sqrt(np.sum((x-y)**2)) <= rad and idx != idy:
e.append(idy)
edges.append(e)
if alg == "Buckets":
edges = []
for i in range(len(nodes)):
edges.append([])
buckets = []
for i in range(num_buckets):
l = []
for j in range(num_buckets):
l.append([])
buckets.append(l)
for x in range(len(nodes)):
buckets[int(nodes[x][0]*num_buckets)][int(nodes[x][1]*num_buckets)].append(x)
for y in range(num_buckets):
yrange = []
if(y == 0):
yrange = [0,1]
elif(y == num_buckets - 1):
yrange = [y-1, y]
else:
yrange = [y-1, y, y+1]
for x in range(num_buckets):
xrange = []
if(x == 0):
xrange = [0,1]
elif(x == num_buckets - 1):
xrange = [x-1, x]
else:
xrange = [x-1, x, x+1]
for itemA in buckets[y][x]:
for idy in yrange:
for idx in xrange:
for itemB in buckets[idy][idx]:
if np.sqrt(np.sum((nodes[itemA]-nodes[itemB])**2)) <= rad and itemA != itemB:
edges[itemA].append(itemB)
return edges
#http://delivery.acm.org/10.1145/330000/322385/p417-matula.pdf?ip=129.119.235.10&id=322385&acc=ACTIVE%20SERVICE&key=F82E6B88364EF649%2E15D8CE2BE55FDC61%2E4D4702B0C3E38B35%2E4D4702B0C3E38B35&CFID=846405691&CFTOKEN=41964261&__acm__=1475437801_60bbe556cff95b888b8a5678bcd70693
def smallestLastOrdering(edges, alg="Brute"):
vertices = []
degrees = [len(x) for x in edges]
maxdegree = max(degrees)
buckets = []
for i in range(maxdegree+1):
buckets.append(deque())
for i in range(len(edges)):
buckets[len(edges[i])].append(i)
j = len(edges)
while(j > 0):
i = 0
while(i < maxdegree and len(buckets[i]) == 0):
i = i+1
ivj = buckets[i].pop()
vertices.append(ivj)
for u in edges[ivj]:
deg = degrees[u]
inlist = True
try:
buckets[deg].remove(u)
except:
inlist = False
if(inlist):
buckets[deg-1].append(u)
degrees[u] = deg-1
j = j - 1
return vertices[::-1]
def generateColoring(order, edges):
colors = []
for i in range(len(edges)):
colors.append(0)
for v in order:
color = 0
adjcolors = [colors[x] for x in edges[v]]
while(color in adjcolors):
color = color + 1
colors[v] = color
return colors
def main():
NUM_NODES = 1000
AVG_DEGREE = 32
DISTRIBUTION = "Square"
radius = calculateRadius(NUM_NODES, AVG_DEGREE, DISTRIBUTION)
points = generatePoints(DISTRIBUTION, NUM_NODES)
print("Generated Points")
edges = findEdges(points, radius, alg="Buckets")
print("Average edge count: ", np.mean([len(x) for x in edges]))
order = smallestLastOrdering(edges)
print("Generated Smallest-Last Ordering")
colors = generateColoring(order, edges)
print("Generated Coloring")
print(colors)
main()