-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
77 lines (68 loc) · 2.63 KB
/
test.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
#!/usr/bin/env python
import copy
def normalize(l):
# normalize the cycle, pick up the cycle which the first element is maximum.
return max([l[i:]+l[:i] for i in range(len(l))])
def even(x):
if x < 2:
return x
if x % 2 == 0:
return x
else:
return x - 1
def mirror(index):
if index%2 == 0:
return index + 1
else:
return index - 1
def find_loops_Hugen(permutation):
order = int(len(permutation)/2)
# find all loops in a directed graph (before add_vertex).
def is_loop(num):
if (num in trace) or (mirror(num) in trace):
if num in trace:
index = trace.index(num)
elif mirror(num) in trace:
index = trace.index(mirror(num))
loops.append(trace[index:len(trace)+1])
return
trace.append(num)
for j in range(even(num), even(num)+2):
is_loop(permutation[j-2])
trace.pop()
return
trace = []
loops = []
is_loop(2)
is_loop(3)
# Remove the duplicated loops.
loops_unique = set(map(tuple, map(normalize, loops)))
loops = map(list, list(loops_unique))
print loops
# Rebuild the loop with permutation_org
loops_dict = {}
loops_dict_bk = {}
list2pair = lambda x:[[x[i],x[i+1]] if i!=len(x)-1 else [x[i],x[0]] for i in range(len(x))]
for loop in loops:
length = len(loop)
loops_dict_bk.setdefault(length, []).append(loop)
loop = list2pair(loop)
loop_new = [[0] * (2 * order)]
for line in loop:
if (permutation[line[0]-2] in [line[1], mirror(line[1])]) and (permutation[mirror(line[0])-2] not in [line[1], mirror(line[1])]):
for ind in range(len(loop_new)):
loop_new[ind][line[0] - 2] = 1
elif (permutation[line[0]-2] not in [line[1], mirror(line[1])]) and (permutation[mirror(line[0])-2] in [line[1], mirror(line[1])]):
for ind in range(len(loop_new)):
loop_new[ind][mirror(line[0])-2] = 1
elif (permutation[line[0] - 2] in [line[1], mirror(line[1])]) and (permutation[mirror(line[0]) - 2] in [line[1], mirror(line[1])]):
tem = copy.deepcopy(loop_new)
for ind in range(len(loop_new)):
loop_new[ind][line[0] - 2] = 1
tem[ind][mirror(line[0])-2] = 1
loop_new.extend(tem)
loops_dict.setdefault(length, []).extend(loop_new)
loops_dict = {k:map(list, list(set(map(tuple, v)))) for k,v in loops_dict.items()}
return loops_dict
permutation = [6,5,2,7,4,3]
print find_loops_Hugen(permutation)