-
Notifications
You must be signed in to change notification settings - Fork 0
/
christofides.py
228 lines (168 loc) · 6.37 KB
/
christofides.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# from https://github.com/Retsediv/ChristofidesAlgorithm
def tsp(data):
# build a graph
G = build_graph(data)
print("Graph: ", G)
# build a minimum spanning tree
MSTree = minimum_spanning_tree(G)
print("MSTree: ", MSTree)
# find odd vertexes
odd_vertexes = find_odd_vertexes(MSTree)
print("Odd vertexes in MSTree: ", odd_vertexes)
# add minimum weight matching edges to MST
minimum_weight_matching(MSTree, G, odd_vertexes)
print("Minimum weight matching: ", MSTree)
# find an eulerian tour
eulerian_tour = find_eulerian_tour(MSTree, G)
print("Eulerian tour: ", eulerian_tour)
current = eulerian_tour[0]
path = [current]
visited = [False] * len(eulerian_tour)
length = 0
for v in eulerian_tour[1:]:
if not visited[v]:
path.append(v)
visited[v] = True
length += G[current][v]
current = v
path.append(path[0])
print("Result path: ", path)
print("Result length of the path: ", length)
return length, path
def get_length(x1, y1, x2, y2):
return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** (1 / 2)
def build_graph(data):
graph = {}
for this in range(len(data)):
for another_point in range(len(data)):
if this != another_point:
if this not in graph:
graph[this] = {}
graph[this][another_point] = get_length(data[this][0], data[this][1], data[another_point][0],
data[another_point][1])
return graph
class UnionFind:
def __init__(self):
self.weights = {}
self.parents = {}
def __getitem__(self, object):
if object not in self.parents:
self.parents[object] = object
self.weights[object] = 1
return object
# find path of objects leading to the root
path = [object]
root = self.parents[object]
while root != path[-1]:
path.append(root)
root = self.parents[root]
# compress the path and return
for ancestor in path:
self.parents[ancestor] = root
return root
def __iter__(self):
return iter(self.parents)
def union(self, *objects):
roots = [self[x] for x in objects]
heaviest = max([(self.weights[r], r) for r in roots])[1]
for r in roots:
if r != heaviest:
self.weights[heaviest] += self.weights[r]
self.parents[r] = heaviest
def minimum_spanning_tree(G):
tree = []
subtrees = UnionFind()
for W, u, v in sorted((G[u][v], u, v) for u in G for v in G[u]):
if subtrees[u] != subtrees[v]:
tree.append((u, v, W))
subtrees.union(u, v)
return tree
def find_odd_vertexes(MST):
tmp_g = {}
vertexes = []
for edge in MST:
if edge[0] not in tmp_g:
tmp_g[edge[0]] = 0
if edge[1] not in tmp_g:
tmp_g[edge[1]] = 0
tmp_g[edge[0]] += 1
tmp_g[edge[1]] += 1
for vertex in tmp_g:
if tmp_g[vertex] % 2 == 1:
vertexes.append(vertex)
return vertexes
def minimum_weight_matching(MST, G, odd_vert):
import random
odd_vert = random.shuffle(odd_vert)
while odd_vert:
v = odd_vert.pop()
length = float("inf")
u = 1
closest = 0
for u in odd_vert:
if v != u and G[v][u] < length:
length = G[v][u]
closest = u
MST.append((v, closest, length))
odd_vert.remove(closest)
def find_eulerian_tour(MatchedMSTree, G):
# find neigbours
neighbours = {}
for edge in MatchedMSTree:
if edge[0] not in neighbours:
neighbours[edge[0]] = []
if edge[1] not in neighbours:
neighbours[edge[1]] = []
neighbours[edge[0]].append(edge[1])
neighbours[edge[1]].append(edge[0])
# print("Neighbours: ", neighbours)
# finds the hamiltonian circuit
start_vertex = MatchedMSTree[0][0]
EP = [neighbours[start_vertex][0]]
while len(MatchedMSTree) > 0:
for i, v in enumerate(EP):
if len(neighbours[v]) > 0:
break
while len(neighbours[v]) > 0:
w = neighbours[v][0]
remove_edge_from_matchedMST(MatchedMSTree, v, w)
del neighbours[v][(neighbours[v].index(w))]
del neighbours[w][(neighbours[w].index(v))]
i += 1
EP.insert(i, w)
v = w
return EP
def remove_edge_from_matchedMST(MatchedMST, v1, v2):
for i, item in enumerate(MatchedMST):
if (item[0] == v2 and item[1] == v1) or (item[0] == v1 and item[1] == v2):
del MatchedMST[i]
return MatchedMST
tsp([[1380, 939], [2848, 96], [3510, 1671], [457, 334], [3888, 666], [984, 965], [2721, 1482], [1286, 525],
[2716, 1432], [738, 1325], [1251, 1832], [2728, 1698], [3815, 169], [3683, 1533], [1247, 1945], [123, 862],
[1234, 1946], [252, 1240], [611, 673], [2576, 1676], [928, 1700], [53, 857], [1807, 1711], [274, 1420],
[2574, 946], [178, 24], [2678, 1825], [1795, 962], [3384, 1498], [3520, 1079], [1256, 61], [1424, 1728],
[3913, 192], [3085, 1528], [2573, 1969], [463, 1670], [3875, 598], [298, 1513], [3479, 821], [2542, 236],
[3955, 1743], [1323, 280], [3447, 1830], [2936, 337], [1621, 1830], [3373, 1646], [1393, 1368],
[3874, 1318], [938, 955], [3022, 474], [2482, 1183], [3854, 923], [376, 825], [2519, 135], [2945, 1622],
[953, 268], [2628, 1479], [2097, 981], [890, 1846], [2139, 1806], [2421, 1007], [2290, 1810], [1115, 1052],
[2588, 302], [327, 265], [241, 341], [1917, 687], [2991, 792], [2573, 599], [19, 674], [3911, 1673],
[872, 1559], [2863, 558], [929, 1766], [839, 620], [3893, 102], [2178, 1619], [3822, 899], [378, 1048],
[1178, 100], [2599, 901], [3416, 143], [2961, 1605], [611, 1384], [3113, 885], [2597, 1830], [2586, 1286],
[161, 906], [1429, 134], [742, 1025], [1625, 1651], [1187, 706], [1787, 1009], [22, 987], [3640, 43],
[3756, 882], [776, 392], [1724, 1642], [198, 1810], [3950, 1558]])
# tsp([[1, 1], [2, 5], [8, 0]])
#
# tsp([
# [0, 0],
# [3, 0],
# [6, 0],
#
# [0, 3],
# [3, 3],
# [6, 3],
#
# [0, 6],
# [3, 6],
# [6, 6],
#
# ])