-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
118 lines (78 loc) · 1.81 KB
/
utils.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
from union_find import UF
import distance
import heapq
def EdgeComp(e1,e2):
u,v=e1
x,y=e2
leveluv = distance.hamming(profiles[u],profiles[v])
levelxy = distance.hamming(profiles[x],profiles[y])
if leveluv != levelxy:
return leveluv - levelxy
for k in range (maxlen):
maxuv = max(lvs[u][k], lvs[v][k])
maxxy = max(lvs[x][k], lvs[y][k])
if maxuv != maxxy:
return maxxy - maxuv
minuv = min(lvs[u][k], lvs[v][k])
minxy = min(lvs[x][k], lvs[y][k])
if minuv != minxy:
return minxy - minuv
maxuv = max(u,v)
maxxy = max(x,y)
if maxuv != maxxy:
return maxxy - maxuv
minuv = min(u,v)
minxy = min(x,y)
if minuv != minxy:
return minxy - minuv
def run_kruskal(pm, pl, n, lvs1):
global profiles
global maxlen
global lvs
maxlen = pl #profile length
lvs = lvs1
profiles = pm
edges=[]
n = n
for i in range(n):
for j in range(i +1, n):
edges.append([i,j])
edges.sort(EdgeComp)
# var uf = new UnionFind(n)
uf = UF(n)
tree = []
i=0
while i<len(edges) and len(tree)<n-1:
if uf.find(edges[i][0]) != uf.find(edges[i][1]):
tree.append(edges[i])
uf.union(edges[i][0], edges[i][1])
i+=1
return tree
def run_prim(pm, pl, n, lvs1):
global profiles
global maxlen
global lvs
maxlen = pl #profile length
lvs = lvs1 #lvs calculated previously (Comparator.goeBURST)
profiles = pm
n = n
pi = {}
color = [ 0 for i in range(n)]
tree = []
pqueue = []
heapq.heappush(pqueue, 0)
while len(pqueue) > 0:
u = heapq.heappop(pqueue)
color[u] = 2
if u != 0:
tree.append([pi[u][0],pi[u][1]])
for v in range(len(profiles)):
if color[v] == 0:
color[v] = 1
pi[v] = [u,v]
heapq.heappush(pqueue, v)
elif color[v] == 1 and EdgeComp([u,v], pi[v]) < 0:
pi[v] = [u,v]
#heapq.heapify(pqueue)
#print tree
return tree