-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspp_algorithms.py
212 lines (186 loc) · 5.24 KB
/
spp_algorithms.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
212
from timeit import default_timer as timer
from queues import *
from graph import *
def dynamic(g):
g.initialize()
if not g.is_ordered:
return g
start_time = timer()
for n in g.node_list:
for a in n.outList:
dist = a.tail.d + a.cost
if a.head.d > dist:
a.head.d = dist
a.head.predecessor = a.tail
g.exec_time = timer() - start_time
return g
def dijkstra(g):
if g.negative:
return g
g.initialize()
my_list = g.node_list
start_time = timer()
while my_list.__len__() > 0:
minor = math.inf
for i in my_list:
if i.d < minor:
n = i
minor = i.d
my_list.remove(n)
for a in n.outList:
dist = a.tail.d + a.cost
if a.head.d > dist:
a.head.d = dist
a.head.predecessor = a.tail
g.exec_time = timer() - start_time
return g
def dial_dijkstra(g):
if g.negative:
return g
my_list = g.node_list
g.initialize()
q = CircularQueue(g.C + 1)
q.store(g.s)
start_time = timer()
while len(my_list) > 0:
n = q.next()
if not n.labeled:
my_list.remove(n)
n.labeled = True
for a in n.outList:
dist = a.tail.d + a.cost
if a.head.d > dist:
a.head.d = dist
a.head.predecessor = a.tail
q.store(a.head)
g.exec_time = timer() - start_time
return g
def radix_heap_dijkstra(g):
if g.negative:
return g
my_list = g.node_list
g.initialize()
q = RadixHeap(g.nodes_number * g.C)
q.store(g.s)
g.s.labeled = True
start_time = timer()
while len(my_list) > 0:
n = q.next()
n.labeled = False
my_list.remove(n)
for a in n.outList:
dist = a.tail.d + a.cost
old_d = a.head.d
if old_d > dist:
a.head.d = dist
a.head.predecessor = a.tail
if a.head.labeled:
q.update(node=a.head, old_d=old_d)
else:
a.head.labeled = True
q.store(a.head)
g.exec_time = timer() - start_time
return g
def label_correcting(g: Graph):
g.initialize()
min_dist = -g.nodes_number * g.C
opt_cond = False
start_time = timer()
while not opt_cond:
opt_cond = True
for a in g.arc_list:
dist = a.tail.d + a.cost
if a.head.d > dist:
a.head.d = dist
a.head.predecessor = a.tail
a.head.pred_arc = a
opt_cond = False
n = a.head
if dist < min_dist:
opt_cond = True
g.nCycle = n
g.neg_cycle = True
g.exec_time = timer() - start_time
return g
def fifo_label_correcting(g):
g.initialize()
min_dist = -g.nodes_number * g.C
g.nCycle = None
q = [g.s]
g.s.contained = True
start_time = timer()
while len(q) > 0:
n = q.pop(0)
n.contained = False
for a in n.outList:
dist = a.tail.d + a.cost
if a.head.d > dist:
a.head.d = dist
a.head.predecessor = a.tail
a.head.pred_arc = a
if not a.head.contained:
q.append(a.head)
a.head.contained = True
if dist < min_dist:
g.nCycle = n
g.neg_cycle = True
q.clear()
g.exec_time = timer() - start_time
return g
def deque_label_correcting(g):
g.initialize()
min_dist = -g.nodes_number * g.C
g.nCycle = None
q = [g.s]
g.s.contained = True
start_time = timer()
while len(q) > 0:
n = q.pop(0)
n.contained = False
for a in n.outList:
dist = a.tail.d + a.cost
if a.head.d > dist:
a.head.d = dist
a.head.predecessor = a.tail
a.head.pred_arc = a
if not a.head.contained:
if a.head.previously:
q.insert(0, a.head)
else:
q.append(a.head)
a.head.contained = True
a.head.previously = True
if dist < min_dist:
g.nCycle = n
g.neg_cycle = True
q.clear()
g.exec_time = timer() - start_time
return g
def neg_check(n: Node, p: Node, s: Node):
if n == p or n == s:
return True
i = p
while i is not s:
if n == i:
return True
i = i.predecessor
return False
def neg_check_label_correcting(g: Graph):
g.initialize()
opt_cond = False
start_time = timer()
while not opt_cond:
opt_cond = True
for a in g.arc_list:
dist = a.tail.d + a.cost
neg = neg_check(a.head, a.tail, g.s)
if neg:
g.neg_cycle=True
elif a.head.d > dist:
a.head.d = dist
a.head.predecessor = a.tail
a.head.pred_arc = a
opt_cond = False
n = a.head
g.exec_time = timer() - start_time
return g