def filter_top_litigants_2(g): #avg = library.get_total_votes_against(g) library.ensure_bidirectionality(g) keys = g.keys() descending = [] totalvotes = 0 for i in keys: totalvotes += len(g[i]) descending.append((i, len(g[i]))) #sorts from the higher to the lower degree descending.sort(lambda x, y: x[1] - y[1], reverse=True) malicious = set() avg = 1.0 * totalvotes / len(keys) for i in xrange(len(descending)): node = descending[i][0] votes = len(g[node]) if votes > avg: #else don't stop because values can change #remove the node malicious.add(node) totalvotes -= (2 * votes) # 2 * - because it is one vote in each direction library.delete_node(g, node) #sys.stderr.write("Warning changed len(keys) to len(g.keys())\n") avg = 1.0 * totalvotes / len(g.keys()) #avg = 1.0 * totalvotes / len(keys) return malicious
def filter_top_litigants(g, threshold, delete): avg = library.get_avg_votes_against(g) todel = [] keys = g.keys() #to avoid any kind of bias: random.shuffle(keys) for i in keys: #sys.stderr.write(str(i) + " ") if len(g[i]) > threshold * avg: if delete: #sys.stderr.write(str(i) + " ") library.delete_node(g, i) todel.append(i) #sys.stderr.writ("deleted:" + todel) return todel
def filter_top_litigants_3(g, participations): #avg = library.get_total_votes_against(g) library.ensure_bidirectionality(g) keys = g.keys() descending = [] totalvotes = 0 for i in keys: if participations[i] > 0: totalvotes += (1.0 * len(g[i]) / participations[i]) descending.append((i, 1.0 * len(g[i]) / participations[i])) #sorts from the higher to the lower degree descending.sort(lambda x, y: helper(x[1], y[1]), reverse=True) # sys.stderr.write("descending. len = " + str(len(descending))) # for n in descending: # sys.stderr.write(str(n[0]) + " ") activenodes = len(descending) malicious = set() for i in xrange(len(descending)): node = descending[i][0] votes = 1.0 * len(g[node]) / participations[node] avg = 1.0 * totalvotes / activenodes if votes - avg > 1e-6: #else don't stop because values can change #remove the node #sys.stderr.write("g[" + str(node) + "] =" + str(g[node]) + " votes =" + str(votes) + " avg = " + str(avg) + "\n") malicious.add(node) subvotes = library.delete_node(g, node, participations) totalvotes -= subvotes activenodes -= 1 return malicious
def bfs_classify(arguments): #threshold_of_good = 0.5 graph = library.read_graph() original_graph = copy.deepcopy(graph) #threshold = library.get_threshold(arguments) #deletedlist = library.filter_top_litigants(graph, threshold, True) #sys.stderr.write("Throshold = " + str(threshold) + " Nodes eliminated\n") #for node in deletedlist: # sys.stderr.write(str(node) + " ") good = [] bad = [] done = False while not (done): #keys = graph.keys() root = pick_bfs_root1(graph) #print "root =", root tags = bfs(graph, root, None, mybfs_process) #print tags #odd = [] #even = [] #for i in tags: # if tags[i] % 2 == 1: # odd.append(i) # else: # even.append(i) # #odd.sort() #even.sort() # #print "Odd" #for i in odd: # print i # #print "Even" #for i in even: # print i values = {} for i in tags.keys(): if tags[i] in values: values[tags[i]].append(i) else: values[tags[i]] = [i] odd = [] even = [] for i in values.keys(): #print i, ":", values[i] if i % 2 == 0: odd.extend(values[i]) else: even.extend(values[i]) #odd.sort() #even.sort() if len(even) > len(odd): good.extend(even) bad.extend(odd) else: good.extend(odd) bad.extend(even) #print "len(good) =", len(good) #bad.extend(deletedlist) #deletedlist = [] #get ready for another round keys = graph.keys() for n in keys: if n in good or n in bad: library.delete_node(graph, n) if len(graph) == 0: done = True #switch_wrong_cases(graph_without_naives, good, bad) good.sort() bad.sort() #print "Good nodes:", good #print "Bad nodes:", bad #print "Naive nodes:", deletedlist output = [] keys = original_graph.keys() keys.sort() for i in xrange(len(keys)): # print i n = keys[i] #if n in deletedlist: # outcome = 0 #else: if n in bad: outcome = 1 else: outcome = 0 output.append(outcome) return output