-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcover.py
47 lines (42 loc) · 1.32 KB
/
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
'''
Author: Scott Walters and Marshall Jones
cover.py
'''
import math
import functools
import bigNeighborhood as bn
def greedy(coords):
data = bn.matrix(coords)
neighborhoods = []
for x in data:
neighborhoods.append(functools.reduce(lambda count, y: count + y, x))
covered = []
points = []
while len(covered) != len(data):
biggest = max(neighborhoods)
cover = data[neighborhoods.index(biggest)]
covered.append(coords[neighborhoods.index(biggest)])
points.append(coords[neighborhoods.index(biggest)])
neighborhoods[neighborhoods.index(biggest)] = -1
for i in range (0,len(cover)):
if cover[i] == 1:
neighborhoods[i] = -1
covered.append(coords[i])
return points
def main():
files = ["data1/numbers1.txt", "data1/numbers2.txt"]
#files = ["data1/numbers1.txt"]
for f in range(0, len(files)):
file = open(files[f], 'r')
coords = []
for line in file:
tcoords = line.split()
coords.append([int(tcoords[0]), int(tcoords[1])])
file.close()
output = open("cover_" + str(f+1) + ".txt", "w")
data = greedy(coords)
for line in data:
output.write(str(line) + "\n")
output.close()
if __name__ == "__main__":
main()