-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcycle_graph.py
More file actions
239 lines (213 loc) · 6.7 KB
/
cycle_graph.py
File metadata and controls
239 lines (213 loc) · 6.7 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
class cycle_graph():
"""A package built to support research into graphs for the
William and Mary EXTREEMS-QED program."""
class __cg_Node():
def __init__(self, val):
self.prev = None
self.next = None
self.val = val
self.spin = 0
self.pos = 0
self.n = 0
def __gt__(self, other):
s = self.spin - other.spin
d = other.pos - self.pos
if other.pos > self.pos:
if s > d:
return True
if other.pos < self.pos:
if s > d + self.n:
return True
return False
def __init__(self, list_, n = 0):
self.__base_node = None
self.__standing = None
self.__compare = None
self.__length = 0
self.__input(list_, n)
###Initialization Functions
def __input(self, list_, n = 0):
for i in list_:
self.__insert(i)
self.__pos_assign(n)
self.__spin_assign()
self.__standing = self.__base_node
self.__compare = self.__base_node
def __insert(self, val):
new_node = self.__cg_Node(val)
if self.__length == 0:
new_node.next = new_node
new_node.prev = new_node
self.__base_node = new_node
else:
self.__base_node.prev.next = new_node
new_node.prev = self.__base_node.prev
self.__base_node.prev = new_node
new_node.next = self.__base_node
self.__length = self.__length + 1
current = self.__base_node
for _ in range(self.__length):
current.n = self.__length
current = current.next
def __pos_assign(self, n):
current = self.__base_node
for i in range(1, self.__length + 1):
if i + n > self.__length:
current.pos = i + n - self.__length
else:
current.pos = i + n
current = current.next
def __spin_assign(self):
current = self.__base_node
for _ in range(1, self.__length + 1):
current.spin = current.val - current.pos
current.n = self.__length
current = current.next
self.optimize()
###Property Functions
def __len__(self):
return self.__length
def __str__(self):
if self.__length == 0:
return ""
current = self.__base_node
string = ""
for _ in range(self.__length - 1):
string = string + str(current.val) + " "*(3 - len(str(current.val))) + "-> "
current = current.next
string = string + str(current.val)
return string
def dist_print(self):
""" Returns a string of the spins copying the format of the str function """
if self.__length == 0:
return ""
current = self.__base_node
string = ""
for _ in range(self.__length - 1):
string = string + str(current.spin) + " "*(3 - len(str(current.spin))) + "-> "
current = current.next
string = string + str(current.spin)
return string
def dist_list(self):
""" Returns a list of the spins """
if self.__length == 0:
return ""
current = self.__base_node
list_ = []
for _ in range(self.__length - 1):
list_.append(current.spin)
current = current.next
list_.append(current.spin)
return list_
def total_spin(self):
""" Finds the sum of the absolute values of the spins """
current = self.__base_node
total = 0
for _ in range(self.__length):
total = total + abs(current.spin)
current = current.next
return total
def terms(self):
""" Returns the elements in the cycle in list format """
current = self.__base_node
out = []
for _ in range(self.__length):
out.append(current.val)
current = current.next
return out
def not_ordered(self):
""" Tests the ordering of the cycle and returns false if it is, and true if it is not """
current = self.__base_node
for _ in range(self.__length):
if current.pos != current.val:
return True
current = current.next
return False
def __getitem__(self, key):
if key > self.__length and key < 1:
raise IndexError
current = self.__base_node
while current.pos != key:
current = current.next
return current.val
def value(self, node = ''):
""" Returns the value of the standing node by default.
With the argument 'comp', it returns the value of the comparison node.
With the argument 'next', it returns the value of the next node. """
if node == 'comp':
return self.__compare.val
elif node == 'next':
return self.__standing.next.val
return self.__standing.val
def spin(self, node = ''):
""" Returns the spin of the standing node by default.
With the argument 'comp', it returns the spin of the comparison node.
With the argument 'next', it returns the spin of the next node. """
if node == 'comp':
return self.__compare.spin
elif node == 'next':
return self.__standing.next.spin
return self.__standing.spin
def position(self, node = ''):
""" Returns the postion of the standing node by default.
With the argument 'comp', it returns the postion of the comparison node.comparison."""
if node == 'comp':
return self.__compare.pos
return self.__standing.pos
def compare(self, node = ''):
if node == 'comp':
return self.__compare > self.__standing
return self.__standing > self.__standing.next
###Sorting Functions
def optimize(self):
""" Optimizes the spins by minimizing their values """
temp = self.dist_list()
if sum(temp) == 0:
while max(temp) - min(temp) > self.__length:
max_term = max(temp) - self.__length
min_term = min(temp) + self.__length
temp[temp.index(max(temp))] = max_term
temp[temp.index(min(temp))] = min_term
current = self.__base_node
for i in temp:
current.spin = i
current = current.next
def next_node(self, node = ''):
""" Sends the standing node to the next node by default.
With the argument 'comp', sends the compare node to its next node."""
if node == 'comp':
self.__compare = self.__compare.next
return
self.__standing = self.__standing.next
def prev_node(self):
self.__standing = self.__standing.next
def reset(self, node = ''):
""" Sends the standing node to the base node by default.
With the argument 'comp', sends the compare node to the base node."""
if node == 'comp':
self.__compare = self.__base_node
return
self.__standing = self.__base_node
def send(self, key):
""" Sends the standing node to the position key"""
if key > self.__length and key < 1:
raise IndexError
while self.__standing.pos != key:
self.__standing = self.__standing.next
def swap(self):
"""Swaps the standing node with the its next neighbor"""
if self.compare():
p_1 = self.__standing
p_2 = self.__standing.next
p_1.next = p_2.next
p_2.prev = p_1.prev
p_1.prev.next = p_2
p_2.next.prev = p_1
p_1.prev = p_2
p_2.next = p_1
if p_1 == self.__base_node:
self.__base_node = p_2
elif p_2 == self.__base_node:
self.__base_node = p_1
self.__pos_assign(0)
self.__spin_assign()