-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoints_cover.py
258 lines (247 loc) · 7.86 KB
/
points_cover.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import itertools
import sys
# Global μεταβλητές
graph = {}
lines = []
vertical_lines = []
horizontal_lines = []
slopes = []
constats = []
s = {}
s_vertical = {}
s_horizontal = {}
s_list = []
s_list_horizontal = []
s_list_vertical = []
universe = set()
# Φτιάχνω τον γράφο
def make_the_graph(file):
global graph
count = -1
with open(file, 'r') as file:
for row in file:
count += 1
u = list(str(x) for x in row.split(' '))
if '\n' in u[1]:
u[1] = u[1][:1]
u[0] = int(u[0])
u[1] = int(u[1])
graph[count] = u
for i in range(1,len(graph)):
for j in range(len(graph)-1,i-1,-1):
if graph[j][0] < graph[j-1][0]:
graph[j],graph[j-1] = graph[j-1],graph[j]
# Λίστα με όλες τις πιθανές γραμμές που περνάνε από όλα τα σημεία
def find_all_lines():
global lines, slopes, constats, vertical_lines
combs = list(itertools.combinations(graph, 2))
for i in combs:
y1 = graph[i[0]][1]
y2 = graph[i[1]][1]
x1 = graph[i[0]][0]
x2 = graph[i[1]][0]
if x1 != x2:
slope = (y2 - y1) / (x2 - x1)
c = (-1) * slope * x1 + y1
if c > 0:
line = 'y = ' + str(slope) + ' x + ' + str(c)
elif c < 0:
c = c * (-1)
line = 'y = ' + str(slope) + ' x - ' + str(c)
c = c * (-1)
else:
line = 'y = ' + str(slope) + ' x + ' + str(c)
if line not in lines:
lines.append(line)
slopes.append(slope)
constats.append(c)
if y1 == y2:
horizontal_lines.append(line)
elif y1 != y2:
line = 'x = ' + str(x1)
if line not in lines:
lines.append(line)
vertical_lines.append(line)
slopes.append('-')
constats.append(x1)
# Λίστα με όλα σημεία που περνάνε οι ευθείες
def total_S():
global lines, graph, slopes, constats, s
count = -1
for line in lines: # lines --> λίστα με όλες τις ευθείες
s[line] = set()
if line in vertical_lines:
s_vertical[line] = set()
if line in horizontal_lines:
s_horizontal[line] = set()
count += 1
for dot in graph:
x0 = graph[dot][0]
y0 = graph[dot][1]
if slopes[count] != '-':
y = slopes[count] * x0 + constats[count]
if abs(y - y0) <= 0.0000001:
s[line].add(dot)
if line in horizontal_lines:
s_horizontal[line].add(dot)
else:
x = constats[count]
if x == x0:
s[line].add(dot)
s_vertical[line].add(dot)
for i in s_vertical:
if i == set():
del(i)
for row in s: # φτιάχνω την s_list
s_list.append(s[row])
for row in s_horizontal:
s_list_horizontal.append(s_horizontal[row])
for row in s_vertical:
s_list_vertical.append(s_vertical[row])
# Universe
def sets():
global graph, universe
for i in graph:
universe.add(i)
# Εύρεση λιγότερων ευθειών (1ος Αλγόριθμος) έλεχος κάθε πιθανού υποσυνόλου
def algorithm(lista):
result = list(itertools.chain.from_iterable(itertools.combinations(lista, r) for r in range(len(lista) + 1)))
solution = []
point = set()
z = []
for i in result:
lst = list(i)
for dot in graph:
flag = False
for i in lista:
if dot in i:
flag = True
break
if not flag:
point.add(dot)
check = set() # Έχει μέσα ενωμένα υποσύνολα
for j in lst:
if point != []:
for i in point:
check.add(i)
check = check | j
if check == universe:
solution = lst
break
if point != []:
for i in graph:
if i in point:
z.append(graph[i][0])
z.append(graph[i][1])
z.append(graph[i][0] + 1)
z.append(graph[i][1])
ans = []
for i in solution:
ans.append(list(i))
count = -1
answer = []
for i in ans:
count += 1
a = sorted(i)
answer.append(a)
for i in answer:
for j in range(len(i)):
print('(' + str(graph[i[j]][0]) + ',' + ' ' + str(graph[i[j]][1]) + ')' + ' ', end='')
print()
if point != []:
count = 0
count_ln = 0
for i in z:
print('(' + str(z[count]) + ',' + ' ' + str(z[count + 1]) + ')' + ' ', end='')
count_ln += 1
count += 2
if count_ln % 2 == 0:
print()
if count == len(z):
break
# Άπλιστος αλγόριθμος
def greedy_algorithm(lista):
used = set()
flag = True
ans = []
z = []
while flag:
maximum = set()
for set1 in lista:
f = False
for i in set1:
if i in used:
f = True
break
if not f and len(set1) > len(maximum):
maximum = set1
k = set()
a = True
if maximum == set():
for dot in graph:
if dot not in used and a:
for i in lista:
for j in i:
if j == dot:
k = i
used.add(j)
a = False
ans.append(k)
break
if k == set():
for dot in graph:
if dot not in used:
z.append(graph[dot][0])
z.append(graph[dot][1])
z.append(graph[dot][0] + 1)
z.append(graph[dot][1])
used.add(dot)
else:
ans.append(maximum)
for i in maximum:
used.add(i)
if used == universe:
flag = False
count = -1
answer = []
for i in ans:
count += 1
a = sorted(i)
answer.append(a)
for i in answer:
for j in range(len(i)):
print('(' + str(graph[i[j]][0]) + ',' + ' ' + str(graph[i[j]][1]) + ')' + ' ', end='')
print()
# Είναι σωστό απλά να διορθώσω τα print
if k == set():
count = 0
count_ln = 0
for i in z:
print('(' + str(z[count]) + ',' + ' ' + str(z[count + 1]) + ')' + ' ', end='')
count_ln += 1
if count_ln % 2 == 0:
print()
count += 2
if count == len(z):
break
if __name__ == '__main__':
file = sys.argv[len(sys.argv) - 1]
make_the_graph(file)
find_all_lines()
total_S()
sets()
s_list_h_v = []
for i in s_list_vertical:
s_list_h_v.append(i)
for i in s_list_horizontal:
s_list_h_v.append(i)
if len(sys.argv) == 4:
algorithm(lista=s_list_h_v)
elif len(sys.argv) == 3:
parameter = sys.argv[1]
if parameter == '-g':
greedy_algorithm(lista=s_list_h_v)
elif parameter == '-f':
algorithm(lista=s_list)
elif len(sys.argv) == 2:
greedy_algorithm(lista=s_list)