def __get_best_candidate(self, conductance, edge_cover, stats): membership = {} max_conductance = conductance for (left_node, right_node) in edge_cover: candidate_node = left_node if left_node not in self.honests_graph.structure: candidate_node = right_node candidate_edge = (left_node, right_node) candidate_graph = sypy.CustomGraph( self.honests_graph.structure.copy() ) candidate_graph.structure.add_edges_from([candidate_edge]) (new_conductance, new_cover) = stats.normalized_conductance( candidate_graph, edge_cover=True ) if new_conductance > max_conductance: max_conductance = new_conductance membership = { "node": candidate_node, "edge": candidate_edge, "cover": new_cover } return (membership, max_conductance)
def largest_connected_component(self): cc = nx.connected_components(self.graph.structure) lcc_structure = self.graph.structure.subgraph( max(cc, key=len) ) lcc_graph = sypy.CustomGraph(lcc_structure) return lcc_graph
def __setup_network_graph(self): if self.custom_graph != None: self.graph = self.custom_graph self.known_honests = [] self.is_stitched = True return structure = nx.disjoint_union(self.left_region.graph.structure, self.right_region.graph.structure) return sypy.CustomGraph(structure)
def __createSyPyNetwork__(nodes_val, id_to_edges): """ Internal. Converts a given simulation into a SyPy-friendly 'network' object that allows utilizing other (GSD-based) detection algorithms. Finds the largest connected component, and then creates a graph out of the bidirectional edges and corresponding nodes. """ all_ids = {node.id for node in nodes_val} hon_ids = {node.id for node in nodes_val if node.type == "hon"} syb_ids = [node.id for node in nodes_val if node.type == "syb"] mal_ids = [node.id for node in nodes_val if node.type == "mal"] succ_edges = set() bidir_edges = [] for node_id in all_ids: for edge in id_to_edges[node_id]: if edge.successful: src_id = edge.node_src.id dst_id = edge.node_dst.id if (dst_id, src_id) in succ_edges: bidir_edges += [(dst_id, src_id)] else: succ_edges.add((src_id, dst_id)) nx_graph = nx.Graph() nx_graph.add_nodes_from(all_ids) nx_graph.add_edges_from(bidir_edges) cc_graph = max(nx.connected_component_subgraphs(nx_graph), key=len) cc_nodes = cc_graph.nodes() cc_edges = [] for edge in bidir_edges: if edge[0] in cc_nodes and edge[1] in cc_nodes: cc_edges += [edge] nx_graph = nx.Graph() nx_graph.add_nodes_from(cc_nodes) nx_graph.add_edges_from(cc_edges) initial_sybils = list(set(all_ids) - set(cc_nodes)) honest_edge_counts = {node_id: 0 for node_id in cc_nodes} for edge in cc_edges: if edge[0] in hon_ids and edge[1] in hon_ids: honest_edge_counts[edge[0]] += 1 honest_edge_counts[edge[1]] += 1 known_honests = [ sorted(honest_edge_counts.items(), key=lambda kv: kv[1], reverse=True)[0][0] ] network = sypy.Network(None, None, "test", custom_graph=sypy.CustomGraph(nx_graph)) network.set_data(original_nodes=all_ids, sybils=syb_ids, malicious=mal_ids, known_honests=known_honests, initial_sybils=initial_sybils) return network
import sys sys.path.append("../") import sypy import networkx as nx if __name__ == "__main__": print "\n\nCustom graph..." nx_graph = nx.Graph() nx_graph.add_nodes_from([0,1,2]) nx_graph.add_edges_from([(0,1), (1,2), (2,0)]) graph = sypy.CustomGraph(nx_graph) stats = graph.get_graph_stats() print "order={0}, size={1}, is_connected={2}".format( stats.order, stats.size, stats.is_connected ) print "\n\nImported GEXF graph..." graph = sypy.ImportedGEXFGraph("datasets/ca-AstroPh.gexf") stats = graph.get_graph_stats() print "order={0}, size={1}, is_connected={2}, num_cc={3}".format( stats.order, stats.size, stats.is_connected, stats.num_cc
def __init__(self, network): BaseDetector.__init__(self, network) self.honests_graph = sypy.CustomGraph( self.network.graph.structure.subgraph(self.network.known_honests) )
def __setup_network_graph(self): structure = nx.disjoint_union(self.left_region.graph.structure, self.right_region.graph.structure) return sypy.CustomGraph(structure)