-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutterfly.py
106 lines (89 loc) · 2.7 KB
/
butterfly.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
#Erick Mikoshi - Algorithms Project 1
#Implemented algorithm to measure the validity of butterfly data
#Declaring and initializing variables
user_input = raw_input('What file do you want to use?')
raw_G = [ ]
G = [ ]
colors = [ ]
related = [ ]
raw_list = [ ]
seen = [ ]
count = 0
final = 0
f = open(user_input, 'r')
#Reads the first line of the file
first_line = f.readline()
#Splits the line into an array so the values can be stored as int variables
first_split = first_line.split()
butterflies = int(first_split[0])
measurements = int(first_split[1])
#Creating place holders for the appropriate arrays
for i in range(measurements):
raw_G.append([0, 0, ''])
for i in range(butterflies):
G.append([ ])
related.append([ ])
colors.append(0)
seen.append(0)
other_lines = f.readlines()
#Read in the rest of the file, split it, and append to the raw graph.
for i in other_lines:
sp = i.split()
raw_list.append(sp)
for i in raw_list:
raw_G[count][0] = int(i[0])
raw_G[count][1] = int(i[1])
raw_G[count][2] = i[2]
count += 1
#Logic for creating the actual graph with connectivities, as well as an array
#that corresponds to it with labeled edges.
for i in raw_G:
if not i[0] in G[i[1]]:
G[i[1]].append(i[0])
related[i[1]].append(i[2])
if not i[1] in G[i[0]]:
G[i[0]].append(i[1])
related[i[0]].append(i[2])
#print "Raw Graph: ", raw_G
#print "Connectivity Graph: ", G
#print "Relativity: ", related
#Function to set the colors of each node (3 and 7 are used)
def set_colors(u, v):
#print "u: ",u, "v: ", v, related[v][G[v].index(u)]
if related[v][G[v].index(u)] == 'same':
colors[u] = colors[v]
if related[v][G[v].index(u)] == 'different':
if colors[v] == 7:
colors[u] = 3
if colors[v] == 3:
colors[u] = 7
#Declaring and initializing vairables for DFS. Setting the first node to
#arbitrary color.
start = 0
tbs = [ start ]
colors[0] = 3
#DFS
while len(tbs) > 0:
#print("tbs=", tbs)
v = tbs.pop()
if seen[v] == 0:
seen[v] = 1
#print("JUST SAW", v)
for u in G[v]:
tbs.append(u)
set_colors(u, v)
#print colors
#Butterfly verification. Used cases that would apply to find bad data,
#else it is good data.
for i in range(len(G)):
for j in range(len(G[i])):
#print "label:", related[i][j], ",colori: ", colors[i], "other color: ", colors[G[i][j]]
#print "i: ",i, "j :", G[i][j]
if related[i][j] == 'same' and colors[i] != colors[G[i][j]]:
final = 1
if related[i][j] == 'different' and colors[i] == colors[G[i][j]]:
final = 1
if final == 1:
print "BAD DATA"
else:
print "GOOD DATA"